Convert a Directory to a Partition in Linux

If you have opted for the default options during the installation of your operating system, chances are that all your folders lie in the same partition which is the root ( / ) partition. While this is perfectly okay, it presents a challenge when you want to reinstall your OS as you won't be able to preserve your files and documents. The entire root system is wiped clean. Good practice demands that you create a separate home partition as well as boot and swap partitions so that you can reinstall your OS at ease.

If you already have the home folder together with other folders in the same partition, worry not. 

Here at LinuxAPT, we shall look into how you can migrate the home folder to another partition. We will be using an external drive which we will back up the home directory and configure it as a partition.


Steps to convert a Directory to a Partition in Linux

1. Confirm the size of the home partition and external drive

The first step is to confirm the size of your home directory:

$ df -Th /home

In our case, our directory is 13G.

The external drive should be greater in size than your home directory. We have a 16GB external drive which should be sufficient for the job.


2. Format external USB drive

Here, we will format the external drive using the ext4 file format. First, we will unmount the drive:

$ unmount /dev/sdb

Then we will format it using ext4 file format:

$ mkfs.ext4 /dev/sdb


3. Create a directory for mounting the drive

Next create a directory for mounting the external drive. In our case we have created the /srv/home directory:

$ mkdir -p /srv/home

Then , mount the external drive:

$ mount /dev/sdb /srv/home

To confirm that the drive has been mounted, use the command:

$ df -Th | grep sdb


4. Copy the home directory to the mounted drive

Next, transfer or copy all the files on your home directory to the /srv/home/ mount point:

$ rsync -av /home/* /srv/home/

This might take a while depending on the contents of your home directory especially in the Downloads directory. So, be patient as the copying process progresses.

Once done, you will get a summary of the total size copied. In our case, the size of files transferred was 3GB.

You can verify the disk space usage:

$ df -Th | grep sdb


5. Mount the filesystem

Next, delete all the files and folders under the home directory:

# rm -rf /home/*

Then unmount the /srv/home mount point on which the /dev/sdb device is mounted:

# umount /srv/home

Next, mount the external drive to the home folder:

# mount /dev/sdb /home/

And list its contents to ensure that your folders are intact:

# ls -l /home

Then set the directory permissions as follows just in case the default umask has changed:

# chmod -R 755 /home

NOTE:

The changes that we have made will not survive a reboot. To address this matter, we need to append some parameters to the /etc/fstab file.

But first, let's get the UUID of our external volume. You can achieve this as follows:

$ blkid /dev/sdb
/dev/sdb: UUID="56bd886b-daa0-4bc4-add1-e0e2b64bff01" TYPE="ext4"

Next, access the /etc/fstab file:

# vim /etc/fstab

Then append the following parameters:

UUID=[ID] /home ext4 defaults 0 2

In our case, we have:

UUID="56bd886b-daa0-4bc4-add1-e0e2b64bff01" /home ext4 defaults 0 2

Save the file and reload the /etc/fstab file:

# mount -a

Your home folder has now been migrated and accessible in a separate partition.


[Need to fix any Linux System issue ? We can help you. ]

This article covers moving the home directory into a newly added disk that has a dedicated Partition.

Related Posts