Clone a disk using dd

From LQWiki
Jump to navigation Jump to search

Info

This is a practical example of what I did to clone my disk to be up and running as soon as I get the new laptop. This could also be used to wipe a disk clean (if you sell a computer for example)

Clone the disk

First of all I booted from the laptop on a live CD (Gentoo live cd for example), started ssh and gave root a password.

Then from the laptop, I ran the following command

# dd if=/dev/sda | ssh sys@192.168.1.100 "dd of=/mnt/data/laptop_sda.img"

This command takes an exact copy of a harddisk (/dev/sda) and sends it to /mnt/data via ssh and saves it as an image.

This typically takes several hours to complete so before you run the command use the screen command in case you need to close the terminal.

Check the image

There are many ways to check the state of the image. The following is how I chose to do it.

  1. Install a kubuntu vm in VirtualBox.
  2. Add an extra 'vm disk' (sdb) of 110G to contain the image.
  3. Make the CDRom point to our image.
  4. Boot into the vm

Now I can copy my image back to the secondary device like this:

# dd if=/dev/cdrom of=/dev/sdb bs=512

The i/o might be high so it could take a while. Took me ~17 hours.

  1. Shutdown the vm
  2. swap the boot disk order and make sdb primary master.
  3. Boot the system and see if it works.

The system booted and asked for the password to my encrypted filesystems. It then fully booted regardless of hardware change, etc. I checked the files and found no corruption.

This proves that my image is good and that linux rulz!

Wipe a disk clean

By selecting /dev/zero as the source you can write a bunch of zeros to the disk, essentially wiping it clean.

# dd if=/dev/zero of=/dev/sdb bs=512

See also