HowTo: Mounting Partitions within a XEN domU disk Image

If you have been using virtualisation technologies such as XEN, you would of come across the usage of disk images to store our operating system and data on.  To create one of these we run:

dd if=/dev/zero of=mydisk.img bs=1024k count=0 seek=5000

This would have created for me an image which is approximately 5GB in size. And then I format it to an ext3 file-system with:

This command will of create a new disk image, 5GB in size called mydisk.img.  This is where Debian will be installed but firstly we need to give the new disk a file sytem type, so we run:

mkfs.ext4 mydisk.img

The problems people may face is when the disk image isn’t formatted by dom0, but instead are formatted by an installer and creates more that one partition inside the image.

usually we can mount the newly formatted disk like this:

mount -o loop mydisk.img /mnt/disk1

But since the image which gets created during the installation process of the VM, has multiple partittions, a simple mount won’t work. This is what happens when we try to mount an image which has multiple partitions:

Since the installer creates multiple partitions inside the image, the normal easy mounting within dom0 will not work:

mount -o loop mydisk.img /mnt/disk1: you must specify the filesystem type

We can examine the file further by executing:

fdisk -lu hda

Which outputs:

You must set cylinders.
You can do this from the extra functions menu.

Disk hda: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes

Device Boot      Start         End      Blocks   Id  System
  centos.img   *     1060290    17848214     8393962+  83  Linux
Partition 1 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(1110, 254, 63)
  hda2        17848215    20964824     1558305   83  Linux
Partition 2 has different physical/logical beginnings (non-Linux?):
     phys=(1023, 254, 63) logical=(1111, 0, 1)
Partition 2 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(1304, 254, 63)

The ‘u’ flag inside fdisk tells us the partition table sizes in sectors rather than cylinders.  From here we will need this information so we can calculate the correct offset.

Calculate the Offset in “Bytes”

In order to mount the two partitions from the image, we need to know the starting Byte from where each partition starts. To calculate this offset, we mulitply the (start_sector * sector_byte_size). So for the Partition 1 we have (1060290 * 512) = 542868480 and for Partition 2 we have (17848215 * 512) = 9138286080.

Now we can use those “Start Byte” values to mount those partitions in two seperate locations:

mount -o loop,offset=542868480 mydisk.img /mnt/partition1
mount -o loop,offset=9138286080 mydisk.img /mnt/partition2