VMware 不关机扩容 linux 磁盘

LVM 类型的分区扩容

很多时候可能会看到如下分区情况,对于运维来说其实是不友好的:

1
2
3
4
5
6
7
8
root@test:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
...
sdb 8:16 0 1T 0 disk
└─harbor--vg-harbor--lv 253:1 0 2048G 0 lvm /data
sdc 8:16 0 1T 0 disk
└─harbor--vg-harbor--lv 253:1 0 2048G 0 lvm /data
...

如果节点是虚拟机,其实支持在原有磁盘上直接进行扩容操作,操作步骤如下。

在虚拟化平台对原有磁盘进行扩容,如上则是 sdb,扩容后在系统中刷新磁盘容量信息:

1
echo 1 > /sys/class/block/sdb/device/rescan

使用 pvresize 扩展 PV:

1
pvresize /dev/sdb

扩容逻辑卷和文件系统:

1
2
3
4
lvextend -l +100%FREE /dev/harbor-vg/harbor-lv

# 或 xfs_growfs /data(取决于文件系统)
resize2fs /dev/harbor-vg/harbor-lv

LVM 类型的分区扩容(parted 工具)

现有分区信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@docker-rancher:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 63.8M 1 loop /snap/core20/2571
loop1 7:1 0 63.8M 1 loop /snap/core20/2582
loop2 7:2 0 87M 1 loop /snap/lxd/29351
loop3 7:3 0 44.4M 1 loop /snap/snapd/23771
loop4 7:4 0 89.4M 1 loop /snap/lxd/31333
loop5 7:5 0 50.9M 1 loop /snap/snapd/24505
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot/efi
├─sda2 8:2 0 2G 0 part /boot
└─sda3 8:3 0 96.9G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 96.9G 0 lvm /

在虚拟化平台对 sda 扩容后,手动 rescan 一下使 OS 能够感知:

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

通过 parted 工具先修复 GPT 分区表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@docker-rancher:~# parted /dev/sda
GNU Parted 3.4
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
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 104857600 blocks) or continue with the current setting?
Fix/Ignore? fix
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 1128MB 1127MB fat32 boot, esp
2 1128MB 3276MB 2147MB ext4
3 3276MB 107GB 104GB

(parted) quit

扩展 sda3 分区:

1
parted /dev/sda --script resizepart 3 100%

扩容:

1
2
3
pvresize /dev/sda3
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
xfs_growfs /

效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@docker-rancher:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 63.8M 1 loop /snap/core20/2571
loop1 7:1 0 63.8M 1 loop /snap/core20/2582
loop2 7:2 0 87M 1 loop /snap/lxd/29351
loop3 7:3 0 44.4M 1 loop /snap/snapd/23771
loop4 7:4 0 89.4M 1 loop /snap/lxd/31333
loop5 7:5 0 50.9M 1 loop /snap/snapd/24505
sda 8:0 0 150G 0 disk
├─sda1 8:1 0 1G 0 part /boot/efi
├─sda2 8:2 0 2G 0 part /boot
└─sda3 8:3 0 146.9G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 146.9G 0 lvm /
Author

Warner Chen

Posted on

2025-05-24

Updated on

2025-06-04

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.