[PATCH 1/2] kvm tools: Use kernel dhcp for network autoconfiguration

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch removes the manual/usermode dhcp client configuration and instead
uses the DHCP client built within the kernel.

Since this client is tightly integrated with NFS (if NFS config is set), we
will add a specific NFS root addr in our DHCP offer to point it to a non
existent address so that we won't hang trying to poke it for our root.

Signed-off-by: Sasha Levin <levinsasha928@xxxxxxxxx>
---
 tools/kvm/builtin-run.c     |    2 +-
 tools/kvm/guest/init.c      |    4 ----
 tools/kvm/guest/setnet.sh   |   22 ----------------------
 tools/kvm/include/kvm/uip.h |    2 ++
 tools/kvm/net/uip/dhcp.c    |    8 ++++++++
 5 files changed, 11 insertions(+), 27 deletions(-)
 delete mode 100755 tools/kvm/guest/setnet.sh

diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c
index 591fd77..465bbe7 100644
--- a/tools/kvm/builtin-run.c
+++ b/tools/kvm/builtin-run.c
@@ -764,7 +764,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	if (using_rootfs) {
 		strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
 		if (custom_rootfs)
-			strcat(real_cmdline, " init=/virt/init");
+			strcat(real_cmdline, " init=/virt/init ip=dhcp");
 	} else if (!strstr(real_cmdline, "root=")) {
 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
 	}
diff --git a/tools/kvm/guest/init.c b/tools/kvm/guest/init.c
index 7733026..837acfb 100644
--- a/tools/kvm/guest/init.c
+++ b/tools/kvm/guest/init.c
@@ -30,10 +30,6 @@ int main(int argc, char *argv[])
 
 	do_mounts();
 
-	puts("Setting up network...");
-
-	system("/bin/sh virt/setnet.sh");
-
 	puts("Starting '/bin/sh'...");
 
 	run_process("/bin/sh");
diff --git a/tools/kvm/guest/setnet.sh b/tools/kvm/guest/setnet.sh
deleted file mode 100755
index 3da9c22..0000000
--- a/tools/kvm/guest/setnet.sh
+++ /dev/null
@@ -1,22 +0,0 @@
-for f in /sys/class/net/*; do
-	type=`cat $f/type`
-	if [ $type -eq 1 ]; then
-		f=${f#/sys/class/net/}
-
-		eval "dhcpcd -A $f 2> /dev/null"
-		if [ $? -eq 0 ]; then
-			exit
-		fi
-
-		eval "dhclient $f 2> /dev/null"
-		if [ $? -eq 0 ]; then
-			exit
-		fi
-
-		ifconfig $f 192.168.33.15
-		route add default 192.168.33.1
-		echo "nameserver 8.8.8.8" >> /etc/resolv.conf
-
-		exit
-	fi
-done
diff --git a/tools/kvm/include/kvm/uip.h b/tools/kvm/include/kvm/uip.h
index 344ec09..3501d36 100644
--- a/tools/kvm/include/kvm/uip.h
+++ b/tools/kvm/include/kvm/uip.h
@@ -58,6 +58,8 @@
 #define UIP_DHCP_TAG_SUBMASK_LEN	4
 #define UIP_DHCP_TAG_ROUTER		3
 #define UIP_DHCP_TAG_ROUTER_LEN		4
+#define UIP_DHCP_TAG_ROOT		17
+#define UIP_DHCP_TAG_ROOT_LEN		4
 #define UIP_DHCP_TAG_DNS_SERVER		6
 #define UIP_DHCP_TAG_DNS_SERVER_LEN	4
 #define UIP_DHCP_TAG_DOMAIN_NAME	15
diff --git a/tools/kvm/net/uip/dhcp.c b/tools/kvm/net/uip/dhcp.c
index bd3c53b..e91a7c7 100644
--- a/tools/kvm/net/uip/dhcp.c
+++ b/tools/kvm/net/uip/dhcp.c
@@ -2,6 +2,8 @@
 
 #include <arpa/inet.h>
 
+#define EMPTY_ADDR "0.0.0.0"
+
 static inline bool uip_dhcp_is_discovery(struct uip_dhcp *dhcp)
 {
 	return (dhcp->option[2] == UIP_DHCP_DISCOVER &&
@@ -127,6 +129,12 @@ static int uip_dhcp_fill_option(struct uip_info *info, struct uip_dhcp *dhcp, in
 	*addr		= htonl(info->host_ip);
 	i		+= UIP_DHCP_TAG_ROUTER_LEN;
 
+	opt[i++]	= UIP_DHCP_TAG_ROOT;
+	opt[i++]	= strlen(EMPTY_ADDR);
+	addr		= (u32 *)&opt[i];
+	strncpy((void *) addr, EMPTY_ADDR, strlen(EMPTY_ADDR));
+	i		+= strlen(EMPTY_ADDR);
+
 	i 		= uip_dhcp_fill_option_name_and_server(info, opt, i);
 
 	opt[i++]	= UIP_DHCP_TAG_END;
-- 
1.7.6.1

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux