Create a separate home partition after installing Linux

Q I've read several times about the benefits, but I didn't initially set up my hard disk like that. Is there a way to create a new home partition after the fact or do I need to wipe out everything, reinstall, and restore my home directory to the re-partitioned drive?

A It is possible, but care is needed. As always, backup first! The danger is of the process being interrupted by a power failure or other software crashing the computer. Working with in-use filesystems should be avoided too, so boot from the live CD distro like Knoppix for this task. There are three stages: resize your root partition, create a new home partition in the space made and move your data over. The process is a lot easier if you have plenty of free space; if your drive is nearly full, move some files to DVDs or an external disk. While it is possible to resize a partition from the command line with a combination of cfdisk (or fdisk) and appropriate resizing tools for your filesystem, it is easier with GParted or QTparted (on Ubuntu and Knoppix CDs respectively). The Knoppix live DVD has both.

We use GParted here, but they work similarly. Partitions cannot be resized when mounted, so if a mount point shows alongside the partition in GParted, right-click the partition and select unmount. When resizing a partition, it is normally only possible to move the end point, so start by dragging the end of the root partition to the left until it is the size you want. It is considered good practice to not fill a partition beyond 80 per cent, filling that last 20 per cent can lead to fragmentation, but remember that you are going to remove the contents of /home after resizing, so you can probably pull that slider as far over as you want. Now create a partition in the vacated space and press Apply to start the process.

This is the risky part, it is best to leave the computer undisturbed while it completes this. Once you have created the new partition, it is safest to reboot, to the live disc again, to make sure the kernel knows about the new partition layout. Now you need to move the files from the old home directory to the new partition. This must be done as the root user, so open a terminal and type su to become root - or sudo -i if using an Ubuntu disc. If the root partition is on /dev/hda1 and the new partition on /dev/hda2, the commands to make the copy are

mkdir -p /mnt/{root,home}
mount /dev/hda1 /mnt/root
mount /dev/hda2 /mnt/home
mv /mnt/root/home/* /mnt/home/

or you can replace the last command with

rsync -a /mnt/root/home/ /mnt/home/
rm -fr /mnt/root/home/*

which is a little slower but preserves all timestamps and can be interrupted and resumed is necessary. This assumes you have a relatively small amount of data in the home directory, otherwise you will not be able to shrink the root partition as much as you need and will have a lot of wasted space after moving the file.

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar