tech-userlevel archive

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

dhcpcd glue for rc.d/network and sysinst



Hi all,
the attached patch implements two functions:
(1) It makes ifconfig_bge0=dhcp start/stop dhcpcd, same for single lines
in /etc/ifconfig_bge0.  I'm not sure where this should be documented.
Timeouts etc can be set via dhcpcd_flags.

(2) It adds the glue to use dhcpcd on the initial ramdisk. This is very
bare-bone, e.g. it doesn't set the hostname (no /bin/hostname :-)).
This needs changes for all architectures that bundle dhclient, but it
should be straight to convert the rest.

Comments?

Joerg
Index: distrib/amd64/ramdisks/common/Makefile.ramdisk
===================================================================
RCS file: /data/repo/netbsd/src/distrib/amd64/ramdisks/common/Makefile.ramdisk,v
retrieving revision 1.3
diff -u -p -r1.3 Makefile.ramdisk
--- distrib/amd64/ramdisks/common/Makefile.ramdisk      1 Feb 2008 13:43:12 
-0000       1.3
+++ distrib/amd64/ramdisks/common/Makefile.ramdisk      26 May 2008 03:59:33 
-0000
@@ -39,6 +39,7 @@ HACKSRC=      ${DISTRIBDIR}/utils/libhack
 ${CRUNCHBIN}:  libhack.o
 
 .include "${DISTRIBDIR}/common/Makefile.crunch"
+.include "${DISTRIBDIR}/common/Makefile.dhcpcd"
 .include "${DISTRIBDIR}/common/Makefile.makedev"
 .include "${DISTRIBDIR}/common/Makefile.image"
 
Index: distrib/amd64/ramdisks/ramdisk/Makefile
===================================================================
RCS file: /data/repo/netbsd/src/distrib/amd64/ramdisks/ramdisk/Makefile,v
retrieving revision 1.1
diff -u -p -r1.1 Makefile
--- distrib/amd64/ramdisks/ramdisk/Makefile     2 Jun 2006 22:11:52 -0000       
1.1
+++ distrib/amd64/ramdisks/ramdisk/Makefile     26 May 2008 03:59:46 -0000
@@ -2,8 +2,7 @@
 
 IMAGE=         ramdisk.fs
 IMAGESIZE=     5000k
-IMAGEDEPENDS=  disktab.preinstall \
-               ${NETBSDSRCDIR}/usr.sbin/dhcp/clientscript/dhclient-script
+IMAGEDEPENDS=  disktab.preinstall
 USE_SYSINST=   yes
 SMALLPROG_INET6=1
 
Index: distrib/amd64/ramdisks/ramdisk/list
===================================================================
RCS file: /data/repo/netbsd/src/distrib/amd64/ramdisks/ramdisk/list,v
retrieving revision 1.6
diff -u -p -r1.6 list
--- distrib/amd64/ramdisks/ramdisk/list 6 May 2008 15:32:06 -0000       1.6
+++ distrib/amd64/ramdisks/ramdisk/list 26 May 2008 04:00:01 -0000
@@ -24,7 +24,6 @@ PROG  bin/sync
 
 PROG   libexec/lfs_cleanerd
 
-PROG   sbin/dhclient
 PROG   sbin/disklabel
 PROG   sbin/fdisk
 PROG   sbin/fsck
@@ -65,15 +64,11 @@ PROG        usr/sbin/chown  usr/bin/chgrp
 PROG   usr/sbin/chroot
 PROG   usr/sbin/wiconfig
 
-SPECIAL        dhclient        srcdir  distrib/utils/x_dhclient
 SPECIAL        ifconfig        srcdir  distrib/utils/x_ifconfig
 SPECIAL        ping            srcdir  distrib/utils/x_ping
 SPECIAL        route           srcdir  distrib/utils/x_route
 
 LIBS   libhack.o -lbz2 -ledit -lutil -lcurses -ltermcap -lrmt -lcrypt -ll -lm 
-lz -lprop
 
