Thursday, 15 July 2010

Moving your home directories to a separate partition.

I always have my user homes on a separate partition.

This has a couple of advantages.

1) Frees up space on your root (/) partition.

2) Separates user homes from the OS. If you ever have to re-install the OS you don't lose all your user data.

In general, the steps are as follows;


!!!!! BACKUP YOUR SYSTEM OR YOU COULD LOSE EVERYTHING !!!!!


First, lets take a look around and see how the system is currently configured

We want to know what drives are currently mounted and where.

df -h

The main one you are interested in is the device where / is mounted.

It will probably be /dev/sdan but it may be something different.

Here is an example using sda1;
brettg@earth:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 11G 4.2G 6.4G 40% /
none 1.5G 296K 1.5G 1% /dev
none 1.5G 608K 1.5G 1% /dev/shm
none 1.5G 132K 1.5G 1% /var/run
none 1.5G 0 1.5G 0% /var/lock
none 1.5G 0 1.5G 0% /lib/init/rw


Note: If your root is not on /dev/sda1 then ensure you make a note of where it is mounted.


You will notice that I currently have only a single file system mounted, and that is root on sda1. (You can ignore all the "none" mounts they are used by the kernel)

Next is to identify a suitable place to mount /home.

Note: I will assume that your system has a single hard disk with some free space or an existing EXT partition. If you have a second hard disk then you will need to modify this procedure to suit your configuration!


Let's take a look at all the drives on my system.

sudo fdisk -l

This will show you all the current disks and partitions on your system. Here is mine;
Disk /dev/sda: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00090790

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1459 11717632 83 Linux
/dev/sda2 1459 4623 25410560 83 Linux
/dev/sda3 4623 4866 1952768 82 Linux swap / Solaris


In my case I already have a suitable unmounted partition /dev/sda2. The third partition is (obviously) my swap partition.
Note: If the unused space on your target disk is shown as free space or is partitioned as something else (ie NTFS or FAT) then you will need to use gparted or fdisk to remove it and create a suitable EXT partition (ID=83).

! For Dogs sake ensure you know what is on the partition you are messing with, all data on the partition will be LOST FOR EVER !


Once you have created a suitable partition, mount it to a temporary mount point. I will use the /dev/sda2 from now on as the target. Let's temporarily mount it to /tmp/sda2.


sudo mkdir /tmp/sda2
sudo mount /dev/sda2 /tmp/sda2
cd /tmp/sda2


At this point you should be now looking at the existing contents of the partition. If this is an old partition you probably want to ensure that you do in fact want to delete all the files located here because after the next command there is no going back.

Once you have determined that you do in fact want to trash the contents of the partition type;
sudo rm -rf /tmp/sda2

Next we want to copy our existing home directories over to the new partition. Use this command;

sudo cp -rfvp /home/* /tmp/sda2/

Depending on how much data is in your home directory this may take some time to complete.

When it is done do a quick visual check to see that everything looks OK.

brettg@earth:~$ ls -al /tmp/sda1
total 16
drwxr-xr-x 3 root root 4096 2010-07-14 14:58 .
drwxr-xr-x 22 root root 4096 2010-07-14 16:33 ..
drwxr-xr-x 12 brettg users 4096 2010-02-12 08:55 brettg
drwxr-xr-x 26 andy users 4096 2010-02-12 09:39 andy


Compare this with your existing homes;
brettg@earth:~$ ls -al /home
total 16
drwxr-xr-x 3 root root 4096 2010-07-14 14:58 .
drwxr-xr-x 22 root root 4096 2010-07-14 16:33 ..
drwxr-xr-x 12 brettg users 4096 2010-02-12 08:55 brettg
drwxr-xr-x 26 andy users 4096 2010-02-12 09:39 andy


Both should be the same.

If all is ok you can now do the final steps, which is adding the drive to fstab.

I like to use a disks uuid rather than the physical device. It makes things much easier down the track when you want to move or add drives to your system.

Let's find the uuid of our new partition;

sudo blkid /dev/sda2

This will return something like this;

/dev/sda2: UUID="ee88bfd6-1a7e-486f-85ff-2f2e4c81bd6d" TYPE="ext4"

You want to select and copy the uuid string without the quotes.

Now, edit /etc/fstab

sudo vi /etc/fstab

Add a line like this;

UUID=ee88bfd6-1a7e-486f-85ff-2f2e4c81bd6d /home ext4 errors=remount-ro 0 1

making sure you use your own uuid.

Let's test to see if you can mount the new home partition.

sudo mount /home

If all goes well there should be no error returned.

Let's see if it is mounted;

brettg@earth:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 11G 4.2G 6.4G 40% /
/dev/sda2 25G 2.2G 23.7G 9% /home
none 1.5G 300K 1.5G 1% /dev
none 1.5G 608K 1.5G 1% /dev/shm
none 1.5G 132K 1.5G 1% /var/run
none 1.5G 0 1.5G 0% /var/lock
none 1.5G 0 1.5G 0% /lib/init/rw


As we can see above, /dev/sda2 is now mounted to /home

And that's it. All we need to do is reboot and make sure everything is working OK.

Note: Be aware that the original data in /home on /dev/sda1 is still there and taking up the same space as it was before. It is hidden underneath the mounted drive. Once you have determined that everything is OK, you might want to login as root, manually umount /home to make the old data reappear and then rm -rf the old data. Of course the normal caveats apply and you should make triple sure you are really deleting what you think you are deleting before hitting Enter

Always remember when messing with partitions and rm -rf IRREVERSIBLE DATALOSS IS POSSIBLE !!!!!


Good luck and take care . . .

6 comments:

brad said...

Great tutorial!

This is the only tutorial I found that showed how to use uuid instead of /dev/sda etc for fstab.

Learned a lot more than I expected

Thanks

Brett said...

Thanks Brad, glad I could help

Miley said...

Thanks Brett. Even helped this noob. Well done. Miles

mundy5 said...

Thank you so much, I have been trying to do this all day following all kinds of blogs and forum posts but kept running into all kinds of errors. Your method of copying over the /home directory was the first one that made any sense. After following your instructions, everything worked like a charm.

Brett said...

Thanks for the comment mundy!

Anonymous said...

Thank you - added /home partition to Linux Mint 10

Clear and concise instructions - and worked for me.

Sleepy