Mount RPi image file contains multiple partitions

Note for myself
http://raspberrypi.stackexchange.com/a/13138

Citation.

This question is answered as part of the answer to other questions, but it deserves canonical treatment here so it does not have to keep being repeated.

You can't mount the image as a whole because it actually contains two partitions and a boot sector. However, you can mount the individual partitions in the image if you know their offset inside the file. To find them, examine the image as a block device with fdisk -l whatever.img. The output should include a table like this:

Device Boot Start End Blocks Id System
whatever.img1 8192 122879 57344 c W95 FAT32 (LBA)
whatever.img2 122880 5785599 2831360 83 Linux

These are the two partitions. The first one is labelled "FAT32", and the other one "Linux". Above this table, there's some other information about the device as a whole, including:

Units: sectors of 1 * 512 = 512 bytes

We can find the offset in bytes by multiplying this unit size by the Start block of the partition:

1st partition 512 * 8192 = 4194304
2nd partition 512 * 122880 = 62914560

These can be used with the offset option of the mount command. We also have a clue about the type of each partition from fdisk. So, presuming we have directories /mnt/img/one and /mnt/img/two available as mount points:

mount -v -o offset=4194304 -t vfat whatever.img /mnt/img/one
mount -v -o offset=62914560 -t ext4 whatever.img /mnt/img/two

You can now access the two partitions. If you do not intend to change anything in them, use the -r (read-only) switch too. If you do change anything, those changes will be included in the .img file.

Note that the first partition is probably mounted on /boot in the second partition when the system is running.

Mount RPi image file contains multiple partitions

Leave a comment