This guide offers detailed instructions for migrating your home directory into its own dedicated partition. Setting up /home on a separate partition is beneficial because your settings, files, and desktop will be maintained if you upgrade, (re)install Ubuntu or another distro. This works because /home has a subdirectory for each user's settings and files which contain all the data & settings of that user. Telling Ubuntu to use an existing home partition can be done by selecting "Manual Partitioning" during the installation of Ubuntu and specifying that you want your home partitions mount point to be /home, ensure you mark your /home partition not be formatted in the process. You should also make sure the usernames you enter for accounts during installation match usernames that existed in a previous installation.
This guide will follow these 6 basic steps:
- Setup your new partition
- Backup and edit your fstab to mount the new partition as /media/home (just for the time being) and reboot.
- Use rsync to migrate all data from /home into /media/home.
- Edit fstab again so the new partition mounts as /home instead of /media/home but not reboot just yet.
- Move /home to /old_home and reboot
- Delete /old_home.
The guide is written in such a way so that at any point in time if there is a system failure, power outage or random restart that it will not have a negative impact on the system and SHOULD safeguard against the possibility of the user accidentally deleting their home directory in the process.
Creating a new partition
Setting up /home on a separate partition is beneficial because your settings, files, and desktop will be maintained if you upgrade, (re)install Ubuntu or another distro. This works because /home has a subdirectory for each user's settings and files which contain all the data & settings of that user. Also, fresh installs for linux typically like to wipe whatever partition they are being installed to so either the data & settings need to be backed-up elsewhere or else avoid the fuss each time by having /home on a different partition.
This is beyond the scope of this page. Try here if you need help. Memorize or write down the location of the partition, something like /sda3. When you do create a new partition it is highly suggested that you create an ext3 or ext4 partition to house your new home directory.
Find the uuid of the Partition
The uuid (Universally Unique Identifier) reference for all partitions can be found by opening a command-line to type the following:
For some older releases of Ubuntu the "blkid" command might not work so this could be used instead
sudo vol_id -u <partition>
sudo vol_id -u /dev/sda3
Now you just need to take note (copy&paste into a text-file) the uuid of the partition that you have set-up ready to be the new /home partition.
Your fstab is a file used to tell Ubuntu what partitions to mount at boot. The following commands will duplicate your current fstab, append the year-month-day to the end of the file name, compare the two files and open the original for editing.
1. Duplicate your fstab file:
sudo cp /etc/fstab /etc/fstab.$(date +%Y-%m-%d)
2. Compare the two files to confirm the backup matches the original:
cmp /etc/fstab /etc/fstab.$(date +%Y-%m-%d)
3. Open the original fstab in a text editor:
gksu gedit /etc/fstab
and add these lines into it
# (identifier) (location, eg sda5) (format, eg ext3 or ext4) (some settings) UUID=???????? /media/home ext3 defaults 0 2
and replace the "????????" with the UUID number of the intended /home partition.
NOTE: In the above example, the specified partition in the new text is an ext3, but if yours is an ext4 partition, you should change the part above that says "ext3" to say "ext4", in addition to replacing the ???'s with the correct UUID. Also note that if you are using Kubuntu, Xubuntu or Lubuntu you may need to replace "gedit" with "kate", "mousepad" or "leafpad", respectively. They are text editors included with those distributions.
4. Save and Close the fstab file, then type the following command:
sudo mkdir /media/home
This command will create a new directory, later used for temporarily mounting the new partition. At the end of the procedure this directory can be removed.
Now you can restart your machine or instead of rebooting you might prefer to just re-load the updated fstab
sudo mount -a
Either should have mounted the new partition as /media/home. We will edit the fstab again later so this arrangement of the partition is only temporary.
Copy /home to the New Partition
Next we will copy all files, directories and sub-directories from your current /home directory into the new partition. If you do not have an encrypted home file system, just do the following:
sudo rsync -aXS --exclude='/*/.gvfs' /home/. /media/home/.
The --exclude='/*/.gvfs' prevents rsync from complaining about not being able to copy .gvfs, but I believe it is optional. Even if rsync complains, it will copy everything else anyway. (See here for discussion on this)
Encrypted file systems
If you have an encrypted home file system, then the above will just leave you with an unencrypted copy of your files, which is probably not what you want. You could re-encrypt them after copying, or copy them in their encrypted form. Here is one way to do that.
First, you'll need to shut down, and reboot from a LiveCD or USB stick. Then you'll need to mount your root partition and new home partition. (You can do this by selecting those devices in the file viewer). They will be mounted under /media/ubuntu - so for example, if you named your root partition linux-root, then your old home directory will be found at /media/ubuntu/linux-root/home. And if you named your new home partition linux-home, then this will be found at /media/ubuntu/linux-home. So, now you can copy your encrypted home files (here assuming your partitions are named linux-root and linux-home):
sudo rsync -aXS /media/ubuntu/linux-root/home/. /media/ubuntu/linux-home/.
There is no point trying to exclude any files with specific names, because the names of the files are encrypted too!
Leave your machine running from the LiveCD or USB for the moment.
Check Copying Worked
You should now have two duplicate copies of all the data within your home directory; the original being located in /home and the new duplicate located in /media/home. You should confirm all files and directories copied over successfully. One way to do this (for an unencrypted file system) is by using the diff command:
sudo diff -r /home /media/home -x ".gvfs/*"
If you are doing this from a LiveCd or to an exisitng partition that already has stuff on it you may find differences but hopefully it should be obvious which diffs you can ignore.
You can also expect to see some errors about files not found. These are due to symbolic links that point to places that don't presently exist (but will do after you have rebooted). You can ignore these - but check out anything else.
Encypted file systems
If you have an encrypted file system, the command will look more like this.
sudo diff -r /media/ubuntu/linux-root/home /media/ubuntu/linux-home
Now you can shutdown, remove the LiveCD / USB stick, and reboot as normal.
Preparing fstab for the switch
We now need to modify the fstab again to point to the new partition and mount it as /home. So again on a command-line
gksu gedit /etc/fstab
and now edit the lines you added earlier, changing the "/media/home" part to simply say "/home" so that it looks like this:
# (identifier) (location, eg sda5) (format, eg ext3 or ext4) (some settings) UUID=???????? /home ext3 defaults 0 2
Then, press Save, close the file but don't reboot just yet.
Moving /home into /old_home
Backing up your old home, just in case things have not gone completely smoothly, is best done right now. Here is how:
As long as you have not rebooted yet, you will still see 2 copies of your /home directory; the new one on the new partition (currently mounted as /media/home) and the old one still in the same partition it was always in (currently mounted as /home). We need to move the contents of the old home directory out of the way and create an empty "placeholder" directory to act as a mount point for our new partition.
Type the following string of commands in to do all this at once:
cd / && sudo mv /home /old_home && sudo mkdir /home
By default, when you open a terminal window it places you within your home directory. Typing cd / takes us to the root directory and out of home so we can then use the sudo mv command to essentially rename /home into /old_home, and finally create a new, empty /home placeholder.
With your fstab now edited to mount your new partition to our /home placeholder and the original /home now called /old_home, reboot your computer. Your new partition will mount as /home and everything should look exactly the same as it did before you started.
Instead of rebooting you might prefer to just re-load the updated fstab
sudo mount -a
There is no need to reboot - unless you have an encrypted file system.
If you recieve an error like 'The volume may already be mounted', use the following command to unmount the drive first
sudo umount /media/home/
Then try mounting again
sudo mount -a
Deleting the old Home
You can delete your old home directory with:
cd / sudo rm -r /old_home
Be careful with the above command as mistyping it could result in the deletion of other files and directories.
Technical Notes and Resources
Rsync was chosen over cp and find|cpio because it seemed to maintain permissions.
Different filesystems on the same disk
If you're moving from Windows and your new home partition is going to be an old ntfs partition (the D: disk) while you convert the C: disk to a journaling partition where you install Linux, this won't work, there will be a huge load on the processor. You should convert the two partitions to ext3 or ext4 or keep both partitions as ntfs (I haven't checked this last option). But working with two different filesystems on the same drive simultaneously doesn't seem to be a good option.