S4 Clone your Ubuntu installation onto a new hard disk

Just upgraded your system with a shiny new hard disk and want to make it your new book disk? Cloning Ubuntu to another hard disk is easy. In fact, Ubuntu provides tools to clone the entire hard disk— including the Windows partition, if there's one on there. This is the kind of fundamental task that Linux excels at, in fact.

Three things must be done. First, you must discover how Ubuntu refers to the hard disks. Secondly, you must install the ddrescue software and then use it to clone the disk. Thirdly, once ddrescue has finished, you must use the Gparted utility to expand the disk partition(s) (assuming that the new disk is bigger than the old one, which is almost certainly going to be the reason for upgrading in the first place).

It's not a good idea to clone a hard disk that's in use (any more than it's a good idea to repair a car while it's being driven), so you must use your Ubuntu install CD's live distro mode. To carry out the instructions below, boot from your Ubuntu install CD and select Try Ubuntu from the boot menu.

Note that ALL the stages below are carried out using the Ubuntu install CD's live distro mode. At no point do you need to boot into your standard Ubuntu installation, apart from to test the cloned disk at the end.

Preparing to clone

Before starting, it's a good idea to do three things in preparation. Firstly, backup all valuable personal files to CD/DVD-R/RW disc, USB keystick or an external hard disk. The instructions that follow involve drastic fundamental disk management and the possibility of data loss is present.

Secondly, it's a good idea to check the file system of the original hard disk for errors, and possibly enact repairs. This can be done by following the instructions in Tip 223, on page 258. Ideally, you should check the Windows file system for errors too. This can be done within Windows itself, or by following the instructions in Tip 38, on page 98.

Thirdly, remove any USB memory sticks, card readers or other kinds of attachable storage, such as MP3 players or mobile phones. This will avoid confusion when partitioning.

Following all this, open a terminal window and type the following, which will scan the hard disks and list their partitions: $ sudo fdisk -l

Here are the results from my test system:

Disk /dev/sda: 81.9 GB, 81964302336 bytes 255 heads, 63 sectors/track, 9964 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x1c381c37

Here are the results from my test system:

Disk /dev/sda: 81.9 GB, 81964302336 bytes 255 heads, 63 sectors/track, 9964 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x1c381c37

Device Boot

Start

End

Blocks

Id

System

/dev/sdal *

l

4742

38090083+

7

HPFS/NTFS

/dev/sda2

4743

9964

41945715

5

Extended

/dev/sda5

4743

9744

40178533+

83

Li nux

/dev/sda6

9745

9964

1767118+

82

Linux swap/Solaris

Disk /dev/sdb: 120.0 GB, 120034123776 bytes 255 heads, 63 sectors/track, 14593 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xb94838a4

Disk /dev/sdb: 120.0 GB, 120034123776 bytes 255 heads, 63 sectors/track, 14593 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xb94838a4

Disk /dev/sdb doesn't contain a valid partition table

There are two hard disks listed in the results: look for the headings Disk /dev/sda and Disk /dev/sdb. I've boldened them for you to see more clearly. Beneath each heading is technical information about the disk, and beneath that is listed the partitions on that disk.

It should be obvious that, on my test computer, /dev/sdb is the new hard disk because it has no partitions (it "doesn't contain a valid partition table"), while /dev/sda has the standard partition layout of an Ubuntu system, so is clearly the old disk. Yours will probably be very similar, if not identical.

Look for the reference to your new hard disk and make a note of it. In my case, I make a note of /dev/sdb. Then type the following to start the cfdisk partitioning program, which we'll use to write an initial partition table to the disk: $ sudo cfdisk -z /dev/sdb

