#!/bin/sh
# QEMU/VDE network environment preparation script
RUN='/run/vde'
qemu=/opt/qemu/build/qemu-system-x86_64

case "$1" in
  start)
        echo "Starting VDE network for QEMU: "
	# Create run dir
	sudo mkdir -p $RUN && sudo chown `id -un`:`id -gn` $RUN
	# Main switch
	vde_switch -daemon -s $RUN/sw_main.sock -p $RUN/sw_main.pid
	# Proxy switch -.-
	vde_switch -daemon -s $RUN/sw_proxy.sock -p $RUN/sw_proxy.pid
	# Slirp NAT
	slirpvde --dhcp --daemon -s $RUN/sw_main.sock -p $RUN/slirp.pid
	echo "Run:\nqemu -m 512 -nic vde,mac='52:54:00:12:34:56',sock=$RUN/sw_proxy.sock -hda alpine1.qcow2 -nographic"
	$qemu -m 512 -nic vde,mac='52:54:00:12:34:66',sock=$RUN/sw_main.sock -hda alpine2.qcow2 -daemonize -vnc :1 -pidfile $RUN/vm_2.pid
	;;
  stop)
        echo "Stopping VDE network for QEMU: "
	kill `cat $RUN/*.pid | xargs`
        ;;
  restart|reload)
        $0 stop
        sleep 1
        $0 start
        ;;
  status)
	ps -fq `cat $RUN/*.pid | xargs | sed 's/ /,/g'`
	;;
  *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
exit 0
