From matt@mail.forko.com  Mon Oct 20 04:14:58 2003
Return-Path: <matt@mail.forko.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 566E316A4C0
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 20 Oct 2003 04:14:58 -0700 (PDT)
Received: from mail.forko.com (forko.com [206.14.189.156])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 50B1843F3F
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 20 Oct 2003 04:14:57 -0700 (PDT)
	(envelope-from matt@mail.forko.com)
Received: (qmail 69372 invoked by uid 1000); 20 Oct 2003 04:10:02 -0700
Message-Id: <20031020111002.69371.qmail@mail.forko.com>
Date: 20 Oct 2003 04:10:02 -0700
From: Matt Peterson <matt@peterson.org>
Reply-To: Matt Peterson <matt@peterson.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc: phk@freebsd.org, Dirk-Willem van Gulik <dirkx@webweaving.org>
Subject: [patch] rc.d/diskless detection, dhcp rebuild
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         58272
>Category:       conf
>Synopsis:       [patch] rc.d/diskless detection, dhcp rebuild
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Oct 20 04:20:16 PDT 2003
>Closed-Date:    Mon Aug 30 17:16:03 GMT 2004
>Last-Modified:  Mon Aug 30 17:16:03 GMT 2004
>Originator:     Matt Peterson
>Release:        FreeBSD 5.1-CURRENT i386
>Organization:
Bay Area Wireless Users Group
>Environment:
>Description:

The following patch fixes a few issues with the rc.d/diskless script:

1. Add detection based on read-only / (popular for embedded systems
which are booting from flash, not NFS/PXE/etc).

2. Require syslog & named before, since they may require a writable
/var

3. Move newalias rebuild, only if sendmail is enabled

4. Re-work DHCP domain-name & DNS to use kenv dhcp.* values (plumbed
from PXE boot loader) OR read from dhclient.leases (in the case of
r/o flash using DHCP).

Much of these tweaks are down right ugly, I welcome a more sanatized
version to be committed.  Quick glance from phk was postive, request
this PR assigned to DougB; thx!

>How-To-Repeat:
>Fix:

--- diskless.orig	Mon Oct 13 01:18:05 2003
+++ diskless	Mon Oct 20 03:59:59 2003
@@ -29,11 +29,20 @@
 
 # PROVIDE: diskless
 # REQUIRE: initdiskless rcconf mountcritlocal
-# BEFORE: addswap random
+# BEFORE: addswap random named syslogd
 # KEYWORD: FreeBSD
 
-dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
-[ ${dlv:=0} -eq 0 ] && exit 0
+# Detect boot from flash (read-only /) or PXE (nfs /)
+### TODO: grep requires /usr, re-work to use /sbin/something?
+rootdev=`sysctl -n kern.rootdev`
+ro_test=`mount | grep $rootdev | grep read`
+
+if [ -n "$ro_test" ]; then
+ dlv="2"
+else
+ dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
+ [ ${dlv:=0} -eq 0 ] && exit 0
+fi
 
 name="diskless2"
 
@@ -87,11 +96,15 @@
 #
 echo "+++ populate /var using /etc/mtree/BSD.var.dist"
 /usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var > /dev/null
+
+# Make sendmail happy
+#
 case ${sendmail_enable} in
 [Nn][Oo][Nn][Ee])
 	;;
 *)
 	/usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / > /dev/null
+	/usr/bin/newaliases
 	;;
 esac
 
@@ -104,11 +117,6 @@
 echo "+++ create lastlog"
 /usr/bin/touch /var/log/lastlog
 
-# Make sure our aliases database is uptodate, the aliases may have
-# been overriden in /conf.
-#
-/usr/bin/newaliases
-
 # XXX make sure to create one dir for each printer as requested by lpd
 #
 # If we do not have a writable /tmp, create a memory
@@ -145,23 +153,43 @@
 
 # generate our hostname
 #
-if [ -z "`hostname -s`" -a \
-    "x`/bin/kenv dhcp.host-name`" != "x" ]; then
-        hostname=`/bin/kenv dhcp.host-name`
-        hostname $hostname
-	echo "Hostname is $hostname"
-fi
-
-# if the info is available via dhcp/kenv
-# build the resolv.conf
-#
-if [ ! -e /etc/resolv.conf -a \
-    "x`/bin/kenv dhcp.domain-name-servers`" != "x" ]; then
-        echo domain `/bin/kenv dhcp.domain-name` > /etc/resolv.conf
-
-        set `/bin/kenv dhcp.domain-name-servers`
-        for ns in `IFS=','; echo $*`; do
-                echo nameserver $ns >> /etc/resolv.conf;
-        done
+if [ -z "`hostname -s`" ] ; then
+ if [ $dlv = 2 ] ; then
+  hostname="`grep 'host-name ' /var/db/dhclient.leases | \
+  sed -e 's/^.*me \"//' -e 's/\"\;$//'`"
+  kenv dhcp.host-name="$hostname"
+  echo here
+ fi
+ echo two
+ hostname "`kenv dhcp.host-name`"
+ echo "Hostname is $hostname"
+fi
+
+# populate resolv.conf with domain and name servers 
+# TODO: snarf dhclient_flags for -lf different leases file location
+#
+# writable?
+if [ -w /etc/resolv.conf -o ! -f /etc/resolv.conf ] ; then
+# domain already exist?
+ if [ -z "`grep 'domain' /etc/resolv.conf > /dev/null 2>&1`" ] ; then
+# non-PXE boot, snarf from dhclient leases file, fill in kernel enviro
+  if [ $dlv = 2 ] ; then
+   /bin/kenv dhcp.domain-name=`grep 'domain-name ' \
+   /var/db/dhclient.leases | sed -e 's/^.*me \"//' -e 's/\"\;$//'` \
+   > /dev/null 2>&1;
+  fi		
+ echo domain `/bin/kenv dhcp.domain-name` > /etc/resolv.conf
+ fi
+
+ if [ -z "`grep 'nameserver' /etc/resolv.conf > /dev/null 2>&1`" ] ; then
+  if [ $dlv = 2 ] ; then
+   /bin/kenv dhcp.domain-name-servers=`grep domain-name-servers \
+   /var/db/dhclient.leases | sed -e 's/^.*ers //' -e 's/;//'` \
+   > /dev/null 2>&1;
+  fi
+ set `/bin/kenv dhcp.domain-name-servers`
+  for ns in `IFS=','; echo $*`; do
+   echo nameserver $ns >> /etc/resolv.conf;
+  done
+ fi
 fi
