Laptop hard disk replacement, part two

Way back in May I had a hard disk in my laptop fail, and after the warranty replacement disk showed up it took me a few days to get around to doing the replace. It didn’t get off to a very good start though…

In the past when I’ve done this kind of recovery I copied the details of the partition table and copied each partition in turn off the failed drive. This time however in my haste (or something) I imaged the entire 80GB in a single file by using dd pointing at the disk device node (/dev/sda, say) instead of each partition.

When it came to restore, this brought me undone. I tried the reverse of what I’d done to create the image, just dd to the disk device node, but at around the point where the first partition would have ended it failed with an I/O error.

Assuming that I did actually have a valid image of the entire drive, I needed to find a utility that would treat a disk file as an image of an entire drive, partition table and all. Then, theoretically, I could copy each partition out of the source file in the same way I was used to.

As usual, the lazyweb came through for me. I found a beaut utility called kpartx, apparently part of multipath-tools. I also found some blog posts describing how to use the tool for what I needed. What kpartx does by itself is scan a block device or normal file (significant, as I’m sure when I first used it I had to use losetup) and create device-mapper mappings to the partitions it finds. kpartx was just what I needed!

This blog post describes the process I went through (and no losetup here either). For me, I let kpartx create device-mapper nodes for the partitions in the disk image, and then used dd to copy from each of those partitions to the real partitions in the new disk (carefully created using fdisk to match the originals). Then I set up grub on the new disk, and installed it into the laptop. It worked first go!

So now on its third hard drive, this Sony Vaio (same one with the weird wireless issue in Hardy) gets yet another lease on life. The battery is almost expired though, so I think it won’t be long before it goes to the Old Hardware Shelf…

Edit: I realised my confusion about losetup. kpartx uses losetup under the covers if it detects that you’re trying to work on a disk file instead of a block device. When you use the -a or -l switches to kpartx, if it needs to it straps up a loop device for you and automatically works on that; running kpartx with the -d removes not only the device-mapper nodes but the loopback node as well.

Leave a comment