kpartx – mounting partitions inside disk images

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.

About Danielle

Danielle is an Australian software engineer, computer scientist and feminist. She has worked on GNOME for some time now. Opinions and writing are solely her own and so not represent her employer, the GNOME Foundation, or anyone else but herself.
This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to kpartx – mounting partitions inside disk images

  1. davidz says:

    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

  2. Danielle says:

    @davidz: so in Linux 3.2 losetup will take care of everything for you?

  3. Davidlohr says:

    You can also use partx for this.

  4. Danielle says:

    @Davidlohr: what’s the difference between partx and kpartx?

  5. Davidlohr Bueso says:

    @Danielle: they’re quite similar, but kpartx only deals with disk images while partx also allows you to map partitions within a block device.

    http://blog.stgolabs.net/2011/10/partx-telling-kernel-about-disk.html

Comments are closed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>