Centos缩容home磁盘空间到根目录/下操作

2022-11-29 17:41:59 浏览数 (1)

背景

自己在家里老笔记搭建的CentOS7系统用来跑一些服务,前段时间通过samba共享存储把小米摄像头视频进行实时转存到笔记本上,最近发现磁盘不足了,因为我默认是挂载到根目录下的,以前安装操作系统时把所有空间分配给了/home目录下,现在根目录的磁盘空间,又不想更改目录,所以打算把/home目录的空间腾出挂载给根目录,于是有了本篇文章。

准备工作

查看当前磁盘分配:

代码语言:javascript复制
[root@lcrycentos7 ~]# df -hT
Filesystem                      Type      Size  Used Avail Use% Mounted on
devtmpfs                        devtmpfs  1.7G     0  1.7G   0% /dev
tmpfs                           tmpfs     1.7G     0  1.7G   0% /dev/shm
tmpfs                           tmpfs     1.7G  170M  1.6G  10% /run
tmpfs                           tmpfs     1.7G     0  1.7G   0% /sys/fs/cgroup
/dev/mapper/centos_centos7-root xfs        50G   28G   23G  55% /
/dev/sda1                       xfs      1014M  311M  704M  31% /boot
/dev/mapper/centos_centos7-home xfs       411G   33M  411G   1% /home
overlay                         overlay    50G   28G   23G  55% /var/lib/docker/overlay2/0e259cb791ddad0681ebfd184c6408ed5078d362c54b9a9a0815c6edc1f98cf7/merged
tmpfs                           tmpfs     347M     0  347M   0% /run/user/0
  1. 确认 /home 挂载点的文件系统路径 /dev/mapper/centos_centos7-home 所对应的卷组 centos 和逻辑卷 home
  2. 确认 / 挂载点的文件系统路径 /dev/mapper/centos_centos7-root 所对应的卷组 centos 和逻辑卷 root
  3. Centos系统默认是xfs文件系统,且xfs文件系统只能扩容不能缩减,在必须缩减逻辑卷容量的前提下,需要使用 xfsdump/xfsrestore 进行备份和还原

扩缩容操作

备份 /home 挂载点内容

代码语言:javascript复制
## 若没有先安装工具
[root@lcrycentos7 ~]# yum -y install xfsdump

[root@lcrycentos7 ~]# xfsdump -f /home.xfsdump /home
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control

 ============================= dump label dialog ==============================

please enter label for this dump session (timeout in 300 sec)
 -> home
session label entered: "home"

 --------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of lcrycentos7:/home
xfsdump: dump date: Sat Apr 16 10:44:14 2022
xfsdump: session id: 44b0f62a-7a5d-40bd-aecb-23b588e7d01f
xfsdump: session label: "home"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 20800 bytes
xfsdump: /var/lib/xfsdump/inventory created

 ============================= media label dialog =============================

please enter label for media in drive 0 (timeout in 300 sec)
 -> home
media label entered: "home"

 --------------------------------- end dialog ---------------------------------

xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 21352 bytes
xfsdump: dump size (non-dir files) : 0 bytes
xfsdump: dump complete: 11 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /home.xfsdump OK (success)
xfsdump: Dump Status: SUCCESS

成功备份后,卸载 /home 挂载点

代码语言:javascript复制
[root@lcrycentos7 ~]# umount /home

减少 /home 挂载点对应的逻辑卷大小

代码语言:javascript复制
[root@lcrycentos7 ~]# lvreduce -L 80G /dev/mapper/centos_centos7-home
  WARNING: Reducing active logical volume to 80.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos_centos7/home? [y/n]: y
  Size of logical volume centos_centos7/home changed from <411.13 GiB (105249 extents) to 80.00 GiB (20480 extents).
  Logical volume centos_centos7/home successfully resized.

增加 / 挂载点对应的逻辑卷大小(将卷组中的空闲空间全部扩展到根分区逻辑卷)

代码语言:javascript复制
[root@lcrycentos7 ~]# lvextend -l  100%FREE /dev/mapper/centos_centos7-root
  Size of logical volume centos_centos7/root changed from 50.00 GiB (12800 extents) to 381.13 GiB (97570 extents).
  Logical volume centos_centos7/root successfully resized.

注:这个是分配所有可用空间给根目录,也可以使用 lvextend -r -L 200G /dev/mapper/centos_centos7-root 命令指定大小。 -L:指定逻辑卷的大小,单位为“kKmMgGtT”字节; -l:指定逻辑卷的大小(LE数)。

执行扩容生效

代码语言:javascript复制
[root@lcrycentos7 ~]# xfs_growfs /dev/mapper/centos_centos7-root
meta-data=/dev/mapper/centos_centos7-root isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=4096  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=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13107200 to 99911680

格式化 /home 挂载点对应的逻辑卷

代码语言:javascript复制
[root@lcrycentos7 ~]# mkfs.xfs -f /dev/mapper/centos_centos7-home
meta-data=/dev/mapper/centos_centos7-home isize=512    agcount=4, agsize=5242880 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=20971520, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=10240, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

重新挂载 /home

代码语言:javascript复制
[root@lcrycentos7 ~]# mount /home

恢复备份内容到 /home 挂载点

代码语言:javascript复制
[root@lcrycentos7 ~]# 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: lcrycentos7
xfsrestore: mount point: /home
xfsrestore: volume: /dev/mapper/centos_centos7-home
xfsrestore: session time: Sat Apr 16 10:44:14 2022
xfsrestore: level: 0
xfsrestore: session label: "home"
xfsrestore: media label: "home"
xfsrestore: file system id: af18ea4a-cd62-4d8f-a1ac-752b805e20db
xfsrestore: session id: 44b0f62a-7a5d-40bd-aecb-23b588e7d01f
xfsrestore: media id: 9cf7c8b3-b948-4535-8750-097a66ccd991
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

检查扩容是否升级

代码语言:javascript复制
[root@lcrycentos7 ~]# df -hT
Filesystem                      Type      Size  Used Avail Use% Mounted on
devtmpfs                        devtmpfs  1.7G     0  1.7G   0% /dev
tmpfs                           tmpfs     1.7G     0  1.7G   0% /dev/shm
tmpfs                           tmpfs     1.7G  170M  1.6G  10% /run
tmpfs                           tmpfs     1.7G     0  1.7G   0% /sys/fs/cgroup
/dev/mapper/centos_centos7-root xfs       382G   28G  354G   8% /
/dev/sda1                       xfs      1014M  311M  704M  31% /boot
overlay                         overlay   382G   28G  354G   8% /var/lib/docker/overlay2/0e259cb791ddad0681ebfd184c6408ed5078d362c54b9a9a0815c6edc1f98cf7/merged
tmpfs                           tmpfs     347M     0  347M   0% /run/user/0
/dev/mapper/centos_centos7-home xfs        80G   33M   80G   1% /home

最后为了保险起见重启服务器再检查一遍

参考

CentOS根目录存储容量调整大小 Ubuntu 20.04.3-live-server 磁盘如何扩容

0 人点赞