Mounting a partition from a raw disk image

In the past I’d backed up an SD card with

dd if=/dev/sdc of=mydisk.img

Now I wanted to get something off of it. However, the SD card had two partitions on it. I wondered if there was a more elegant way to get data out than just writing it to another SD card. Turns out, there is! There’s a great post at dustymabe.com/2012/12/15/mounting-a-partition-within-a-disk-image that explains how to do it.

sudo losetup -v -f mydisk.img
sudo losetup -a

This gives me the /dev/loop* device that my image is mounted as. In my case, it was /dev/loop18. Next, I could use partx to create loop devices for the partitions:

sudo partx -v --add /dev/loop18

This created /dev/loop18p1 through /dev/loop18p(however many partitions I had). Once those partitions have been created, you can mount them like any other partition on a regular block device. Once you’re done,

sudo partx -v --delete /dev/loop18

will get rid of both the device and its partitions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: