Merge from vendor branch FILE:
[dragonfly.git] / etc / rc.d / diskless
1 #!/bin/sh
2 #
3 # Copyright (c) 1999  Matt Dillon
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD: src/etc/rc.d/diskless,v 1.25 2003/02/15 16:34:14 jhay Exp $
28 # $DragonFly: src/etc/rc.d/diskless,v 1.7 2007/05/13 22:25:41 swildner Exp $
29 #
30
31 # PROVIDE: diskless
32 # REQUIRE: initdiskless rcconf mountcritlocal
33 # BEFORE: addswap random
34
35 if [ -r /etc/rc.subr ]; then
36     . /etc/rc.subr
37     dummy_rc_command "$1"
38 fi
39
40 dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
41 [ ${dlv:=0} -eq 0 ] && exit 0
42
43 name="diskless"
44
45 # Provide a function for normalizing the mounting of memory filesystems.
46 #   $1 = size
47 #   $2 = mount point
48 #   $3 = (optional) bytes-per-inode
49 mount_md() {
50         if [ -n "$3" ]; then
51                 bpi="-i $3"
52         fi
53         /sbin/mount_mfs $bpi -s $1 swap $2
54 }
55
56 # If there is a global system configuration file, suck it in.
57 #
58 if [ -r /etc/rc.subr ]; then
59         . /etc/rc.subr
60         load_rc_config $name
61 elif [ -r /etc/defaults/rc.conf ]; then
62         . /etc/defaults/rc.conf
63         source_rc_confs
64 elif [ -r /etc/rc.conf ]; then
65         . /etc/rc.conf
66 fi
67
68 # If we do not have a writable /var, create a memory
69 # filesystem for /var.  We don't have /usr yet so
70 # use mkdir instead of touch to test.  We want mount
71 # to record its mounts so we have to make sure /var/db
72 # exists before doing the mount -a.
73 #
74 if (/bin/mkdir /var/.diskless 2> /dev/null); then
75         rmdir /var/.diskless
76 else
77         echo "+++ mount_md of /var"
78         mount_md ${varsize:=32768} /var
79 fi
80
81 if [ ! -d /var/db ]; then
82         mkdir /var/db
83 fi
84
85 # Now we need the rest of our mounts, particularly /usr
86 #
87 mount -a       # chown and chgrp are in /usr
88
89 # Populate /var
90 #
91 echo "+++ populate /var using /etc/mtree/BSD.var.dist"
92 /usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var > /dev/null
93 case ${sendmail_enable} in
94 [Nn][Oo][Nn][Ee])
95         ;;
96 *)
97         /usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / > /dev/null
98         ;;
99 esac
100
101 echo "+++ create log files based on the contents of /etc/newsyslog.conf"
102 LOGFILES=`/usr/bin/awk '$1 != "#" { printf "%s ", $1 } ' /etc/newsyslog.conf`
103 if [ -n "$LOGFILES" ]; then
104         /usr/bin/touch $LOGFILES
105 fi
106
107 echo "+++ create lastlog"
108 /usr/bin/touch /var/log/lastlog
109
110 # Make sure our aliases database is uptodate, the aliases may have
111 # been overridden in /conf.
112 #
113 /usr/bin/newaliases
114
115 # XXX make sure to create one dir for each printer as requested by lpd
116 #
117 # If we do not have a writable /tmp, create a memory
118 # filesystem for /tmp.  If /tmp is a symlink (e.g. to /var/tmp,
119 # then it should already be writable).
120 #
121 if (/bin/mkdir /tmp/.diskless 2> /dev/null); then
122         rmdir /tmp/.diskless
123 else
124         if [ -h /tmp ]; then
125                 echo "*** /tmp is a symlink to a non-writable area!"
126                 echo "dropping into shell, ^D to continue anyway."
127                 /bin/sh
128         else
129                 mount_md ${tmpsize:=20480} /tmp
130                 chmod 01777 /tmp
131         fi
132 fi
133
134 if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then
135         # we have DEVFS, no worries...
136         true
137 elif (/bin/mkdir /dev/.diskless 2> /dev/null); then
138         # if /dev is writable assume it has already been populated
139         # via rc.d/diskless
140         #
141         rmdir /dev/.diskless
142 else
143         (cd /; find -x dev | cpio -o -H newc) > /tmp/dev.tmp
144         mount_md 4096 /dev 512
145         (cd /; cpio -i -H newc -d < /tmp/dev.tmp)
146         rm -f /tmp/dev.tmp
147 fi
148
149 # generate our hostname
150 #
151 if [ -z "`hostname -s`" ]; then
152         hostname=`/usr/bin/kenv dhcp.host-name`
153         hostname $hostname
154         echo "Hostname is $hostname"
155 fi
156
157 # if the info is available via dhcp/kenv
158 # build the resolv.conf
159 #
160 if [ ! -e /etc/resolv.conf ]; then
161         echo domain `/usr/bin/kenv dhcp.domain-name` > /etc/resolv.conf
162
163         set `/usr/bin/kenv dhcp.domain-name-servers`
164         for ns in `IFS=','; echo $*`; do
165                 echo nameserver $ns >> /etc/resolv.conf;
166         done
167 fi