Wednesday, December 1, 2010

Move Home Folder to Separate Partition

*NOTE* See "Setting Up a Separate Data Partition" before deciding to put your home folder on a separate partition.

There are several benefits to having the home folder (the one in which all user data is kept) stored on a separate partition, not the least of which is making re-installs or new installs cleaner and separate from user data. As with most things on this blog, I learned this from other sources and am consolidating the steps here for documentation's sake and to include small things that were left out of other tutorials.

First, boot from an Ubuntu CD so you are not booted from the drive you need to change. When in the Live CD environment, use GParted to repartition your hard disk drive and create an empty partition to be your home partition, formatted with the same filesystem (i.e. ext3 or ext4) as your root linux installation. I won't cover the details of this process.

Now, for this example, we'll assume your linux root partition is on the hard disk drive /dev/sda and found at /dev/sda5. We'll also assume the new, empty home partition is at /dev/sda7 and both are formatted with the ext4 fielsystem.

Since we booted from Live CD, we must mount the original linux partition and the new one where home will be. We do this by executing the following commands in a terminal:

sudo mkdir /original
sudo mount -t ext4 /dev/sda5 /original
sudo mkdir /new
sudo mount -t ext4 /dev/sda7 /new


Next, we copy the original home to the new home partition, move the original home to home_backup just in case we need a backup, then make a fresh and empty home folder on the root partition into which the new home partition will be mounted:

cd /original/home
find . -depth -print0 | sudo cpio –null –sparse -pvd /new/
sudo mv /original/home /original/home_backup
sudo mkdir /original/home


The above "find" command is one place I ran into problems. The guides I found did not have "sudo" after the pipe, which caused the copy operation to fail due to "permission denied" errors.

Now we need to backup the fstab file, then open it with an editor. (The following uses GEdit graphical editor. You may use your favorite editor as long as you have root privileges. For example, to use a terminal editor, replace the whole gedit line below with "sudo nano -w /original/etc/fstab"):

sudo cp /original/etc/fstab /original/etc/fstab_backup
gksudo gedit /original/etc/fstab


Now add a line to fstab that mounts the home partition into the /home folder on the root partition at boot time:

/dev/sda7 /home ext4 nodev,nosuid 0 2

Another problem I had related to file and folder ownership. I had two users in my home folder, one with user id (uid) 1000 (we'll call him bob). Some of the copied folders and files showed ownership of user "root" instead of uid 1000. To fix this, still booted from the Live CD, I used the following command after ensuring the new home partition was mounted and changing to the proper folder (i.e. "cd /new/bob":

find -user root -print0 | sudo xargs -0 chown 1000:1000

The "find" portion of the above searches the present working directory and below for files and folders owned by "root". The xargs feeds each line produced by the "find" command to chown (change ownership), which then changes the ownership to the uid 1000.

Now you can reboot normally.

No comments:

Post a Comment