理论基础可参考 https://linux.cn/article-12670-1.html
测试中使用loop设备模拟硬盘, 测试添加,扩容,移除等操作
初始化脚本
# 创建并挂在4个loop设备, 模拟4块硬盘
for i in {1..5} ; do
dd if=/dev/zero of=loop-"$i".img bs=1M count=40
sudo losetup /dev/loop"$i" loop-"$i".img
done
# 创建物理卷
sudo pvcreate /dev/loop1 /dev/loop2 /dev/loop3 /dev/loop4
# 创建卷组
sudo vgcreate vg01 /dev/loop1 /dev/loop2
sudo vgcreate vg02 /dev/loop3 /dev/loop4
sudo lvcreate -l 10 -n lv01 vg01
sudo lvcreate -l 10 -n lv02 vg02
# 默认PE大小4M, -s修改
# sudo lvcreate -l 10 -s 16M -n lv02 vg02
# 格式化逻辑卷
sudo mkfs.ext4 /dev/vg01/lv01
sudo mkfs.ext4 /dev/vg02/lv02
# 挂载
sudo mkdir /lv01 /lv02
sudo mount /dev/vg01/lv01 /lv01
sudo mount /dev/vg02/lv02 /lv02
卷组vg扩容
# 给lv01卷添加一块硬盘
sudo pvcreate /dev/loop5
# lv01用的是vg01组,添加到vg01组
sudo vgextend vg01 /dev/loop5
# 查看详情
sudo vgdisplay -v vg01
--- Volume group ---
VG Name vg01
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 108.00 MiB
PE Size 4.00 MiB
Total PE 27
Alloc PE / Size 18 / 72.00 MiB
Free PE / Size 9 / 36.00 MiB
VG UUID FYhnWm-dd6B-P8kj-MK9S-zxR8-W3ve-MPrxGH
--- Logical volume ---
LV Path /dev/vg01/lv01
LV Name lv01
VG Name vg01
LV UUID emAeFr-2YB1-fTJo-uI0h-5mLc-rw7P-9rwlyr
LV Write Access read/write
LV Creation host, time kali, 2022-02-15 13:01:37 +0800
LV Status available
# open 1
LV Size 72.00 MiB
Current LE 18
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Physical volumes ---
PV Name /dev/loop1
PV UUID ViWGQr-fuwf-59AL-Nk29-IJsb-CdCO-EuIPCZ
PV Status allocatable
Total PE / Free PE 9 / 0
PV Name /dev/loop2
PV UUID P5ttDN-p9p0-2XRk-Db0X-3z5t-Yzu6-QztWqW
PV Status allocatable
Total PE / Free PE 9 / 0
PV Name /dev/loop5
PV UUID S2SQBz-pV7Z-8r0z-8cLO-de9Q-dMoD-fQ8I6X
PV Status allocatable
Total PE / Free PE 9 / 9
逻辑卷lv扩容
sudo lvextend -L +30M /dev/vg01/lv01
# sudo lvextend -L +30GB /dev/vg01/lv01
sudo umount /dev/vg01/lv01
sudo e2fsck -f /dev/vg01/lv01
sudo resize2fs /dev/vg01/lv01
sudo mount /dev/vg01/lv01 /lv01
df -h /lv01
# 文件系统 容量 已用 可用 已用% 挂载点
# /dev/mapper/vg01-lv01 94M 1.6M 85M 2% /lv01
其他
# lv卷缩容
sudo lvreduce -L 30M -r /dev/vg01/lv01
# 移动pv
sudo pvmove /dev/loop1 /dev/loop5
sudo vgdisplay -v vg01
# 从vg卷中删除pv
sudo vgreduce vg01 /dev/loop1
# 从pv中删除设备
sudo pvremove /dev/loop1