侧边栏壁纸
博主头像
liveJQ博主等级

沒有乐趣,何来开始

  • 累计撰写 171 篇文章
  • 累计创建 67 个标签
  • 累计收到 2 条评论

CentOS LVM分区的缩小和扩容

liveJQ
2023-09-01 / 0 评论 / 0 点赞 / 461 阅读 / 3,016 字

从 CentOS7.0 开始,默认的文件系统变成了 XFS,比 CentOS6 默认的 EXT4 支持更大的文件系统容量。在这里,唯一需要注意的是,XFS 缩小容量的话会同时失去分区中的数据,所以在缩小分区容量之前,需要先做下数据备份。

目标

将 home 目录缩小至 5G,将剩下的容量全部加到根目录上。

[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        48G 1003M   47G   3% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G  8.8M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda2               xfs       2.0G  145M  1.9G   8% /boot
/dev/mapper/centos-home xfs        50G   33M   50G   1% /home
tmpfs                   tmpfs     379M     0  379M   0% /run/user/0

备份

安装备份工具

yum -y install xfsdump

在根目录下备份为 home.xfsdump,需要保证根目录有足够的空间。

xfsdump -f /home.xfsdump /home

调整分区容量

缩小 home 容量,需要确保当前没有程序正在使用该目录中的数据。

umount /home
lvreduce -L 5G /dev/mapper/centos-home

输出

  WARNING: Reducing active logical volume to 5.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 50.00 GiB (12800 extents) to 5.00 GiB (1280 extents).
  Logical volume centos/home successfully resized.

扩容 root 根目录

lvextend -l +100%FREE /dev/mapper/centos-root

输出

  Size of logical volume centos/root changed from <48.00 GiB (12287 extents) to <93.00 GiB (23807 extents).
  Logical volume centos/root successfully resized.

磁盘扩容成功后需要同步到现有的文件系统(延展空间)

xfs_growfs /dev/centos/root

输出

meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3145472 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=12581888, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6143, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12581888 to 24378368

resize2fs 针对 ext2/ext3/ext4 文件系统的调整。xfs_growfs 针对 xfs 文件系统的调整。

恢复数据

无法直接挂载,缩小后的磁盘需要重新格式化才能使用。

mkfs.xfs -f /dev/mapper/centos-home

重新挂载

mount /dev/mapper/centos-home /home

将数据还原到 home 目录

xfsrestore -f /home.xfsdump /home

输出

xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description: 
xfsrestore: hostname: localhost.localdomain
xfsrestore: mount point: /home
xfsrestore: volume: /dev/mapper/centos-home
xfsrestore: session time: Mon Sep 11 01:22:34 2023
xfsrestore: level: 0
xfsrestore: session label: "home"
xfsrestore: media label: "home"
xfsrestore: file system id: 802b8a6d-5502-4585-8d67-5c994f827e73
xfsrestore: session id: c3dac766-698b-4f62-9b5d-a2b430682cef
xfsrestore: media id: e23b0de1-edf4-4b1f-b86a-a85c9e1c7f02
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 1 directories and 0 entries processed
xfsrestore: directory post-processing
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /home.xfsdump OK (success)
xfsrestore: Restore Status: SUCCESS

最终效果

[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        93G  1.2G   92G   2% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G  8.8M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda2               xfs       2.0G  145M  1.9G   8% /boot
tmpfs                   tmpfs     379M     0  379M   0% /run/user/0
/dev/mapper/centos-home xfs       5.0G   33M  5.0G   1% /home
0

评论区