How can I mount a disk image?

26

11

I have a disk image myimage.disk which contains the partition table and a primary partition (i.e. a FAT32 filesystem). Think that as a USB pen image.

I want to mount the primary partition to a local directory. I know how to mount a partition image using the loop utils but here I have disk image. My guess is that I have to mount the image "skipping" the partition table but how can I do that?

Emiliano

Posted 2011-10-10T10:04:57.610

Reputation: 663

7

See also http://superuser.com/questions/117136/how-can-i-mount-a-partition-from-dd-created-image-of-a-block-device-e-g-hdd-u You may want to use simply losetup --partscan --find --show disk.img followed by mount /dev/loop0p1 /mnt/disk

– Flow – 2014-11-05T13:24:10.047

1

Possible duplicate of Mounting a multi-partition disk image in Linux

– Cristian Ciupitu – 2018-03-15T12:20:21.517

Answers

45

The kpartx tool makes this easier. It creates loop devices in /dev/mapper for each partition in your image. Then you can mount the loop device that corresponds with your desired partition without having to calculate the offset manually.

For example, to mount the first partition of the disk image:

kpartx -a -v myimage.disk
mount /dev/mapper/loop0p1 /mnt/myimage

When you're done with the image, remove the loop devices:

umount /mnt/myimage
kpartx -d -v myimage.disk

Alternatively, if you have a recent kernel, and pass loop.max_part=63 on boot (if loop is built-in) or to modprobe (if loop is a module), then you can do it this way:

losetup /dev/loop0 myimage.disk
partprobe /dev/loop0             # Re-read partition table if /dev/loop0 was used with a different image before
mount /dev/loop0p1 /mnt/myimage

When you're done with the loop:

losetup -d /dev/loop0

scott.squires

Posted 2011-10-10T10:04:57.610

Reputation: 1 001

2If you don't get partition devices (/dev/loop0p1 etc.) after running losetup, run partprobe /dev/loop0. – Vladimir Panteleev – 2016-02-15T20:39:47.303

kpartx seems to be installed by default in Linux Mint 18. – unfa – 2018-02-26T09:55:19.783

1You don't need the -o loop above, /dev/mapper/loop0p1 is already a block device (a mapper device on top of a loop device, you don't want another layer of loop device on top of that). – sch – 2014-02-07T11:45:53.690

4

Found this:

http://www.andremiller.net/content/mounting-hard-disk-image-including-partitions-using-linux

which seems exactly what I was looking for.

Here's the key part:

mount -o loop,ro,offset=32256 hda.img /mnt/rabbit

where the value of offset is in bytes. The suggested way to get the offset is to point parted at the image, then unit B for bytes and take the start value from the print output. As an alternative, assuming you have the disk space, do the obvious: once you have the offset and size, just use dd to extract each partition to a separate file.

Emiliano

Posted 2011-10-10T10:04:57.610

Reputation: 663

1For writing a really great answer, could you explain the most important parts here? Having a link is great, but having the info here would be best. – slhck – 2011-10-10T10:25:59.483

1Here's the key part: mount -o loop,ro,offset=32256 hda.img /mnt/rabbit, where the value of offset is in bytes. The suggested way to get the offset is to point parted at the image, then unit B for bytes and take the start value from the print output. As an alternative, assuming you have the disk space, do the obvious: once you have the offset and size, just use dd to extract each partition to a separate file. – a CVn – 2011-10-10T11:56:09.727

Yes, sorry for being too short. As Michael pointed out the relevent part is, of course, the mount line containing the offset option. – Emiliano – 2011-10-10T13:14:48.400

2

Nowadays you can minimize your work by using udisks. Solution from ArchWiki: udisks - Mount loop devices

To easily mount ISO images, use the following command:

udisksctl loop-setup -r -f image.iso

This will create a loop device and show the ISO image ready to mount. Once unmounted, the loop device will be terminated by udev.

-r there for read only option.

ephemerr

Posted 2011-10-10T10:04:57.610

Reputation: 171

This is amazing, thanks for the info. Crazy how this is the first time ever I found a reference to udisksctl. – Oxwivi – 2019-01-01T12:49:40.123

2

local_image_file=/var/tmp/image.img
first_byte=$(parted -m ${local_image_file} unit B print | grep Linux | cut --fields 2 --delimiter ':' | sed 's/.$//')
loop_file=/var/tmp/loop
mkdir -p ${loop_file}
mount -o loop,ro,offset=${first_byte} ${local_image_file} ${loop_file}

spew

Posted 2011-10-10T10:04:57.610

Reputation: 21

1While this may answer the question, it would be a better answer if you could provide some explanation why it does so. – DavidPostill – 2015-01-21T07:42:48.940