How to mount partitioned disk image files

Mounting unpartitioned disk image files in Linux is easy. You just execute ‘mount -o loop disk.img /mnt’ right?

Well, if your disk image file happens to contain partitions its a little bit trickier. Here is what you need to do:

First you attach the disk image to the first available loopback device
losetup -f -v disk.img

Next, we get a listing of the partition table it contains:

fdisk -ul /dev/loop0

Disk /dev/loop0: 4294 MB, 4294967296 bytes
133 heads, 62 sectors/track, 1017 cylinders, total 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes

Device Boot Start End Blocks Id System
/dev/loop0p1 62 7998619 3999279 83 Linux
/dev/loop0p2 7998620 8386181 193781 82 Linux swap / Solaris

Now for the juicy part. You attach one the partitions contained in the disk image to yet another loopback device by specifying the offset of the partition.

The -o option to losetup tells it the number of bytes to skip at the beginning of the file. To get that number, you multiply the number of bytes per unit (in this case, 512) by the ‘Start” value of the parititon (in this case, 62). So our -o value will be 31744.

losetup -o 31744 -f -v /dev/loop0
Loop device is /dev/loop2

Looks good so far. Now mount it.

mount /dev/loop2 /mnt

Thats it. You should now be able to get at all the files on that partition.

EDIT 2009-01-13
Since writing this I have discovered that you can supply the ‘offset’ argument to mount directly and bypass the entire process of using loopback devices…

fdisk -ul disk.img # to get your offset value as noted above
mount -o loop,offset=31744 disk.img /mnt # to mount a given partition on /mnt

One Response to “How to mount partitioned disk image files”

  1. ngeek says:

    hi,
    you can use kpartx and found your partition into the /dev/mapper

Leave a Reply