linux服务器LVM磁盘容量优化记录

背景

由于磁盘当初安装系统采用的自动分配磁盘空间,导致现在有磁盘空间闲置。

VFS支持动态扩容

扩容操作

lsblk查看磁盘情况。目前系统仅有一块硬盘,分区两个。用了LVM做磁盘。

[root@ww-server-05 ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 111.8G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0 110.8G  0 part 
  ├─centos-root 253:0    0    50G  0 lvm  /
  ├─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0  56.9G  0 lvm  /home

df -h 查看具体的文件系统情况

[root@ww-server-05 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 3.8G     0  3.8G    0% /dev
tmpfs                    3.9G     0  3.9G    0% /dev/shm
tmpfs                    3.9G   11M  3.9G    1% /run
tmpfs                    3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/centos-root   50G   46G  4.9G   91% /
/dev/sda1               1014M  437M  578M   44% /boot
/dev/mapper/centos-home   57G  417M   57G    1% /home
tmpfs                    781M   44K  781M    1% /run/user/0

/dev/mapper/centos-home的使用率非常低,我们57G空间就有点浪费。

准备将/dev/mapper/centos-home 降低到7G空间。剩下 的空间分配给 /dev/mapper/centos-root

先缩减/dev/mapper/centos-home容量

lvresize -L -50G /dev/mapper/centos-home

[root@ww-server-05 ~]# lvresize -L -50G /dev/mapper/centos-home
  WARNING: Reducing active and open logical volume to <6.91 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 <56.91 GiB (14568 extents) to <6.91 GiB (1768 extents).
  Logical volume centos/home successfully resized.

扩容/dev/mapper/centos-root,执行命令:
lvresize -L +50G /dev/mapper/centos-root

[root@ww-server-05 ~]# lvresize -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 centos/root successfully resized.

最后 重新识别centos大小,使用xfs_growfs /dev/mapper/centos-root命令。

[root@ww-server-05 ~]# xfs_growfs /dev/mapper/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 26214400

查看磁盘情况

[root@ww-server-05 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 3.8G     0  3.8G    0% /dev
tmpfs                    3.9G     0  3.9G    0% /dev/shm
tmpfs                    3.9G   11M  3.9G    1% /run
tmpfs                    3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/centos-root  100G   46G   55G   46% /
/dev/sda1               1014M  437M  578M   44% /boot
/dev/mapper/centos-home   57G  417M   57G    1% /home
tmpfs                    781M   44K  781M    1% /run/user/0

如果看这个磁盘不顺眼可以删除他

lvremove /dev/mapper/centos-home
来源: 雨林博客(www.yl-blog.com)