#!/bin/sh -x myname=`basename $0` dirname=`dirname $0` macaddr=00:0B:DC:9B:D6:DA base=dc=priv,dc=lcl exe=qemu-kvm qemu_opts="" brctl=`PATH=/usr/sbin:/sbin /usr/bin/which brctl` #nic_model=",model=rtl8139" usage() { cat << EOT >&2 $myname -h $myname {net|} [-b hostname] EOT [ "$1" ] && exit $1 exit 0 } err() { echo $* >&2 } fatal() { err $@ exit 1 } do_sudo() { if [ "$UID" = 0 ]; then while [[ $1 =~ ^- ]]; do shift; done eval "$@" return $? else echo running sudo $@ #sudo -p "Password for $myname:" $@ eval sudo "$@" return $? fi } ldap_search() { local ldap_server=ldap.lcl local binddn=cn=ldapadm,dc=jannet,dc=de local password_file=$HOME/.feedfs-ldap/passwords/$binddn@$ldap_server [ -r "$password_file" ] || fatal "Password file $password_file doesn't exist" ldapsearch -y $password_file -xZ -h $ldap_server -D $binddn -b $base,ou=networks,dc=jannet,dc=de $@ } list_hostnames() { ldap_search -LLL "(objectClass=dhcpHost)" cn | sed '/cn: / !d; s/cn: //' } boot_net() { [ "$hostname" ] && { set -x macaddr=`ldap_search -LLL "(cn=$short_hostname) dhcpHWAddress" | sed '/dhcpHWAddress:/ !d; s/dhcpHWAddress: ethernet *//'` } xhost + do_sudo /sbin/modprobe kvm-intel ps aux | grep -v grep | grep -q dhcpd || /etc/init.d/dhcpd start ps aux | grep -v grep | grep -q nfsd || /etc/init.d/nfsserver start [ "$nic_model" ] || nic_model=",model=e1000" # need this for net access, would be nicer if it didn't run as root # -nographic do_sudo -E /usr/bin/$exe \ -boot n \ -net tap,ifname=tap0,vlan=0,script=$tmpdir/qemu-ifup,downscript=$tmpdir/qemu-ifdown \ -net nic,vlan=0$nic_model,macaddr=$macaddr \ "$qemu_opts" } boot_image() { local boot_image="$1" test -r "$boot_image" || fatal "inaccessible boot image \"$boot_image\"" xhost + do_sudo -E /usr/bin/$exe \ -net tap,ifname=tap0,vlan=0,script=$tmpdir/qemu-ifup,downscript=$tmpdir/qemu-ifdown \ -net nic,vlan=0$nic_model,macaddr=$macaddr \ "$qemu_opts" \ -drive format=raw,file=$boot_image } boot() { local tmpdir=`mktemp -d /tmp/$myname""_XXXXXX` ( cd $tmpdir ln -s $0 qemu-ifup ln -s $0 qemu-ifdown ) case $1 in net) boot_net ;; *) boot_image "$@" ;; esac rm -rf $tmpdir } # -- here we go OPTIND=1 while getopts lhb:o:x:n: flag; do case $flag in h) usage 0;; l) list_hostnames exit 0;; b) hostname="$OPTARG" short_hostname=`echo $hostname | sed 's/\..*//'` dns_domain=`echo $hostname | cut -d. -f2-` base=`echo $dns_domain | sed 's/\./,/g; s/\(^\|,\)/\1dc=/g'` ;; x) exe="$OPTARG";; o) qemu_opts="$OPTARG";; n) nic_model=",model=$OPTARG";; *) usage 1;; esac done shift $(($OPTIND - 1)) case $myname in *ifup*) do_sudo /sbin/ifconfig $1 up do_sudo $brctl addif priv0 $1 ;; *ifdown*) do_sudo $brctl delif priv0 $1 do_sudo /sbin/ifconfig $1 down ;; qemu.sh) boot "$@" ;; qemu-boot.sh) # legacy boot "$@" ;; *) echo called as unknown executable name $0 ;; esac