-# various files that we need in /etc for the install
-COPY   ${NETBSDSRCDIR}/usr.sbin/dhcp/clientscript/dhclient-script 
sbin/dhclient-script 555
-
 # the disktab explanation file
 COPY   ${CURDIR}/disktab.preinstall            etc/disktab.preinstall
Index: distrib/common/10-resolv.conf
===================================================================
RCS file: distrib/common/10-resolv.conf
diff -N distrib/common/10-resolv.conf
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ distrib/common/10-resolv.conf       26 May 2008 16:11:50 -0000
@@ -0,0 +1,24 @@
+# Minimal version using shell builtins.
+
+make_resolv_conf()
+{
+       if [ -z "${new_domain_name_servers}" -a \
+               -z "${new_domain_name}" -a \
+               -z "${new_domain_search}" ]; then
+               return 0
+       fi
+       local o=/etc/resolv.conf
+       echo "${signature}" > $o
+       if [ -n "${new_domain_search}" ]; then
+               echo "search ${new_domain_search}" >> $o
+       elif [ -n "${new_domain_name}" ]; then
+               echo "search ${new_domain_name}" >> $o
+       fi
+       for x in ${new_domain_name_servers}; do
+               echo "nameserver ${x}" >> $o
+       done
+}
+
+case "${reason}" in
+BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)      make_resolv_conf;;
+esac
Index: distrib/common/99-print-sysinst
===================================================================
RCS file: distrib/common/99-print-sysinst
diff -N distrib/common/99-print-sysinst
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ distrib/common/99-print-sysinst     26 May 2008 16:18:05 -0000
@@ -0,0 +1,11 @@
+# $NetBSD$
+
+case "${reason}" in
+BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)
+       cat > /tmp/dhcpcd-lease << EOF
+host-name=${new_host_name}
+domain-name=${new_domain_name}
+EOF
+       set > /tmp/dhcpcd-lease-all
+       ;;
+esac
Index: distrib/common/Makefile.dhcpcd
===================================================================
RCS file: distrib/common/Makefile.dhcpcd
diff -N distrib/common/Makefile.dhcpcd
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ distrib/common/Makefile.dhcpcd      26 May 2008 13:21:00 -0000
@@ -0,0 +1,9 @@
+# $NetBSD$
+
+IMAGEDEPENDS+=         \
+       ${DESTDIR}/etc/dhcpcd.conf \
+       ${DESTDIR}/libexec/dhcpcd-run-hooks \
+       ${DESTDIR}/libexec/dhcpcd-hooks/10-resolv.conf \
+       ${DESTDIR}/libexec/dhcpcd-hooks/15-hostname
+
+LISTS+=                ${DISTRIBDIR}/common/list.dhcpcd
Index: distrib/common/list.dhcpcd
===================================================================
RCS file: distrib/common/list.dhcpcd
diff -N distrib/common/list.dhcpcd
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ distrib/common/list.dhcpcd  26 May 2008 16:18:59 -0000
@@ -0,0 +1,12 @@
+#      $NetBSD$
+#
+# list file (c.f. parselist.awk) for DHCP-enabled install media.
+#
+
+PROG   sbin/dhcpcd
+
+COPY   ${DESTDIR}/etc/dhcpcd.conf etc/dhcpcd.conf
+COPY   ${DESTDIR}/libexec/dhcpcd-run-hooks libexec/dhcpcd-run-hooks 555
+
+COPY   ${NETBSDSRCDIR}/distrib/common/10-resolv.conf 
libexec/dhcpcd-hooks/10-resolv.conf
+COPY   ${NETBSDSRCDIR}/distrib/common/99-print-sysinst 
libexec/dhcpcd-hooks/99-print-sysinst
Index: distrib/common/mtree.common
===================================================================
RCS file: /data/repo/netbsd/src/distrib/common/mtree.common,v
retrieving revision 1.8
diff -u -p -r1.8 mtree.common
--- distrib/common/mtree.common 24 Aug 2007 17:11:58 -0000      1.8
+++ distrib/common/mtree.common 26 May 2008 13:33:10 -0000
@@ -7,6 +7,7 @@
 ./dev
 ./etc
 ./libexec
