Skip to content

Linux Disk Images

Mounting a Disk Image

Explains on how to mount an image (dd copied) from a disk containing multiple partitions.

Setup Loop Device

The command losetup sets up the device scanning for partitions. The image and partitions are here mounted read-only as precaution only.

losetup --partscan --find --show --read-only <assets-path> 

On success, it reports the loop device /dev/loopX in the output.

When looking in to the devices the partitions are visible as /dev/loopXp1 and /dev/loopXp2 and so on.

To remove the device(s) use the detach option on the new created loop device.

losetup --detach <loopdev>

Mounting a Loop Partition

mount -o read-only /dev/loopXp1 /mnt/<mount-dir>

Alternate using a Mount Offset

List partitions

Use fdisk to find partitions sector offsets and the sector size.

fdisk -lus <assets-path> 

Mount a partition

Needs calculation for the byte offset of the sector for the partition using a calculation.

mount -o loop,ro,offset=$(expr <offset> \* <sector-size>) <assets-path> <mnt-dir>

Saving a Disk Image

Command(s) to save a disk image in a zipped form using a pipe to gzip.

dd if=/dev/sdX status=progress | gzip -c > gzip-file-out.assets.gz

Restoring a Disk Image

Command(s) to restore a disk image in a zipped form using a pipe to gzip.

gunzip -c gzip-file-in.assets.gz | dd of=/dev/sdX status=progress

Changing the UUID of a Partition

When a disk image is restored the partitions get the same UUID used by the original.

To have a different UUID a new one must be generated using uuidgen

uuidgen
tune2fs /dev/sdXN -U <the-new-uuid>
Or all at-once
tune2fs /dev/sdXN -U $(uuidgen)
The system might be complaining before executing a check must be performed on the partition.
e2fsck -f /dev/sdXN