#!/bin/bash

if [ -z $1 ] || [ $1 == -h ]; then
echo
echo "Install VM:              vm.sh -i <vm-Nr.> <Img-Size>"
echo "Start VM:                vm.sh    <vm-Nr.> <CPU-Cores> <RAM in GB>"
echo "Start VM with Snapshot:  vm.sh -s <vm-Nr.> <CPU-Cores> <RAM in GB>"
echo
exit 0
fi

if [ -z `mount|grep ramdisk|awk '{print $1}'` ]; then
   sudo mount -t ramfs ramfs /media/ramdisk
   sudo chown ms /media/ramdisk/
fi

cd /media/ramdisk/

if [ $1 == "-i" ]; then
   echo "install vm5$2 ..."
   qemu-img create -f qcow2 vm5$2.img $3G

   status=`sudo lxc-info -s deb-inst|awk '{print $2}'`
      if [ $status == STOPPED ]; then
         sudo /usr/bin/lxc-start deb-inst
      fi
   echo "Install Server ... RUNNING"

   if [ ! -f linux ]; then 
	wget http://ftp.de.debian.org/debian/dists/stable/main/installer-amd64/current/images/netboot/debian-installer/amd64/linux
   fi
   if [ ! -f initrd.gz ]; then
	wget http://ftp.de.debian.org/debian/dists/stable/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz
   fi

echo
echo "sudo qemu-system-x86_64 -m 2048 -cpu host -machine type=pc,accel=kvm -hda /media/ramdisk/vm5$2.img -display none -serial telnet:localhost:995$2,server,nowait -k de -kernel linux -append "console=ttyS0 language=en country=DE locale=en_US.UTF-8 debian-installer/keymap=de keymap=de hostname=vm5$2 domain="" mirror/country=Germany mirror/http/hostname=ftp.de.debian.org mirror/http/proxy="" mirror/http/directory=/debian url=http://192.168.1.85/preseed_vm5$2.cfg" -initrd initrd.gz gz -monitor telnet:127.0.0.1:9912,server,nowait"

echo
echo "Monitor on -> telnet localhost 9912"
echo "Serial Console on -> telnet localhost 995$2"
echo

sudo qemu-system-x86_64 -m 2048 -cpu host -machine type=pc,accel=kvm -hda /media/ramdisk/vm5$2.img -display none -serial telnet:localhost:995$2,server,nowait -k de -kernel linux -append "console=ttyS0 language=en country=DE locale=en_US.UTF-8 debian-installer/keymap=de keymap=de hostname=vm5$2 domain="" mirror/country=Germany mirror/http/hostname=ftp.de.debian.org mirror/http/proxy="" mirror/http/directory=/debian url=http://192.168.1.85/preseed_vm5$2.cfg" -initrd initrd.gz -monitor telnet:127.0.0.1:9912,server,nowait

sudo qemu-system-x86_64 -m 4096 -cpu host -smp 2 -machine type=pc,accel=kvm -hda /media/ramdisk/vm5$2.img -display none -k de -netdev tap,id=net5$2,ifname=tap5$2,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown -device virtio-net-pci,netdev=net5$2,mac=52:54:00:12:34:5$2 -serial telnet:localhost:995$2,server,nowait -boot c &

echo "vm5$2 started with 2 CPU-Cores and 4096 MB RAM"
echo
sudo /usr/bin/lxc-stop deb-inst
else
   if [ $1 == "-s" ]; then
let mem=$4*1024
sudo qemu-system-x86_64 -m $mem -cpu host -smp $3 -machine type=pc,accel=kvm -hda /media/ramdisk/vm5$2.img -snapshot -display none -k de -netdev tap,id=net5$2,ifname=tap5$2,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown -device virtio-net-pci,netdev=net5$2,mac=52:54:00:12:34:5$2 -boot c &
echo "vm5$2 started with Snapshot"
echo
  else
let mem=$3*1024
sudo qemu-system-x86_64 -m $mem -cpu host -smp $2 -machine type=pc,accel=kvm -hda /media/ramdisk/vm5$1.img -display none -k de -netdev tap,id=net5$1,ifname=tap5$1,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown -device virtio-net-pci,netdev=net5$1,mac=52:54:00:12:34:5$1 -boot c &
echo "vm5$1 started"
  fi
fi