You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.1 KiB

title aliases summary date lastmod categories tags
Mounting disk images on linux /posts/2022-08-06-mounting-disk-images-on-linux I sometimes create full-disk-images of USB drives or hard drives and in rare cases I have to look into them. This is how I usually do that. 2022-08-06T21:02:21+02:00 2024-09-28T23:48:06+0000
computerstuff
linux
raspberry-pi

Let's assume you created a disk image with dd on a linux computer like

$ sudo dd if=/dev/sda of=disk.img bs=4M status=progress conv=fsync

There are several partitions in that image and we want to access the linux filesystem on it. For reference, I'll bring in some old backup I made from a Raspberry Pi. That backup is taken from a 8GB sdcard, which is 2.6GB compressed with xz.

When uncompressed, look at the partition table with fdisk:

$ fdisk -l disk.img
Festplatte disk.img: 7,4 GiB, 7948206080 Bytes, 15523840 Sektoren
Einheiten: Sektoren von 1 * 512 = 512 Bytes
Sektorgröße (logisch/physikalisch): 512 Bytes / 512 Bytes
E/A-Größe (minimal/optimal): 512 Bytes / 512 Bytes
Festplattenbezeichnungstyp: dos
Festplattenbezeichner: 0x1b7f4bbb

Gerät      Boot Anfang     Ende Sektoren Größe Kn Typ
disk.img1         8192   532479   524288  256M  c W95 FAT32 (LBA)
disk.img2       532480 15523839 14991360  7,1G 83 Linux

We will refer to this output later again.

Using losetup

The output of fdisk is not that important to us, unless we have an unknown disk image that we need to inspect first. I already know the partitions. The first is the FAT32 partition used for UEFI and the second is the root file system.

Creating and mounting the loop device

$ sudo losetup --partscan --find --show disk.img
/dev/loop1

The second line is the output of the program. I used losetup already today, so this is not loop0 but loop1. You may get /dev/loop0 usually.

Mount the new virtual loop device to the directory that you like. This is ~/tmp in my case.

$ sudo mount /dev/loop1p2 tmp

Removing the loop device

$ sudo umount tmp
$ sudo losetup -d /dev/loop1

Using fdisk and mount

From the output above, we see that 532480 is the starting unit of the linux filesystem in this image file. Further above you see the Units (Einheiten): 1 Unit is 1 sector of 512 Bytes.

I use a german speaking computer, so you might look for Start or Offset or Beginning---you know what to look for...

We calculate the needed offset like: 532480 * 512 = 272629760

And the resulting command is

$ sudo mount -o loop,offset=272629760 disk.img tmp/

A remount is simple as

$ sudo umount tmp

When do you need this stuff

I often create quick and dirty (big) card images from my Raspberry Pies. They are saved and easy to copy over to another storage (because they are a single file).

If you have less space, dd is probably not the best method to create a disk backup. partimage for example creates images from partitions, but it only saves the used data from that partition. Those images are smaller.