jw-pkg/scripts/qemu-boot.sh
Jan Lindemann 15f3e12fca qemu-boot.sh: Code adapted to name change
Signed-off-by: Jan Lindemann <jan@janware.com>
2016-11-01 16:24:41 +00:00

155 lines
2.6 KiB
Bash

#!/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|<image-file>} [-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-boot.sh) # legacy
boot "$@"
;;
*)
echo called as unknown executable name $0
;;
esac