Linux不使用LVM, 在线扩展磁盘大小

首先在Proxmox页面扩展VM磁盘的大小(Resize disk), 此处扩展20GB.

在VM内找到这个需要扩展的磁盘, 此处是/dev/sda. 因为采用的是VirtIO SCSI, 所以VM内此时dmesg会提示如下的消息:

1
2
3
[1478256.083257] sd 2:0:0:0: Capacity data has changed
[1478256.083554] sd 2:0:0:0: [sda] 251658240 512-byte logical blocks: (129 GB/120 GiB)
[1478256.083652] sda: detected capacity change from 107374182400 to 128849018880

使用fdisk命令打开这个磁盘, 可以看到GPT PMBR size mismatch的错误, 稍后会修复这个问题.

1
2
3
4
5
6
7
8
root@lsp-sg-game-1:~# fdisk /dev/sda

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

GPT PMBR size mismatch (209715199 != 251658239) will be corrected by write.
The backup GPT table is not on the end of the device. This problem will be corrected by write.

使用 p 命令查看当前磁盘上的分区

1
2
3
4
5
6
7
8
9
10
11
12
13
Command (m for help): p

Disk /dev/sda: 120 GiB, 128849018880 bytes, 251658240 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: gpt
Disk identifier: 04727CFD-CF21-418E-9D6E-3521DAC444F4

Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 209715166 209711071 100G Linux filesystem

删除/dev/sda上的2号分区, 即/dev/sda2

1
2
3
4
Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

新建2号分区, First sectorLast sector保持默认即可, 提示已存在ext4文件系统时注意不要删除.

1
2
3
4
5
6
7
8
9
Command (m for help): n
Partition number (2-128, default 2): 2
First sector (4096-251658206, default 4096):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4096-251658206, default 251658206):

Created a new partition 2 of type 'Linux filesystem' and of size 120 GiB.
Partition #2 contains a ext4 signature.

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

最后使用w命令保存.

1
2
3
4
Command (m for help): w

The partition table has been altered.
Syncing disks.

使用partprobe命令重新加载分区表, 此时再使用dffdisk命令分别查看文件系统和磁盘分区

1
2
3
root@lsp-sg-game-1:~# df -h .
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 99G 69G 25G 74% /
1
2
3
4
5
6
7
8
9
10
11
12
root@lsp-sg-game-1:~# fdisk -l /dev/sda
Disk /dev/sda: 120 GiB, 128849018880 bytes, 251658240 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: gpt
Disk identifier: 04727CFD-CF21-418E-9D6E-3521DAC444F4

Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 251658206 251654111 120G Linux filesystem

此时会发现磁盘分区已经扩大了, 但是文件系统还没有. 使用resize2fs命令完成这个扩展

1
2
3
4
5
root@lsp-sg-game-1:~# resize2fs /dev/sda2
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 15
The filesystem on /dev/sda2 is now 31456763 (4k) blocks long.

再使用df命令查看, 可以发现文件系统已经扩展完成了.

1
2
3
root@lsp-sg-game-1:~# df -h .
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 118G 65G 48G 58% /

以上只针对Proxmox VirtIO磁盘进行的在线升级. 对于不支持在线升级的情况, 使用gparted ISO重启VM, 然后按照页面提示操作即可.

参考

Expanding virtual disk without downtime - is it possible?

Linux追加磁盘扩展

Resize a Linux filesystem without LVM

Changing a partition with fdisk shows a warning like “partition#x contains ext4-signature”

Resize disks - Proxmox VE