This is a neat trick, and so I thought worth reblogging. I was handed a corrupt SD memory card yesterday and asked if I could try and get some Christmas photos off it.
Typically, the first thing I did was image the card with ddrescue (package gddrescue in Debian/Ubuntu), confirming that the partition table was intact, it looks like the problem was a corrupted FAT, so I wanted to run fsck on the partition.
Back in the day this would have involved either dd-ing the partition out of the image, or re-imaging the card, both slow, or screwing around with loopback offsets. Instead I discovered a tool called kpartx which maps the partitions from the image into the device mapper, giving a device like /dev/mapper/loop0p1, which can be used like a regular block device.
Unfortunately while different, both FATs were damaged, and has been incorrectly repaired by something, and I ended up recovering the files with photorec instead.
5 thoughts on “kpartx – mounting partitions inside disk images”
Actually with Linux 3.2, loop devices are now finally partitioned (e.g. the kernel scans and creates partitions).
Btw, with the new Disks application (A rewrite of the GNOME Disk Utility nee palimpsest in 3.2 and earlier) in GNOME 3.4 all you need to do is to click the + button in the lower left. See this Google+ entry for more info. HTH
@davidz: so in Linux 3.2 losetup will take care of everything for you?
You can also use partx for this.
@Davidlohr: what’s the difference between partx and kpartx?
@Danielle: they’re quite similar, but kpartx only deals with disk images while partx also allows you to map partitions within a block device.