Mount a single partition from an image of an entire disk

For those of you out there with Raspberries Pi who want to mount your SD cards on your Linux VMs:

  1. Get the partition layout of the image:

     $ sudo fdisk -lu sda.img
     ...
     Units = sectors of 1 * 512 = 512 bytes
     Sector size (logical/physical): 512 bytes / 512 bytes
     ...
       Device Boot      Start         End      Blocks   Id  System
     sda.img1   *          56     6400000     3199972+   c  W95 FAT32 (LBA)
  2. Calculate the offset from the start of the image to the partition start:

     Sector size * Start = (in this case) 56 * 512 = 28672
  3. Mount it on /dev/loop0 using the offset

     sudo losetup -o 28672 /dev/loop0 sda.img
  4. Now the partition resides on /dev/loop0. You can fsck it, mount it etc:

     sudo fsck -fv /dev/loop0
     sudo mount /dev/loop0 /mnt

    and unmount it:

     sudo umount /mnt
     sudo losetup -d /dev/loop0

Super useful. Thanks arrange!