LVM缩扩容操作
已有分区缩容至其他LVM分区
首先使用lvscan命令扫描LVM分区,确认缩扩容方案(这里将home分区缩减至10G,剩余空间全部分配至root分区)
[root@it-zoomvpn-master ~]# lvscan
ACTIVE '/dev/centos/swap' [7.75 GiB] inherit
ACTIVE '/dev/centos/home' [41.70 GiB] inherit
ACTIVE '/dev/centos/root' [50.00 GiB] inherit
[root@it-zoomvpn-master ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 99.5G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 7.8G 0 lvm [SWAP]
└─centos-home 253:2 0 41.7G 0 lvm
sr0 11:0 1 1024M 0 rom
接下来备份home分区至tmp目录,(若home分区文件较多,可使用xfsdump工具进行备份)
[root@it-zoomvpn-master ~]# cp -R /home/ /tmp/
备份完成后,卸载home分区(卸载时若出现target busy,可使用fuser -km /home命令强制终止相关进程)
[root@it-zoomvpn-master ~]# umount /home
umount: /home:目标忙。
(有些情况下通过 lsof(8) 或 fuser(1) 可以
找到有关使用该设备的进程的有用信息)
[root@it-zoomvpn-master ~]# fuser -km /home
/home: 19524ce
[root@it-zoomvpn-master ~]# umount /home
接下来使用lvreduce命令,将home分区缩减到10G
[root@it-zoomvpn-master ~]# lvreduce -L 10G /dev/centos/home
WARNING: Reducing active logical volume to 10.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce home? [y/n]: y
Size of logical volume centos/home changed from 41.70 GiB (10674 extents) to 10.00 GiB (2560 extents).
Logical volume home successfully resized.
使用lvextend命令将压缩出来的可用空间全部分配至root分区
[root@it-zoomvpn-master ~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 81.76 GiB (20930 extents).
Logical volume root successfully resized.
使用xfs_growfs延展root分区(分区格式为ext2/3/4时,使用resize2fs命令)
[root@it-zoomvpn-master ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 21432320
接下来恢复home分区,首先将压缩后的home分区格式化
[root@it-zoomvpn-master ~]# mkfs.xfs -f /dev/mapper/centos-home
meta-data=/dev/mapper/centos-home isize=256 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
挂载home分区,恢复原home分区文件
[root@it-zoomvpn-master ~]# mount /home
[root@it-zoomvpn-master ~]# cp -R /tmp/home/ /
[root@it-zoomvpn-master home]# ll /home
总用量 8
drwx------. 6 root root 4096 2月 26 17:25 ituser
drwx------. 14 root root 4096 2月 26 17:25 tom
[root@it-zoomvpn-master home]# chown -R ituser:ituser /home/ituser/
#恢复过来的文件夹owner是root,这里需要手动更改为正确的owner
[root@it-zoomvpn-master home]# ll
总用量 8
drwx------. 6 ituser ituser 4096 2月 26 17:25 ituser
drwx------. 14 root root 4096 2月 26 17:25 tom
最后确认下各分区空间,扩容完成。
[root@it-zoomvpn-master home]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 99.5G 0 part
├─centos-root 253:0 0 81.8G 0 lvm /
├─centos-swap 253:1 0 7.8G 0 lvm [SWAP]
└─centos-home 253:2 0 10G 0 lvm /home
新增磁盘扩容至已有LVM分区
添加磁盘后,查看现有磁盘结构
[root@SSHVL037 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 500G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 31G 0 part │ ├─centos-root 253:0 0 127.8G 0 lvm / │ └─centos-swap 253:1 0 3.2G 0 lvm [SWAP] ├─sda3 8:3 0 100G 0 part │ └─centos-root 253:0 0 127.8G 0 lvm / ├─sda4 8:4 0 1K 0 part ├─sda5 8:5 0 100G 0 part /var/log ├─sda6 8:6 0 100G 0 part /var/lib/docker └─sda7 8:7 0 168G 0 part └─vg_kundbdata-lv_kundbdata 253:2 0 168G 0 lvm /kundbdata sr0 11:0 1 1024M 0 rom vda 252:0 0 100G 0 disk
将vda扩容至vg_kundbdata-lv_kundbdata分区,首先fdisk格式化vda为lvm格式
[root@SSHVL037 ~]# fdisk /dev/vda 欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。 Device does not contain a recognized partition table 使用磁盘标识符 0x2dd24079 创建新的 DOS 磁盘标签。 命令(输入 m 获取帮助):p 磁盘 /dev/vda:107.4 GB, 107374182400 字节,209715200 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x2dd24079 设备 Boot Start End Blocks Id System 命令(输入 m 获取帮助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p 分区号 (1-4,默认 1): 起始 扇区 (2048-209715199,默认为 2048): 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-209715199,默认为 209715199): 将使用默认值 209715199 分区 1 已设置为 Linux 类型,大小设为 100 GiB 命令(输入 m 获取帮助):t 已选择分区 1 Hex 代码(输入 L 列出所有代码):8e 已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):wq The partition table has been altered! Calling ioctl() to re-read partition table. 正在同步磁盘。
确认vg卷组名称,将刚才创建的lvm分区添加进入卷组
[root@SSHVL037 ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 2 2 0 wz--n- 130.99g 0 vg_kundbdata 1 1 0 wz--n- <168.00g 0 [root@SSHVL037 ~]# vgextend vg_kundbdata /dev/vda1 Physical volume "/dev/vda1" successfully created. Volume group "vg_kundbdata" successfully extended [root@SSHVL037 ~]# vgdisplay --- Volume group --- VG Name vg_kundbdata System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 267.99 GiB PE Size 4.00 MiB Total PE 68606 Alloc PE / Size 43007 / <168.00 GiB Free PE / Size 25599 / <100.00 GiB # 可以看到可用空间多出100G VG UUID bh5Pyt-4OrC-sdvl-HV40-Odk9-pFhW-GHOZ7D
扩容现有lvm分区
[root@SSHVL037 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos -wi-ao---- 127.79g swap centos -wi-ao---- <3.20g lv_kundbdata vg_kundbdata -wi-ao---- <168.00g [root@SSHVL037 ~]# lvextend -l +100%FREE /dev/mapper/vg_kundbdata-lv_kundbdata Size of logical volume vg_kundbdata/lv_kundbdata changed from <168.00 GiB (43007 extents) to 267.99 GiB (68606 extents). Logical volume vg_kundbdata/lv_kundbdata successfully resized. [root@SSHVL037 ~]# xfs_growfs /dev/mapper/vg_kundbdata-lv_kundbdata meta-data=/dev/mapper/vg_kundbdata-lv_kundbdata isize=512 agcount=4, agsize=11009792 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=44039168, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=21503, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 44039168 to 70252544 [root@SSHVL037 ~]# lvdisplay --- Logical volume --- LV Path /dev/vg_kundbdata/lv_kundbdata LV Name lv_kundbdata VG Name vg_kundbdata LV UUID geEBFw-6iTk-7AF9-pJ84-gFam-9i7z-62mlhL LV Write Access read/write LV Creation host, time SSHVL037, 2022-11-26 14:23:41 +0800 LV Status available # open 1 LV Size 267.99 GiB # 可以看到空间已经扩容为268G Current LE 68606 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2
至此扩容完成
最后,have a nice day~