mount a disk image from the command line

January 27, 2016 - Reading time: 2 minutes

If it was a hard-drive image with a MBR partition table, I would fdisk the image to find the offset for the partition I need to mount.

fdisk -lu /path/disk.img

Then I would mount it passing the offset.

mount -o loop,offset=xxxx /path/disk.img /mnt/disk.img.partition

The offset value is in bytes, whereas fdisk shows a block count, so you should multiply the value from the "Begin" or "Start" column of the fdisk output by 512 (or whatever the block size is) to obtain the offset to mount at.

On most modern GNU system the mount command can handle that:

mount -o loop file.iso /mnt/dir

to unmount you can just use the umount command

umount /mnt/dir

If your OS doesn't have this option you can create a loop device:

losetup -f # this will print the first available loop device ex:/dev/loop0
losetup /dev/loop0 /path/file.iso #associate loop0 with the specified file
mount /dev/loop0 /mnt/dir #It may be necessary specify the type (-t iso9660)

to umount you can use -d:

umount /mnt/dir
losetup -d /dev/loop0

If the file have partitions, example a HD image, you can use the -P parameter (depending on you OS), it will map the partitions in the file content:

losetup -P /dev/loop0 /path/file.iso # will create /dev/loop0 
ls /dev/loop0p* #the partitions in the format /dev/loop0pX