查看需要扩容磁盘大小
root@debian:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 487M 0 part /boot
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 29.5G 0 part
├─debian--vg-root 254:0 0 28.6G 0 lvm /
└─debian--vg-swap_1 254:1 0 980M 0 lvm [SWAP]
sr0 11:0 1 378M 0 rom
由上述信息所知,sda为30G,debian–vg-root为28.6G,本次的目的为将debian–vg-root扩容10G
关机扩容磁盘
开机查看
root@debian:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 487M 0 part /boot
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 29.5G 0 part
├─debian--vg-root 254:0 0 28.6G 0 lvm /
└─debian--vg-swap_1 254:1 0 980M 0 lvm [SWAP]
sr0 11:0 1 378M 0 rom
现在已经看到,sda为40G,物理磁盘成功扩容了,但是sda5及debian–vg-root还是原来的大小,需要使用growpart及lvm进行分区及文件系统的扩容
不关机的情况下需要重新扫描磁盘
for host in $(ls /sys/class/scsi_host) ; do echo "- - -" > /sys/class/scsi_host/$host/scan; done
for scsi_device in $(ls /sys/class/scsi_device/); do echo 1 > /sys/class/scsi_device/$scsi_device/device/rescan; done
安装cloud-guest-utils
root@debian:~# apt install cloud-guest-utils
Debian系(Debian/Ubuntu等)安装cloud-guest-utils,红帽系(Redhat/Centos等)安装cloud-utils-growpart,其他Linux系统执行查找安装相应工具
扩容分区
root@debian:~# growpart /dev/sda 2
CHANGED: partition=2 start=1001470 old: size=61913057 end=62914527 new: size=82884577 end=83886047
root@debian:~# growpart /dev/sda 5
CHANGED: partition=5 start=1001472 old: size=61913055 end=62914527 new: size=82884575 end=83886047
将sda5扩容,不知道是不是bug还是debian下的情况,直接运行growpart /dev/sda 5会报错/dev/sda3: No free sectors available,所以先扩容sda5上面的sda2,再移动到sda5上面。如果有明白其中原理的朋友请指点一下
查看新分区大小
root@debian:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 487M 0 part /boot
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 39.5G 0 part
├─debian--vg-root 254:0 0 28.6G 0 lvm /
└─debian--vg-swap_1 254:1 0 980M 0 lvm [SWAP]
sr0 11:0 1 378M 0 rom
经过上面的操作,我们已经看到sda5的大小已经增加了10G,但是debian–vg-root还没有进行扩容,下面使用lvm进行问价系统的扩容
刷新pv物理卷
root@debian:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda5 debian-vg lvm2 a-- <29.52g 0
root@debian:~# pvscan
PV /dev/sda5 VG debian-vg lvm2 [<29.52 GiB / 0 free]
Total: 1 [<29.52 GiB] / in use: 1 [<29.52 GiB] / in no VG: 0 [0 ]
root@debian:~# pvresize /dev/sda5
Physical volume "/dev/sda5" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
root@debian:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda5 debian-vg lvm2 a-- <39.52g 10.00g
通过pvresize,可以看到PFree已经由0变为10G,下面只需要将这10G加入到debian–vg-root即可
查看逻辑卷
root@debian:~# lvdisplay
--- Logical volume ---
LV Path /dev/debian-vg/root
LV Name root
VG Name debian-vg
LV UUID hTstOe-wqvC-vGkX-KYv1-MgrB-7B1j-e9YmXt
LV Write Access read/write
LV Creation host, time debian, 2022-09-30 19:53:43 +0800
LV Status available
# open 1
LV Size 28.56 GiB
Current LE 7312
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0
--- Logical volume ---
LV Path /dev/debian-vg/swap_1
LV Name swap_1
VG Name debian-vg
LV UUID HIdZqn-dg8F-tnD1-zPCO-h3os-1K3A-y4zVap
LV Write Access read/write
LV Creation host, time debian, 2022-09-30 19:53:43 +0800
LV Status available
# open 2
LV Size 980.00 MiB
Current LE 245
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:1
用lvdisplay查看下逻辑卷,我们需要扩容的逻辑卷为/dev/debian-vg/root
扩容逻辑卷
root@debian:~# lvextend -L +10G /dev/debian-vg/root
Size of logical volume debian-vg/root changed from 28.56 GiB (7312 extents) to 38.56 GiB (9872 extents).
Logical volume debian-vg/root successfully resized.
root@debian:~# resize2fs /dev/debian-vg/root
resize2fs 1.46.2 (28-Feb-2021)
Filesystem at /dev/debian-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 5
The filesystem on /dev/debian-vg/root is now 10108928 (4k) blocks long.
经过朋友提示,可以直接使用lvextend -rL +10G /dev/debian-vg/root进行逻辑卷的扩容,-r参数会自动判断文件系统,不需要再运行resize2fs
还可以直接使用lvextend -rl +100%FREE /dev/debian-vg/root将剩余容量全部加入
查看结果
root@debian:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 487M 0 part /boot
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 39.5G 0 part
├─debian--vg-root 254:0 0 38.6G 0 lvm /
└─debian--vg-swap_1 254:1 0 980M 0 lvm [SWAP]
sr0 11:0 1 378M 0 rom
root@debian:~# df -h
文件系统 容量 已用 可用 已用% 挂载点
udev 957M 0 957M 0% /dev
tmpfs 195M 592K 195M 1% /run
/dev/mapper/debian--vg-root 38G 1.4G 35G 4% /
tmpfs 975M 0 975M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 470M 86M 360M 20% /boot
tmpfs 195M 0 195M 0% /run/user/0
最后我们发现/dev/debian-vg/root已经由原来的28.6G扩容为38.6G,目标完成
感谢
感谢Redhat培训讲师陈老师对此文的帮助及修正