[root@localhost]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 50G 22G 28G 3% /
/dev/mapper/centos-home 1857G 33M 1857G 1% /home
overlay 50G 22G 28G 3% /var/lib/docker/overlay2/c2952714eb9921fe3ad9bfe4a35bd19b8879f4a9b61e76a50f8619b9a20f0b2b/merged
overlay 50G 22G 28G 3% /var/lib/docker/overlay2/11f8e2a8da7f087573b35ff382059c233f2082dc6fb16b19f4131819eec18be1/merged
This guide will move space from the /home directory to the root partition.
Step 1: Backup the /home Directory
Before removing and recreating the /home logical volume, all data should be backed up first.
mkdir /mnt/home_backup
Explanation: Create a temporary directory at /mnt/home_backup to store the backup data.
rsync -avx /home/ /mnt/home_backup/
Explanation: Use the rsync tool to back up all data from the /home directory into /mnt/home_backup. The -a option enables archive mode, -v enables verbose output, and -x ensures only data from the current file system is synchronnized.
Step 2: Unmount the /home Logical Volume
Before deleting the logical volume, ensure that the /home volume is not in use.
umount /home
Explanation: Unmount the /home file system so it is no longer in use. If the system reports that the file system is busy, use the following command to force unmounting:
umount -l /home
Explanation: The -l option performs a "lazy" unmount, which immediately removes the mount point from the file system and unmounts it when it is no longer in use.
Step 3: Remove the /home Logical Volume
Once the /home volume has been successfully unmounted, delete the logical volume to free up space.
lvremove /dev/centos/home
Explanation: Delete the /home logical volume from the centos volume group, freeing up the disk space it occupied.
Step 4: Expand the Root Volume (or Another Logical Volume)
Use the freed space from the deleted /home logical volume to expand another logical volume (such as the root volume /).
lvextend -L +<size_to_add> /dev/centos/root</size_to_add>
Explanation: Add the specified <size_to_add></size_to_add> to the root logical volume /dev/centos/root. The + indicates an increase in the existing size, rather than setting a new fixed size.
xfs_growfs /dev/centos/root
Explanation: Extend the XFS file system to utilize the additional space allocated to the logical volume. This step ensures the file system can make use of the expanded space.
Step 5: Create a New /home Logical Volume
After expanding the other volume, use the remaining available space to create a new /home logical volume.
lvcreate -l 100%FREE -n home centos
Explanation: Create a new logical volume named home, using all the remaining space in the centos volume group. -l 100%FREE indicates using all the unused space in the volume group.
mkfs.xfs /dev/centos/home
Explanation: Format the new /dev/centos/home logical volume as an XFS file system so it can store data.
Step 6: Mount the New /home Logical Volume
Now, the new logical volume can be mounted to the /home directory.
mkdir /home
Explanation: Create the /home directory to serve as the mount point for the new logical volume.
mount /dev/centos/home /home
Explanation: Mount the new logical volume to the /home directory, making it part of the file system.
Step 7: Restore /home Data
Next, restore the previous backed-up /home data to the new logical volume.
rsync -avx /mnt/home_backup/ /home/
Explanation: Copy the backup data from /mnt/home_backup back to the new /home logical volume.
Step 8: Update /etc/fstab
To ensure the new /home logical volume is automatically mounted during boot, update the /etc/fstab file.
nano /etc/fstab
Explanation: Open the /etc/fstab file with the nano editor.
/dev/centos/home /home xfs defaults 0 0
Explanation: Add a line to /etc/fstab to ensure the /dev/centos/home logical volume is automatically mounted to /home on boot.
Save the file and exit the editor.
Step 9: Clean Up Backup Data
After confirming that the /home data has been successfully restored and the system is functioning properly, the backup data can be removed.
rm -rf /mnt/home_backup
Explanation: Delete the /mnt/home_backup directory and its contents to free up temporary storage space.
By following these steps, you can effectively back up the /home directory, remove and recreate the /home logical volume, expend another volume, and restore the data. This process helps manage and optimize system storage resources, ensuring data safety and system stability.