Typo
[linux_from_scratch_hints.git] / OLD / bsd-init.txt
blob9a4d8d5381f682649a5298c72920e13c1a0eba78
1 TITLE:          Setting up LFS to have BSD-style (Slackware) init.
2 LFS VERSION:    2.4+
3 AUTHOR:         Marc Heerdink <marc@koelkast.net>
5 SYNOPSIS:
6 This hint deals with an alternative way of booting your system.
8 HINT:
9 TABLE OF CONTENTS:
11 1. Preface
12 2. Setting up the inittab
13 3. Creating the boot scripts
14 4. The rc.local issue
15 5. The words of wisdom
16 6. The end
19 1. PREFACE
21    Since LFS uses SYSV init scripts by default, about everybody who has an LFS
22  system uses this kind of init. But a few days ago, i read about someone on the
23  mailing list who wanted to setup BSD style init. Since i was using this since
24  the beginning i decided to write a hint for everybody who wants to use BSD
25  style init (or just wants to try it).
27    BSD init uses the normal SYSV init program, but a different inittab and has
28  the boot scripts arranged different. BSD boots your system in a much less
29  complicated way, so the scripts are easier to maintain. I think one should
30  read this hint before installing either init, because the decision should
31  preferably made before the first boot.
33    This HINT will be never complete, comments can be sent to
34  marc@koelkast.net. I'm working on a hint for a mixed-mode init like I
35  used on my first LFS system (I currently have only BSD style init scripts).
38 2. SETTING UP THE INITTAB
40    The inittab resides in /etc and configures your init. Before you start
41  writing it, you should realize that a wrong inittab will probably result (in
42  the worst case) in a kernel panic, but at least a lot of trouble booting your
43  system. You'll have to choose what runlevels you want to use before you begin,
44  too. I have the following setup (this example is quite good and will be used
45  during the rest of this hint):
47     RUNLEVEL:       DESCRIPTION:
48        0       Power down the system
49        S       Single user mode
50        1       Alias for S
51        2       Multi user runlevel with console login
52        3       Multi user runlevel with graphical login
53        4       Alias for 2
54        5       Alias for 2
55        6       Reboot the system
57    I don't recommend changing the purpose of runlevels 0, 6 and S because
58  they should be configured like this for many programs. So we'll stick with
59  their function. Because I use only 3 modes for booting (Single User, Console
60  Multi User and Graphical Multi User) runlevels 4 and 5 are aliases for
61  runlevel 2 (default). You can change their purpose to whatever you like, but
62  I suggest you'll do that after you finished this hint.
64    Now let's get to business! Put this in your /etc/inittab:
66 -------------------------------/etc/inittab----------------------------------
67 id:2:initdefault:
69 si:S:sysinit:/etc/rc.d/rc.sysinit
71 l0:0:wait:/etc/rc.d/rc.0
72 l1:1:wait:/etc/rc.d/rc.1
73 l2:2:wait:/etc/rc.d/rc.2
74 l3:3:wait:/etc/rc.d/rc.3
75 l4:4:wait:/etc/rc.d/rc.4
76 l5:5:wait:/etc/rc.d/rc.5
77 l6:6:wait:/etc/rc.d/rc.6
79 ca:12345:ctrlaltdel:/sbin/shutdown -t1 -r now
81 su:S1:respawn:/sbin/sulogin
83 c1:2345:respawn:/sbin/agetty tty1 38400 linux
84 c2:2345:respawn:/sbin/agetty tty2 38400 linux
85 c3:2345:respawn:/sbin/agetty tty3 38400 linux
86 c4:2345:respawn:/sbin/agetty tty4 38400 linux
87 c5:2345:respawn:/sbin/agetty tty5 38400 linux
88 c6:2345:respawn:/sbin/agetty tty6 38400 linux
89 ----------------------------end of /etc/inittab------------------------------
91    This is a pretty basic configuration but should do for about everybody. As
92  you can see, init first starts /etc/rc.d/rc.sysinit and then loads the needed
93  file for the default runlevel (/etc/rc.d/rc.2).
96 3. CREATING THE BOOT SCRIPTS
98    Let's create /etc/rc.d/rc.sysinit first:
100 -----------------------------/etc/rc.d/rc.sysinit----------------------------
101 #!/bin/sh
102 echo "Mounting root device read-only..."
103 /bin/mount -n -o remount,ro /
105 echo "Initializing swap partitions..."
106 /sbin/swapon -a
108 /sbin/fsck -A -a -C
109 if [ $? -gt 1 ]; then
110    echo
111    echo "ERROR:"
112    echo "Your filesystem has been severely damaged. You can probably correct this"
113    echo "problem by running e2fsck manually (eg. with the -v and -y options). After"
114    echo "you logout, the system will reboot."
115    echo
116    PS1="(Repair filesystem)# "
117    export PS1
118    /sbin/sulogin
119    /bin/umount -a -r
120    /sbin/reboot -f
123 echo "Remounting root device read-write..."
124 /bin/mount -n -v -o remount,rw /
126 echo "" >/etc/mtab
127 /bin/mount -f -o remount,rw /
129 echo "Mounting other local filesystems..."
130 /bin/mount -a -v -tnonfs
132 echo "Setting up hostname..."
133 /bin/hostname `cat /etc/HOSTNAME |cut -d . -f1`
134 /bin/domainname `cat /etc/HOSTNAME |cut -d . -f2-`
136 if [ -f "/tmp/random-seed" ]; then
137   echo "Initializing random number generator..."
138   /bin/cat /tmp/random-seed >/dev/urandom
139   rm -f /tmp/random-seed
142 echo "Loading keymap..."
143 /usr/bin/loadkeys -d
145 echo "Setting system time from hardware clock..."
146 /sbin/hwclock --hctosys --utc
148 echo "Starting system and kernel log daemons...."
149 /usr/sbin/syslogd
150 /usr/sbin/klogd -c3
152 echo "Updating module dependencies..."
153 /sbin/depmod -a
154 -------------------------end of /etc/rc.d/rc.sysinit-------------------------
156    To make the hostname lines work as expected, create a file /etc/HOSTNAME
157  which holds your fqdn (Full Qualified Domain Name). That is, for example,
158  foo.bar.com or gimli.gimli.org. The last line is optional and is only useful
159  if you're using  modules. A last note on the hwclock command: if your system
160  clock isn't configured for using UTC (that means you're using local time) you
161  should drop the --utc options from that line. Read the Time hint for more
162  information.
164    Now let's create the script for the single user runlevel. Since this
165  runlevel won't be used very often to boot in, but instead to fall back to if
166  something happens to the system, all running programs will be killed so you're
167  in a very clean environment when running in single user mode.
169 --------------------------------/etc/rc.d/rc.1-------------------------------
170 #!/bin/sh
171 echo "Unmounting remote filesystems..."
172 /bin/umount -a -tnfs
174 # insert a line for each network card you use here. This is an example for
175 #  a single network card set-up (configured as eth0):
177 # echo "Bringing down network interface eth0..."
178 # /sbin/ifconfig eth0 down
180 echo "Sending all processes the TERM signal..."
181 /sbin/killall5 -15
182 sleep 1
184 echo "Sending all processes the KILL signal..."
185 /sbin/killall5 -9
186 ----------------------------end of /etc/rc.d/rc.1----------------------------
188    If this script has run, no daemons have been left except the kernel daemons
189  and init. After it has finished sulogin will be started (that's what the line
190  "su:S1:respawn:/sbin/sulogin" is for :) so only root can use the system. All
191  virtual consoles will be disabled.
193    Let's get on to the next script, /etc/rc.d/rc.2. This file has many common
194  options in it, eg. to set up networking and start network daemons. Remove
195  every line you won't use, but don't add anything before you read chapter 4.
197 --------------------------------/etc/rc.d/rc.2-------------------------------
198 #!/bin/sh
199 # In this example, the network card is configured with 192.168.0.2 as ip
200 #  address and a netmask of 255.255.255.0. This network card uses 192.168.0.1
201 #  as the default gateway. This is the set up you would use if the box
202 #  192.168.0.1 would be the gateway.
203 # NOTE:
204 #  The "window 16384" option in the route command is optional but will
205 #  your network speed.
206 echo "Setting up networking..."
207 /sbin/ifconfig eth0 192.168.0.2 broadcast 192.168.0.255 netmask 255.255.255.0
208 /sbin/route add -net default gw 192.168.0.1 netmask 0.0.0.0 window 16384 metric 1
210 echo "Mounting remote filesystems..."
211 /bin/mount -a -v -tnfs
213 if [ -x /etc/rc.d/rc.local ]; then
214   /etc/rc.d/rc.local
216 ----------------------------end of /etc/rc.d/rc.2----------------------------
218    Now copy /etc/rc.d/rc.2 to /etc/rc.d/rc.3 and add the following to the
219  bottom of the file:
221 ------------------------------------snip-------------------------------------
222 echo "Starting graphical login manager..."
224 if [ -x /opt/kde/bin/kdm ]; then
225   /opt/kde/bin/kdm -nodaemon
226 elif [ -x /usr/bin/gdm ]; then
227   /usr/bin/gdm -nodaemon
228 elif [ -x /usr/X11R6/bin/xdm ]; then
229   /usr/X11R6/bin/xdm -nodaemon
230 else
231  echo "You chose to start graphical login mode, but you don't have either KDM or"
232  echo "GDM or XDM installed. This script looks for these display managers in the"
233  echo "following locations:"
234  echo
235  echo "   KDM      /opt/kde/bin/kdm"
236  echo "   GDM      /usr/bin/gdm"
237  echo "   XDM      /usr/X11R6/bin/xdm"
238  echo
239  echo "This message will go away in 10 seconds, and after that you will be dropped"
240  echo "in runlevel 2."
241  sleep 10
242  /sbin/telinit 2 
244 ----------------------------end of /etc/rc.d/rc.3----------------------------
246    The script is pretty self-explaining. It looks for the most commonly used
247  display manages in their default locations. If none of them is found, a
248  warning will be displayed and the system will change to runlevel 2 with a
249  normal console login screen.
251    Now we have created all bootscripts except /etc/rc.d/rc.0 and
252  /etc/rc.d/rc.6. Since they both perform pretty much the same function, we'll
253  create it only once:
255 --------------------------------/etc/rc.d/rc.0-------------------------------
256 #!/bin/sh
257 echo "Sending all processes the TERM signal..."
258 /sbin/killall5 -15
259 sleep 1
261 echo "Sending all processes the KILL signal..."
262 /sbin/killall5 -9
263 sleep 1
265 echo "Deactivating swap partitions..."
266 /sbin/swapoff -a
268 echo "Saving random seed to a temporary file..."
269 /bin/dd if=/dev/urandom of=/tmp/random-seed count=1 bs=512 2>/dev/null
271 echo "Saving the system time to hardware clock..."
272 /sbin/hwclock --systohc --utc
274 echo "Unmounting remote filesystems..."
275 /bin/umount -a -f -tnfs
277 case "$0" in
278   *6)
279     /sbin/reboot -w
280     ;;
281   *0)
282     /sbin/halt -w
283     ;;
284 esac
286 echo "Remounting root filesystem read-only..."
287 /bin/mount -n -o remount,ro /
289 echo "Unmounting local filesystems..."
290 /bin/umount -a -tnonfs
292 echo "Flushing filesystem buffers..."
293 /bin/sync
295 case "$0" in
296   *6)
297     echo "Please stand by while rebooting..."
298     /sbin/reboot -d -f -i
299     ;;
300   *0)
301     echo "Bye..."
302     /sbin/halt -d -f -p
303     ;;
304 esac
305 ----------------------------end of /etc/rc.d/rc.0----------------------------
307    Some notes on this file: the hwclock should be configured like the one in
308  /etc/rc.d/rc.sysinit (no --utc if your hardware clock uses local time). The
310   case "$0" in
311     *6)
312       /sbin/reboot -w
313       ;;
314     *0)
315       /sbin/halt -w
316       ;;
317   esac
319  construction writes some status information to /etc/wtmp. It's a good idea to
320  do this, but you can safely remove it. At about two-third of the file, I call
321  /bin/sync. This program flushes the filesystem buffers so you won't loose any
322  data. Like the construction above, this is optional but I recommend it.
324    Now we have create all the required files, some changes have to made to
325  make them work. Run the following commands to do this:
327   chmod 755 /etc/rc.d/rc.0 /etc/rc.d/rc.1 /etc/rc.d/rc.2
328   chmod 755 /etc/rc.d/rc.3 /etc/rc.d/rc.sysinit
329   ln -s /etc/rc.d/rc.2 /etc/rc.d/rc.4
330   ln -s /etc/rc.d/rc.2 /etc/rc.d/rc.5
331   ln -s /etc/rc.d/rc.0 /etc/rc.d/rc.6
333    You've done it! Take a deep breath and type (as root) "reboot" and watch
334  your system boot with BSD style init scripts! If you have troubles using
335  these scripts, drop me a line: marc@koelkast.net.
338 4. THE RC.LOCAL ISSUE
340    As you probably know, it is common to have a /etc/rc.d/rc.local file where
341  you put commands in that will be executen at the end of the boot process. You
342  can use it to create for example up-to-date issue files or to pick a random
343  message of the day. But since you created all bootscripts yourself, you can
344  change them as much as you like, and you probably won't need this script. So
345  what's it going to be?
347    I personally recommend you create this script, for the purpose of
348  portability. Many daemons write one or two lines to this file, and it saves
349  you trouble if it's already present. This is how you create one:
351    Put this in a file /etc/rc.d/rc.local:
353 -------------------------------/etc/rc.d/rc.local----------------------------
354 #!/bin/sh
355 -----------------------------end /etc/rc.d/rc.local--------------------------
357    No, I didn't make a typo... :) I can't make this file for you, because
358  you have to decide for yourself what you want to put in it (and, of course,
359  what you don't want in it). Make this script executable by doing this:
361   chmod 755 /etc/rc.d/rc.local
363    It is very easy for you to disable /etc/rc.d/rc.local; just remove
364  the executable flag from the script and it will be skipped at boot time.
367 5. THE WORDS OF WISDOM
369    I suppose you want to adapt these scripts to your personal needs. Before
370  you do that, I would like to give you some advice on where to put the programs
371  you start and what programs you shouldn't use.
373    Let's start with the kernel modules. These are often loaded in a very early
374  stage, so we'll do that too. I suggest you insert modprobe lines after the
375  depmod line in /etc/rc.d/rc.sysinit. One exception should be made for network
376  cards, especially on machines with much traffic or machines with l337 h4x0r5
377  as clients. To be sure you're safe, you should load the network card modules
378  from /etc/rc.d/rc.2 and /etc/rc.d/rc.3, since they're only needed in multi
379  user modes. In case of a system error, you can reboot your system safely in
380  single user mode without networking.
382    Many people use hdparm to tweak their hard drives. I run hdparm from the
383  system initialization script, because I want a fast hard drive in single user
384  mode too :). It doesn't make too much sense to me to run hdparm when
385  everything else is already loaded, since booting the system is quite hard disk
386  intensive and a faster hard drive will really boost it.
388    Networking daemons, such as name servers, apache and mysql should obviously
389  be loaded from the multi user startup scripts, since you won't need them in
390  single user environments.
392    If you have installed netkit-base for your network card, you will probably
393  want a loopback device available for many programs. To get one, put the
394  following in /etc/rc.d/rc.sysinit just before setting up the hostname:
396 ----------------------------------------------------------------------------
397 echo "Setting up loopback networking..."
398 /sbin/ifconfig lo 127.0.0.1
399 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo
400 ----------------------------------------------------------------------------
402    As a final tip in this section, I would like to point out to you that I
403  have often had a multi-user environment without networking. It would be a
404  good practice for you to set up this environment on, for example, runlevel 5
405  and set it up in a secure way. This implies you have to decide whether you
406  really need a service or not, to avoid any damage that may be caused by your
407  ignorance.
410 6. THE END
412    I hope you learnt from this hint how a BSD style init works. Although this
413  setup is not the same as Slackwares or BSDs setup, the idea is basically the
414  same. If you have comments on this hint, or you just liked it, please mail me
415  at marc@koelkast.net. Bye for now, and watch out for hint updates.
417 __END__