Loopback mounting an image file with partitions

When you use ‘dd’ to image an entire disk to an image file you need to calculate an offset to loopback-mount a specific partition. Steps:

  1. Use fdisk to print the relevant info
fdisk -ul disk.img

Look for this line:

Units = sectors of 1 * 512 = 512 bytes
  1. Suppose we want to mount the second partition, look at the ‘Start’ column of the fdisk output:
   Device Boot      Start         End      Blocks   Id  System
disk.img1              16        8255        4120   83  Linux
disk.img2            8256    15659007     7825376   83  Linux
  1. To mount the second partition execute this command:
mount -o loop,ro,offset=$((512*8256)) disk.img /mnt/tmp

The trick here is to calculate the offset as UNIT_SIZE*PARTITION_START. Happy restoring!