How to get copy, conversion power with ddJanuary 24, 2008 at 6:43 pm | Posted in Uncategorized | Leave a comment
System administrators often have to copy data around. Copying and converting ordinary data is easily accomplished with the Linux command called cp. However, if the data is not ordinary, cp is not powerful enough. The needed power can be found in the dd command, and here are some ways to put that power to good use.The dd command handles convert-and-copy tasks. Obviously, “cc” would have been a better name, but there already was a command with that name when dd was invented. It doesn’t matter since it’s a cool command.
Cloning a hard drive with dd
The dd command doesn’t just copy files; it copies blocks, too. As a simple example, I’ll show you how to clone your entire hard drive.
Assuming that /dev/sda is the drive that you want to clone, and /dev/sdb is an empty drive that can be used as the target, using the dd command rather easy:
dd if=/dev/sda of=/dev/sdb
In that example, dd is used with only two parameters only: if, which is used to specify an input file, and of, which is used to specify the output file. In this case, both input and output files are device files. Wait until the command is finished, and you will end up with an exact copy of the original hard drive.
In the latter example, the contents of a device were copied to another device. A slight variation to that is the way that dd is used to clone a DVD or CD-ROM and write it to an ISO file. If your optical drive can be accessed via /dev/cdrom, then you can clone the optical disk using:
dd if=/dev/cdrom of=/tmp/cdrom.iso.
You can also mount that ISO-file using mount -o loop /tmp/cdrom.iso /mnt. Next, you can access the files in the ISO-file from the directory where the ISO is mounted.
Creating a backup 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
dd if=/dev/sda of=/boot/mbr_backup bs=512 count=1.
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.
The backup copy of your MBR may be useful if, some day, you can’t boot your server anymore because of a problem in the MBR. In case that happens, just boot from a rescue disk and use the command:
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.
Extending swap space
In the last example of how dd can save your life, I’ll show you how to extend your swap space by adding a swap file.
Let’s say that you’re alerted at 3 a.m. by a message saying your server is about to run out of memory entirely, due to an undiscovered memory leak. All you have to do is create an empty file and specify that it should be added to the swap space.
Creating this empty file is an excellent task for dd:
dd if=/dev/zero of=/swapfile bs=1024 count=1000000
This would write a file of one gigabyte, and that can be added to the swap space using mkswap /swapfile and 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.