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 226 17:25 ituser
drwx------. 14 root root 4096 226 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 226 17:25 ituser
drwx------. 14 root   root   4096 226 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

最后,have a nice day~


评论