If necessary, replace /dev/sdb with the details of the new hard disk you discovered earlier. All you have to do when cfdisk starts is type (W (note that's (Shift)+(w]), and then type yes to write a blank partition table. Then hit Q to quit cfdisk. Don't worry about the handful of minor errors that are reported—these can be ignored.

Cloning the disk

Now we have this information, we can install ddrescue and use it to clone the disk. This needs to be installed because it isn't a default system tool. Although the computer is running the Ubuntu install CD live distro mode, it's still possible to install additional software from the online repositories. However,, before doing this, it's necessary to enable the Universe software repository (of course, you will need to use Network Managaer to get online too, if you haven't already). Click System ^ Administration ^ Software Sources and put a check in the box alongside Community-maintained Open Source software (universe). Then click the Close button and agree to refresh the list of software when asked.

Following this, type the following command at the prompt to install ddrescue:

$ sudo apt-get install gddrescue

[[Author: Not a typo! the package name is gddrescue]] Following this, the ddrescue command is used as follows—first we specify the old hard disk, and then specify the new hard disk. The -v command option is added to ensure ddrescue provides a status report as it progresses: $ sudo ddrescue -v /dev/sda /dev/sdb

It's EXTREMELY important that you ensure you get the old and new disk in the right order. otherwise you might overwrite the data on your old disk!

Once the cloning has finished—it will probably take an hour or maybe more, depending on the size of the original hard disk—you should shutdown the computer, remove the old disk (you must disconnect the old disk before you can continue!) and boot from the cloned copy to test things out. Remember that Windows XP/Vista might object to a new hard disk as part of its "Windows Genuine Advantage" system, and you might have to revalidate online. Of course, Ubuntu will work fine without any such worries.

Assuming everything works correctly, you can move onto the next step: expanding the partitions to take advantage of the larger hard disk.

Expanding the partitions

Before attempting to expand the partitions, it's a good idea to check your Ubuntu partition's file system is sound. To do this, boot into the Ubuntu install CD's live distro mode as before. Open a terminal window and type the following to perform a disk check (these steps assume that Ubuntu is installed alongside Windows on your hard disk in the standard configuration):

once this has completed, close the terminal window and click system - Administration ^ Partition Editor.

What happens next depends on your requirements. If you just want to expand the Ubuntu partition, follow these steps:

1. In the Partition list, right-click the linux-swap entry and select Swapoff. This will stop Ubuntu's live distro mode accessing the swap partition, so that it can be moved on the hard disk.

2. Before anything else can happen, the extended partition that contains Ubuntu must be resized. Right-click the extended entry in the list and select Resize/Move. In the dialog box that appears, change the Free Space Following (MiB) box to read 0. Then hit [Tab]. This will cause the partition to be expanded to fill the space. Hit the Resize/Move button when done. Bear in mind that no changes are carried until the Apply button is hit, which you will do after making all the changes to the disk's partitions.

3. Right-click the linux-swap partition once again and select Resize/Move. In the dialog box that appears, click and drag the graphical representation of the partition to the end of the free space (in other words, click and drag it to the right of the graphical display). Following this the Free Space Following (MiB) box should read 0. Click Resize/Move.

4. Back in the main GParted program window, right-click the ext3 entry in the list and select Resize/Move. Click and drag the rightmost edge of the partition in the graphical representation so that it "grows" to fill the free space. Eventually the Free Space Following (MiB) box will read 0. When this is the case, click the Resize/Move button.

5. Finally, click the Apply button on the main GParted toolbar. Then click Apply in the dialog box that appears and sit back and wait while the partitions are moved and resized. If you would like to see what's happening, click the small arrow alongside Details in the Applying pending operations dialog box.

6. When GParted has finished, close the program and then open a terminal window. Type the following, which will once again check the ubuntu partition for errors (and, again, these steps assume that ubuntu is installed alongside Windows on your hard disk in the standard configuration):

If there are any errors, you'll be prompted to repair them. usually you can agree to the repair.

Following the file system check, you can reboot your computer from the new hard disk. You should find the Ubuntu partition is now larger.

If you wish to resize your Windows partition too, these steps are still relevant. However, you will have to move the swap and ext3 partitions, as well as the extended partition containing them, before resizing the NTFS partition.

If you want to dispose of the old hard disk, or pass it on to somebody else, be sure to securely wipe it, as described in Tip 113, on page 162. However, don't do so until you're 100% sure your new cloned copy is working correctly (I usually wait at least a week or two to ensure the copy works fine before doing anything to the old disk).

Was this article helpful?

0 0

Post a comment