> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/di-25-zhang-ufs-wen-jian-xi-tong/di-25.2-jie-tian-jia-ufs-ci-pan.md).

# 25.2 添加 UFS 磁盘

本节介绍如何向当前仅有一块硬盘的计算机添加新的 NVMe 固态硬盘（Solid-State Drive, SSD）。首先，关闭计算机并根据计算机、控制器和硬盘厂商的说明安装硬盘。重启系统并切换为 `root` 用户。

检查 **/var/run/dmesg.boot** 文件，确保新硬盘已被识别。

```sh
nda1 at nvme0 bus 0 scbus2 target 0 lun 2
nda1: <VMware Virtual NVMe Disk 1.4 VMware NVME_0000>
nda1: Serial Number VMware NVME_0000
nda1: nvme version 1.4
nda1: 10240MB (20971520 512 byte sectors)

……省略其他无关输出……
```

本例中，新增的 10G 大小的 NVMe 硬盘显示为 **nda1**。

## 创建分区表

本示例在新硬盘上创建一个单一分区。优先使用 GPT 分区方案，而非灵活性较差的 MBR 方案。

> **注意**
>
> 如果要添加的磁盘非空，可使用 `gpart delete` 删除旧的分区信息。
>
> **示例 1** 删除 **nda1** 磁盘上的第 **1** 个分区：
>
> ```sh
> # gpart delete -i 1 nda1
> ```
>
> **示例 2** 销毁 **nda1** 磁盘上的整个分区表及所有分区：
>
> ```sh
> # gpart destroy -F nda1
> ```

为 **nda1** 创建 GPT 分区表：

```sh
# gpart create -s GPT nda1
```

## 创建单一分区

随后添加一个分区。为提升在较大硬件块大小的硬盘上的性能，分区对齐到 1MB 边界：

```sh
# gpart add -t freebsd-ufs -a 1M nda1
```

> **技巧**
>
> 此处的 `-t` 仅用于写入分区类型的 GUID，并非实际执行格式化，与格式化的文件系统无关。

可以使用 `gpart show` 查看磁盘分区信息：

```sh
# gpart show -p nda1
=>      40  20971440    nda1  GPT  (10G)
        40      2008          - free -  (1004K)
      2048  20967424  nda1p1  freebsd-ufs  (10G)
  20969472      2008          - free -  (1004K)
```

以上命令创建了 GPT 分区表，新建了 10G 容量的磁盘分区 **nda1p1**，文件系统使用 UFS。

## 创建多个分区

为便于使用，可创建多个分区。

以下为磁盘 **nda1** 创建 4 个分区，容量分别为 1GB、2GB、3GB 和 4GB。使用 `-s` 参数指定分区大小（支持 G、M 等单位）。

* 创建第 1 个分区：1G

```sh
# gpart add -t freebsd-ufs -a 1M -s 1G nda1
```

* 创建第 2 个分区：2G

```sh
# gpart add -t freebsd-ufs -a 1M -s 2G nda1
```

* 创建第 3 个分区：3G

```sh
# gpart add -t freebsd-ufs -a 1M -s 3G nda1
```

* 创建第 4 个分区，使用剩余全部容量：

```sh
# gpart add -t freebsd-ufs -a 1M nda1
```

可以使用 `gpart show` 查看磁盘分区信息：

```sh
# gpart show -p nda1
=>      40  20971440    nda1  GPT  (10G)
        40      2008          - free -  (1004K)
      2048   2097152  nda1p1  freebsd-ufs  (1.0G)
   2099200   4194304  nda1p2  freebsd-ufs  (2.0G)
   6293504   6291456  nda1p3  freebsd-ufs  (3.0G)
  12584960   8384512  nda1p4  freebsd-ufs  (4.0G)
  20969472      2008          - free -  (1004K)
```

以上命令分别创建了分区 **nda1p1**、**nda1p2**、**nda1p3**、**nda1p4**。

## 格式化文件系统

在新硬盘的新分区上创建文件系统：

```sh
# newfs -U /dev/nda1p1
```

一次性格式化 **/dev/nda1p1**、**/dev/nda1p2**、**/dev/nda1p3**、**/dev/nda1p4**：

```sh
# for i in 1 2 3 4; do newfs -U /dev/nda1p$i; done
```

## 挂载文件系统

创建一个空目录作为 **挂载点**，即把新硬盘挂载到原硬盘文件系统中的位置：

```sh
# mkdir -p /mnt/newdisk
```

临时挂载 **/dev/nda1p3** 到 **/mnt/newdisk**：

```sh
# mount /dev/nda1p3 /mnt/newdisk
```

执行写入测试：

```sh
# cd /mnt/newdisk && touch test.txt
/mnt/newdisk # ls -al
total 9
drwxr-xr-x  3 root wheel    512 May 30 17:42 .
drwxr-xr-x  4 root wheel      4 May 30 17:39 ..
drwxrwxr-x  2 root operator 512 May 30 17:32 .snap
-rw-r--r--  1 root wheel      0 May 30 17:42 test.txt
```

最后，在 **/etc/fstab** 中添加一个条目，使新硬盘在启动时自动挂载：

```sh
/dev/nda1p3     /mnt/newdisk    ufs     rw              0       0
```

可以在不重启系统的情况下手动挂载新硬盘：

```sh
# mount /mnt/newdisk
```

验证挂载情况：

使用 `mount` 命令验证（不附带参数）

```sh
# mount
/dev/nda1p3 on /mnt/newdisk (ufs, local, soft-updates)

……省略其他输出……
```

也可使用 `df` 命令验证挂载情况：

```sh
# df -hl
/dev/nda1p3           2.9G    8.0K    2.7G     0%    /mnt/newdisk

……省略其他输出……
```

查看已挂载的文件系统内容：

```sh
# ls /mnt/newdisk/
/mnt/newdisk/.snap/    /mnt/newdisk/test.txt
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/di-25-zhang-ufs-wen-jian-xi-tong/di-25.2-jie-tian-jia-ufs-ci-pan.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
