背景

pve 上的 k8s 虚拟机分配的硬盘容量告急,需要进行扩容操作。操作系统为 Debian 12。 虚拟机硬盘原始大小为 96G,计划扩容为 128G。

操作方式

先在 PVE GUI 页面上对特定硬盘增加容量。

进入虚拟机终端,查看 dmesg 日志,可以发现系统检测到了硬盘容量变更。

root@rancher-homelab-1:~# dmesg | grep 'capacity change'
[3419061.180984] sda: detected capacity change from 201326592 to 268435456

使用 fdisk 命令查看当前分区,显示仍为 96G。

root@rancher-homelab-2:~# fdisk -l /dev/sda
Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 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: 0xda2259ac

Device     Boot Start       End   Sectors Size Id Type
/dev/sda1  *     2048 201326591 201324544  96G 83 Linux

使用 fdisk 命令进行分区扩容。

root@rancher-homelab-1:~# fdisk /dev/sda

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

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help):

先输入 p 命令打印分区信息。可以看到识别到硬盘 /dev/sda 大小为 128G,但分区 /dev/sda1 只有 95G。

Command (m for help): p

Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 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: 0xda2259ac

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 201326591 201324544   95G 83 Linux

输入 d 命令删除分区,并输入 p 检查分区是否已删除。

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): p
Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 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: 0xda2259ac

输入 n 命令创建分区,选择 primary

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-268435455, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-268435455, default 268435455):

Created a new partition 1 of type 'Linux' and of size 128 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): p

Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 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: 0xda2259ac

Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1        2048 268435455 268433408  128G 83 Linux

输入 a 命令将该分区标记为 bootable。

Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.

Command (m for help): p
Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 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: 0xda2259ac

Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1  *     2048 268435455 268433408  128G 83 Linux

输入 w 命令写入新的分区。

Command (m for help): w
The partition table has been altered.
Syncing disks.

使用 df 命令查看文件系统仍然显示为 95G。

root@rancher-homelab-2:~# df -h
Filesystem                                              Size  Used Avail Use% Mounted on
udev                                                     16G     0   16G   0% /dev
tmpfs                                                   3.2G  8.5M  3.2G   1% /run
/dev/sda1                                                95G   55G   36G  61% /
tmpfs                                                    16G     0   16G   0% /dev/shm
tmpfs                                                   5.0M     0  5.0M   0% /run/lock
tmpfs                                                   3.2G     0  3.2G   0% /run/user/0

执行命令 resize2fs /dev/sda1

root@rancher-homelab-2:~# resize2fs /dev/sda1
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 12, new_desc_blocks = 16
The filesystem on /dev/sda1 is now 33554176 (4k) blocks long.

重新使用 df 命令检查,可以观察到正确识别为 126G。

root@rancher-homelab-2:~# df -h
Filesystem                                              Size  Used Avail Use% Mounted on
udev                                                     16G     0   16G   0% /dev
tmpfs                                                   3.2G  8.5M  3.2G   1% /run
/dev/sda1                                               126G   55G   66G  46% /
tmpfs                                                    16G     0   16G   0% /dev/shm
tmpfs                                                   5.0M     0  5.0M   0% /run/lock
tmpfs                                                   3.2G     0  3.2G   0% /run/user/0