Home > Enterprise Linux Tips > Administrator > Linux/Unix commands: How to get copy, conversion power with dd
Enterprise Linux Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ADMINISTRATOR

Linux/Unix commands: How to get copy, conversion power with dd


Sander van Vugt
02.19.2007
Rating: -4.67- (out of 5)


Enterprise IT tips and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


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.

More Linux/Unix commands:
50 nifty Linux command-line tools

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.

Rate this Tip
To rate tips, you must be a member of SearchEnterpriseLinux.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Enterprise Linux Web Server & Application Server
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts