# 格式化与挂载

在上一节中，我们把硬盘`/dev/sdb`分成了一个主分区和两个逻辑分区。这一节，我们将讲述如何对分区进行格式化与挂载，以及卸载。注意：扩展分区不能格式化与挂载。

首先，我们再来确认一下硬盘`/dev/sdb`的分区情况，如下

```
[root@localhost ~]# lsblk /dev/sdb
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdb      8:16   1 223.4G  0 disk 
├─sdb1   8:17   1    50G  0 part 
├─sdb2   8:18   1     1K  0 part 
├─sdb5   8:21   1    80G  0 part 
└─sdb6   8:22   1  93.4G  0 part 
[root@localhost ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 239.9 GB, 239902654464 bytes, 468559872 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x93de47a2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   104859647    52428800   83  Linux
/dev/sdb2       104859648   468559871   181850112    5  Extended
/dev/sdb5       104861696   272633855    83886080   83  Linux
/dev/sdb6       272635904   468559871    97961984   83  Linux
```

接下来，我们将主分区`/dev/sdb1`格式化为xfs（ftype=1）类型的文件系统

```
[root@localhost ~]# mkfs.xfs -f -i attr=2 -l lazy-count=1,sectsize=4096 -b size=4096 -d sectsize=4096 -n ftype=1 /dev/sdb1
```

然后，我们创建目录`/data1`，把分区`/dev/sdb1`挂载到目录`/data1`下

```
[root@localhost ~]# mkdir /data1
[root@localhost ~]# mount /dev/sdb1 /data1
```

此时，我们查看挂载情况，如下

```
[root@localhost ~]# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda1      xfs        50G  7.8G   43G  16% /
devtmpfs       devtmpfs   63G     0   63G   0% /dev
tmpfs          tmpfs      63G     0   63G   0% /dev/shm
tmpfs          tmpfs      63G   18M   63G   1% /run
tmpfs          tmpfs      63G     0   63G   0% /sys/fs/cgroup
tmpfs          tmpfs      13G   40K   13G   1% /run/user/0
/dev/sdb1      xfs        50G   33M   50G   1% /data1
```

## 自动挂载

我们上面手动将`/dev/sdb1`挂载到了路径`/data1`，但是手动挂载，主机重启后该挂载就会失效（不过虽然会失效，但设备中的内容不会丢失）。所以，我们需要设置在主机启动的时候能自动地将设备`/dev/sdb1`挂载到目录`/data1`下。在`/etc/fstab`中添加如下一行（关于语法的解释参考文章《fstab》），那么主机启动的时候，就会自动地进行挂载。

```
/dev/sdb1   /data1  xfs  defaults  0   0
```

需要注意的是，在`/etc/fstab`中可以实现自动挂载，但是在设置自动挂载前，还是需要先手动对设备`/dev/sdb1`格式化。

## 卸载

如果要卸载分区`/dev/sdb1`，直接执行命令`umount /dev/sdb1`或者`umount /data1`。`umount`后再进行挂载，原来设备的内容还是可以看到的。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pshizhsysu.gitbook.io/linux/ci-pan-yu-fen-qu/ge-shi-hua-yu-gua-zai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
