虚机磁盘扩容

vmware 的虚机磁盘扩容,从 vSphere 到 OS 层面

超详细

vSphere Client

不用关机,直接编辑虚拟机硬件,把磁盘大小改大,如 100G => 120G

确定后,等进度条,一般很快就完成

vmware 层面就搞定了

OS

OS 层面可能并未同步更新

可使用echo 1 > /sys/block/sda/device/rescan触发一下

1
2
3
4
5
6
7
8
9
10
11
12
13
fdisk -l

Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: Virtual disk

echo 1 > /sys/block/sda/device/rescan

fdisk -l

GPT PMBR size mismatch (209715199 != 251658239) will be corrected by write.
The backup GPT table is not on the end of the device.
Disk /dev/sda: 120 GiB, 128849018880 bytes, 251658240 sectors
Disk model: Virtual disk

由于是 AlmaLinux 9,这里我们并不使用 fdisk 进行分区调整,而是使用 parted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
parted -l

Warning: Not all of the space available to /dev/sda appears to be used, you can
fix the GPT to use all of the space (an extra 41943040 blocks) or continue with
the current setting?
Fix/Ignore? fix
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 129GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 630MB 629MB fat32 EFI System Partition boot, esp
2 630MB 1704MB 1074MB xfs
3 1704MB 107GB 106GB lvm


parted /dev/sda resizepart 3 100%

parted -l

Model: VMware Virtual disk (scsi)
Disk /dev/sda: 129GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 630MB 629MB fat32 EFI System Partition boot, esp
2 630MB 1704MB 1074MB xfs
3 1704MB 129GB 127GB lvm

此时的 pv 还未更新,使用pvresize生效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
pvdisplay

--- Physical volume ---
PV Name /dev/sda3
VG Name almalinux_211
PV Size 98.41 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 25193
Free PE 0
Allocated PE 25193
PV UUID 77PAja-dDP3-K2Ii-5jgw-AOFW-KnF5-ib8qg2

pvresize /dev/sda3

pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name almalinux_211
PV Size 118.41 GiB / not usable 1.98 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 30313
Free PE 5120
Allocated PE 25193
PV UUID 77PAja-dDP3-K2Ii-5jgw-AOFW-KnF5-ib8qg2

接着lvextend你要扩容的 lv,通过df查看挂载点对应的 lv,比如这里我们扩容 root

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
df -hT

Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 7.7G 0 7.7G 0% /dev/shm
tmpfs tmpfs 3.1G 65M 3.1G 3% /run
/dev/mapper/almalinux_211-root xfs 61G 56G 4.9G 93% /
/dev/sda2 xfs 960M 324M 637M 34% /boot
/dev/sda1 vfat 599M 7.1M 592M 2% /boot/efi
/dev/mapper/almalinux_211-home xfs 30G 4.5G 26G 16% /home

lvextend -l +100%FREE /dev/almalinux_211/root

Size of logical volume almalinux_211/root changed from <60.84 GiB (15574 extents) to <80.84 GiB (20694 extents).
Logical volume almalinux_211/root successfully resized.

这里直接再df查看会发现 root 并未扩大,需要最 grow 一下生效,这里是 xfs 所以使用 xfs_growfs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
xfs_growfs /dev/almalinux_211/root

meta-data=/dev/mapper/almalinux_211-root isize=512 agcount=4, agsize=3986944 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=15947776, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 15947776 to 21190656

df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 7.7G 0 7.7G 0% /dev/shm
tmpfs 3.1G 65M 3.1G 3% /run
/dev/mapper/almalinux_211-root 81G 56G 26G 69% /
/dev/sda2 960M 324M 637M 34% /boot
/dev/sda1 599M 7.1M 592M 2% /boot/efi
/dev/mapper/almalinux_211-home 30G 4.5G 26G 16% /home

打完收工