So you have a disk image (in my case, a Raspberry Pi image), and you want to access the information therein. What do you do? Sure, you could dd the image on to some media (flash card, USB, etc...) large enough to contain it, but if it is of any substantial size (more than 2GB really), this can suddenly become a very tedious and time consuming process, especially if you have limited media to use. A better option would be to mount the image, and just read it directly, but unlike an ISO, there's more involved than just mounting the image.

For starters, an ISO does not contain a partition table, and so cannot contain multiple partitions, and can thus be mounted with ease. A disk image, on the other hand, can contain a partition table, and multiple partitions, making mounting slightly more difficult. In the end, it all depends on the disk image, what it contains, and how it was taken/created. For the sake of the article (and for my own documentation), we're going to go over mounting a Raspberry Pi image.

Since we know exactly what the image is, we know there are (likely, custom images aside) a couple of partitions and some kind of boot sector. We can mount the individual partitions in the image if you know their offset inside the file. To gather these details, we can actually run fdisk on the image, like this:

user@host:~$ fdisk -l RasPi.img

Disk RasPi.img: 7969 MB, 7969177600 bytes
255 heads, 63 sectors/track, 968 cylinders, total 15564800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000981cb

     Device Boot      Start         End      Blocks   Id  System
RasPi.img1            8192      122879       57344    c  W95 FAT32 (LBA)
RasPi.img2          122880    15564799     7720960   83  Linux
user@host:~$

From this output, we can see a few key details we will need to use when we want to try to mount, namely:

Units: sectors of 1 * 512 = 512 bytes
     Device Boot      Start         End      Blocks   Id  System
RasPi.img1            8192      122879       57344    c  W95 FAT32 (LBA)
RasPi.img2          122880    15564799     7720960   83  Linux

The "Start" is the Sector number where that partition starts. The Units is the number of bytes per sector. The offset for the mount command is in bytes, so all we need to do is multiply the Start by the number of bytes per sector (512). In this case:

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

So, all we need to do now is run the mount commands:

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

BEWARE: Mounting the images in this fashion sets them up to be writable; so any changes you make here will be reflected within the image. If you delete or move a file out of the image, it will not be present if you "burn" the RasPi image to a card. To avboid this, us the '-r' mount flag to mount the image as read-only. On that same note, and file you ADD will indeed be present, so that can be very useful if you want to try to automate some image customizations.

And that is how you mount a Disk Image to access it directly. Let me know what you think in the comments below!