June 13

Linux: Moving Ubuntu To Another Hard Drive(Disk)

Moving to a Bigger Disk

As time moves on and storage becomes bigger and cheaper it’s quite common to want to replace the disks in your MythTV box. There are a few options. You can add more disks, but this is restricted by the number of SATA connectors on your motherboard. You can replace your disk and reinstall, but if you have a working installation, why go to the trouble? The option I chose recently was to take a system with one 80GB disk (OS) and one 1.5TB disk (recordings) and replace the 80GB disk with a new 2TB one. That involved copying the smaller disk onto the new one and creating a new partition on the remaining space.

The procedure detailed here will obviously work on any Linux box. It has nothing specifically to do with MythTV.
Procedure Overview

The basic procedure involves installing the old and new disks into the chassis, booting from an Ubuntu Live CD and copying the raw data across. We will use the ddrescue utility to do this. This will copy the entire disk, regardless of how much of it is used. Once this is done, the new disk should boot up as an exact replacement of the old one. We will then create a new partition on the remaining free space. I ended up with a new OS disk with a 72MB partition and a 1.8TB partition, plus the old 1.5TB storage disk.
Procedure – Cloning an Ubuntu Install Onto a Bigger Disk
Boot From a Live CD

It’s not a good idea to clone a partition which is in use, so we will boot from a Live CD. This will give us a fully working Ubuntu system with access to all disks in the PC.

Download an Ubuntu Live CD image and burn it. You can also use a USB image if you don’t have have a CD drive.

Power down your PC and remove all drives apart from the old OS disk (what we will call the source disk). Install the new empty disk (which we will call the target), then boot from the CD.
Identity Which Disks are Present

This is VERY IMPORTANT! We must identify the device numbers of the source and targets disks. Running the following command will give us this information:

sudo fdisk -l | grep Disk
Disk /dev/sda: 80.4 GB
Disk /dev/sdb: 2000.4 GB

As you can see above, the small 80GB source drive in my system is /dev/sda and the target 2TB drive is /dev/sdb. The rest of the commands in these instructions will use these device names. Make sure you change these if your system is different!

If you are still unsure, the following command will give you some more information on the drives present:

sudo lshw -C disk

Write Empty Partition Table onto the New Disk

Now we will create an empty partition table on the new disk:

sudo cfdisk -z /dev/sdb
W (write empty partition table)

Install ddrescue

It is possible to do the disk copy using the simple dd utility. However, it does no error checking. The ddrescue utility is much better for this activity as it will not copy any bad sectors to the new disk. It also gives a better indication of what’s going on. We will need to install ddrescue on the temporary Live CD version of Ubuntu we are running. Ubuntu makes this very easy.

First, enable the universe repositiory by uncommenting the entries in /etc/apt/sources.list

sudo nano /etc/apt/sources.lst

Remove the hash from the start of each line containing the word universe, eg:

deb http://archive.ubuntu.com/ubuntu maverick universe
deb-src http://archive.ubuntu.com/ubuntu maverick universe
deb http://archive.ubuntu.com/ubuntu maverick-updates universe
deb-src http://archive.ubuntu.com/ubuntu maverick-updates universe
deb http://security.ubuntu.com/ubuntu maverick-security universe
deb-src http://security.ubuntu.com/ubuntu maverick-security universe

Update the repository index and install the package:

sudo apt-get update
sudo apt-get install ddrescue

Clone the Disk

Here we will use ddrescue to make an exact copy of the first disk on the second. Again, make sure your device names are correct (see above). An 80GB disk takes about an hour to copy.

sudo ddrescue -v /dev/sda /dev/sdb

Test the New Disk

Once ddrescue has done its stuff, the new disk should work exactly like the old one. Shut down the system and remove the boot CD. I recommend taking the original drive out and trying to boot from the new one. Hopefully, your Linux install should function exactly as it did before, except you now have plenty unused space ready for a new partition and filesystem.
Create Additional Partition

Now we will use the cfdisk utility to create an ext4 partition on the free space of the new drive (which is now /dev/sda):

sudo cfdisk /dev/sda

Once that is done, you will need to create a filesystem on the new partition and mount it before it can be used. We first use fdisk again to get the partition number.

sudo fdisk -l

Device Boot Start End Blocks Id System
/dev/sda1 * 1 9541 76638051 83 Linux
/dev/sda2 9542 9729 1510110 5 Extended
/dev/sda3 9730 243201 1875363840 83 Linux
/dev/sda5 9542 9729 1510078+ 82 Linux swap / Solaris

On my system, it’s the big /deb/sda3 partition.

You may now create a Linux ext4 filesystem (again, make sure you change the device name to match your system!):

sudo /sbin/mke2fs -t ext4 /dev/sda3

Get UUID of new partition

OK, we’re nearly there. The last step is to set the system up to mount the disk every time it boots. Ubuntu no longer uses device names for this, is uses the unique ID of the partition. This is so that the system still works as intended if the device names change for any reason. Run the following to get the UUID of the disks on your system:

ls /dev/disk/by-uuid -alh
lrwxrwxrwx 1 root root 10 2011-05-05 22:28 3317c035-c5f9-4f92-95ef-6d0afe8cb59f -> ../../sdb2
lrwxrwxrwx 1 root root 10 2011-05-09 07:15 73b51bb7-c8c3-438f-99f2-05c7ff3471b0 -> ../../sda3
lrwxrwxrwx 1 root root 10 2011-05-05 22:28 ad739750-08bc-46f4-8cdf-12d7bbb0798d -> ../../sda1
lrwxrwxrwx 1 root root 10 2011-05-05 22:28 b6dae2c7-49f3-441f-8123-64a4603ae379 -> ../../sda5
lrwxrwxrwx 1 root root 10 2011-05-05 22:28 fff90199-0b52-47d2-97c8-0bf85577c3cc -> ../../sdb1

/dev/sda3 on my system has UUID 73b51bb7-c8c3-438f-99f2-05c7ff3471b0

Create a directory as the mount point for the partiton. I will be using /mnt/store2

sudo mkdir /mnt/store2

Edit the /etc/fstab file:

sudo nano /etc/fstab

Add the following line (replace the UUID of the partition and the mount directory with the ones for your system, see above):

UUID=73b51bb7-c8c3-438f-99f2-05c7ff3471b0 /mnt/store2 ext4 defaults,errors=remount-ro,relatime 0 1

To test it, either reboot or run the command:

sudo mount /mnt/store2

By: G. Parker


Copyright 2021. All rights reserved.

Posted June 13, 2012 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B