How to mount multiple partitions from disk image simultaneously?

You need to specify the length of the partition(s) to avoid overlap. Option sizelimit, see man mount, man losetup.


losetup 2.21 -P option

losetup -P -f --show my.img

Creates one /dev/loopXpY per partition.

Advantage: executable pre-installed in many distros (util-linux package).

Disadvantage: quite recent option, not present in Ubuntu 14.04, before that just use kpartx: https://unix.stackexchange.com/a/405639/32558

losetup -P automation

Usage:

$ los my.img
/dev/loop0
/mnt/loop0p1
/mnt/loop0p2

$ ls /mnt/loop0p1
/whatever
/files
/youhave
/there

$ sudo losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                                                                      DIO
/dev/loop1         0      0         0  0 /full/path/to/my.img

$ # Cleanup.
$ losd 0
$ ls /mnt/loop0p1
$ ls /dev | grep loop0
loop0

Source:

los() (
  img="$1"
  dev="$(sudo losetup --show -f -P "$img")"
  echo "$dev"
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    dst="/mnt/$(basename "$part")"
    echo "$dst"
    sudo mkdir -p "$dst"
    sudo mount "$part" "$dst"
  done
)
losd() (
  dev="/dev/loop$1"
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    dst="/mnt/$(basename "$part")"
    sudo umount "$dst"
  done
  sudo losetup -d "$dev"
)

Use kpartx tool. It will map image partitions using /dev/mapper which you can mount directly.

$ sudo kpartx -a disk.img
$ sudo mount -o loop /dev/mapper/loop0p2 /mnt

PS. Do not forget to remove mappings after you are done: sudo kpartx -d disk.img