春江暮客

春江暮客的个人学习分享网站

Reducing /home partition size and increasing /root space in CentOS 8

2019-11-02 Technology
Reducing /home partition size and increasing /root space in CentOS 8

I recently set up a CentOS 8 virtual machine with 127GB of disk space and then realized that /home had been given tens of gigabytes that I did not really need. Since I mainly work as root in this environment, the more practical layout was to shrink /home and give that space back to /root.

This guide records the CentOS 8 LVM workflow for doing that. It differs in a few important details from many older CentOS 7 examples, especially around logical-volume names and the way xfs_growfs is applied.

Steps:

  1. Check space usage with df -h and back up /home.
  2. Unmount the /home file system.
  3. Delete the LV where /home is located.
  4. Extend the LV where /root is located.
  5. Extend the /root file system.
  6. Recreate the /home LV and mount /home.
  7. Check the final adjustment result.

Before You Do Anything

  • This process deletes the original /home logical volume, so back it up first.
  • It is safest on a VM, test machine, or any environment where a short maintenance window is acceptable.
  • If real user data lives under /home, do not skip the backup and restore steps.

Check space usage with df -lh and back up /home

First, log in via SSH and use df -lh to check space usage.

df -lh

df_h_space

Root is already insufficient, and since I’m the only one using the VPS, I don’t need /home at all. 1GB for /home is enough; the rest can go to /root, which will give root an additional 73GB of space. Because I didn’t take a screenshot at the beginning, what you see now is 1GB, but initially, /home was 74GB. Back up /home files to the /tmp directory.

tar cvf /tmp/home.tar /home
# zip -r /tmp/home.zip /home

backup_home


Unmount the /home file system

fuser -km /home/
umount /home

Release the /home directory’s occupation and unmount the /home directory.


Delete the LV where /home is located

This step is significantly different in CentOS 8, because in CentOS 7 the directory was /dev/mapper/centos-home, while in CentOS 8 it’s /dev/mapper/cl-home. Therefore, pay attention to the device name when unmounting.

lvremove /dev/mapper/cl-home

remove_cl_home


Extend the LV where /root is located

Extend the root space LV.

lvextend -L +73G /dev/mapper/cl-root

Extend the /root file system

This step is where the root space is actually increased. There’s a significant difference between CentOS 7 and CentOS 8. In CentOS 7, xfs_growfs /dev/mapper/centos-root was used. Logically, in CentOS 8 it should be xfs_growfs /dev/mapper/cl-root, but the result is:

xfs_growfs /dev/mapper/cl-root

xfs_growfs_error

After some exploration, I found that you should just use /.

xfs_growfs /

xfs_growfs_success


Recreate the /home LV and mount /home

Create a 1GB /home partition.

lvcreate -L 1G -n home cl

lvcreate_1g_home Set the file system type.

mkfs.xfs /dev/cl/home

mkfs_xfs_home

Mount to the /home directory.

mount /dev/cl/home /home

Restore files to the /home directory.

mv /tmp/home.tar /home
cd /home
tar xvf home.tar
mv home/* .
rm -rf home*

Also check /etc/fstab before you call the job finished. If the old /home mount entry still points to the previous logical volume name, the system may fail to mount /home correctly after reboot. In practice, you should make sure the new /dev/cl/home entry, or its UUID, is what the system will mount.


Check the final adjustment result

Check the size of each partition.

df -lh

adjust_success


Summary:

This article walks through the key CentOS 8 steps for shrinking /home and growing /root: back up first, remove the old logical volume, extend root, then rebuild /home and verify the mount configuration. That is more useful than copying commands blindly because each stage has a clear purpose.

If the final layout still looks wrong, check three things first: whether you referenced the correct LV names, whether xfs_growfs / actually succeeded, and whether /etc/fstab still points to an outdated /home device.

友情链接

其它