It is a pretty common practice to use the command dd to make backup images of drives and partitions. It’s as simple as the command:
dd if=[input] of=[output]
A while back I did just that and made a dd backup of not just a partition but of an entire hard drive. This was very simple (I just used if=/dev/sda instead of something like if=/dev/sda2). The problem came when I tried to mount this image. With a partition image you can just use the mount command like normal, i.e. something like this:
sudo mount -o loop -t [filesystem] [path to image file] [path to mount point]
Unfortunately this doesn’t make any sense when mounting an image of an entire hard drive. What if the drive had multiple partitions? What exactly would it be mounting to the mount point? After some searching I found a series of forum posts that dealt with just this scenario. Here are the steps required to mount your whole drive image:
1) Use the fdisk command to list the drive image’s partition table:
fdisk -ul [path to image file]
This should print out a lot of useful information. For example you’ll get something like this:
foo@bar:~$ fdisk -ul imagefile.img You must set cylinders. You can do this from the extra functions menu. Disk imagefile.img: 0 MB, 0 bytes 32 heads, 63 sectors/track, 0 cylinders, total 0 sectors Units = sectors of 1 * 512 = 512 bytes Disk identifier: 0x07443446 Device Boot Start End Blocks Id System imagefile.img1 * 63 499967 249952+ 83 Linux imagefile.img2 499968 997919 248976 83 Linux
2) Take a look in what that command prints out for the sector size (512 bytes in the above example) and the start # for the partition you want to mount (let’s say 63 in the above example).
3) Use a slightly modified version of the mount command (with an offset) to mount your partition.
mount -o loop, offset=[offset value] [path to image file] [path to mount point]
Using the example above I would set my offset value to be sector size * offset, so 512*63 = 32256. The command would look something like this:
mount -o loop, offset=32256 image.dd /mnt/point
That’s it. You should now have that partition from the dd backup image mounted to the mount point.
I recently re-built an older PC from a laundry list of Frankenstein parts. However before installing anything to the hard drive I found I wanted to check it for physical errors and problems as I couldn’t remember why I wasn’t using this particular drive in any of my other systems.
From an Ubuntu 12.04 live CD I used GParted to to delete the old partition on the drive. This let me start from a clean slate. After the drive had absolutely nothing on it I went searching for an easy way to test the drive for errors. I stumbled across this excellent article and began using badblocks to scan the drive. Basically what this program does is write to every spot on the drive and then read it back to ensure that it still holds the data that was just written.
Here is the command I used. NOTE: This command is destructive and will damage the data on the hard drive. DO NOT use this if you want to keep the data that is already on the drive. Please see the above linked article for more information.
badblocks -b 4096 -p 4 -c 16384 -w -s /dev/sda
What does it all mean?
- -b sets the block size to use. Most drives these days use 4096 byte blocks.
- -p sets the number of passes to use on the drive. When I used the option -p 4 above it means that it will write/read from each block on the drive 4 times looking for errors. If it makes it through 4 passes without finding new errors then it will consider the process done.
- -c sets the number of blocks to test at a time. This can help to speed up the process but will also use more RAM.
- -w turns on write mode. This tells badblocks to do a write test as well.
- -s turns on progress showing. This lets you know how far the program has gotten testing the drive.
- /dev/sda is just the path to the drive I’m scanning. Your path may be different.
A new episode of the Listener Feedback podcast has been released. This episode features the Internet nerdcore gangster rap super group Futuristic Sex Robotz with their album Hotel Coral Essex. Download it here.