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

25

6

I downloaded an raw SD card image that has two partitions.

It caused some file system errors when I tried to dd it directly into an SD card. I am not sure if the card is defective or the image.

Is there a way to examine this image without writing it to a physical card? Like trying to mount the partitions separately or checking the tables?

Atilla Filiz

Posted 2010-11-16T09:00:56.883

Reputation: 777

Answers

22

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?

Catskul

Posted 2010-11-16T09:00:56.883

Reputation: 573

Thanks @frank-breitling. Edit was rejected by reviewers before I had a chance to get to it. – Catskul – 2017-01-09T18:47:33.197

15

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"
)

Ciro Santilli 新疆改造中心法轮功六四事件

Posted 2010-11-16T09:00:56.883

Reputation: 5 621

1

You could try:

mount -t type -o loop ./image /mnt

where "type" = fs type and "image" is the name of your downloaded file

Tog

Posted 2010-11-16T09:00:56.883

Reputation: 4 747

3Thinking about it, that may not work with an image containing multiple partitions. – Tog – 2010-11-16T09:53:52.240

2you'll have to specify the offset of the single partition to mount with something like "mount -o loop,ro,offset=XXXXXXXX imagefile /mnt" – Mr Shunz – 2010-11-16T10:02:55.867

1Wouldn't losetup work to specify an offset? – Tog – 2010-11-16T11:47:40.700

you're right... with losetup you can set an offset to the partition to create a loopback device that can be simply mounted with mount /dev/loopX /mnt – Mr Shunz – 2010-11-18T11:55:41.660

1

This answer on ServerFault suggests:

use losetup to get a /dev/loop? device, then use kpartx on it to create dev mappings for the partitions in the image file.

Mr Shunz

Posted 2010-11-16T09:00:56.883

Reputation: 2 037