Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

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 -o loop /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
mount /dev/loop0p1 /mnt/myimage

When you're done with the loop:

losetup -d /dev/loop0
share|improve this answer
    
+1 never heard about this one. –  Emiliano Oct 13 '11 at 7:35
    
You 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 Feb 7 at 11:45
add comment

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.

share|improve this answer
    
For 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 Oct 10 '11 at 10:25
    
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. –  Michael Kjörling Oct 10 '11 at 11:56
    
Yes, sorry for being too short. As Michael pointed out the relevent part is, of course, the mount line containing the offset option. –  Emiliano Oct 10 '11 at 13:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.