Linux How-to: Create a Separate Home Partition (re-post)

Original Article 06/04/2008: Cover:  Full Circle Magazine Issue #15
If you ran the default installation of Ubuntu, using the 'guided' partitioning option, you probably have two partitions on your hard drive; an Ubuntu system partition and a swap partition. Which is fine until you get into running regular backups. Then the Ubuntu 8.04 upgrade. Or a disaster to recover. Suddenly, having  all your data and programs living on one partition seems like a bad idea.

Don't believe the way that most file managers present the contents of your machine; 'home' is  still part of your main filesystem. However, keeping the 'system' (Ubuntu) and 'data' (your own stuff) segregated on separate disk partitions has certain advantages. If I need to re-install or re-format the system partition, I can happily wipe it, knowing nothing happens to the /home folder. My backups become simpler. Upgrades get easier. If only there was a way to separate /home...
There is, but this method comes with no warranties and is liable to all manner of user-goofs; so backup everything first!

1. Resize your existing partitions to create space for the new  /home
Unless you have unused disk space, you will need to resize the existing filesystem partition to make room. Make sure you know how much space you currently use for home and allow some room for growth, before you start this. You can use an extra internal hard drive, but don't try this on a USB drive, kids!

You will need one of the partition editors capable of on-the-fly move/resize operations leaving the disk contents in place – the Parted / Gparted / Visparted family do very well. You need to run it from an Ubuntu Live CD, Parted Magic or System Rescue CD,  for two reasons:
  • In order to resize your existing / partition, it needs to be unmounted; for that it can't be booted into a regular Ubuntu installation.
  • If you screw up your installation by accident, you can use the live CD to restore your old settings and, in the worst situation, at least recover your important files.
Launch Parted, selecting your partition to resize, then select the Resize/Move button to bring up the settings dialog box.

You can click-and-drag on the partition graphic to set the partition size or type a number in the New Size box; it measures in megabytes so you want a number in thousands for gigabytes;  tab out of the box to update the Parted dialog. When you're happy, click on Resize/Move button bottom-right to confirm your settings. Nothing happens until you click on the Apply button in the main window.

This may take a while. Each section in the progress box is expandable so you can see what's happening at every stage.

2. Create a new partition in the freed space
Still in Parted, select the New option and specify your partition for the new /home. It uses the same dialog box. 

Hint: stick with one type of filesystem. If your main partition is ext3 – which is a good safe choice – then create the new partition as ext3. If you're on ext2 or Reiser, keep the new partition the same. All the Parted versions show the partitions colour-coded by type on-screen.

Note the partition names, making sure you know which is the old and the new. In my example:
   hda1 is oldhome
   hda3 will be the newhome

3. Mount old and new partitions
Quit Parted. We're still using our Live CD session and we'll move things around under the temporary filesystem it creates. Open a terminal; make a new directory as a placeholder then mount the new partition under it:
$sudo mkdir /mnt/newhome
$sudo mount -t ext3 /dev/hda3 /mnt/newhome

Then do the same for your main filesystem partition:
$sudo mkdir /mnt/oldhome
$sudo mount -t ext3 /dev/hda1 /mnt/oldhome

We need to use sudo for most of these commands as they need root permissions to run.

4. Move the old home into the new home folder/partition
Change directory into to the oldhome mountpoint
cd /oldhome
Since the “/home” directory will have hard links, soft links, files and nested directories, a regular copy (cp) may not do the job completely, so we'll use a different method. Take a deep breath:
sudo find . -depth -print0 | sudo cpio --null --sparse --preserve-modification-time -pvd /mnt/newhome/

What this does is to string two commands together:
find is a native command that follows the pattern:

