Archives

Categories

Xen for Training

I’m setting up a training environment based on Xen. The configuration will probably be of use to some people so I’m including it below the fold. Please let me know if you have any ideas for improvements.

The interface for the user has the following documentation:

  • sudo -u root xen-manage create centos|debian [permissive]

    Create an image, the parameter debian or centos specifies which
    distribution you want to use and the optional parameter permissive
    specifies that you want to use Permissive mode (no SE Linux access controls
    enforced).

    Note that creating an image will leave you at it’s console. Press ^]
    to escape from the console.
  • sudo -u root xen-manage list

    Display the Xen formation on your DomU. Note that it doesn’t tell you whether
    you are using Debian or CentOS, you have to access the console to do that.
  • sudo -u root xen-manage console

    Access the console.
  • sudo -u root xen-manage destroy

    Destroy your Xen image – if it’s crashed and you want to restart it.


Firstly the file /etc/sudoers (edited by visudo) has a series of lines such as the following:
user01 ALL=NOPASSWD:/usr/local/sbin/xen-manage

The file /usr/local/sbin/xen-manage has the following:
#!/bin/sh
case "$1" in
list)
  xm list | egrep "^Name|^$SUDO_USER"
  exit 0
;;
destroy)
  xm destroy $SUDO_USER
;;
console)
  xm console $SUDO_USER
;;
create)
  file=/etc/xen/$SUDO_USER
  if [ "$2" = "debian" ]; then
    file=$file-debian
  else
    file=$file-centos
  fi
  if [ "$3" = "permissive" ]; then
    file=$file-permissive
  fi
  xm create -c $file
;;
*)
  echo "Error, use commands list, create, or destroy"
;;
esac

Here is the template configuration file for Debian:
kernel = "/boot/vmlinuz-2.6.18-5-xen-686"
ramdisk = "/boot/initrd.img-2.6.18-5-xen-686"
memory = 128
name = "userXX"
vif = [ 'mac=00:16:3e:00:00:XX, bridge=xenbr1' ]
disk = [ 'phy:/dev/xenvg/debian-XX,sda1,w', 'phy:/dev/xenvg/swap-XX,sda2,w', 'phy:/dev/xenvg/debian-XX,sda3,w' ]
root = "/dev/sda1 ro"
extra = "2 audit=1 selinux=1 enforcing=1"

Here is the template configuration file for CentOS:
kernel = "/boot/vmlinuz-2.6.18-5-xen-686"
ramdisk = "/boot/initrd.img-2.6.18-5-xen-686"
memory = 128
name = "userXX"
vif = [ 'mac=00:16:3e:00:01:XX, bridge=xenbr1' ]
disk = [ 'phy:/dev/xenvg/centos-XX,sda1,w', 'phy:/dev/xenvg/swap-XX,sda2,w', 'phy:/dev/xenvg/centos-XX,sda3,w' ]
root = "/dev/sda1 ro"
extra = "2 audit=1 selinux=1 enforcing=1"

Note that the Debian and CentOS templates have different MAC addresses, this means that they get different IP addresses from the DHCP server and the user won’t have ssh known_hosts problems. The scripts do not permit the user to run both Debian and CentOS images at the same time (due to RAM limits) but it would still be annoying to have to edit the known_hosts file when changing distribution (there will be several changes through the tutorial).

Here is the script that makes the Xen configuration files, creates LVM volumes, and copies the example data to the volumes, you will need to change $VG as appropriate:
#!/bin/sh

VG=xenvg

for n in 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ; do
  sed -e s/XX/$n/ < debian-template > user$n-debian
  sed -e s/XX/$n/ -e "s/ enforcing=1//" < debian-template > user$n-debian-permissive
  lvcreate -n debian-$n -L 1G $VG
  dd if=dev/$VG/debian-template of=dev/$VG/debian-$n bs=4096k
  lvcreate -n centos-$n -L 1G $VG
  dd if=dev/$VG/centos-template of=dev/$VG/centos-$n bs=4096k
  lvcreate -n swap-$n -L 128M $VG
  mkswap /dev/$VG/swap-$n
done

Comments are closed.