> 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.3-jie-ufs-ci-pan-kuo-zhan.md).

# 25.3 UFS 磁盘扩展

> **警告**
>
> UFS 文件系统只能扩大，不能缩小。

使用 `gpart show` 命令查看系统中所有磁盘的分区布局：

```sh
# gpart show
=>       3  41943035  da0  GPT  (20G)
         3       122    1  freebsd-boot  (61K)
       125     66584    2  efi  (33M)
     66709   2097152    3  freebsd-swap  (1.0G)
   2163861  10486633    4  freebsd-ufs  (5.0G)
  12650494  29292544       - free -  (14G)
```

系统盘上的 UFS 分区初始大小为 5 GB，输出显示 **da0** 磁盘仅含一个 UFS 分区。

> **注意**
>
> 此方案仅适用于向后扩展；如果 `freebsd-ufs` 分区前方存在空余空间，则无法使用此方法扩展。

## 执行扩容命令

> **警告**
>
> 如果使用的是 GPT 分区表，扩容操作在虚拟机或云服务器环境中可能会破坏 GPT 分区表，原因是虚拟机或云平台在调整磁盘大小时可能未正确更新 GPT 分区表的备份副本，因此需要先恢复 `da0` 磁盘的分区表：
>
> ```sh
> # gpart recover da0
> ```
>
> 执行上述操作后，后续步骤保持一致。

调整 da0 磁盘上编号为 4 的 `freebsd-ufs` 分区大小：

```sh
# gpart resize -i 4 da0
da0p4 resized
```

选项 `-i` 用于指定待扩容的分区编号，本例用于扩展 `freebsd-ufs` 分区。

## 扩展文件系统

使用 `growfs` 服务扩展文件系统，该操作仅需执行一次，完成后无需重复运行：

```sh
# service growfs onestart
Growing root partition to fill device
da0 recovering is not needed
da0p4 resized
growfs: no room to allocate last cylinder group; leaving 7.7MB unused
super-block backups (for fsck_ffs -b #) at:
 11544384, 12827072, 14109760, 15392448, 16675136, 17957824, 19240512, 20523200, 21805888, 23088576, 24371264,
 25653952, 26936640, 28219328, 29502016, 30784704, 32067392, 33350080, 34632768, 35915456, 37198144, 38480832
```

growfs 是 FreeBSD 用于扩展 UFS 文件系统的工具，通过调整柱面组和超级块来利用新增的分区空间。可使用 `growfs -N` 在测试模式下预览扩展参数而不实际执行。

## 验证扩容结果

查看已挂载文件系统的磁盘使用情况：

```sh
# df -hl
Filesystem         Size    Used   Avail Capacity  Mounted on
/dev/gpt/rootfs     18G    4.8G     12G    29%    /
devfs              1.0K      0B    1.0K     0%    /dev
/dev/gpt/efiesp     32M    651K     31M     2%    /boot/efi
tmpfs               20M    4.0K     20M     0%    /tmp
tmpfs               32M    156K     32M     0%    /var
```

参数说明：

* `-h`：以易读格式显示，单位为 KB、MB、GB 等。
* `-l`：仅显示本地文件系统。

输出显示分区扩展操作已完成，文件系统已调整至新的大小。

## 参考文献

* FreeBSD Project. ffs -- Berkeley fast file system\[EB/OL]. \[2026-04-14]. <https://man.freebsd.org/cgi/man.cgi?query=ffs&sektion=4>. UFS/FFS 文件系统概述手册页，描述伯克利快速文件系统的设计与实现。
* FreeBSD Project. mount\_ufs -- mount a UFS file system\[EB/OL]. \[2026-04-14]. <https://man.freebsd.org/cgi/man.cgi?query=mount&sektion=8>. UFS 文件系统挂载命令手册页。
* FreeBSD Project. growfs -- expand a UFS file system\[EB/OL]. \[2026-04-14]. <https://man.freebsd.org/cgi/man.cgi?query=growfs&sektion=8>. UFS 文件系统扩容工具手册页。
* FreeBSD Project. fsck\_ffs -- file system consistency check and interactive repair\[EB/OL]. \[2026-04-14]. <https://man.freebsd.org/cgi/man.cgi?query=fsck_ffs&sektion=8>. UFS 文件系统一致性检查与修复工具手册页。


---

# 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.3-jie-ufs-ci-pan-kuo-zhan.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.
