6. Ultimate Qemu script with VIRTIO

#!/usr/bin/ksh

root=$(cd $(dirname $(readlink -f $0))/..; pwd)

wakes=/windows/perso/wakes

mem=512

vm_name=$(basename $0 .sh)

img=$wakes/vmdisk/$vm_name.img

pidfile=$wakes/vmdisk/$vm_name.pid

qemu=/usr/kvm/bin/qemu-system-x86_64

cdrom=$wakes/iso/wakes-full-alpha1-???-i386.iso

mac=$(print $vm_name | md5sum | \
  sed 's/^\(..\)\(..\)\(..\)\(..\).*$/52:54:\1:\2:\3:\4/')

[ ! -f "$img" ] && qemu-img create $img 10G

case $1 in
  install)
    $qemu -name $vm_name -boot once=d -hda $img -cdrom $cdrom
  ;;
  start)
    $qemu -name $vm_name -boot c -hda $img -pidfile $pidfile -cdrom $cdrom \
      -net nic,vlan=0,macaddr=$mac -m $mem -vga vmware -k fr \
      -net tap,vlan=0,ifname=$vm_name,script=/etc/qemu-ifup 
  ;;
  stop)
    kill $(cat $pidfile)
    rm $pidfile
  ;;
  ssh)
    xterm -T $vm_name -bg black -fg green -e \
      ssh -Y -o StrictHostKeyChecking=no $vm_name 2> /dev/null &
  ;;
  ncurses_start)
    xterm -geometry 80x25 -title "$vm_name console" -e \
      $qemu -name $vm_name -boot c \
      -drive file=$img,if=virtio,index=0,media=disk,boot=on \
      -pidfile $pidfile -cdrom $cdrom \
      -net nic,vlan=0,macaddr=$mac,model=virtio -m $mem -vga vmware -curses \
      -net tap,vlan=0,ifname=$vm_name,script=/etc/qemu-ifup 2> /dev/null &
  ;;
  failsafe)
    xterm -geometry 80x25 -title "$vm_name console" -e \
      $qemu -name $vm_name -boot c \
      -drive file=$img,if=virtio,index=0,media=disk,boot=on \
      -pidfile $pidfile -cdrom $cdrom \
      -kernel /boot/vmlinuz-failsafe -initrd /boot/initrd-failsafe \
      -append "root=/dev/mapper/rootvg-lvol0" \
      -net nic,vlan=0,macaddr=$mac,model=virtio -m $mem -vga vmware -curses \
      -net tap,vlan=0,ifname=$vm_name,script=/etc/qemu-ifup 2> /dev/null &
  ;;
  *)
    print -u2 "usage: $0 install|start|stop|ncurses_start|ssh"
    exit 1
  ;;
esac

exit 0