-

>Release-Note:
>Audit-Trail:

From: Matt Peterson <matt@peterson.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc: Dirk-Willem van Gulik <dirkx@webweaving.org>, phk@freebsd.org
Subject: Re: misc/58272: [patch] rc.d/diskless detection, dhcp rebuild
Date: Mon, 20 Oct 2003 08:11:21 -0700

 On Mon, Oct 20, 2003 at 07:33:10AM -0700, Dirk-Willem van Gulik wrote:
 > 
 > Comparing this to
 > http://wleiden.webweaving.org:8080/svn/node-config/factory/trunk/nodeconfig/
 > where do the same.
 > 
 >  > 3. Move newalias rebuild, only if sendmail is enabled
 > 
 > We moved this to /var/run with a link from /etc
 > 
 > > 4. Re-work DHCP domain-name & DNS to use kenv dhcp.* values (plumbed
 > > from PXE boot loader) OR read from dhclient.leases (in the case of
 > > r/o flash using DHCP).
 > 
 > And same for resolv.conf to /var/run - as dhclient will actualy block/hang
 > when it cannot write its resolv.conf to /etc.
 
 Right, I assume resolv.conf and other etc files are sym-link to a
 writable directory.
 
 > > +rootdev=`sysctl -n kern.rootdev`
 > > +ro_test=`mount | grep $rootdev | grep read`
 > 
 > Rather than detect we also found it useful to have this under
 > mount_rw_root="NO sort of control.
 
 Ahh, you mean 'root_rw_mount', ya; totally, I've updated the patch to
 use vs. the ghetto detection method.
 
 http://matt.peterson.org/FreeBSD/stripdown/diskless.diff

From: Matt Peterson <matt@peterson.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc: Dirk-Willem van Gulik <dirkx@webweaving.org>, dougb@freebsd.org,
	phk@freebsd.org
Subject: Re: misc/58272: [patch] rc.d/diskless detection, dhcp rebuild
Date: Tue, 21 Oct 2003 13:25:05 -0700

 Actually, we need to re-think this.  In our situation (again, not PXE
 boot, but CF card mounted read-only), dhclient is currently run after
 the diskless script (thus it can't plum the system's resolv.conf,
 hostnamed and related).  See below...
 
 <snip>
 Starting file system checks:
 /dev/ad0s1a: FILE SYSTEM CLEAN; SKIPPING CHECKS
 /dev/ad0s1a: clean, 78015 free (87 frags, 9741 blocks, 0.1%
 fragmentation)
 +++ mount_md of /var
 +++ populate /var using /etc/mtree/BSD.var.dist
 +++ create log files based on the contents of /etc/newsyslog.conf
 +++ create lastlog
 grep: /var/db/dhclient.leases: No such file or directory
 dhcp.host-name=""
 Hostname is 
 Setting hostname: template.barwn.org.
 lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
         inet 127.0.0.1 netmask 0xff000000 
         inet6 ::1 prefixlen 128 
         inet6 fe80::1%lo0 prefixlen 64 scopeid 0x5 
 Starting dhclient.
 sis0: Applying short cable fix (reg=f5)
 sis0: Applying short cable fix (reg=f8)
 sis0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
         inet6 fe80::200:24ff:fec0:5480%sis0 prefixlen 64 scopeid 0x2 
         inet 10.1.7.25 netmask 0xffffffe0 broadcast 10.1.7.31
         ether 00:00:24:c0:54:80
         media: Ethernet autoselect (100baseTX <full-duplex>)
         status: active
 </snip>
 
 How do you (rcNG folks) recommend we fix this?  I guess we can break
 diskless up into two scripts?

From: Dirk-Willem van Gulik <dirkx@webweaving.org>
To: Matt Peterson <matt@peterson.org>
Cc: FreeBSD-gnats-submit@freebsd.org, dougb@freebsd.org,
	phk@freebsd.org
Subject: Re: misc/58272: [patch] rc.d/diskless detection, dhcp rebuild
Date: Tue, 21 Oct 2003 13:50:16 -0700 (PDT)

 > Actually, we need to re-think this.  In our situation (again, not PXE
 > boot, but CF card mounted read-only), dhclient is currently run after
 > the diskless script (thus it can't plum the system's resolv.conf,
 > hostnamed and related).  See below...
 
 dhclient wants contineous access - so it wil have to live on /var/* or in
 a /etc overlay which is RW.
 
 Our solution has been to let a /etc/resolv.conf be set up as a link from
 /etc/ - and modify dhclient-script in /sbin to cope with both symlinks and
 a normal /etc.
 
 Dw
State-Changed-From-To: open->closed 
State-Changed-By: arved 
State-Changed-When: Mon Aug 30 17:15:35 GMT 2004 
State-Changed-Why:  
I think this has been fixed by splitting up diskless a few months ago. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=58272 
>Unformatted:
