jump to navigation

The power of “dd” on Linux :Cloning a disk and more January 20, 2011

Posted by Tournas Dimitrios in Linux.

Whether you’re setting up multiple computers or doing a full backup, cloning hard drives is a common maintenance task . The program we’ll use is called dd, and it’s included with pretty much all Linux distributions. dd is a utility used to do low-level copying – rather than working with files, it works directly on the raw data on a storage device.

dd gets a bad rap, because like many other Linux utilities, if misused it can be very destructive. If you’re not sure what you’re doing, you can easily wipe out an entire hard drive, in an unrecoverable way.Of course, the flip side of that is that dd is extremely powerful, and can do very complex tasks with little user effort.

If you’re careful, and follow these instructions closely, you can do the following tasks :

  • Cloning a whole hard disk or only specific partition
  • Making a Hard Disk Image File
  • Restoring from a image file
  • Producing a CD / DVD image
  • Making a swap file
  • Creating a back up of the master boot record

This article assumes that you already know what a filesystem is , how to create and format partitions and what boot records are . We will go straight to practical examples ,  be carefull with these commands , a mistake can destroy  your Linux installation .

Practical examples of   ” dd “
dd   if=/dev/sda   of=/dev/sdb

Hard  disc  clone :

Here, if=… sets the source and of=… sets the destination. “dd” doesn’t care of the contents of the hard disk. It just reads bytes from /dev/sda and writes them into /dev/sdb. It doesn’t know what are files. So, the hard disk file system and how many partitions it has are not important. For example, if /dev/sda is splitted into three partitions, the /dev/sdb will have the same partitions. i.e. “destination” is completely same with “source”.

Notice: to execute “dd” you should login as “root”

dd if=/dev/sda1 of=/dev/sdb5

Partition  clone :

Backing up a hard disk partition is much similar to backing up a whole hard disk. The reason is that Unix/Linux uses device name, such as /dev/sda1, /dev/sda5… to indicate the partitions.

dd if=/dev/sda of=~/disk1.img

Making  a hard  disc image file  :

Most of time you don’t want to make a complete duplication of your hard disk. You may prefer to creating an image file of the hard disk and save it in other storage devices

Since you have created an image file, you can compress it with “gzip” or “bzip2”:
gzip disk1.img #generates disk1.img.gz or
bzip2 disk1.img #generates disk1.img.bz2

  • dd if=disk1.img of=/dev/sda
  • dd if=disk2.img of=/dev/sda1

Restoring from an image file :

To restore a partition or a hard disk from an image file, just exchange the arguments “if” and “of”  .

dd if=/dev/cdrom of=/tmp/cdrom.iso

CD / DVD   clone :

Actually the process is the same as cloning a hard disc  ,I mention this just for completeness .

dd if=/dev/sda of=/boot/mbr_backup bs=512 count=1

Creating a copy of the master boot record :

So far, we have used dd to perform tasks that can be done with other utilities as well. Now, we can go beyond that. In the following example, create a backup of the Master Boot Record (MBR).
Copy the first 512 bytes of your hard drive, which contains the MBR, to a file. For instance, you could do this with the command

In this scenario, two new parameters are used:

  • First, there’s the parameter bs=512, which specifies that a blocksize of 512 bytes should be used.
  • Next, the parameter 1 is used to indicate that only one such block has to be copied. Without that parameter, you would copy your entire hard drive.
dd if=/boot/mbr_backup of=/dev/sda bs=446 count=1 In this restore command, only 446 bytes are written back. This is because you may have changed the partition table since you’ve created the backup. By writing back only the first 446 bytes of your backup file, you don’t overwrite the original partition table, which is between bytes 447 and 511.

dd  if=/boot/mbr_backup   of=/dev/sda  bs=446   count=1

dd if=/de/hda   of=partitionTable.back  bs=66 count=1   seek=446
Just copy the partition-table  ( don’t include the boot-loader) . The seek options defines the offset value .
This command can be used in reverse order to restore the partition table .
dd if=/dev/zero of=/swapfile bs=1024 count=1000000

Creating an empty file :

Creating an empty file and converting it so  that it can  be added to the swap space.

  1. dd if=/dev/zero of=/swapfile bs=1024 count=1000000
  2. mkswap   swapfile
  3. swapon   swapfile

In this article, you’ve learned how to do some basic troubleshooting using the dd utility. As you have read, dd is a very versatile utility that goes far beyond the capabilities of ordinary copy tools like cp. Its abilities to work with blocks instead of files are especially valuable.



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s