Clone Disk Drives with Ubuntu. Make an Exact Copy of Your Hard Drive.

Before I start, I would like to clarify that this step by step tutorial applies not only to duplicating hard drives that have Linux OS on them. You can clone pretty much any drive. What is on the hard disk is irrelevant; it could be Windows, Mac OS, Linux, just data, etc. There have to be just a few basic things in place:

  1. The target drive should be the same size or bigger than the source disk drive.
  2. Have a Linux Live CD or a Linux bootable USB drive or some other way of booting into Linux (we will be using Ubuntu’s Live CD for this tutorial).
  3. Access to the internet.
  4. There is a presumption that you know how to install a hard drive.

Making an exact copy of a hard drive (or any drive for that matter – CD, DVD, USB, etc.) is very easy and quick with Linux. One of the most popular commands on Linux to do this is dd. It is a very powerful utility that was originally developed for the UNIX operating system and is now default on every Linux distribution. It does a bit for bit copy of the data and it does not care about cylinders, partitions or files. Here is an example of a dd command that would make an exact copy of one disk to another:

dd if=/dev/sda of=/dev/sdb bs=64k

The bs option specifies the block size and it could be omitted, but it would speed up the process since the default block size is only 512 bits. dd is very effective and powerful command but it is not very suitable when you are trying to make a copy of a failing or failed disk. dd is not designed to read and recover bad sectors.

There are a number of other open source programs developed since dd (dd variants) that would address situations where there might be some bad drive sectors and they perform faster and more efficient than dd. Some of those are: dd_rescue, dd_rhelp and GNU ddrescue. GNU ddrescue is the one that I would recommend using if you want to clone a drive. It works both for a perfectly good drives that you would like to clone and for failed drives that you would want to recover data from.

Install the new drive.

The new drive should be of the same or bigger size. You might have to get the BIOS to recognize the new disk; in most cases that is not necessary. After you have put the drive in, boot into Linux from another device. An Ubuntu Live CD would be perfect for that. You can download an ISO image from here.

Now you have to find out what the drives’ logical names are. Open up a terminal window: Accessories -> Terminal or Alt + F2, then type in gnome-terminal and hit Enter.
In the terminal window type sudo lshw -C disk:


