Move a harddisk
Posted on Thu 10 March 2016 in linux
When your harddisk bites the dust or you get a brand new SSD, you surely want to move the data from the old to the new harddisk. Here is a more or less simple way to transfer the data.
Disclaimer: the following tutorial may harm your system so please be carefull and make a backup in advance.
Prerequisites
Partitioning
In the first step, put the new harddisk in your computer so that is recognised by the old linux system. Then, partition your new harddisk, e.g. by using cfdisk
or more fancy gparted
-- just make sure that the size of the partions are as big as the ones on the old disk, otherwise you will run out of space, while copying. Since the new harddisk should be used for booting, set the boot flag for the root partition of the new harddisk.
Assumptions
For the next steps, the following assumptions are made:
There is an old harddisk with the following partitions:
Partition | Description |
---|---|
/dev/sda1 |
root file system |
/dev/sda2 |
swap |
/dev/sda3 |
home file system |
The same partitions have also been created on the new harddisk:
Partition | Description |
---|---|
/dev/sdb1 |
root file system |
/dev/sdb2 |
swap |
/dev/sdb3 |
home file system |
Copying the data
First, mount the root and the home partition of the new harddisk as follows:
mkdir -p /mnt/{root,home}
mount /dev/sdb1 /mnt/root
mount /dev/sdb2 /mnt/home
To copy the system partition rsync
is used since it can take care of all the permissions, symbolic links etc.
First, the root partition is copied (here a couple of directories must be excluded):
rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/home/*"} / /mnt/root/
Then, the home partition is copied:
rsync -aAXv /home/ /mnt/home/
Grub
Since the new hard disk should be used for booting, grub2
must be reinstalled again. This must be done using the chroot
environment. Due to the fact that grub requires access to /proc
, /dev
and /sys
, those mount points must be delegated to the chroot environment:
mkdir -p /mnt/root/{proc,dev,sys}
mount -t proc none /mnt/root/proc
mount -o bind /dev /mnt/root/dev
mount -t sysfs sys /mnt/root/sys
Use the blkid
command to obtain the unique ids (UUID) of the partitions of the new harddisk.
Now, edit the /etc/fstab
file to modify the corresponding mount points using the UUIDs of the new partions. As a result, the new partitions will be used on next startup.
Afterwards, grub2
can be reinstalled by first updating its configuration, then, installing it into the MBR of the new harddisk and finally, cross your fingers rebooting the system:
update-grub
grub-install --recheck --no-floppy /dev/sdb
sync & reboot
Make sure that the boot order is changed accordingly in the BIOS, otherwise the old harddisk will be used for booting!