#/bin/bash
#Christian Leber <christian@leber.de> | ijuz on IRC | 2005
#do what you want with it, but don't make me responsible when you screw up your box :-))

echo edit the variables at the beginning first
echo you need debootstrap
exit 1

directory=/xen
imgname=debian_00
debmirror=http://ftp.de.debian.org/debian
#sizes in 1k blocks
imgsize=500k
swapsize=64K

gatewayip=192.168.3.3
nodeip=192.168.3.21
nameserverip=192.168.3.3

xenUkernel=/boot/vmlinuz-2.6.11-xenU

################# most likely you don't have to change anything after this ####

mountpt=$directory/$imgname.mount
echo the imagename is: $imgname
imgname_img=$directory/$imgname.img
imgname_swp=$directory/$imgname.swp.img

dd if=/dev/zero of=$imgname_img bs=1k seek=$imgsize count=1
dd if=/dev/zero of=$imgname_swp bs=1k seek=$swapsize count=1
mkswap $imgname_swp
mkfs.ext3 -F $imgname_img

mkdir $imgname.mount
mount -o loop $imgname_img $mountpt
debootstrap --arch i386 sarge $mountpt $debmirror

# config the system
echo $imgname > $imgname.mount/etc/hostname
echo deb $debmirror sarge main > $mountpt/etc/apt/sources.list
echo deb-src $debmirror sarge main >> $mountpt/etc/apt/sources.list
echo deb http://security.debian.org/ sarge/updates main >> $mountpt/etc/apt/sources.list

cat > $mountpt/etc/fstab << "EOF"
/dev/hda1          /             ext3      defaults,errors=remount-ro    0     0 
/dev/hda2          swap          swap      sw                            0     0 
proc               /proc         proc      defaults                      0     0 
EOF

cat > $mountpt/etc/network/interfaces << "EOF"
# Begin /etc/network/interfaces
 
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
   address NODEIP
   netmask 255.255.255.0
   gateway GATEWAYIP
 
# End /etc/network/interfaces
EOF

sed -e "s/GATEWAYIP/$gatewayip/g" $mountpt/etc/network/interfaces > $mountpt/etc/network/interfaces.tmp
mv $mountpt/etc/network/interfaces.tmp $mountpt/etc/network/interfaces
sed -e "s/NODEIP/$nodeip/g" $mountpt/etc/network/interfaces > $mountpt/etc/network/interfaces.tmp
mv $mountpt/etc/network/interfaces.tmp $mountpt/etc/network/interfaces

echo "nameserver $nameserverip" > $mountpt/etc/resolv.conf

#rename /lib/tls
mv $mountpt/lib/tls $mountpt/lib/tls.disabled

umount $mountpt
rmdir $mountpt


#now the xm config
xmfile=$imgname.xmconf

echo kernel = \"$xenUkernel\" >$xmfile
echo memory = 64 >>$xmfile
echo name = \"$imgname\" >>$xmfile
echo disk = [ \'file:$imgname_img,hda1,w\' , \'file:$imgname_swp,hda2,w\' ] >>$xmfile
echo root = \"/dev/hda1 ro\" >> $xmfile
echo
echo
echo now start your domain with: xm create $directory/$xmfile