sudo lshw -C disk
  *-disk:0
       description: ATA Disk
       product: WDC WD400BB-75FJ
       vendor: Western Digital
       physical id: 0
       bus info: scsi@0:0.0.0
       logical name: /dev/sda
       version: 14.0
       serial: WD-WMAJA3488275
       size: 37GiB (40GB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=5 signature=000b280f
  *-disk:1
       description: ATA Disk
       product: ST340016A
       vendor: Seagate
       physical id: 1
       bus info: scsi@0:0.1.0
       logical name: /dev/sdb
       version: 3.75
       serial: 3HS63J2C
       size: 37GiB (40GB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=5 signature=000b280f
  *-cdrom:0
       description: SCSI CD-ROM
       physical id: 2
       bus info: scsi@1:0.0.0
       logical name: /dev/cdrom
       logical name: /dev/scd0
       logical name: /dev/sr0
       capabilities: audio
       configuration: status=nodisc

In my case I have two disks- disk:0 and disk:1. The logical name of disk:0 is /dev/sda and the one for the second disk is /dev/sdb. Make a note of that. In your case that might be different. Identify which drive will be the source and which one the copy. There are 2 things in the above output that will help you do that- the product and the size. You can also use the command sudo fdisk -l. It will show you the hard drives and their partitions.

Prepare the target drive.

Now that you have identified the target drive you need to put an initial partition on it. In the terminal window you have opened execute:

cfdisk -z /dev/sdb

The cfdisk program will start, then type W and then yes to confirm. This is simple enough but you could also use the GParted program that comes with Ubuntu to do the same.

Install the GNU ddrescue program

Before you can install ddrescue you need to enable the Universe Software Repository. Go to System -> Administration -> Software Sources and then check the box next to “Community-maintained Open Source software (universe)“. Close the window. It will ask you to whether you want to refresh the list of software- go ahead and agree to that. After it finishes you can install ddrescue by running this in the terminal window:

sudo apt-get install gddrescue

Clone the disk.

Now you are ready to clone the drive by executing ddrescue. Specify the source disk first and then the target disk. You can use the -v option to be able to see the progress of the operation:

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

Make sure you get the order of the drives right or you could overlay the old drive with the new drive and loose all the data!

Depending on the size of your source drive this operation could take a couple of hours or even more. Once it finishes the new drive will be an exact copy of the old one. You can run a quick check on the file systems of the new drive:

e2fsck -fp /dev/sdb1

If the new drive is bigger than the old one you need to extend the partition(s) on it or create another one to make use of the rest of the space. The GParted program that comes with Ubuntu is ideal for this.

Once you are done, remove the old drive and boot from the new disk.

Credit: Ubuntu Kung Fu. Published article from the book.

Saturday, November 7th, 2009 Linux, Ubuntu

10 Comments to Clone Disk Drives with Ubuntu. Make an Exact Copy of Your Hard Drive.

  1. Great article, thank you! It appears you don’t even need to set up the partitions – just run the

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

    boom! perfect copy. Well… b..ooo..oo..(several hours later)m

  2. Mike on November 17th, 2009
  3. Thanks for the instructions, I’m working through them now. Fingers crossed…

    Anyhow, just dropping you a note to say I don’t know whether you do or don’t need to create the partition first, but if you choose to, I found that sudo cfdisk was required, otherwise I got an error ‘cannot open disk’.

    Thanks buddy.

  4. luc on February 10th, 2010
  5. Great Instructions on cloning!! I hate having to setup someone else’s box… so much easier to just clone your own rig so you already have it configured and ready to go!

    I have done it now successfully twice going from same size to same size USB flash drives.

    I just tried to clone a 4GB to an 8GB usb flash drive and then tried to use gparted to resize the partition… Now it won’t boot? Did I skip a step?

    Here’s what I did:
    I completed the clone process using ddrescue running on a separate ubuntu 9.10 box. Once ddrescue completed i then rebooted from the 8GB and it successfully booted into Ubuntu with all of my settings and preferences (of course with roughly 4GB now showing as “unallocated” in GParted) I then rebooted the computer from the original 4GB stick, once connected I then plugged in the 8gb stick to an available USB port. It was recognized by the system so I unmounted it before running GParted. Then finally I went into GParted and chose to resize the partition on the 8GB stick. After all this work, when I reboot to the 8GB stick i get a “Boot error” message on a blank black screen with a cursor blinking underneath it. What am I missing?

  6. MGG on February 13th, 2010
  7. In fact, am I wrong to assume that I should be resizing the live cd partition? Am I only limited to creating a second partition for data?

    I want to be able to give these to my wife and kids without having to explain why all the photos, music, documents aren’t in their system folders, but rather on a second drive (partition), etc, etc…

    resizing the drive in the future is important to me also as the price on 16GB and 32GB or larger drives come down in price in the future.

    Thanks again for your help!

  8. MGG on February 14th, 2010
  9. Been playing with unetbootin for windows and having a similar problem creating persistent drives where I can save my settings and preferences for future live sessions.

    Just wasted a bunch of time setting up Moblin and EasySUSE on separate drives only to find out that they couldn’t save any of my settings… Half the drive is “unallocated” and when they do boot up they seem to still think they are “live cd’s” rather than usb flash drives.

    Even still, unetbootin would only create default installations from the original source image files…

    Is there a way to just create my own disk image of MY current 4GB USB bootable drive running Ubuntu 9.10 with the CompizFusion all dialed in, etc, and then just burn those images to new USB sticks in the future letting me pick right up where I last left off (all network configs, installed packages and plug-ins still intact)?

    Am I best off doing something like that under Linux, Windows or Mac?

    This looks interesting:
    http://usb-image-tool.en.softonic.com/

    Anyone have any experiences positive or negative? Could be malware or spyware for all i know? What do you use?

    I came really close to pulling it off recently with this:
    https://wiki.ubuntu.com/Win32DiskImager

    but I still wasn’t able to make the new usb stick bootable…

    One of you guys is about to open the flood gates for me with some small thing that I am overlooking, or some small piece missing from my GParted puzzle!

    Thanks for the help!

    I really want to try Moblin, Fedora and EasySUSE – but if I can’t make a persistent drive where it saves the settings in between live sessions then they are useless to me!

  10. MGG on February 14th, 2010
  11. MGG on February 14th, 2010
  12. Hi,

    Thanks for these instructions. Just one remark: I’ve found that I had to reboot after ddrescue and before ‘e2fsck -fp /dev/sdb1′. No big deal, but your readers might get an error message when trying to check (which could scare the **** out of them ;-)

    Regards,
    Tom.

  13. Tom on June 17th, 2010
  14. Hi guys, this is the best and easiest way to clone your drive. I have made a clone in a couple of hours.
    I am on Ubuntu 8.04
    Thanks for the tutorial.

  15. Richard on August 9th, 2010
  16. Hi guys,

    I will try that tonight. I have to clone a USB stick plugged to my IPBOX9000HD with an Enigma distribution on it. Let’s see if it works.

    Just a question. As a perfect clone, my boot loader should not be lost, right ?

    Thanks.

  17. ranskalex on September 2nd, 2010
  18. @ ranskalex,

    Yes, that is correct. The boot loader will not be lost.

  19. dimitar on September 2nd, 2010

Leave a comment

Search

 

Archive

September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  

Other