brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: mount -o loop,ro opens a disk image or ISO without writing to it

Disk
~/tips/tip-disk-mount-loop-ro-image

mount -o loop,ro backup.img /mnt/img

Mounts an image file as a read-only folder, so you can copy files out of it safely.

An ISO, a backup image of a filesystem or a VM’s raw disk file: you can browse its contents like a folder. I’d mount those read-only.

mkdir -p /mnt/img
mount -o loop,ro backup.img /mnt/img

loop makes the kernel treat the file as a disk, and mount detects the filesystem type by itself. ro means nothing can be written. An ext4 image file then mounts on /dev/loop0 with ro,relatime, and touch inside it fails with “Read-only file system”.

Read-only matters for evidence and backups. Even just mounting a filesystem read-write can change it: the journal gets replayed and times get updated, so the image is no longer what it was. Mount it with ro and copy out what you need.

When you’re done:

umount /mnt/img

This works for an image of one filesystem. A full disk image with a partition table has the filesystem at an offset, and mount can’t find it this way. That’s where losetup -P comes in, which creates a device for every partition. For ext4, ro may still replay a dirty journal on some kernels; add noload to the options when the image must stay byte for byte identical.

Further reading: mount(8) manual page, losetup(8) manual page

More Disk tips sudo e2freefrag /dev/sdb1 sudo smartctl -a --json /dev/sda | jq '.smart_status.passed' lsscsi -s udisksctl power-off -b /dev/sdX du -sh — .[!.]* * 2>/dev/null | sort -h All Disk tips (44) All topics

Link