Hello!

Recently I built a custom image which depended on a DHCP server to
configure Ethernet. runqemu with tap for networking doesn't provide one
at the moment, so ssh logins did not work.

Instead of customizing images for runqemu, I think it would be better to
adapt runqemu and uses images as on the target device. Attached two
patches which work for me. Is that of interest for inclusion upstream?

I suppose more work is needed, in particular around how to enable or
disable this feature. Patching the .bb files also leads to a (to me)
strange QA error. See the comments in the patches for details.

-- 
Best Regards, Patrick Ohly

The content of this message is my personal opinion only and although
I am an employee of Intel, the statements I make here in no way
represent Intel's position on the issue, nor am I authorized to speak
on behalf of Intel on this matter.


From 700304071404ed22c81e57c169ef50ef424b66bc Mon Sep 17 00:00:00 2001
From: Patrick Ohly <[email protected]>
Date: Mon, 3 Nov 2014 17:37:20 +0100
Subject: [PATCH 1/3] recipes-connectivity: enable dhcp-native

This depends on bind-native. It is needed for running a user-space
dhcpd for qemu guests.

TODO: modifying bind_9.9.5.bb like this triggers an error:

ERROR: QA Issue: /work/tizen/yocto/poky/meta/recipes-connectivity/bind/bind_9.9.5.bb: Variable RPROVIDES is set as not being package specific, please fix this. [pkgvarcheck]

Need to fix this, but I don't know how. I don't see what triggers the
error.

Signed-off-by: Patrick Ohly <[email protected]>
---
 meta/recipes-connectivity/bind/bind_9.9.5.bb | 1 +
 meta/recipes-connectivity/dhcp/dhcp.inc      | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/meta/recipes-connectivity/bind/bind_9.9.5.bb b/meta/recipes-connectivity/bind/bind_9.9.5.bb
index ebd38eff..bcc1601 100644
--- a/meta/recipes-connectivity/bind/bind_9.9.5.bb
+++ b/meta/recipes-connectivity/bind/bind_9.9.5.bb
@@ -88,3 +88,4 @@ CONFFILES_${PN} = " \
 	${sysconfdir}/bind/db.root \
 	"
 
+BBCLASSEXTEND = "native"
diff --git a/meta/recipes-connectivity/dhcp/dhcp.inc b/meta/recipes-connectivity/dhcp/dhcp.inc
index 4949e02..a8ddff8 100644
--- a/meta/recipes-connectivity/dhcp/dhcp.inc
+++ b/meta/recipes-connectivity/dhcp/dhcp.inc
@@ -113,3 +113,5 @@ pkg_postrm_dhcp-client() {
         echo "Not removing ${localstatedir}/lib/dhcp as it is non-empty."
     fi
 }
+
+BBCLASSEXTEND = "native"
-- 
2.1.1

From ecca5665e81b1a3e4de113b7a1af76d94d1f61ef Mon Sep 17 00:00:00 2001
From: Patrick Ohly <[email protected]>
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 <[email protected]>
---
 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 <<EOF
+option domain-name "localdomain";
+authoritative;
+
+# Basically forever...
+default-lease-time 60000;
+max-lease-time 60000;
+
+subnet 192.168.7.0 netmask 255.255.255.0 {
+  range 192.168.7.$n2 192.168.7.$n2;
+  option routers 192.168.7.$n1;
+}
+EOF
+        touch $DHCPLEASE
+        echo $DHCPD -d -cf $DHCPCONF -lf $DHCPLEASE -pf $DHCPPID $TAP
+        # TODO: where should output go?
+        $DHCPD -d -cf $DHCPCONF -lf $DHCPLEASE -pf $DHCPPID $TAP &
+        pid=$!
+        while [ ! -s $DHCPPID ]; do
+            sleep 1
+            if ! ps $pid | grep -q dhcpd; then
+                echo "$DHCPD terminated unexpectedly."
+                return 1
+            fi
+        done
+
         KERNEL_NETWORK_CMD="ip=192.168.7.$n2::192.168.7.$n1:255.255.255.0"
         QEMU_TAP_CMD="-net tap,vlan=0,ifname=$TAP,script=no,downscript=no"
         if [ "$KVM_ACTIVE" = "yes" ]; then
-- 
2.1.1

-- 
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

Reply via email to