The period is the end of the clause, so by default look at everything in the current folder (remember we're in the /oldhome/home folder structure).
  • -depth says search all the way to the lowest level of the folder tree
  • -print0 is to include file names with spaces
  • The 'pipe' | redirects the output of the find to be the input for the cpio command which always takes its instructions from a prior command. So:
  • cpio  is the copy command
  • --null expect to receive filenames containing spaces
  • --preserve-modification-time keeps all the 'last modified timestamps' intact through the copy process.
  • p is for pass-through or 'by-pass the archive step' and copy direct to destination
  • v is verbose - list the files copied on-screen;
  • d specifies make-directories - create the corresponding folders in the destination folder.

Finally the /mnt/newhome/ is the destination folder for the copied files.
This copies everything while preserving the last modification timestamps.

4. Unmount the /newhome new partition
Having copied what we need, we unmount it in order to re-mount it in the right place later.
$sudo umount /mnt/newhome

5. Rename the old /home as a safe backup
Make sure everything has moved cleanly before this step! The folder name '/home' is a significant reserved name in the filesystem and we need to rename the old /home so that our regular Ubuntu session can't mount it when we reboot - we want it to use the new one:
$sudo mv /oldhome/home /oldhome/home_backup

The move command 'mv' is a shorthand way to rename a file or folder.  The original home is still there under a different name. Nothing deleted yet.

6. Mount your new home
Crete a new placeholder in the filesystem where the new /home will be mounted:
$sudo mkdir /oldhome/home

Mount the new home under this placeholder:
$sudo mount /dev/hda3 /home

You should find all your personal files looking exactly as they did before. This is fine and dandy for the Live CD session, but now have to make this permanent for when you reboot into your regular Ubuntu desktop. Otherwise the new home won't be mounted and you won't see your Ubuntu desktop!

8. Modify fstab to make things permanent
We have to modify the /etc/fstab file. Fstab is the File System Table, the quick lookup the Ubuntu kernel uses like its school register to see who's in class, what their names are and where they sit. Except fstab lists drives and partitions, not school kids. The command:
$sudo gedit /etc/fstab

opens the Gedit text editor so you can add the line for your new partition:
/dev/hda3 /home ext3 nodev,nosuid 0 2

This line essentially says 'mount /dev/sda3 under the mountpoint /home,' with some options. Amend this for your partition name and filesystem.

If Gedit gives you trouble saving this change, then my fall-back is a character-mode text editor such as Vi, or  Nano which is on the Ubuntu Live CD:
sudo nano /etc/fstab

Go to the bottom of the file, add the line then to save and quit,  the key-strokes in i are control-o, enter, control-x.

9. Reboot computer and verify
Close the Live CD session then reboot into your regular Ubuntu session; make sure everything is working right, home is mounted where we specified and all folders accessible.

10. Remove the old home
You've now got two copies of home on your system, so if you're satisfied the new one is good, clear out the old:
$sudo rm -r /old_home
This deletes the original home folder and contents.

What if it doesn't work?
There are two real possibilities:

1. You mess up the find | cpio copy command and not everything ends up where it should; the filesystem can't mount what it can't find!
Solution: using the  live CD, reboot to a live session and  manually mount both old and new, compare and check the contents. You may need to back out and run the copy again.

2. You mess up your /etc/fstab and didn't configure it correctly - I have done this...
Solution: using the  live CD, reboot to a live session and open the etc/fstab file again:
  • Check you specified the right filesystem type – ext2, ext3 or Reiser.
  • Check the device name according to your partitions – is your machine using sda or hda? Is the disk a, b, c or some other letter? Have you got the partition number right?

Check the mount options -  try 'defaults' in place of the nodev,nosuid options, as in:
/dev/sdb2 /home ext2 defaults 0 2

Of course, item #3 is you still can't identify the problem, in which case recovery is required.
Boot up the live CD, go to a terminal, and type:
sudo mkdir /recovery
sudo mount -t ext3 /dev/hda1 /recovery
sudo cp -R /recovery/home_backup /recovery/home
sudo cp /recovery/etc/fstab_backup /recovery/etc/fstab

And relax...
There it is; home on its own partition. You can do the same for any part of your filesystem if you want to segregate the contents -  just don't think this takes the place of backups, regular backups are still crucial.  RC