Last Updated: February 25, 2016
·
1.437K
· phsym

Mount a partition from a raw Virtual Machine disk file

May be usefull for anyone working with virtualization, for example if you want to mount a template image with multiple partitions locally to modify it, push some configurations, etc ...

For example, let's call our image file FooBar-disk1.raw

fdisk -l FooBar-disk1.raw

Disk FooBar-disk1.raw: 322.1 GB, 322122547200 bytes
255 heads, 63 sectors/track, 39162 cylinders, total 629145600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Device Boot                      Start         End      Blocks   Id  System
FooBar-disk1.raw1   *          63    12611024     6305481   83  Linux
FooBar-disk1.raw2        12611025    25222049     6305512+  83  Linux
FooBar-disk1.raw3        25222050   629137529   301957740    5  Extended
FooBar-disk1.raw5        25222113    29447144     2112516   83  Linux
(...)

In order to mount a partition, we have to find its start sector, multiply it with the size of a sector (here it's 512 bytes).

Then we can mount the partition, using its start address as an offset in the mount command :

To mount partition 1 on /mnt:

mount -o offset=$((63*512)) FooBar-disk1.raw /mnt

To mount partition 2 on /mnt:

mount -o offset=$((12611025*512)) FooBar-disk1.raw /mnt