52

Normal filesystem images can be easily mounted:

mount system.img /mnt

Examined, and modified. But when I try this with a Raspberry Pi system image (e.g. raspbian), I get:

mount: unknown filesystem type '(null)'

And no matter what I try with -t, it won't work. How can I mount this image?

1

5 Answers 5

34

losetup provides partition probing through -P. Using this makes mounting partitions of a full disk image such as the Raspbian SD card image very easy:

losetup -P /dev/loop0 raspbian.img
mkdir /mnt/raspbian
mount /dev/loop0p2 /mnt/raspbian
mount /dev/loop0p1 /mnt/raspbian/boot
1
  • 1
    This seems less prone to human error. Can I also use labels in the mount commands? Feb 1, 2018 at 11:26
97

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

If you get an "overlapping loop" error here, your version of mount requires you to specify the size as well as the offset of the first partition. Unmount that, and use the number of blocks (57344) * 512 (= 29360128):

mount -v -o offset=4194304,sizelimit=29360128 \
    -t vfat whatever.img /mnt/img/one  

The second partition doesn't need a sizelimit since there's nothing after it in the image.

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.

5
  • 14
    kpartx makes it even easier for you: kpartx -a whatever.img makes the appropriate (loop) nodes, which you can later mount at will. Run kpartx -d whatever.img to delete the (unmounted) loop nodes.
    – user29510
    Dec 28, 2015 at 11:39
  • 1
    similar: stackoverflow.com/a/1421075/544721 Feb 4, 2017 at 8:42
  • How to mount root and boot partition simultaneously : unix.stackexchange.com/q/342463/9689 ? Feb 4, 2017 at 10:35
  • 1
    P.S. If one want's to mount both partitions at same time, in order to avoid "overlap" error, just specify size of partition with sizelimit option. For more info: unix.stackexchange.com/a/342466/9689 . (Btw. above answer may be updated to provide example usage of that option) Feb 4, 2017 at 21:56
  • 4
    Rather than multiplying manually, you can use bash's Arithmetic Expansion: $((8192*512)).
    – Ruslan
    Aug 8, 2017 at 13:46
3

Raspbian/Raspberry Pi OS repositories have the Gnome disk utility package that installs Image Mounter and adds "Mount Image" to the right-click context menu.

Viz.:

sudo apt-get install gnome-disk-utility

This allows you to mount and use the contents of a disk image with multiple partitions.

I do not know if it mounts R/O or R/W.

2

At first I was using awk/grep to parse fdisk and pull out offsets, but found it could be a bit complicated, flakey, and hard to understand later. The easiest method I have found:

IMAGE_FILE="YOUR IMAGE FILE GOES HERE"
TMP=$(mktemp -d)
LOOP=$(sudo losetup --show -fP "${IMAGE_FILE}")
sudo mount ${LOOP}p2 $TMP
sudo mount ${LOOP}p1 $TMP/boot/

# do stuff to ${TMP}/ which is rpi filesystem

# cleanup
umount ${TMP}/boot/
umount ${TMP}
rmdir ${TMP}

This creates a temp-dir for you, auto-assigns the next loop-device and sets it up for you as p1 and p2 partition devices, and mounts boot and root.

You can also put the cleanup part in an exit-trap, so if something fails you won't have dangling mounts/files:

set -e
function cleanup {
  sudo umount -f $TMP/boot/
  sudo umount -f $TMP
  rmdir $TMP
}
trap cleanup EXIT
0

In case fdisk -l shows up with sectors instead of blocks on Arch linux... Another way to find the number of blocks in case of the "overlapping loop error" is to subtract the offset in bytes of the first partition from the second partition and divide by two. For example (62914560-4194304)/2 = 29360128

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.