Sunday, May 13, 2012

Tools for managing SD images - kpartx

So for people who need to deal with creating or maintaining SD card images, or indeed full image backups of any storage device, kpartx is a godsend.

You most likely already know that you can mount images (created with dd) using loop:

mount -o loop -t vfat some-sd-image.img /mnt/mountpoint

But, this doesn't work for images that were created from the whole block device, as most of the RasPi images are.  You can mount them in the same way, but you need to work out what the sector offset is of the partition in order to just mount that (replacing sectors with the appropriate number for this image):

mount -o loop,offset=$(({sectors} * 512)) some-sd-image.img /mnt/mountpoint

Tedious, and hard to do - there's a full description here.

Well, there is a piece of magic which I hadn't previously heard of called kpartx, which allows you to do all of this in an easy command (described here).  To list the partitions on an image:

kpartx -l some-sd-image.img

And to add them as loop devices:

kpartx -a some-sd-image.img

Which can then be mounted as:

mount -t vfat /dev/loopXpY /mnt/mountpoint

You can clean up the /dev/loopX* again (after appropriate umount) with:

kpartx -d some-sd-image.img


No comments:

Post a Comment