diff --git a/scripts/qemu-boot.sh b/scripts/qemu-boot.sh new file mode 100644 index 00000000..ea3eeb85 --- /dev/null +++ b/scripts/qemu-boot.sh @@ -0,0 +1,155 @@ +#!/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 +} + + +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 + 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" ] && { + local short_hostname=`echo $hostname | sed 's/\..*//'` + 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 + + # 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,model=e1000,macaddr=$macaddr \ + "$qemu_opts" +} + +boot_image() +{ + local boot_image="$1" + + test -r "$boot_image" || { + echo "inaccessible boot image \"$boot_image\"" >&2 + exit 1 + } + + 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" \ + $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: flag; do + case $flag in + h) + usage 0;; + l) + list_hostnames + exit 0;; + b) + hostname="$OPTARG";; + x) + exe="$OPTARG";; + o) + qemu_opts="$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-netboot.sh) # legacy + boot "$@" + ;; +*) + echo called as unknown name $0 + ;; +esac +