From 7eeef124e30ca2123f2c5623005e9ef28e6a4df8 Mon Sep 17 00:00:00 2001 From: Sasha Levin Date: Wed, 14 Sep 2011 10:11:09 +0300 Subject: [PATCH] kvm tools: Use kernel dhcp for network autoconfiguration 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. Acked-by: Asias He Signed-off-by: Sasha Levin Signed-off-by: Pekka Enberg --- 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 6234b65b1699..741548f72392 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 773302602534..837acfb18c28 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 3da9c22ba9b0..000000000000 --- 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 344ec0983836..3501d36106b5 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 bd3c53ba1416..e91a7c7a6aca 100644 --- a/tools/kvm/net/uip/dhcp.c +++ b/tools/kvm/net/uip/dhcp.c @@ -2,6 +2,8 @@ #include +#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; -- 2.39.5