MAC OS X, Linux, Windows and other IT Tips and Tricks

05 Feb 16 Creating a new Xen Debian virtual machine from scratch


In this tutorial a new virtual machine based on Debian Jessie distribution will be created from scratch with minimal components.
Assumption: The Xen Hypervisor should already be installed and running in the main system (DOM0).

Creating the Xen Virtual Machine

This virtual machine will be created with the xen tools which bootstraps the creation of the VM.
mkdir -p /virtual/xen/
cd /virtual/xen/
xen-create-image --dir=. --dist=jessie --size=10Gb --swap=2048Mb --ip= --gateway= --netmask= --memory=4096Mb --arch=amd64 --role=udev

Install the kernel and pyGrub
– Put the produced disk.img and swap.img in the proper path.
eg. in /virtual/xen/MAIL/
Mount the disk image in loop
mkdir /mnt/MAIL
mount /virtual/xen/MAIL/disk.img /mnt/MAIL -o loop,rw

Mount /sys, /proc, /dev and chroot to it
mount /proc /mnt/MAIL/proc -o bind
mount /sys /mnt/MAIL/sys -o bind
mount /dev /mnt/MAIL/dev -o bind
chroot /mnt/MAIL

Install the grub-legacy in VM
apt-get update
apt-get install grub-legacy linux-image-3.2.0-4-amd64 mc
mkdir /boot/grub
mcedit /boot/grub/menu.lst
default 0
timeout 2
title Debian GNU/Linux
root (hd0,0)
kernel /vmlinuz root=/dev/xvda1 ro
initrd /initrd.img
title Debian GNU/Linux (recovery mode)
root (hd0,0)
kernel /vmlinuz root=/dev/xvda1 ro single
initrd /initrd.img

Leave chroot and unmount all.
umount /mnt/MAIL/dev
umount /mnt/MAIL/sys
umount /mnt/MAIL/proc
umount /mnt/MAIL/

Adjust the VM xen configuration(/etc/xen/ as follows:
Replace the older kernel and initrd lines in the Xen DOMu configuration file as follows:
kernel = '/boot/vmlinuz-2.6.32-5-xen-amd64'
ramdisk = '/boot/initrd.img-2.6.32-5-xen-amd64'

For Debian squeeze hypervisor:
bootloader = '/usr/lib/xen-default/bin/pygrub'
For Debian wheezy hypervisor:
bootloader = '/usr/lib/xen-4.1/bin/pygrub'
For Debian jessie hypervisor:
bootloader = '/usr/lib/xen-4.4/bin/pygrub'

Adjust the paths of the disks properly:
disk = [

Test the pyGRUB configuration with the VM disk
Note: A GRUB menu should appear for a few seconds and then disappear with an error message. Ignore the error message. Most important is that the Grub menu appears.
For Debian squeeze hypervisor:
/usr/lib/xen-default/bin/pygrub /virtual/xen/MAIL/disk.img
For Debian wheezy hypervisor:
/usr/lib/xen-4.1/bin/pygrub /virtual/xen/MAIL/disk.img
For Debian jessie hypervisor:
/usr/lib/xen-4.4/bin/pygrub /virtual/xen/MAIL/disk.img

Start the VM
The Grub menu should appear and start booting.
xm create /etc/xen/ -c

Important note: Normally after such Bootstrap of a new Xen VM the VM uses the Hypervisor kernel when booting. This means, each VM is not capable to update its kernel independantly. This above method makes the VM fully independent of the Hypervisor kernel and gets its own kernel. The only disadvantage I see is that with some kernel updates the /boot/grub/menu.lst file gets automatically replaced during the kernel upgrade, you then NEED to recover the previous /boot/grub/menu.lst which is normally saved under /boot/grub/menu.lst~ before you reboot the VM. In case you forgot, then simply mount the VM image in loop as explained above and replace the file as needed. You should then be able afterwards to boot th VM.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: