Kelson Martins Blog

Recently I was handed a machine which had its purpose reassigned and I was required to re-integrate a mounted volume into Linux main partition.
Although it is a simple task to be performed, I believe that it deserves a post given that similar scenario may not be unusual.

Linux Scenario

Consider the following setup scenario on a Linux machine:
[kelson@localhost ~]$ df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G   20G   31G  40% /
/dev/mapper/centos-home  442G   33M  442G   1% /home
This relates to a machine with 2 hard disks, a 50G disk mounted on “/” and a 500G disk mounted on “/home” respectively.
Our job is to reintegrate /home back into main partition “/”, freeing the 500G disk to be re-assigned or removed completely.
As expected, we have the following on /etc/fstab
[kelson@localhost ~]$ cat /etc/fstab
/dev/mapper/centos-root /                       xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0

Analysis

Breaking down the steps necessary to achieve such requirement, we have the following:
– Backup data from “/home”.
– Unmount “/home” data volume.
– Move “/home” which was previously backed up into the /home from the main partition.
– Update /etc/fstab to remove the automatic mount.
Also, it is also important to ensure that no user is logged. This will help to avoid discrepancy in the contents of “/home”

Solution

Based on our analysis, the following commands do the work:
cp -rp /home/ /home2
umount -fl /home
mv /home2 /home
Firstly, we copy the contents of /home into any arbitrary folder, “home2” in my example. Note that we must use -rp parameters, to ensure that we copy all contents recursively maintaining proper permissions.
We can then unmount the disk, followed by moving the backed data back.
Observation: depending on the size of your “/home”, it is recommended to use the screen command to copy the data. This will avoid any issues when copying the file, even during network disruptions.
As for the last step, we must remove (or comment) the mount entry from /etc/fstab to ensure that the disk will not be mounted again on boot.
[kelson@localhost ~]$ vi /etc/fstab
#/dev/mapper/centos-home /home                   xfs     defaults        0 0
Now you have successfully freed the disk, its original “/home” contents back into the main partition.

Going further

In my scenario, I was required to use the disk into a new mount point.
For that, something similar to the following does the job:
mkdir -p /data
echo "/dev/mapper/centos-home /data                   xfs     defaults        0 0" >> /etc/fstab
mount -a
Firstly we create a new directory.
We then add the appropriate entry in /etc/fstab to mount the disk into /data on boot.
Finally, we use mount -a command to mount all devices from /etc/fstab that are not yet mounted.

Final Considerations

Although simple, the task of mounting, remounting and moving data back and forth is a common requirement for someone that administrate Linux VMs, especially when the machine has its purpose modified.
Hopefully, this was a useful guide and any comment or observation is well appreciated.
Stay tuned!

Software engineer, geek, traveler, wannabe athlete and a lifelong learner. Works at @IBM

Next Post