由于项目规定程序目录在根目录,在安装系统时没有及时规划分区,导致根分区空间太小,现在决定对根分区进行扩容.
准备将home目录所在的分区进行缩容,将缩容的空间扩容到根分区.

[root@service1 cloud]# df -Th
Filesystem              Type      Size  Ued Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G 1006M   49G   2% /
devtmpfs                devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs                   tmpfs     7.8G     0  7.8G   0% /dev/shm
tmpfs                   tmpfs     7.8G  9.2M  7.8G   1% /run
tmpfs                   tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/xvda1              xfs      1014M  145M  870M  15% /boot
/dev/mapper/centos-home xfs        42G   33M   42G   1% /home
tmpfs                   tmpfs     1.6G     0  1.6G   0% /run/user/0

#卸载磁盘home目录的挂载分区

[root@service1 cloud]# umount /dev/mapper/centos-home 

#将卸载的磁盘分区重新进行格式化成ext4格式.

[root@service1 cloud]# mkfs.ext4 /dev/mapper/centos-home 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2695168 inodes, 10778624 blocks
538931 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2160066560
329 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

#强制检查 /dev/mapper/centos-home 是否正常,如果有异常便自动修复

[root@service1 cloud]# e2fsck -f /dev/mapper/centos-home 
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/centos-home: 11/2695168 files (0.0% non-contiguous), 214252/10778624 block

#resize2fs扩展文件系统,将/dev/mapper/centos-home文件系统扩展为20G大小.

[root@service1 cloud]# resize2fs /dev/mapper/centos-home 20G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/centos-home to 5242880 (4k) blocks.
The filesystem on /dev/mapper/centos-home is now 5242880 blocks long.

#挂载文件系统到home目录,我们发现为20G.

[root@service1 cloud]# mount /dev/mapper/centos-home /home/
[root@service1 cloud]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G 1006M   49G   2% /
devtmpfs                devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs                   tmpfs     7.8G     0  7.8G   0% /dev/shm
tmpfs                   tmpfs     7.8G  9.2M  7.8G   1% /run
tmpfs                   tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/xvda1              xfs      1014M  145M  870M  15% /boot
tmpfs                   tmpfs     1.6G     0  1.6G   0% /run/user/0
/dev/mapper/centos-home ext4       20G   45M   19G   1% /home

#lvreduce缩小逻辑卷,/dev/mapper/centos-home文件系统对应的逻辑卷为/dev/mapper/centos-home,缩小逻辑区22G。

[root@service2 ~]# lvreduce -L 22G /dev/mapper/centos-home
  WARNING: Reducing active and open logical volume to 22.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y
  Size of logical volume centos/home changed from <41.12 GiB (10526 extents) to 22.00 GiB (5632 extents).
  Logical volume centos/home successfully resized.

查看卷组信息:

[root@service1 cloud]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree 
  centos   1   3   0 wz--n- <99.00g 19.12g

#lvextend是扩展逻辑卷,将缩小的22G逻辑卷扩展到根目录分区的逻辑卷中.

[root@service1 cloud]# lvextend -L +19G /dev/mapper/centos-root 
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 69.00 GiB (17664 extents).
  Logical volume centos/root successfully resized.

#因为根目录的文件系统类型为xfs文件类型,使用xfs_growfs同步根分区.

[root@service1 cloud]# xfs_growfs /dev/centos/root 
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=13107200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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 18087936
[root@service1 cloud]# 

#查看后发现,根分区的目录以扩展了19G.
#由于home目录也是xfs文件类型,所以需要将home目录卸载到重新格式化成xfs,然后再挂载即可.

#格式化为xfs文件系统并重新挂载.

mkfs.xfs -f /dev/mapper/centos-home
mount /dev/mapper/centos-home /home
#
[root@service1 cloud]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        69G 1006M   68G   2% /
devtmpfs                devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs                   tmpfs     7.8G     0  7.8G   0% /dev/shm
tmpfs                   tmpfs     7.8G  9.2M  7.8G   1% /run
tmpfs                   tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/xvda1              xfs      1014M  145M  870M  15% /boot
tmpfs                   tmpfs     1.6G     0  1.6G   0% /run/user/0
/dev/mapper/centos-home xfs        22G   33M   22G   1% /home
[root@service1 cloud]#