57

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
  • 1,226

7 Answers7

71

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
  • 1,181
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"
)
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
2

You can use guestfish, a command-line wrapper for libguestfs:

libguestfs is a set of tools for accessing and modifying virtual machine (VM) disk images. You can use this for viewing and editing files inside guests, scripting changes to VMs, monitoring disk used/free statistics, creating guests, P2V, V2V, performing backups, cloning VMs, building VMs, formatting disks, resizing disks, and much more

All this functionality is available through a scriptable shell called guestfish, or an interactive rescue shell virt-rescue.

Get a shell to interact with the disk image (use -r to make the volumes read-only, -w if you want to be able to edit files on the image):

$ sudo guestfish -r -a DiskImage.img
Welcome to guestfish, the guest filesystem shell for
editing virtual machine filesystems and disk images.

Type: ‘help’ for help on commands ‘man’ to read the manual ‘quit’ to quit the shell

><fs> run ><fs> list_filesystems /dev/sda1: vfat /dev/sda2: ext4

First volume is my boot volume, second is my root. To interact with the files on either of them, mount them first:

><fs> mount /dev/sda2 /
><fs> ls /
bin
boot
dev
etc
home
...

To edit a file directly:

><fs> edit /home/pi/.bashrc

You can do lots of other things as well, like copy files to/from the image from/to your local machine or change the partitions. You can also script all these commands together from your local shell as such:

sudo guestfish add DiskImage.img : run : list_filesystems : mount /dev/sda2 / : ls / : edit /home/pi/.bashrc

And when you are done, the disk image itself will have changed. Burn your new image to a new SD card and voilà!

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

First display the partition table to get the start of the partitions:

$ fdisk -l ./sdcard-20250422
Disk ./sdcard-20250422: 59.63 GiB, 64021856256 bytes, 125042688 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xd5c79402

Device Boot Start End Sectors Size Id Type ./sdcard-20250422p1 8192 532479 524288 256M c W95 FAT32 (LBA) ./sdcard-20250422p2 532480 125042687 124510208 59.4G 83 Linux

Then mount with the loop offset option (convert the start in Kilobytes):

$ sudo mkdir /mnt/rootfs
$ sudo mount -o loop,offset=$((532480/2))K   ./sdcard-20250422 /mnt/rootfs/

No need to tinker with losetup as /dev/loop* devices are managed anyway

Emmanuel
  • 101
0

You could try:

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

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

Tog
  • 5,065