53

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?

5 Answers 5

63

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?

3
  • Thanks @frank-breitling. Edit was rejected by reviewers before I had a chance to get to it.
    – Catskul
    Commented Jan 9, 2017 at 18:47
  • Thanks, it worked. However, it changed the modification or creation date of "file.iso" (created via "cat /dev/sda > file.iso" in my case). I could see that kpartx changed the date via "stat file.iso". Did it change any of the bytes of the ISO file? I hope not. I wish I knew that kpartx or partx might modify the disk image/file.iso file before using the kpartx software. Maybe in the future I will get hashes of "file.iso" (of some other disk or partition) before and after using kpartx with it to see if kpartx modified any file data.
    – Rublacava
    Commented Dec 31, 2020 at 4:18
  • You can check by setting the file to read-only, but I feel pretty confident that neither tool modifies the data.
    – Catskul
    Commented Dec 31, 2020 at 5:46
39

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"
)
1
  • i'm using a slightly modified version of this which checks if the file/loop exists before running
    – mekb
    Commented Oct 17, 2021 at 1:39
10

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
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.

0

You could try:

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

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

4
  • 3
    Thinking about it, that may not work with an image containing multiple partitions.
    – Tog
    Commented Nov 16, 2010 at 9:53
  • 2
    you'll have to specify the offset of the single partition to mount with something like "mount -o loop,ro,offset=XXXXXXXX imagefile /mnt" Commented Nov 16, 2010 at 10:02
  • 1
    Wouldn't losetup work to specify an offset?
    – Tog
    Commented Nov 16, 2010 at 11:47
  • 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 Commented Nov 18, 2010 at 11:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .