Thursday, July 18, 2013

Migrating Home Mount Point From an Old Hard Drive to a New Replacement

The other day I found that a rather old hard drive I had been using to mount my home directory on my Linux machine had a number of bad sectors. I figured it was about time to update it to a new drive since, at 320GB, it was getting small for me along with the fact I didn't want to have to recover from the drive failing completely.

I found a suitable 1TB replacement, got it installed, then booted from a Linux Mint Live DVD. I used cfdisk to partition the hard drive similar to my old one with a swap partition and the remainder of the drive being ext4.

First use lsblk to see a layout of your hard drives:

aerotachyon@mint % lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0  83.9G  0 disk
└─sda1   8:1    0  83.9G  0 part
sdb      8:32   0 298.1G  0 disk
├─sdb1   8:33   0  11.5G  0 part
└─sdb2   8:34   0 286.7G  0 part
sdc      8:64   0 931.5G  0 disk

In my case, sda is my root drive, sdb is the old drive with home, and sdc is the new replacement for home. Next, to open cfdisk on the new drive I use:

aerotachyon@mint % sudo cfdisk /dev/sdc

From there I usually create the swap partition first. Choose 'New', then choose whether the partition will be primary or logical, I chose primary (A good run down of partioning can be read about at the Arch Wiki here). Type in the size of the partition in MB, and change it's type to 82, which is Linux swap. Press down to select the remaining free space and go through the same process, but this time have it's size be the remainder of the drive and make the type 83, which is Linux. Mark the partition you will use for the home mount point as bootable, as cfdisk won't allow you to write the partition table to the disk unless you do. Finally write the partition table to the disk.

At this point there is still a little more configuring that is needed for the new hard drive. You need to make the actual filesystems. Running lsblk again shows us the names of the new partitions:

aerotachyon@mint % lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0  83.9G  0 disk
└─sda1   8:1    0  83.9G  0 part
sdb      8:32   0 298.1G  0 disk
├─sdb1   8:33   0  11.5G  0 part
└─sdb2   8:34   0 286.7G  0 part
sdc      8:64   0 931.5G  0 disk
├─sdc1   8:65   0  11.5G  0 part
└─sdc2   8:66   0 920.1G  0 part

To make the the home portion of the new drive ext4 and the swap partition swap, use:

aerotachyon@mint % sudo mkfs.ext4 /dev/sdc2
aerotachyon@mint % sudo mkswap /dev/sdc1

Now you are ready to start copying files. Here, what is important is copying file permissions, hidden files, symlinks and not changing modification times. Also something that shows progress will be really nice since chances are a lot of data is being copied. Something that does all of this, and can resume if the transfer is stopped for some reason, is rsync.

First we need to create places to mount the old and new hard drives, mount them, then invoke the rsync command:

aerotachyon@mint % sudo mkdir /mnt/oldhome
aerotachyon@mint % sudo mkdir /mnt/newhome

aerotachyon@mint % sudo mount /dev/sdb2 /mnt/oldhome
aerotachyon@mint % sudo mount /dev/sdc2 /mnt/newhome

aerotachyon@mint % sudo rsync -avP /mnt/oldhome/ /mnt/newhome/

Note that the trailing forward slashes on the directories are necessary to let rsync know to copy the contents of the folder into the other folder. As for the rsync options, the -a stands for archive mode, which means it actually is selecting the options -rlptgoD. Going down the list in order:

r - will go into all the directories
l - will keep symlinks as symlinks
p - will preserve permissions
t - will preserve modification times
g - will preserve the group
o - will preserve the owner
D wil preserve device files and special files

The final two options:

v - will print every file that is being copied
P - will keep partial files for resuming and will also track progress of individual file transfers

This checks off everything on our list.

Now while the transfer is taking place, you can update your /etc/fstab since it will be necessary to properly mount your new drive as home when you boot, and the transfer can take quite a while. (Note: Here I am assuming you are using UUID's to designate your drives in the fstab. If not, from what I understand, as long as your new /home drive is plugged in to the same spot on your motherboard as your old one, an update of the fstab isn't necessary.)

First you need to create a directory and mount your root drive:

aerotachyon@mint % sudo mkdir /mnt/root
aerotachyon@mint % sudo mount /dev/sda1 /mnt/root

Next open the /etc/fstab file in an editor. Here I use vi:

aerotachyon@mint % sudo vi /mnt/root/etc/fstab

My fstab looked like this:

#
# /etc/fstab: static file system information
#
# <file system>    <dir>    <type>    <options>    <dump>    <pass>
# /dev/sda1
UUID=8fba4dae-d320-4d1d-8fe3-56118bf1c9f5    /             ext4          defaults,relatime    0 1

# /dev/sdb2
UUID=b49ef1a1-c63b-468e-a552-b16db8a1ce29    /home         ext4          defaults,relatime    0 2

# /dev/sdb1
UUID=d6dda3f4-3fdb-435e-ba92-333c0a41d089    none          swap          defaults      0 0

Here all I need to do is update the UUID values from the ones on the old hard drive to the new ones. (Note here that the lines preceded by # are comments and do not need to be edited. However, for the sake of clarity it is a good idea to update them.)

To find these values, I know of two ways. The first and easiest is:

aerotachyon@mint % lsblk -f
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda                                                     
└─sda1 ext4         8fba4dae-d320-4d1d-8fe3-56118bf1c9f5
sdb                                                     
├─sdb1 swap         d6dda3f4-3fdb-435e-ba92-333c0a41d089
└─sdb2 ext4         b49ef1a1-c63b-468e-a552-b16db8a1ce29
sdc                                                     
├─sdc1 swap         d9538553-1392-4933-b856-894f0f6ac682
└─sdc2 ext4         4d82a479-ccf5-4291-827d-97345e703f8f

For some reason, however, lsblk wasn't showing the UUID's from the live DVD. The other option is to list the files in the directory /dev/disk/by-uuid/ by typing:

aerotachyon@mint % ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Jul 18 04:21 4d82a479-ccf5-4291-827d-97345e703f8f -> ../../sdc2
lrwxrwxrwx 1 root root 10 Jul 18 04:21 8fba4dae-d320-4d1d-8fe3-56118bf1c9f5 -> ../../sda1
lrwxrwxrwx 1 root root 10 Jul 18 04:21 b49ef1a1-c63b-468e-a552-b16db8a1ce29 -> ../../sdb2
lrwxrwxrwx 1 root root 10 Jul 18 04:21 d6dda3f4-3fdb-435e-ba92-333c0a41d089 -> ../../sdb1
lrwxrwxrwx 1 root root 10 Jul 18 04:21 d9538553-1392-4933-b856-894f0f6ac682 -> ../../sdc1

Once you have the UUIDs, replace the old UUIDs in the fstab and save, then wait for the transfer to finish. Finally, unmount all your drives, reboot, and you're good to go.

No comments:

Post a Comment