How to mount a multi-partition disk image in Linux?

You can use kpartx or partx to create loop devices for the partitions on the image, and then mount them. So either:

$ sudo kpartx -v -a file.iso
add map loop0p1 (253:17): 0 8382464 linear 7:1 2048
$ mount /dev/mapper/loop0p1 ./mnt_point
...  do something with the partition  ...
$ umount ./mnt_point
$ kpartx -d -v file.iso
del devmap : loop0p1
loop deleted : /dev/loop0

or:

$ sudo partx -a -v file.iso
partition: none, disk: file.iso, lower: 0, upper: 0
Trying to use '/dev/loop0' for the loop device
/dev/loop0: partition table type 'dos' detected
range recount: max partno=1, lower=0, upper=0
/dev/loop0: partition #1 added
$ mount /dev/loop0p1 ./mnt_point
...  do something with the partition  ...
$ umount /dev/loop0p1 ./mnt_point
$ sudo partx -d -v /dev/loop0
partition: none, disk: /dev/loop0, lower: 0, upper: 0
/dev/loop0: partition #1 removed

See also How can I mount a disk image?


losetup -Pf in util-linux >= 2.21 (Ubuntu 16.04)

sudo losetup -Pf disk.img
sudo mkdir /mnt/loop0p1
sudo mount /dev/loop0p1 /mnt/loop0p1

See the losetup(8) man page and also https://askubuntu.com/questions/69363/mount-single-partition-from-image-of-entire-disk-device/673257#673257

losetup -P automation

Here are functions to automate if further. 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"
)

The answer by @Catskul and @Cristian Ciupitu is perfectly fine, but it misses the loop unmount command. So if you have to do a second image, you will end up with using loop1, loop2 etc.

you can check which loop devices are connected to which images by calling losetup:

pk:~# partx -v -a /home/pkolmann/img/Test.img
partition: none, disk: /home/pkolmann/img/Test.img, lower: 0, upper: 0
Trying to use '/dev/loop1' for the loop device
/dev/loop1: partition table type 'dos' detected
range recount: max partno=2, lower=0, upper=0
/dev/loop1: partition #1 added
/dev/loop1: partition #2 added
pk:~# losetup
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                    DIO LOG-SEC
/dev/loop1         0      0         0  0 /home/pkolmann/img/Test.img   0     512
/dev/loop0         0      0         0  0 /home/pkolmann/img/Test.img   0     512

after unmounting the partitions with

pk:~# partx -v -d /dev/loop0
partition: none, disk: /dev/loop0, lower: 0, upper: 0
/dev/loop0: partition #1 removed
/dev/loop0: partition #2 removed
pk:~# partx -v -d /dev/loop1
partition: none, disk: /dev/loop1, lower: 0, upper: 0
/dev/loop1: partition #1 removed
/dev/loop1: partition #2 removed

the loop devices are still used:

pk:~# losetup
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                    DIO LOG-SEC
/dev/loop1         0      0         0  0 /home/pkolmann/img/Test.img   0     512
/dev/loop0         0      0         0  0 /home/pkolmann/img/Test.img   0     512

These need to be removed extra:

wspk:~# losetup -d /dev/loop0
wspk:~# losetup -d /dev/loop1
wspk:~# losetup