How to upgrade a Linux laptop to a new hard disk

Recently my laptop hard disk died suddenly, and I had an unused 2.5" laptop disk lying around, so I just put that in, and installed Ubuntu Linux LTS 14.04, then added the packages I needed.

However, using smartmontools, I found out that the not so new disk has unhealthy statistics, and did not want to risk down time on my main computer.

So I decided to be proactive and put a new disk in it. Because I have spend the better part of a day to install what I need, and added to it over the weeks, I did not want to do a fresh install. Rather, I wanted to copy what I have to a new disk. This is a fairly doable process for someone who is familiar with the Linux command line.

I had an older 256GB OCZ Vertex 4 SSD as well, and wanted to use it for faster I/O.

The main tutorial I used was: Migrate Disk in Ubuntu. However, because I am using Grub2, not plain Grub, I found that a few steps need to change.

The article below is how I did all this.

Hardware You Will Need

Because this is a laptop, and there is no room to have another disk in it, get yourself a USB or eSATA disk drive dock or a disk drive enclosure. Those can be helpful for backup in the future, so they are good to have around.

Insert the new disk in the dock/enclosure, and connect it to the host computer. Using dmesg, you will find out the drive letter (e.g. sdb).

Partition The New Hard Disk

First you need to partition the disk the way you want it. There are different schools of thought here, with some wanting /boot on its own partition, /var in another, and /home in a yet another. Others want it simple with everything in one partition. With smaller disks, the latter is more appropriate.

For my purposes, this is a small disk (256GB), so no need for separate partitions. Also, it does not need GPT, nor my laptop support EFI BIOS. So again, keeping things simple with just MS-DOS style partitions, and an MBR.

You can use fdisk, parted, cfdisk, or gdisk, depending on what features you want.

I used fdisk.

fdisk /dev/sdb

Disk /dev/sda: 256.1 GB, 256060514304 bytes
255 heads, 63 sectors/track, 31130 cylinders, total 500118192 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00069eac

Device Boot Start End Blocks Id System
/dev/sda1 63 16000739 8000338+ 82 Linux swap / Solaris
/dev/sda2 * 16000740 500118191 242058726 83 Linux

Do not forget to add a swap partition that is at least as large as your RAM size, otherwise hibernate will not work.

Create The File System, and Mount The New Disk

Now, create a new file system on the partition that want the root file system to reside on.

mkfs -t ext4 /dev/sdb2

And then create a directory for the the new disk:

mkdir /mnt/disk2

Mount the new disk's root partition on the new directory:

mount /dev/sdb2 /mnt/disk2

Copy The Old Disk To The New Disk

Now, we need to copy the existing disk to the new disk. It is best if you log out from any desktop GUI that you are in, and do this from a terminal (Alt-F1).

cd /
time find . -xdev -print0 | cpio -pa0V /mnt/disk2

Note that -xdev is required, otherwise special files are copied. For example /proc/kcore is 128TB in size!

Mount Virtual File Systems

In preparation for chroot, we need to map the existing virtual file systems to the new disk.

mount --bind /dev /mnt/disk2/dev
mount --bind /sys /mnt/disk2/sys
mount --bind /proc /mnt/disk2/proc

Change Root

Now we chroot, so the new disk's UUID is used for Grub2.

chroot /mnt/disk2

Grub2

Now, create the new Grub2 configuration:

grub-mkconfig -o /boot/grub/grub.cfg

And then let Grub2 install a new MBR:

grub-install /dev/sdb

Change fstab

Then you need to find out the UUID for the root partition on the new disk. You can use blkid, tune2fs, or simply ls, like so:

ls -l /dev/disk/by-uuid/

lrwxrwxrwx 1 root root 10 Jul 27 22:08 2aca8caa-4ee9-477a-ae1a-7cc0d1f8a6a9 -> ../../sda1
lrwxrwxrwx 1 root root 10 Jul 27 22:34 9f491cf9-5e8d-467f-a1ec-244dead0af49 -> ../../sdb2
lrwxrwxrwx 1 root root 10 Jul 27 22:08 cb79050e-0bd9-49a6-8c4f-a9984dafb1e7 -> ../../sda5

Note the line for sdb2, which is our root partition on the new disk.

You then have to edit the /etc/fstab, and change the UUID for the root device to the above.

Enable Swap

Turn off all swap:

swapoff -a

Initialize swap on the new disk's swap partition:

mkswap /dev/sdb1

Setting up swapspace version 1, size = 8000332 KiB
no label, UUID=b4b6931b-e002-4e93-ab3e-14225b676754

Again, Edit /etc/fstab and change (or add) the above UUID

swapon -a

Reboot With the New Disk

Shutdown the computer, replace the old disk with the new disk, then start the computer.

Everything should be as they are.

Change Swap Partition UUID for Hibernate

For Hibernate functionality to work again, you have to configure the UUID of the Swap partition in a few places, as per this article.

If You Are Booted Into Grub Rescue

If you get booted into Grub2 rescue, then Grub2 did not write a proper MBR.

You can boot using the following Grub Rescue commands:

grub rescue> set prefix=(hd0,1)/boot/grub
grub rescue> set root=(hd0,1)
grub rescue> insmod normal
grub rescue> normal

Login, and use the command:

grub-install /dev/sda

Further Reading

Contents: 

Tags: