Mount a disk image

3 years ago, I wrote about network booting and using NFS for a Raspberry Pi.

When the whole software is already installed on a SD card in the RPi, the scenario is pretty easy - dump the whole filesystem on the NFS server. But what when the only thing available is the downloaded disk image file ? Dumping it on a SD card before copying it on the NFS server can’t be the solution, there must be a way to directly extract the content of the image file to the NFS server.

The problem is that it is not possible to simply mount the disk image as it contains multiple partitions. Actually it is possible to mount every single partition, knowing its offset within the image file, but this implies some manual work and math to compute the correct offset.

Fortunately there is the tool kpartx, part of the multipath tools. Upon calling the tool, it will create loopback devices all partitions found in the image : /dev/loop0p0, /dev/loop0p1, /dev/loop0p2 etc… Then each partition can be mounted as usual.

  • Make all files available : kpartx -a file.img
  • Remove the loop devices : kpartx -d file.img