#!/bin/bash goodbye() { : rm -rf $tmp_files } usage() { cat << EOT >&2 $myname -h $myname {net|} [-b hostname] EOT [ "$1" ] && exit $1 exit 0 } log() { echo "$log_delim $*" /usr/bin/logger -t "$myname" "$*" } err() { echo "$log_delim $*" >&2 /usr/bin/logger -t "$myname" "$*" } fatal() { err "Fatal: $@ ... giving up" exit 1 } do_sudo() { if [ "$UID" = 0 ]; then while [[ $1 =~ ^- ]]; do shift; done log running $@ eval "$@" return $? fi log running sudo $@ #sudo -p "Password for $myname:" $@ eval sudo "$@" return $? } ldap_search() { local ldap_server=ldap.lcl local binddn=cn=ldapadm,dc=jannet,dc=de local password_files="$HOME/.feedfs-ldap/passwords/$binddn@$ldap_server $HOME/.ldap.secret" local p password_file for p in $password_files; do [ -r "$p" ] || continue password_file=$p break done [ "$password_file" ] || fatal "None of the LDAP password files $password_files exists" 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() { local mac [ "$hostname" ] && { #set -x mac=`ldap_search -LLL "(cn=$short_hostname) dhcpHWAddress" | sed '/dhcpHWAddress:/ !d; s/dhcpHWAddress: ethernet *//'` macaddr=",macaddr=$mac" } systemctl try-start dhcpd-priv.lcl systemctl try-start nfsserver [ "$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/$qemu_exe \ -boot n \ -net tap,ifname=tap0,script=$tmpdir/qemu-ifup,downscript=$tmpdir/qemu-ifdown \ -net nic$nic_model$macaddr \ "$qemu_opts" # invalid param macaddr # do_sudo -E /usr/bin/$qemu_exe \ # -boot n \ # -net bridge,br=$bridge,$nic_model,macaddr=$macaddr \ # $qemu_opts # access denied by ACL files # do_sudo -E /usr/bin/$qemu_exe \ # -boot n \ # -net bridge,br=$bridge,$nic_model \ # $qemu_opts } boot_image() { local boot_image="$1" test -r "$boot_image" || fatal "inaccessible boot image \"$boot_image\"" #-net bridge,br=$bridge \ #-net nic,vlan=1$nic_model,macaddr=00:02:36:22:13:6c \ #-net tap,ifname=tap1,vlan=1,script=$tmpdir/qemu-ifup,downscript=$tmpdir/qemu-ifdown \ #set -x do_sudo -E /usr/bin/$qemu_exe \ -net nic,$nic_model$macaddr \ -net tap,ifname=tap0,vlan=0,script=$tmpdir/qemu-ifup,downscript=$tmpdir/qemu-ifdown \ "$qemu_opts" \ -drive file=$boot_image } boot() { tmpdir=`mktemp -d /tmp/$myname-XXXXXX` ln -s $exe $tmpdir/qemu-ifup ln -s $exe $tmpdir/qemu-ifdown #do_sudo /sbin/modprobe kvm-intel tmp_files="$tmp_files $tmpdir" xhost + case $1 in net) shift boot_net "$@" ;; *) boot_image "$@" ;; esac } # -- here we go myname=`basename $0` log_delim="===========================" log running $0 $@ [ -x $0 ] || chmod u+x $0 || fatal "$0 is not executable" exe=`readlink -f $0` dirname=`dirname $exe` bridge=priv0 #bridge=in1 macaddr=,macaddr=00:0B:DC:9B:D6:DA base=dc=priv,dc=lcl qemu_exe=qemu-kvm qemu_opts="" ip=`PATH=/usr/sbin:/sbin:/usr/bin /usr/bin/which ip` #nic_model=",model=rtl8139" tmp_files="" tmpdir="" trap goodbye INT QUIT EXIT KILL TERM PIPE OPTIND=1 while getopts lhb:o:x:n:B:M: 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) qemu_exe="$OPTARG";; o) qemu_opts="$OPTARG";; n) nic_model=",model=$OPTARG";; B) bridge=$OPTARG;; M) macaddr=",macaddr=$OPTARG";; *) usage 1;; esac done shift $(($OPTIND - 1)) case $myname in *ifup*) virt_if=$1 touch /tmp/i-ran-$myname do_sudo $ip link set $virt_if up do_sudo $ip link set dev $virt_if master $bridge ;; *ifdown*) virt_if=$1 do_sudo $ip link set dev $virt_if nomaster do_sudo $ip link set dev $virt_if down ;; qemu.sh) boot "$@" ;; qemu-boot.sh) # legacy boot "$@" ;; *) echo called as unknown executable name $0 ;; esac