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

16 Mar 13 Installing an Ubuntu 12.0.4 LTS as Xen DOMu in Debian Squeeze hypervisor

Lately I needed to install Zimbra 8.0.3 which only installs easily in an Ubuntu 10.0.4 or 12.0.4 LTS system.
So I decided for that to install an Ubuntu 12.0.4 LTS as Xen DOMu in a Debian Squeeze Xen Hypervisor and here is how I did it.

The following commands can be put into a runnable bash script. Adapt the constants below to your system and simply run it, or simply(recommended) cut and paste the commands in bash terminal. Then afterwards (indicated below)inside the chroot the commands need to be done manually.

# Settings some user changeble constants
# Installing required packages
apt-get update
apt-get install gnupg
# Creating, formating and mounting an empty 10GB VM
mkdir -p $virtbase/$VM
dd if=/dev/zero of=$virtbase/$VM/disk.img bs=1GB count=10
mkfs.ext3 $virtbase/$VM/disk.img
mkdir -p /mnt/$VM
mount $virtbase/$VM/disk.img /mnt/$VM -o loop,rw
# create and format a 2GB swap file
dd if=/dev/zero of=$virtbase/$VM/swap.img bs=1GB count=2
mkswap $virtbase/$VM/swap.img
# Creating a temporary ubuntu bootstrap environment
mkdir -p $tempdir
cd $tempdir
# Getting the ubuntu bootstrapper and keys
wget ""
ar -xf debootstrap_1.0.40~precise1_all.deb
tar xzf data.tar.gz
tar xzf control.tar.gz
wget ""
tar xzf ubuntu-keyring_2011.11.21.tar.gz
# Bootstrapping Ubuntu system into the mounted empty VM
DEBOOTSTRAP_DIR=$tempdir/usr/share/debootstrap usr/sbin/debootstrap --arch amd64 --keyring=ubuntu-keyring-2011.11.21/keyrings/ubuntu-archive-keyring.gpg precise /mnt/$VM
# Chroot to the new VM to prepare it
mount /sys /mnt/$VM/sys -o bind
mount /proc /mnt/$VM/proc -o bind
chroot /mnt/$VM/

From this point you need to cut-and-paste the commands in the console manually

Note: Ignore minor errors during apt-get install because we are in chroot and many things will not behave as normal but install properly.

Expand the repositories to get more packages to be available
sed -i 's/precise main/precise main restricted multiverse universe/' /etc/apt/sources.list
apt-get update

Remove server-useless resolvconf
apt-get remove resolvconf
Give a password to root user
Install the kernel and old grub(for virtual booting), ssh service and and some useful programs
apt-get install linux-image-virtual grub nano mc ssh fail2ban less manpages libgmp3c2 libperl5.14 sysstat sqlite3
Stop fail2ban
/etc/init.d/fail2ban stop
Add the correct mounts in /etc/fstab
echo "/dev/xvda1 / ext3 noatime,nodiratime,errors=remount-ro,usrquota,grpquota 0 1" > /etc/fstab
echo "/dev/xvda2 none swap sw 0 0" >> /etc/fstab

Create the grub config file
mkdir -p /boot/grub
nano /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

Here we need to configure the server name, and network addresses
Constants:(Create a script (/root/ with the following content and fill in those Variables definitions accordingly.)

Call/load the script which will set the proper variables for the processes below:
. /root/

Configure the server name
echo "$Hostname" > /etc/hostname
echo "$Hostname" > /etc/mailname

Configure /etc/hosts
echo "$InternetIP $Hostname $(echo $Hostname | cut -d. -f1)" >>/etc/hosts
echo "$LANIP $(echo $Hostname | cut -d. -f1).srv" >>/etc/hosts

Configure the resolver
cat > /etc/resolv.conf << EOF
nameserver $DNS1
nameserver $DNS2
nameserver $DNS3
search $search

Configure the netwwork
cat > /etc/network/interfaces << EOF
# /etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address $InternetIP
netmask $Netmask
broadcast $Broadcast
gateway $Gateway
auto eth1
iface eth1 inet static
address $LANIP

Leave chroot and unmount all of the VM
umount /mnt/UBUNTU/sys
umount /mnt/UBUNTU/proc
umount /mnt/UBUNTU

Configure the xen configuration file for UBUNTU
Note: Replace the {InternetIP} and {LANIP} with the appropriate ones.
nano $virtbase/$VM/${VM}.cfg
bootloader = '/usr/lib/xen-default/bin/pygrub'
memory = '2500'
root = '/dev/xvda1 ro'
disk = [
name = 'UBUNTU'
vif = [ 'ip={InternetIP},mac=00:16:3E:3D:6B:11,bridge=eth0' , 'ip={LANIP},mac=00:16:3E:D7:9C:11,bridge=dummy0']
on_poweroff = 'destroy'
on_reboot = 'restart'
on_crash = 'restart'
vnc = 0
vncunused = 0
extra = "console=hvc0"
vcpus = 1
cpus ="1"

Create a symlink of the xen config file
ln -s $virtbase/$VM/${VM}.cfg /etc/xen/${VM}.cfg
Start the VM
xm create /etc/xen/${VM}.cfg
Connect to the VM via SSH
ssh root@$LANIP
(Give the root password you configured with passwd previoulsy)

Reader's Comments


    you can just use one option –dist and no need do more things
    xen-create-image –hostname zimbra.xen –ip –netmask –gateway –size=100G –fs=ext4 -dist=precise

    Reply to this comment

Leave a Reply

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

%d bloggers like this: