Scripting Xen Image to VirtualBox VDI conversion

Written by NickWal

Having initially done the conversion of xen images to virtualbox using a relatively manual process. I was interest to see how much of this could be scripted. Particularly interesting was the use of grub to install a boot sector on a raw image file, so I would then not have to boot a CD-ROM image inside the virtual machine to install grub manually. I divided the scripts into 3 tasks.

  1. Create the disk image, complete with partition table
  2. Install the linux kernel, grub and fixup any files that need to be changed
  3. Convert the disk image to native Virtualbox format

Below the first script, using parted to create and populate the partition table. kpartx to mount the disk image, and res2fs to ensure the images fit the new disk partitions.

mkfedorasolr-1.sh

#!/bin/bash  
rm fedora-solr.raw  
dd if=/dev/zero of=fedora-solr.raw bs=1 count=1 seek=35G   
parted fedora-solr.raw mklabel msdos  
parted fedora-solr.raw mkpartfs primary ext2 0 12G #root  
parted fedora-solr.raw mkpartfs primary ext2 12G 24G #data  
parted fedora-solr.raw mkpartfs primary linux-swap 24G 26G #swap  
parted fedora-solr.raw print all #just check what you have done  
sudo kpartx -a fedora-solr.raw   
ls -l /dev/mapper  
sudo dd if=fedora-solr/root.img of=/dev/mapper/loop0p1  
sudo dd if=fedora-solr/data.img of=/dev/mapper/loop0p2  
sudo mkswap -f /dev/mapper/loop0p3  
sudo e2fsck -f /dev/mapper/loop0p1  
sudo resize2fs /dev/mapper/loop0p1  
sudo e2fsck -f /dev/mapper/loop0p2  
sudo resize2fs /dev/mapper/loop0p2  
sudo kpartx -d fedora-solr.raw 

Below the second script. Note the following techniques

  1. Using mount –bind to ensure devices are present in the chrooted environment
  2. mount –bind is also used to make the raw image visible from inside the chrooted environment. This is necessary for the installation of grub
  3. copy /etc/resolv.conf into the chrooted environment to make sure that apt-get and friends will be able to resolv dns
  4. The use of chrooted environment using linux32. This allows the preparation of a 32bit disk image from within a 64 bit environment
  5. Grub is used on the disk image file rather than the loopback device itself, since grub returns an error 22 if you try an use the loopback device
  6. Sed is used to edit key files such as /etc/fstab, /etc/mtab and /boot/grub/menu.lst to ensure the system boots and mounts correctly once under the virtual machine

mkfedorasolr-2.sh

#!/bin/bash  
sudo kpartx -a fedora-solr.raw   
sudo mkdir -p /mnt/loop2  
sudo mount -t auto /dev/mapper/loop0p2 /mnt/loop2  
sudo mount --bind /dev /mnt/loop2/dev  
sudo mkdir -p /mnt/loop2/raw  
sudo mount --bind /workarea/raw /mnt/loop2/raw  
sudo cp /etc/resolv.conf /mnt/loop2/etc/  
ls -l /mnt/loop2  
sudo linux32 chroot /mnt/loop2 /bin/bash <<EOF  
mount -t proc proc proc  
apt-get -y update  
apt-get -y install linux-image  
mkdir -p /boot/grub/  
cp --archive /usr/lib/grub/i386-pc/* /boot/grub/  
grub --no-curses --device-map=/dev/null <<EOT  
device (hd0) /raw/fedora-solr.raw  
root (hd0,1)  
setup (hd0)  
EOT  
update-grub -y  
sed -i -e 's!/dev/xv!/dev/s!g' /etc/fstab  
sed -i -e 's!/dev/xv!/dev/s!g' /etc/mtab  
sed -i -e 's!/dev/xv!/dev/s!g' /boot/grub/menu.lst  
sed -i -e 's!(hd0,0)!(hd0,1)!g' /boot/grub/menu.lst  
umount proc  
EOF  
ls -l /mnt/loop2/boot/  
ls -l /mnt/loop2/boot/grub/  
sudo umount /mnt/loop2/raw  
sudo umount /mnt/loop2/dev  
sudo rmdir /mnt/loop2/raw  
sudo umount /mnt/loop2  
sudo kpartx -d fedora-solr.raw

A very simple script to invoke the conversion of the disk image into the native virtualbox format. This script will be enhanced to create and start the virtual machine too.

mkfedorasolr-3.sh

#!/bin/bash  
rm ../virtualbox/fedora-solr.vdi  
VBoxManage convertdd fedora-solr.raw ../virtualbox/fedora-solr.vdi