If you have played around with virtualisation technologies, specifically Xen, then you may have experienced creating a disk image. To create a disk image on Linux, I execute the following command:
dd if=/dev/zero of=OpenSuse-10.2.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:
If I now run a file command against this image, it should tell me what type of file-system it is.
OpenSuse-10.2.img: Linux rev 1.0 ext3 filesystem data (large files)
Thats all fine when there is a single partition for your disk image. Recently I happened to encounter a problem whereby there was more than a single partition on an image.
Enter “Xen Virtual Machine Management”
I recently have been playing with the “Xen Virtual Machine Management” which is available in OpenSuse 10.2, and it totally rocks. It automates the process of creating you the configuration file for your Xen VM along with the disk images and the final VM without having you to type anything on the command line. Not that I don’t like the command line but I was just pointing our that OpenSuse developers have done a great job with the Xen Manager.
After helping you create the necessary configuration, you can start installing your VM through a little xterm window. This is not one of those “bootstrap” scripts working in the background, but a full OpenSuse Operating System install, just like how you would do when you install an Operating System on your desktop, except this is in text mode.
Now because you are creating a full Operating system installation for your VM, you can basically go through the installation process and create your partitions for your different mount points, select the required packages to be installed and so on. So in the end you end up with a disk image with multiple partitions in it. This is where I encountered the problem. Normally when you have a single partition as show in our earlier example, you can mount the image through a loop-back device like shown below:
mount -o loop OpenSuse-10.2.img mnt
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 I try to mount an image (hda is the image) which has multiple partitions:
mount -o loop hda mnt
mount: you must specify the filesystem type
And if I run the same file command on this image file, the following is the output:
hda: x86 boot sector; partition 1: ID=0x83, active, starthead 0, startsector 1060290, 16787925 sectors; partition 2: ID=0x83, starthead 254, startsector 17848215, 3116610 sectors
As you can see within the output, its lists two partitions for the image. We can look at the detailed listing by executing:
fdisk -lu hda
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 hda1 * 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 within the fdisk command gives the partition tables sizes in sectors instead of cylinders. This is important for us as we will be using that information for some calculation. Also important to us is the “Bytes/sector” values
Calculate the Offset in “Bytes”
Now in order to mount the two partitions from the image file, we need to know the starting Byte from which 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. I pretty well know that the Partition 1 was the root partition and Partition 2 was the Home partition so I will mount them into appropriate folder names.
Now we can use those “Start Byte” values to mount those partitions in two seperate locations:
mount -o loop,offset=542868480 hda rootmount/
mount -o loop,offset=9138286080 hda homemount/