Thursday, September 20, 2007

How To: Move Home Directory to New Partition

Hi All,

This is my first how to post in this blog. I am thinking of posting many how to articles like this one, in future, so please encourage me if you like this post. :-)

So, here I begin with..

This guide explains you how to move or copy your existing home directory to a new or existing partition.

What is home directory anyway ?

Home directory in Linux is a directory where all user specific settings and files & documents are stored.

If you have used Mac OS X earlier then, its similar to /users directory in Mac OS X.
OR
If you have used Windows XP earlier then, its like your Documents and Settings folder where all your user specific setting and files/documents are stored.

Why to keep your home directory in a separate partition ?

If you have done a default installation of your Linux Distro then, its most likely that your home directory is placed in your root partition it self. (If you are planning for fresh installation of Linux Distro, its a good idea idea to keep your home directory in a separate partition.)

Advantages of keeping your home directory in a separate partitions are:
  1. Your settings and files are kept intact even when you reinstall your Linux distro.
  2. If you have many Linux disto's then you can share the same home directory between them to avoid the wastage of disk space. This will also make you to have same settings and files in all Linux distor's that you have installed.
Sounds great right ?

Yes, it is. But the real problem is that, the default installation process of many Linux Distro's keep the home directory in root partition it self.

If you have already installed your Linux Distro and if your home directory is in your "root" partition, you can use this article to copy or move your /home to a new partition.

Assumptions:

In this "how to", I have assumed that your new partition's device file is /dev/sda2. You may need to change this, depending upon your new partitions device file name.

User skill level: Beginner.

Step 01: Create new partition

To copy or move your home directory to separate partition, first you need to create a partition in your hard disk. I recommend the utility named gparted to create new partitions OR if you already have a partition with enough free space then you can use that partition.

in ubuntu use can install gparted using following command

apt-get install gparted

To run this command open Applications->Terminal and paste above code. You may need to enter your login password.

If you are creating a new partition then make sure that you will create a partition with sufficient size, so that you need not move it once again to some other partition. ;-)

Step 02: Mount the new partition

Lets first create the folder to mount our new partition.

sudo mkdir /new_home

Now mount our new partition /dev/sda2 to this directory.

sudo mount /dev/sda2 /new_home

Note: You may have to change the sda2 in the above code with your new home partition's device file name.

You can list all your partition's using following code.

sudo fdisk -l /dev/sda

Where sda is the primary master SATA hard disk. If you have a PATA hard disk, you may have to replace sda with hda.

Also, if you have Linux installed in your primary slave the device name many change to sdb.

Step 03: Copy the data from old home directory

In this step lets copy the data from old home directory to our new partition.

There are two methods to copy our home directory content.

Method 01: using cpio command

Since the “/home” directory will have hardlinks, softlinks, files and nested directories, a regular copy (cp) may not do the job completely. Therefore, we use something we learn from the Debian archiving guide.

cd /home/
find . -depth -print0 | cpio --null --sparse -pvd /new_home/

Make sure everything copied over correctly. You might have to do some tweaking and honing to make sure you get it all right, just in case.

Now unmount the new partition.

sudo umount /new_home


Method 02: using rsync command


rsync command is generally used to synchronize the contents of two folders or to take the backups. Let's use this command to copy our home directory content.

rsync -acvSHx --exclude lost+found /home/ /new_home

If you read the rsync manual, a is the archive flag (=-rlptgoD)
c does a checksum
v is verbose
S handles sparse files
H preserves hard links
x stays in one file system

note the trailing slash on /home/

This is important, Please use it as it is given above. This says to rsync that we want to copy the contents of home directory, not the home directory.

once its done make sure everything that you require is copied over correctly.

Now unmount the new partition.

sudo umount /new_home


Step 04: Logoff from KDE/GNOME/XFace

If you are in KDE/GNOME/Xface then first logoff. This is required for our next step.


Step 05: Enter to terminal


Once you are in GDM/KDM login screen press ALT + CTRL + F1 to enter into the terminal mode. Enter your user name and password in the terminal to login.

remember that you can exit from terminal mode by pressing ALT + CTRL + F7


Then login to Terminal mode by pressing ALT CTRL F1. (You can exit Terminal mode by pressing ALT CTRL F7)


Step 06: Rename our old home directory


cd /
sudo mv /home /old_home


note that, "cd /" command is important, otherwise you may get errors while renaming your home directory. This is because by default your present working directory is set to your home directory, hence if you don't do "cd /", you may get errors.

Since we moved /home to /old_home, there is no longer a /home directory. So first we should recreate a new /home directory.

sudo mkdir /home


Step 07: Mount new partition


Now mount our new partition.

sudo mount /dev/sda2 /home

(Again, you have to change “sda2″ to whatever the new partition’s device file name is.)

Cursorily verify that everything works right.


Step 08: Go back to KDE/GNOME/XFace


Now go back to your KDE/GNOME/Xface by pressing ALT + CTRL + F7

Note that, you need to immediatley complete Step 09, before you shutdown/restart your system. You new home directory won't be loaded, until you complete the step 09.


Step 09: Adding the fstab entry

This step is crucial, so complete this step before you restart/shutdown your system.

Now, you have to tell Linux to mount your new home when you boot. For this you need to edit fstab file which is present in "/etc/fstab"

First lets backup our current fstab.

sudo cp /etc/fstab /etc/fstab.bak

now edit the fstab in your favourite editor.

sudo gedit /etc/fstab

If you are using KDE the replace gedit with kate.

Now goto the end of the fstab file and enter following.
Method 01: Classic

# Entry for /dev/sda2
/dev/sda2 /home reiserfs defaults,user_xattr 0 2

Note:
  1. I have assumed that your new partition is of type reiserfs. Change this field according to your partition type. Most likely its ext3 in your case.
  2. Change sda2 to your device file name.
Method 02: New way

If you want to uuid code instead of /dev/sda2 then first the uuid code of your hard disk partition.

You can get the uuid code your partition bye entering following in console.

sudo vol_id /dev/sda2

This will return you something like the following.


ID_FS_USAGE=filesystem
ID_FS_TYPE=reiserfs
ID_FS_VERSION=3.6
ID_FS_UUID=d48a9b6a-b4f2-432f-8590-fa9c71f87f74
ID_FS_LABEL=
ID_FS_LABEL_SAFE=


Your disks UUID code is d48a9b6a-b4f2-432f-8590-fa9c71f87f74

Now goto the end of the fstab file and enter following.

# Entry for /dev/sda2
UUID=d48a9b6a-b4f2-432f-8590-fa9c71f87f74 /home reiserfs defaults,notail,user_xattr 0 2


Step 10: Delete your old home directory. (optional)

Once all this is done, and everything works fine, you can delete the “/old_home” directory by using:

sudo rm -rf /old_home

Note that, this step is optional.

If you perform this step, you are no longer able to rollback your system to previous home directory using the Recovery methods mentioned bellow, in case of any failure after executing above given steps.


Recovery in case of failure:

coming soon...

No comments: