From ecca5665e81b1a3e4de113b7a1af76d94d1f61ef Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Mon, 3 Nov 2014 17:42:19 +0100 Subject: [PATCH 2/3] runqemu: automatically run DHCP server Some images are not able to configure their Ethernet based on the "ip" kernel parameter, for example Tizen. Instead of having to modify images, it is easier to provide what these images expect and have a DHCP server running on the host. At the moment, the modified scripts expect that "dhcp-native" was built for the host. runqemu-ifup sets capabilities such that the dhcpd can be run as normal user with just minimal privileges and then runqemu-internal takes care of bringing the daemon up and taking it down again. All dhcpd files are stored together with the tap lock file under /tmp. dhcpd output goes to stdout. Signed-off-by: Patrick Ohly --- scripts/runqemu-ifup | 20 ++++++++++++++++++++ scripts/runqemu-internal | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/scripts/runqemu-ifup b/scripts/runqemu-ifup index b5a3db9..0806c40 100755 --- a/scripts/runqemu-ifup +++ b/scripts/runqemu-ifup @@ -57,6 +57,14 @@ if [ ! -x "$TUNCTL" ]; then exit 1 fi +# TODO: make dhcpd optional? +DHCPD=$NATIVE_SYSROOT_DIR/usr/sbin/dhcpd +if [ ! -x "$DHCPD" ]; then + # TODO (?): integrate into qemu-helper-native + echo "Error: Unable to find dhcpd binary in '$NATIVE_SYSROOT_DIR/usr/sbin', please bitbake dhcp-native" + exit 1 +fi + TAP=`$TUNCTL -b $GROUP 2>&1` STATUS=$? if [ $STATUS -ne 0 ]; then @@ -89,6 +97,15 @@ if [ ! -x "$IPTABLES" ]; then exit 1 fi +SETCAP=`which setcap 2> /dev/null` +if [ "x$SETCAP" = "x" ]; then + SETCAP=/sbin/setcap +fi +if [ ! -x "$SETCAP" ]; then + echo "$SETCAP cannot be executed" + exit 1 +fi + n=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ] $IFCONFIG addr add 192.168.7.$n/32 broadcast 192.168.7.255 dev $TAP $IFCONFIG link set dev $TAP up @@ -103,4 +120,7 @@ echo 1 > /proc/sys/net/ipv4/ip_forward echo 1 > /proc/sys/net/ipv4/conf/$TAP/proxy_arp $IPTABLES -P FORWARD ACCEPT +# allow running dhcpd as normal user +$SETCAP cap_net_raw,cap_net_bind_service+ep $DHCPD + echo $TAP diff --git a/scripts/runqemu-internal b/scripts/runqemu-internal index 38745dd..3a9c0b4 100755 --- a/scripts/runqemu-internal +++ b/scripts/runqemu-internal @@ -172,6 +172,13 @@ else exit 1 fi + DHCPD=$OECORE_NATIVE_SYSROOT/usr/sbin/dhcpd + if [ ! -x "$DHCPD" ]; then + # TODO (?): integrate into qemu-helper-native + echo "Error: Unable to find dhcpd binary in '$NATIVE_SYSROOT_DIR/usr/sbin', please bitbake dhcp-native" + exit 1 + fi + POSSIBLE=`$IFCONFIG link | grep 'tap' | awk '{print $2}' | sed s/://` TAP="" LOCKFILE="" @@ -222,6 +229,10 @@ else fi cleanup() { + # Ensure that we don't kill an unrelated process. + if [ ! -e "$DHCPPID" ] && [ -s $DHCPPID ] && ps $(cat $DHCPPID) | grep -q dhcpd; then + kill $(cat $DHCPPID) + fi if [ ! -e "$NOSUDO_FLAG" -a "$USE_PRECONF_TAP" = "no" ]; then # Redirect stderr since we could see a LD_PRELOAD warning here if pseudo is loaded # but inactive. This looks scary but is harmless @@ -249,6 +260,38 @@ else n1=$(($n0 * 2 + 1)) n2=$(($n1 + 1)) + DHCPDIR=$LOCKFILE.dhcp + rm -rf $DHCPDIR + mkdir $DHCPDIR + DHCPCONF=$DHCPDIR/dhcpd.conf + DHCPLEASE=$DHCPDIR/dhcpd.leases + DHCPPID=$DHCPDIR/dhcpd.pid + cat >$DHCPCONF <