Search this site


Metadata

Articles

Projects

Presentations

Mounting partitions within a disk image in Linux

When you create a loop device from a disk image with losetup, it doesn't bother reading the partition table from the disk image so you don't get the nice and easy access to, for example, /dev/loop0p1 for partition 1.

FreeBSD seems to get this right, as I recall, but Linux does not.

fdisk outputs these devices, but they don't exist:

% sudo fdisk -l /dev/loop0 | grep '^/'
/dev/loop0p1   *           1        1043     8377866    7  HPFS/NTFS
/dev/loop0p2            1044        2088     8393962+   7  HPFS/NTFS
Linux's mount(8) command gives you the '-o offset=XXX' option. The offset is a byte offset, and lets you decide how far into your disk image you want to start. However, fdisk doesn't output in bytes, it outputs in cylinders or sectors.

Not to worry, it helpfully outputs the conversion between the units and bytes:

% sudo fdisk -l /dev/loop0 | grep Units
Units = cylinders of 16065 * 512 = 8225280 bytes
Knowing this, let's use awk to generate the offsets for us:
% sudo fdisk -l /dev/loop0 
  | awk '/^Units/ { bytes=$(NF-1) } /^\// { print $1 "[" $NF "]: mount -o offset=" $3 * bytes }'
/dev/loop0p1[HPFS/NTFS]: mount -o offset=8225280
/dev/loop0p2[HPFS/NTFS]: mount -o offset=17174384640
Now simply mount them with 'mount -t ntfs -o loop,offset=XXXX mydiskimage /mnt' or whatever you want :)

5 responses to 'Mounting partitions within a disk image in Linux'

Showing last 5 comments... (Click here to view all comments)

Russell Coker wrote at Wed Jul 16 08:19:55 2008...
Linux has the program kpartx which uses device-mapper to create new block devices referring to the partitions.

Andrew wrote at Mon Aug 18 07:08:34 2008...
Thanks. I was unaware of the offset option. I was attempting to mount a USB drive image and it simply wasn't working. With the offset of 512 I'm able to mount the partition without issues. I also drive the kpartx program but it did not create any of the block devices for the partitions though the command executed without error.

Mike wrote at Wed Oct 21 18:19:05 2009...
by mounting the loop device with max_part=63 (for example--default is 0), you actually get the /dev/loop0p1 (and other) partition devices

Mike wrote at Wed Oct 21 18:19:43 2009...
I mean loading the loop module with max_part=63

Jordan Sissel wrote at Thu Oct 29 20:04:47 2009...
@Mike
Neat, didn't know about the max_part option.

@Russel Coker
I think I avoided using kpartx for some reason that escapes me right now, probably for the reason that Mike explains a solution for.


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: