使用nas过程中,linux起始盘一般设置为32G,结果在开始使用docker后,盘不够大,很快就满了。因此,如何拓展硬盘呢?

注意:本文仅适合PVE、云服务器、各类虚拟机的硬盘扩容,不适合物理机扩容。本次演示系统是DEBIAN

安装工具

# apt install -y cloud-guest-utils
// ↑ 提供growpart命令工具
# apt install parted
// ↑ 提供parted命令工具

本文涉及到的命令:fdisklsblkpartproberesize2fsgrowpart

查看磁盘

首先使用fdisk命令查看磁盘状态。

root@NasServer:~# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p                              //这里
Disk /dev/sdc: 64 GiB, 68719476736 bytes, 134217728 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x423e575f

Device     Boot    Start       End  Sectors Size Id Type
/dev/sdc1  *        2048  65107967 65105920  31G 83 Linux    //需要对这个磁盘扩容
/dev/sdc2       65107968 134217727 69109760  33G 83 Linux

Command (m for help): d    //删除磁盘
Partition number (1,2, default 2): 2     //选择2号盘

Partition 2 has been deleted.

Command (m for help): w    //保存修改
The partition table has been altered.
Syncing disks.

扩容磁盘

此时我们可以用命令fdisk查看磁盘情况

root@NasServer:~# fdisk -l
Disk /dev/sdc: 64 GiB, 68719476736 bytes, 134217728 sectors      //总容量64G
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x423e575f

Device     Boot Start      End  Sectors Size Id Type
/dev/sdc1  *     2048 65107967 65105920  31G 83 Linux      //可以看到只有一个盘了

root@NasServer:~# growpart /dev/sdc 1                      //扩容命令
-bash: growpart:未找到命令

结果上网一查才知道,这个命令需要额外安装软件包。

# apt install -y cloud-guest-utils
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成
将会同时安装下列软件:
  gdisk
建议安装:
  cloud-init
下列【新】软件包将被安装:
  cloud-guest-utils gdisk
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 244 kB 的归档。
解压缩后会消耗 959 kB 的额外空间。
获取:1 http://mirrors.tuna.tsinghua.edu.cn/debian bullseye/main amd64 cloud-guest-utils all 0.31-2 [23.7 kB]
获取:2 http://mirrors.tuna.tsinghua.edu.cn/debian bullseye/main amd64 gdisk amd64 1.0.6-1.1 [220 kB]
已下载 244 kB,耗时 0秒 (839 kB/s)
正在选中未选择的软件包 cloud-guest-utils。
(正在读取数据库 ... 系统当前共安装有 40207 个文件和目录。)
准备解压 .../cloud-guest-utils_0.31-2_all.deb  ...
正在解压 cloud-guest-utils (0.31-2) ...
正在选中未选择的软件包 gdisk。
准备解压 .../gdisk_1.0.6-1.1_amd64.deb  ...
正在解压 gdisk (1.0.6-1.1) ...
正在设置 gdisk (1.0.6-1.1) ...
正在设置 cloud-guest-utils (0.31-2) ...
正在处理用于 man-db (2.9.4-2) 的触发器 ...

之后再次执行命令

root@NasServer:~# growpart /dev/sdc 1
CHANGED: partition=1 start=2048 old: size=65105920 end=65107968 new: size=134215647 end=134217695
root@NasServer:~# resize2fs /dev/sdc1
resize2fs 1.46.2 (28-Feb-2021)
Filesystem at /dev/sdc1 is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 8
The filesystem on /dev/sdc1 is now 16776955 (4k) blocks long.

至此,扩容已完成,我们使用另一个命令看下:

lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  3.6T  0 disk
sdb      8:16   0  3.6T  0 disk
sdc      8:32   0   64G  0 disk
└─sdc1   8:33   0   64G  0 part /                            //可以看到这里已经有64G的容量了。

参考资料