Wednesday, 27 August 2008

How to create a seperate /home directory on ubuntu

This guide will show you how to create a seperate /home directory on another partition on your pc.
I had a spare hard drive and an old pc so i decided to give myself some more space and move and run my home directory from the spare hard drive and this is how i did it. You need to set up your hard drives so as your original is the master and the second drive you are adding is the slave, this can be done easily by moving the small jumper pin on the back of the hard drive over to the second last pin position or maybe the last pin position, check and see which works for you althought the second last position has always worked for me.

O.K now were about to begin, please follow these instructions carefully as i assume no responsibility if you screw up but you should be fine as long as you follow the backup part in the instructions :)

Step 1

Boot up your pc with your ubuntu live CD, now open the terminal and type:

$ sudo fdisk -l

this will show you an output of your hard drives, mine looks like this:

Disk /dev/sda: 81.9 GB, 81964302336 bytes
255 heads, 63 sectors/track, 9964 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xdf5edf5e

Device Boot Start End Blocks Id System
/dev/sda1 * 1 9789 78630111 83 Linux
/dev/sda2 9790 9964 1405687+ 5 Extended
/dev/sda5 9790 9964 1405656 82 Linux swap / Solaris

Disk /dev/sdb: 30.0 GB, 30060527616 bytes
255 heads, 63 sectors/track, 3654 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00256c7d

Device Boot Start End Blocks Id System
/dev/sdb1 1 3654 29350723+ 83 Linux

Be sure to keep track of the names of your partitions—these names are very important (/dev/hda1, /dev/hdb1, /dev/sda2, etc.).
Im going to be mounting my new home directory onto the /dev/sdb hard drive.

Step 2
Now open gparted partition editor, you can open the partition editor by opening another terminal and typing:

$ sudo gparted

find the partition in gparted that you want to mount your new home directory onto (in my case /dev/sdb) and save it as filesystem ext3. When you're satisfied with your new partition layout, click Apply.
Once the changes have been applied, make note of the partition name of your new partition and then quit GParted.

Step 3
Now, in my example, my original partition that Im using was /dev/sdb, and it created a new partition called /dev/sdb1, and my original /home folder lives on /dev/sda1. It's very important that you substitute in your own appropriate partition names where you see me using my own.

Now, back in the terminal, I'm going to mount /dev/sda1 and /dev/sdb1 by using the following commands:

$ sudo mkdir /old
$ sudo mount -t ext3 /dev/sda1 /old
$ sudo mkdir /new
$ sudo mount -t ext3 /dev/sdb1 /new

Step 4
Now we're going to back up the old /home directory on the old hard drive and move it to the new hard drive using these commands:

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

Note: I have tested the second command myself, and it works, but some have pointed out it might make sense to preface the first part of the command with sudo in case one of the other users has subdirectories manually marked as unreadable to the user making the move. Since I have not tested this out and all directories and readable to all by default, I'm offering this as only an alternative in case the command iv already given doesnt work for you:
sudo find . -depth -print0 | sudo cpio --null --sparse -pvd /new/

Step 5
Next, we're going to specify to use the new home partition as /home, type this into the terminal:

$ sudo cp /old/etc/fstab /old/etc/fstab_backup
$ sudo nano /old/etc/fstab

You'll then be taken to the nano text editor. Add in this line:

/dev/sdb1 /home ext3 nodev,nosuid 0 2

Then save (Control-X), confirm (Y), and exit (Enter)

Note: Remember to change my /dev/sdb1 to you new home partition.

After you reboot, you should be now using your new /home partition.

If you find that you are running out of room on your old partition and you're pretty confident everything is working as it should be, then go ahead and delete the backup of home using:

$ sudo rm -rf /home_backup

What if it doesn't work?
You know, it really should work, but if you somehow messed up your /etc/fstab and didn't configure it correctly... well, that's why we have a live CD, so we can fix things.

Boot up the live CD, go to a terminal, and type:

$sudo mkdir /recovery
$ sudo mount -t ext3 /dev/sda1 /recovery
$ sudo cp -R /recovery/home_backup /recovery/home
$ sudo cp /recovery/etc/fstab_backup /recovery/etc/fstab

Then, reboot. :D

No comments: