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:

[code lang=”bash”]
VBoxManage convertdd -static abox.vdi /tmp/abox.img
[/code]

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:

[code lang=”bash”]
VBoxManage internalcommands converttoraw myosimage.vdi /tmp/myosimage.img
[/code]

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:

[code lang=”bash”]
fdisk -lu myosimage.img
[/code]

This output the following for my image:

[code lang=”text”]
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
[/code]

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:

[code lang=”bash”]
mount -o loop,offset=106928640 myosimage.img /mnt/
[/code]

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

[code lang=”bash”]
dd if=myosimage.img of=mypartimage.img bs=512 skip=208845 count=3453974
e2fsck mypartimage.img
[/code]

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 ld.so.conf directive and move /lib/tls directory out of the way first:

[code lang=”bash”]
echo “hwcap 0 nosegneg” > /mnt/etc/ld.so.conf.d/nosegneg.conf
mv /mnt/lib/tls /mnt/lib/tls.disabled
[/code]

Next the base device entries needed to be created:

[code lang=”bash”]
for i in console null zero ; do /sbin/MAKEDEV -d /mnt/dev -x $i ; done
[/code]

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

[code lang=”bash”]
e2label mypartimage.img “”
[/code]

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:

[code lang=”bash”]
/dev/sda1 / ext3 defaults 1 1
/dev/sda2 /mnt ext3 defaults 1 2
/dev/sda3 swap swap defaults 0 0
[/code]

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.

13 thoughts on “How to transfer a Linux image from VirtualBox to Xen

  1. Pingback: Running Asterisk in the cloud with Amazon EC2 @ IONCANNON

  2. carson Post author

    I don’t think you can transfer a windows server image over so I haven’t tried. I’m not sure you can select anything but linux kernels to boot from.

  3. carson Post author

    I was using Linux. I can’t easily test on OS X so I’m not sure what you would need to do there to get the image converted.

  4. dave

    Could you expand on: “I added a few scripts and created rc.local to let me in when the instance was started” I have successfully created a custom AMI and registered it, but once launched, I am unable to SSH into it. Yes, I have checked that port 22 is authorized. Thanks.

  5. Nehemiah

    what if one of those images are in an LVM? I used virtual box to setup a ubuntu server install (9.10) and I couldn’t mount the lvm partition. can I use dd to extract the data anyway?

  6. Seth

    Perhaps

    losetup -f raw.img
    kpartx -a /dev/loop0

    partprobe
    blkid

    Treat the raw disk as a regular disk :)

  7. Peter

    Thanks, Carson. This post was very useful to me.

    Is there a one-off error in the count parameter to your dd command (because the end sector number is inclusive)?

  8. Pingback: Converting from VirtualBox or VMWare to EC2 now Easier than Ever

  9. carson Post author

    @Peter There may be but I haven’t noticed any problems after an fsck so it is hard to tell. Either way you would probably be OK.

Comments are closed.