#!/bin/bash -e # Default memory allocation. memsize=64 nics=1 if test $# -lt 1; then echo Usage: `basename $0` imagename "[memsize(MB)]" "[nics]" echo Default memsize is ${memsize}MB. echo Default number of NICs is $nics. exit 1 fi img=${1} memory=${2:-"$memsize"} nics=${3:-"$nics"} if test $# -le 3 ; then shift $# else shift 3 fi case $img in /*) ;; *) img=`pwd`/$img ;; esac if ! test -s "$img" ; then if test -s "$img.img" ; then img="$img".img fi fi # Sanity check that we have a root filesystem image. if ! test -s ${img} ; then echo "No image!" exit 1 fi name=`basename "$img"` name=${name//.img} # Create a temporary directory to store the kernel image and any other stuff # which we'll need. tmpdir=`mktemp -d ${TMPDIR:-/tmp}/bootXXXXXX` unmount() { test -d ${tmpdir}/root && umount ${tmpdir}/root rm -fr ${tmpdir} } trap unmount EXIT # Mount the filesystem for a bit. mkdir ${tmpdir}/root mount -o loop -t ext3 ${img} ${tmpdir}/root || exit # Check which version of the kernel is installed. kversion=`chroot ${tmpdir}/root rpm -q --qf '%{version}-%{release}' kernel-xenU`xenU # Get a copy of the kernel image. cp ${tmpdir}/root/boot/vmlinuz-${kversion} ${tmpdir} # Ensure that the root device exists in the guest. rootdev=`awk '{ if ( $2 == "/" ) print $1 }' ${tmpdir}/root/etc/fstab` chroot ${tmpdir}/root MAKEDEV -x $rootdev `echo $rootdev | sed s,'[[:digit:]]*$',,g` # Calculate module dependencies. chroot ${tmpdir}/root depmod -a ${kversion} # Okay, we're ready. umount ${tmpdir}/root rmdir ${tmpdir}/root # Check that the dom0 xend can figure out which major/minor to emulate. If it # can't, then we're screwed. if ! test -e "$rootdev" ; then echo "Warning! $rootdev does not exist on the dom0 host!" MAKEDEV -x `basename $rootdev` fi # Create the guest. if test -r xmdefconfig ; then cp xmdefconfig ${tmpdir}/xmdefconfig else touch ${tmpdir}/xmdefconfig fi xm create -c -f ${tmpdir}/xmdefconfig \ name=${name} \ kernel=${tmpdir}/vmlinuz-${kversion} \ nics=${nics:-1} \ memory=${memory} \ disk=file:${img},`basename $rootdev`,w \ root=$rootdev \ extra="ro selinux=0 $*" \ restart=onreboot