How to transfer a Linux image from VirtualBox to Xen

There have been times recently when I wanted to pull a VirtualBox Linux instance I had into Xen. I kept thinking it had to be fairly easy but I kept putting off trying it until recently when I ran into something I wanted to install from a CD image into an Amazon EC2 AMI. It turns out the main hurdle in transferring an image is lack of documentation.

I'm using VirtualBox 2.1.0 so some of the following commands may not work with older versions. I learned the hard way that they have changed a number of tools for VirtualBox and some of the older tools where probably easier to use and documented better. I installed the package I was using from an ISO image and then started trying to extract the part that I needed from the VDI that was created.

My first attempt at extracting the partition required me to convert my dynamic VDI into a static image. To dump a dynamic VDI into a static image you run this command:

VBoxManage convertdd -static abox.vdi /tmp/abox.img

I thought I could find the image by hand in the VDI after I had it in a raw format. There were a number of hints that I found that made me think I could just pull the partition out without much of a problem: VirtualBox and forensics tools and a forum post. However I found that just looking around wasn't easy enough to find where the partition started so I moved on to trying to find something else that could scan the disk and find it.

I rand into TestDisk and gave it a try. When it would scan the disk it found the /boot partition but for some reason it wasn't finding the root partition so I moved on.

I then took a look at the format for VDI disks to see if it was possible to pull it out given the header information with a simple program but that looked like it would be a lot of work so it was back to square one.

Along the way I happened to came across information about an undocumented command to export raw disk image. This turned out to be the break I needed because running the following command will result in only the disk image itself without any VirtualBox residue:

VBoxManage internalcommands converttoraw myosimage.vdi /tmp/myosimage.img

At this point things became a lot easier. There were multiple partitions on the resulting disk image but I only needed the / partition. To extract the root partition I first listed the partitions with this command:

fdisk -lu myosimage.img

This output the following for my image:

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

Disk myosimage.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x0003f47f

      Device Boot                Start             End         Blocks    Id  System
myosimage.img1   *              63       208844        104391   83  Linux
myosimage.img2          208845      3662819     1726987+  83  Linux
myosimage.img3         3662820     4192964       265072+  82  Linux swap / Solaris

To figure out where the root partition starts I just multiplied the start sector by the number of bytes per sector: 208845 * 512 = 106928640

I then did a quick test to make sure I had the correct partition:

mount -o loop,offset=106928640 myosimage.img /mnt/

This looked good so I extracted the partition from the disk and did a filesystem check on it:

dd if=myosimage.img of=mypartimage.img bs=512 skip=208845 count=3453974
e2fsck mypartimage.img

Extracting the partition you want is about 80% of the battle. Getting it to run under Xen after extraction is just a matter of fixing anything that was left out because the install was done under a "real" machine.

I add a nosegneg directive and move /lib/tls directory out of the way first:

echo "hwcap 0 nosegneg" >  /mnt/etc/
mv /mnt/lib/tls /mnt/lib/tls.disabled

Next the base device entries needed to be created:

for i in console null zero ; do /sbin/MAKEDEV -d /mnt/dev -x $i ; done

I then removed the disk label from the partition using e2label:

e2label mypartimage.img ""

Because I was sending this image to EC2 I recreated the fstab with the following entries that are specific to the way EC2 allocates disks to a node:

/dev/sda1  /         ext3    defaults        1 1
/dev/sda2  /mnt      ext3    defaults        1 2
/dev/sda3  swap      swap    defaults        0 0

The finally, again because I was going to EC2 I added a few scripts and created rc.local to let me in when the instance was started.

This seems to be a fairly easy process now that I have done it from start to finish once.

Leave a Reply

Your email address will not be published. Required fields are marked *