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.
104 lines
3.0 KiB
104 lines
3.0 KiB
---
|
|
title: Mounting disk images on linux
|
|
summary: >
|
|
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. (After
|
|
I looked it up again ;-))
|
|
date: 2022-08-06T21:02:21+02:00
|
|
categories: [computerstuff]
|
|
tags: [linux,raspberry,reminders]
|
|
|
|
---
|
|
|
|
Let's assume you created a disk image with `dd` on a linux computer like
|
|
|
|
~~~console
|
|
$ 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_:
|
|
|
|
~~~console
|
|
$ 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
|
|
|
|
~~~console
|
|
$ 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.
|
|
|
|
~~~console
|
|
$ sudo mount /dev/loop1p2 tmp
|
|
~~~
|
|
|
|
### Removing the loop device
|
|
|
|
~~~console
|
|
$ 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
|
|
|
|
~~~console
|
|
$ sudo mount -o loop,offset=272629760 disk.img tmp/
|
|
~~~
|
|
|
|
A remount is simple as
|
|
|
|
~~~console
|
|
$ 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.
|