Mounting a Partition from an Image File that Contains Multiple Partitions

Recently I had been messing around with my Raspberry Pi devices that I have. I was having an issue with one of them seemed to be related to power. As it turns out it actually had errors in the root filesystem. I wrote a quick post about this – see here.

I decided to post a quick method for mounting partitions from image files. This time though, without using a loopback device and rather mounting in a normal method. In fact, and not surprisingly, the process is exactly the same.

So, firstly you need to establish details about the image file: sector size and partition offset. You can do this by running fdisk -l <path_to_image_file>. You should see output like this…

fdisk output

Now, we are really only interested in the pieces highlighted in the image above so that we can mount the partition out of the image.

mount <path_to_image_file> <path_to_dir_to_mount_to> -o offset=$((<sector_size>*<start_of_sector>))

To mount the second partition from the above image to the /tmp/scratch directory; the the command would be…

mount /tmp/c\$/rpi-xbmc_20130329.img /tmp/scratch -o offset=$((512*151552))

You should now have the partition that you want from the image mounted. Remember, any changes that you now make in the mounted location will automatically be saved into the image file.