CentOS 7调整LVM的home和/分区大小

服务器系统是“CentOS Linux release 7.1.1503 (Core)”,此处只是做记录使用,调整过程中做好数据备份。

1、查看现有分区大小

# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   50G  787M   50G    2% /
devtmpfs                 3.9G     0  3.9G    0% /dev
tmpfs                    3.9G     0  3.9G    0% /dev/shm
tmpfs                    3.9G  8.7M  3.9G    1% /run
tmpfs                    3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/centos-home   91G   33M   91G    1% /home
/dev/sda1                497M  123M  374M   25% /boot

2、备份并卸载、删除home分区

备份home分区
# tar cvf /root/home.tar /home/

关闭正在使用/home分区的进程
# fuser -m /home/

卸载home分区
# umount /home

删除home分区所在的lv
# lvremove /dev/mapper/centos-home 
Do you really want to remove active logical volume home? [y/n]: y
  Logical volume "home" successfully removed

3、扩展root分区所在的lv

# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               148.56 GiB
  PE Size               4.00 MiB
  Total PE              38031
  Alloc PE / Size       14816 / 57.88 GiB
  Free  PE / Size       23215 / 90.68 GiB
  VG UUID               obgcgh-jGqr-5D9S-5oji-ilnt-DMm3-NuxaQ0
此时可以看到Free部分是90.68G,分配60G到/分区,剩余的用来创建home分区。

增加50G给root分区:
# lvextend -L +50G /dev/mapper/centos-root 
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 100.00 GiB (25600 extents).
  Logical volume root successfully resized

扩容root分区所在的lv:
# xfs_growfs /dev/mapper/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 26214400

4、创建home分区的lv:

此时用vgdisplay查看剩余40.68G,可以指定加多少G,也可以给空闲全部给home分区。
# lvcreate -l +100%FREE -n /dev/mapper/centos-home
  Logical volume "home" created.

格式化分区:
# mkfs.xfs /dev/mapper/centos-home 

挂载home分区:
# mount /dev/mapper/centos-home

恢复备份的数据:
# tar xvf /root/home.tar -C /home/

5、查看调整结果

# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  100G  976M  100G    1% /
devtmpfs                 3.9G     0  3.9G    0% /dev
tmpfs                    3.9G     0  3.9G    0% /dev/shm
tmpfs                    3.9G  8.7M  3.9G    1% /run
tmpfs                    3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/centos-home   41G   33M   41G    1% /home
/dev/sda1                497M  123M  374M   25% /boot

上述是调整过程,生产环境中调整一定要做好数据备份。