+./libexec/dhcpcd-hooks
 ./mnt
 ./mnt2
 ./kern
Index: distrib/utils/sysinst/net.c
===================================================================
RCS file: /data/repo/netbsd/src/distrib/utils/sysinst/net.c,v
retrieving revision 1.117
diff -u -p -r1.117 net.c
--- distrib/utils/sysinst/net.c 29 Mar 2008 15:19:53 -0000      1.117
+++ distrib/utils/sysinst/net.c 26 May 2008 15:36:14 -0000
@@ -38,6 +38,7 @@
 
 /* net.c -- routines to fetch files off the network. */
 
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -99,7 +100,7 @@ static char *url_encode (char *dst, cons
 
 static void write_etc_hosts(FILE *f);
 
-#define DHCLIENT_EX "/sbin/dhclient"
+#define DHCPCD "/sbin/dhcpcd"
 #include <signal.h>
 static int config_dhcp(char *);
 static void get_dhcp_value(char *, size_t, const char *);
@@ -1091,8 +1092,7 @@ mnt_net_config(void)
 
                add_rc_conf("defaultroute=\"%s\"\n", net_defroute);
        } else {
-               add_rc_conf("dhclient=YES\n");
-               add_rc_conf("dhclient_flags=\"%s\"\n", net_dev);
+               add_rc_conf("ifconfig_%s=dhcp\n", net_dev);
         }
 
 #ifdef INET6
@@ -1117,73 +1117,55 @@ int
 config_dhcp(char *inter)
 {
        int dhcpautoconf;
-       int result;
-       char *textbuf;
-       int pid;
 
-       /* check if dhclient is running, if so, kill it */
-       result = collect(T_FILE, &textbuf, "/tmp/dhclient.pid");
-       if (result >= 0) {
-               pid = atoi(textbuf);
-               if (pid > 0) {
-                       kill(pid, 15);
-                       sleep(1);
-                       kill(pid, 9);
-               }
-       }
-       free(textbuf);
+       /*
+        * Don't bother checking for an existing instance of dhcpcd, just
+        * ask it to renew the lease.  It will fork and daemonize if there
+        * wasn't already an instance.
+        */
 
-       if (!file_mode_match(DHCLIENT_EX, S_IFREG))
+       if (!file_mode_match(DHCPCD, S_IFREG))
                return 0;
        process_menu(MENU_yesno, deconst(MSG_Perform_DHCP_autoconfiguration));
        if (yesno) {
-               /* spawn off dhclient and wait for parent to exit */
+               /* spawn off dhcpcd and wait for parent to exit */
                dhcpautoconf = run_program(RUN_DISPLAY | RUN_PROGRESS,
-                   "%s -q -pf /tmp/dhclnt.pid -lf /tmp/dhclient.leases %s",
-                   DHCLIENT_EX, inter);
+                   "%s -d -n %s", DHCPCD, inter);
                return dhcpautoconf ? 0 : 1;
        }
        return 0;
 }
 
 static void
