不關機擴充 VM 的 GPT 磁碟格式空間

請搭配 https://xbddc.github.io/2018/12/21/expand-space-without-rebooting-vm/ 服用。

操作步驟

  1. 擴充 Raw Disk 磁碟大小
  2. 確認 Linux 已偵測到變更
1
2
3
4
5
# dmesg | tail
[3362536.189973] virtio_blk virtio1: new size: 71720960 512-byte logical blocks (36.7 GB/34.2 GiB)
[3362536.189984] vda: detected capacity change from 19541262336 to 36721131520
[3362866.358221] EXT4-fs (vda1): resizing filesystem from 4742395 to 8936699 blocks
[3362866.389273] EXT4-fs (vda1): resized filesystem to 8936699
  1. 找到要 resize 的磁碟編號。注意只能是位於最末段的磁碟,如下的 vda1:
1
2
3
4
5
# df
Filesystem 1K-blocks Used Available Use% Mounted on
...
/dev/vda1 18318068 16648976 1652708 91% /
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# sgdisk -p /dev/vda
Disk /dev/vda: 38166527 sectors, 18.1 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 877716F7-31D0-4D56-A1ED-4D566EFE418E
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 38166494
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number Start (sector) End (sector) Size Code Name
1 227328 38166494 18.0 GiB 8300
14 2048 10239 4.0 MiB EF02
15 10240 227327 106.0 MiB EF00
  1. 刪除並重新建立第 1 個分割區,並更新 partition table 取得新的設定資訊
1
2
3
# sgdisk -d 1 /dev/vda 
# sgdisk -N 1 /dev/vda
# partprobe /dev/vda
  1. 最後就執行 resize2fs 調整 ext2 大小:
1
2
3
4
5
# resize2fs /dev/vda1
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/vda1 is now 8936699 (4k) blocks long.
  1. 確認一下結果,可以發現使用率從原本的 91% 降至 49%:
1
2
3
4
5
# df
Filesystem 1K-blocks Used Available Use% Mounted on
...
/dev/vda1 34578052 16649988 17911680 49% /
...

這樣就完成啦!

References