jw-pkg/scripts/qemu-boot.sh
Jan Lindemann 94c9cbd77f qemu-boot.sh: Add support for booting RPi
Signed-off-by: Jan Lindemann <jan@janware.com>
2017-03-25 15:10:42 +00:00

223 lines
3.9 KiB
Bash

#!/bin/bash
goodbye()
{
rm -f $tmp_files
}
usage()
{
cat << EOT >&2
$myname -h
$myname {net|<image-file>} [-b hostname]
EOT
[ "$1" ] && exit $1
exit 0
}
log()
{
echo "=========================== $*"
}
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()
{
local mac
[ "$hostname" ] && {
set -x
mac=`ldap_search -LLL "(cn=$short_hostname) dhcpHWAddress" |
sed '/dhcpHWAddress:/ !d; s/dhcpHWAddress: ethernet *//'`
macaddr=",macaddr=$mac"
}
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/$qemu_exe \
-boot n \
-net tap,ifname=tap0,vlan=0,script=$tmpdir/qemu-ifup,downscript=$tmpdir/qemu-ifdown \
-net nic,vlan=0$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,vlan=0$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`
(
cd $tmpdir
ln -s $exe qemu-ifup
ln -s $exe qemu-ifdown
)
#do_sudo /sbin/modprobe kvm-intel
xhost +
case $1 in
net)
boot_net
;;
*)
boot_image "$@"
;;
esac
rm -rf $tmpdir
}
# -- here we go
log running $0 $@
myname=`basename $0`
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=""
brctl=`PATH=/usr/sbin:/sbin /usr/bin/which brctl`
#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*)
set -x
#[ "$1" = tap1 ] || exit 0
#do_sudo /sbin/ifconfig tap1 up
#do_sudo $brctl addif in1 tap0
#do_sudo /sbin/ifconfig tap0 up
#do_sudo $brctl addif in2 tap1
#exit 0
virt_if=$1
do_sudo /sbin/ifconfig $virt_if up
# this is most certainly always priv0
do_sudo $brctl addif $bridge $virt_if
;;
*ifdown*)
# this is most certainly always priv0
do_sudo $brctl delif $bridge $1
do_sudo /sbin/ifconfig $1 down
;;
qemu.sh)
boot "$@"
;;
qemu-boot.sh) # legacy
boot "$@"
;;
*)
echo called as unknown executable name $0
;;
esac