-get_dhcp_value(char *targ, size_t l, const char *line)
+get_dhcp_value(char *targ, size_t l, const char *var)
 {
-       int textsize;
-       char *textbuf;
-       char *t;
-       char *walkp;
-
-       textsize = collect(T_FILE, &textbuf, "/tmp/dhclient.leases");
-       if (textsize < 0) {
-               if (logging)
-                       (void)fprintf(logfp,
-                           "Could not open file /tmp/dhclient.leases.\n");
-               (void)fprintf(stderr, "Could not open /tmp/dhclient.leases\n");
-               /* not fatal, just assume value not found */
+       static const char *lease_data = "/tmp/dhcpcd-lease";
+       FILE *fp;
+       char *line;
+       size_t len, var_len;
+
+       if ((fp = fopen(lease_data, "r")) == NULL) {
+               warn("Could not open %s", lease_data);
+               *targ = '\0';
+               return;
        }
-       if (textsize >= 0) {
-               (void)strtok(textbuf, " \t\n"); /* jump past 'lease' */
-               while ((t = strtok(NULL, " \t\n")) != NULL) {
-                       if (strcmp(t, line) == 0) {
-                               t = strtok(NULL, " \t\n");
-                               /* found the tag, extract the value */
-                               /* last char should be a ';' */
-                               walkp = strrchr(t, ';');
-                               if (walkp != NULL) {
-                                       *walkp = '\0';
-                               }
-                               /* strip any " from the string */
-                               walkp = strrchr(t, '"');
-                               if (walkp != NULL) {
-                                       *walkp = '\0';
-                                       t++;
-                               }
-                               strlcpy(targ, t, l);
-                               break;
-                       }
-               }
+
+       var_len = strlen(var);
+
+       while ((line = fgetln(fp, &len)) != NULL) {
+               if (line[len - 1] == '\n')
+                       --len;
+               if (len <= var_len)
+                       continue;
+               if (memcmp(line, var, var_len))
+                       continue;
+               if (line[var_len] != '=')
+                       continue;
+               line += var_len + 1;
+               len -= var_len + 1;
+               strlcpy(targ, line, l > len ? len + 1: l);
+               break;
        }
-       free(textbuf);
-       return;
+
+       fclose(fp);
 }
Index: etc/defaults/rc.conf
===================================================================
RCS file: /data/repo/netbsd/src/etc/defaults/rc.conf,v
retrieving revision 1.92
diff -u -p -r1.92 rc.conf
--- etc/defaults/rc.conf        15 Apr 2008 11:17:47 -0000      1.92
+++ etc/defaults/rc.conf        26 May 2008 20:11:36 -0000
@@ -152,6 +152,7 @@ racoon=NO                                   # IKE daemon
 auto_ifconfig=YES                              # config all avail. interfaces
 net_interfaces=""                              # used only if above is NO
 flushroutes=YES                                        # flush routes in 
netstart
+dhcpcd_flags=""                                        # For ifconfig_XXX=dhcp.
 dhclient=NO                                    # behave as a DHCP client
                        dhclient_flags=""       # blank: config all interfaces
 ntpdate=NO             ntpdate_flags="-b -s"   # May need '-u' thru firewall
Index: etc/rc.d/network
===================================================================
RCS file: /data/repo/netbsd/src/etc/rc.d/network,v
retrieving revision 1.51
diff -u -p -r1.51 network
--- etc/rc.d/network    10 Feb 2007 19:40:58 -0000      1.51
+++ etc/rc.d/network    26 May 2008 20:12:23 -0000
@@ -200,7 +200,10 @@ network_start()
                                        ipf -y >/dev/null
                                fi
                        fi
-                       if [ -n "$args" ]; then
+                       if [ "$args" = "dhcp" ]; then
+                               echo -n " $int"
+                               dhcpcd -n ${dhcpcd_flags} $int
+                       elif [ -n "$args" ]; then
                                echo -n " $int"
                                ifconfig $int $args
                        elif [ -f /etc/ifconfig.$int ]; then
@@ -213,6 +216,10 @@ network_start()
                                        "!"*)
                                                eval ${args#*!}
                                                ;;
+                                       dhcp)
+                                               dhcpcd -n ${dhcpcd_flags} \
+                                                   $int
+                                               ;;
                                        *)
                                                eval ifconfig $int $args
                                                ;;
@@ -385,6 +392,7 @@ network_stop()
                        eval args=\$ifconfig_$int
                        if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
                                echo -n " $int"
+                               dhcpcd -k $int 2> /dev/null
                                ifconfig $int down
                                if ifconfig $int destroy 2>/dev/null && \
                                   checkyesno ipfilter; then


Home | Main Index | Thread Index | Old Index