background

The CentOS7 system I built in my old notebook at home is used to run some services. Some time ago, I used samba shared storage to transfer the Xiaomi camera video to my notebook in real time. Recently, I found that the disk is insufficient, because I mount it to the root directory by default. Previously, when installing the operating system, all the space was allocated to the/home directory. Now, the disk space in the root directory is insufficient, I don't want to change the directory, so I plan to free up the space of the/home directory and mount it to the root directory, so I have this article.

preparation

To view the current disk assignments:

 [ 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. Confirm the file system path of the/home mount point /dev/mapper/centos_centos7-home Corresponding volume group centros and logical volume home
  2. Confirm/mount the file system path of the point /dev/mapper/centos_centos7-root Corresponding volume group centros and logical volume root
  3. The Centos system defaults to the xfs file system, and the xfs file system can only be expanded but not reduced. If the logical volume capacity must be reduced, you need to use the xfsdump/xfsrestore Backup and restore

Expansion and reduction operation

  1. Backup/home mount point content

     ##If the tool is not installed first [ 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
  2. After successful backup, uninstall the/home mount point

     [ root@lcrycentos7  ~]# umount /home
  3. Reduce the size of the logical volume corresponding to the/home mount point

     [ 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.
  4. Increase/mount the logical volume size corresponding to the point (expand all the free space in the volume group to the logical volume of the root partition)

     [ 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.
    Note: This is to allocate all available space to the root directory. You can also use lvextend -r -L 200G /dev/mapper/centos_centos7-root Command to specify the size.
    -50: Specifies the size of the logical volume, in "kKmMgGtT" bytes;
    -l: Specifies the size (number of LE) of the logical volume.
  5. Execute capacity expansion to take effect

     [ 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
  6. Format the logical volume corresponding to the/home mount point

     [ 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
  7. Remount/home

     [ root@lcrycentos7  ~]# mount /home
  8. Restore the backup content to the/home mount point

     [ 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
  9. Check whether the capacity expansion is upgraded

     [ 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
  10. Finally, to be safe, restart the server and check again

reference resources

CentOS root directory storage capacity resizing
How to expand Ubuntu 20.04.3 live server disks

Article Contents