• No results found

First user process started (PID 1) Parent of all other processes Styled after System Vinit

Configured through/etc/inittab

Slide 118:init(8)

init(8)

Wheninitstarts, it’s the only process running (ignoring some processes running in the kernel), and has a process

6Actually, it’s a horrible hack. 7Nonetheless, this is what Debian do.

ID of 1. If the system’s going to do anything else, it’s up toinitto cause it to happen.

The version ofinitthat Linux uses these days is modelled after the System V version, and differs from the older BSD version in many ways. This need not concern you unless you’re likely to have to deal with BSD-like systems (or have come from a BSD environment), but it does at least explain why the RPM package containinginitis calledSysVinit.

one of 0, 1, 2, 3, 4, 5, 6

system state, characterised by a set of services maintained (and only known about) byinit

Slide 119: Runlevels

Runlevels

init’s design is based around the concept of “runlevels”. A runlevel is a state the system can be in, and is characterised by the set of system services that are available in that state.init’s main job is to handle changes in runlevel. This includes first entering a runlevel when the system starts, and switching to runlevel 0 or 6 just before it stops.

The runlevels available are numbered 0 through 6. 0 and 6 are special: entering runlevel 0 instructs the system to shut down, while entering runlevel 6 instructs it to reboot. Runlevel 1 is conventionally used for “single-user mode”, in which the system has as little running as possible. This mode is useful for doing particularly awk- ward bits of reconfiguration (like shuffling filesystems between discs), which aren’t really feasible with everything running. Runlevels 2 through 5 are available for your use, and will be explained a little more in Section 7.

Contains allinit’s configuration Line-oriented

Third colon-separated field is the important one

Slide 120:/etc/inittab

/etc/inittab init’s operation is entirely configured by the/etc/inittabfile. In addition to comments (beginning with#) and blank lines, it contains lines with five fields separated by colons. In a piece of impressive obfuscation, the third field is the main one for determining the meaning of each line. The file format is fully documented ininittab(5). The standard Red Hat/etc/inittabcontains, amongst other junk, the following:

id:3:initdefault si::sysinit:/etc/rc.d/rc.sysinit l0:0:wait:/etc/rc.d/rc 0 l1:1:wait:/etc/rc.d/rc 1 l2:2:wait:/etc/rc.d/rc 2 l3:3:wait:/etc/rc.d/rc 3

l4:4:wait:/etc/rc.d/rc 4 l5:5:wait:/etc/rc.d/rc 5 l6:6:wait:/etc/rc.d/rc 6 1:2345:respawn:/sbin/mingetty tty1 2:2345:respawn:/sbin/mingetty tty2 3:2345:respawn:/sbin/mingetty tty3 4:2345:respawn:/sbin/mingetty tty4 5:2345:respawn:/sbin/mingetty tty5 6:2345:respawn:/sbin/mingetty tty6 x:5:respawn:/etc/X11/prefdm -nodaemon inittabdetails

So, we’ve got five kinds of line,initdefault,sysinit,wait,onceandrespawn.initdefaultindicates which runlevelinitshould switch to on startup. In this case, this is runlevel 3.

Run at startup Mounts/proc

Sets system clock

Sets up keyboard and text screen Sets up USB devices

Checks and mounts local filesystems Enables swap on partitions

Enables quotas Cleans up

Enables swapping on files

Slide 121:/etc/rc.d/rc.sysinit

/etc/rc.d/rc.sysinit

Thesysinitline tellsinitto run the/etc/rc.d/rc.sysinitscript when it first starts. This script does every- thing you see happening between the “Welcome to Red Hat Linux” and “Entering runlevel: 3” messages, notably setting the system clock, setting up the screen and keyboard, mounting and checking filesystems and enabling swapping.

waitentries andrc

The various entries labelled “wait” tellinitwhat command to run when it enters a given runlevel. In this case, it runs/etc/rc.d/rcand passes it the runlevel on the command line. rc, not to be confused with the Plan 9 shell of the same name, is responsible for running the scripts that start and stop system services.

K20rstatd -> ../init.d/rstatd

. . .

S99linuxconf -> ../init.d/linuxconf S99local -> ../rc.local

Kill andStart scripts

Called in numerical order in each group

Called with argumentsstopandstartrespectively

Slide 122:rcn.dscripts

rcn.dscripts

These scripts are stored in various directories under/etc/rc.d8, namedrc0.d throughrc6.d, one for each runlevel. Each of these directories contains a collection of symbolic links to files in/etc/rc.d/init.d, with slightly special names. For instance, on a newly-installed Red Hat system,/etc/rc.d/rc2.dcontains:

K03rhnsd -> ../init.d/rhnsd K05atd -> ../init.d/atd K05saslauthd -> ../init.d/saslauthd K20nfs -> ../init.d/nfs K24irda -> ../init.d/irda K44rawdevices -> ../init.d/rawdevices K50xinetd -> ../init.d/xinetd K72autofs -> ../init.d/autofs K74ntpd -> ../init.d/ntpd K75netfs -> ../init.d/netfs K86nfslock -> ../init.d/nfslock K87portmap -> ../init.d/portmap K95firstboot -> ../init.d/firstboot K95kudzu -> ../init.d/kudzu S08iptables -> ../init.d/iptables S09isdn -> ../init.d/isdn S10network -> ../init.d/network S12syslog -> ../init.d/syslog S17keytable -> ../init.d/keytable S20random -> ../init.d/random S24pcmcia -> ../init.d/pcmcia S26apmd -> ../init.d/apmd S55sshd -> ../init.d/sshd S80sendmail -> ../init.d/sendmail S85gpm -> ../init.d/gpm S90crond -> ../init.d/crond S90cups -> ../init.d/cups S90xfs -> ../init.d/xfs S95anacron -> ../init.d/anacron S99local -> ../rc.local

Each link’s name begins with three magic characters. The first one is eitherSorK, indicating a service to be either started or killed whenrcenters this runlevel. The next two characters indicate the order in which the scripts should be run. Whenrcenters a new runlevel, it first runs theKill scripts in numerical order, then theStart scripts in numerical order.9

Note that bothSandKlinks point into the same directory. If you check the links for various other runlevels, you’ll find that scripts pointed to byKlinks in some are pointed to bySlinks in others. In order that the scripts can tell whether they’re meant to start or kill their respective services,rcpasses each script an argument ofstartorstop, depending on what it wants to the script to do.

I shan’t attempt to explain the precise purpose of all the init.d scripts. Most of them just start or stop a single daemon with the same name as the script. A few do special things, notably the network setup script

/etc/rc.d/init.d/network, which will be explained in the network concepts section.

Press [I] when you’re told to

Start service foo (Y)es/(N)o/(C)ontinue? [Y]

[Y] – start the service [N] – don’t start the service [C] – start this and all subsequent

Slide 123: Interactive startup

Interactive startup

Permenantly enabling and disabling services will be covered in the section on service configuration, but it’s worth mentioning here the “Interactive startup” option. When a Red Hat Linux system starts, it displays a message:

Press ’I’ to enter interactive startup.

If you do as it says (before the “Entering non-interactive startup” message),rcwill prompt you before running each Start script. Choosing [Y]es or [N]o at one of these prompts does the obvious thing. Choosing [C]ontinue causesrcto run the rest of the scripts without asking.

1. Reboot your machine 2. Press [I] at the right moment

3. Start your system up without Sendmail running

Slide 124: Exercise: Interactive startup

9There’s an obvious bogosity here, in that there’s no guarantee that the kill links in the new run level correspond to the start links in the old

runlevel. The Right Thing would have been for the kill scripts to have been associated with the old runlevel, not the new one, but there’s too much tradition around now to fix this, and it works by and large well enough.

Run whenwaitentries are finished Restarted if/when they die

mingettyprovides console logins

prefdmrunsxdm,gdmorkdm, which provide X logins

Slide 125:respawnentries

respawnentries

The remaining interesting lines ininittabare those with therespawnaction. These tellinitto run the program specified at the end of the line once it’s run all of thewaitentries for the runlevel. When the program does,init

immediately restarts it. All of therespawnentries for a runlevel are run at once.

The main use ofrespawnentries is to run interactive login programs. The first batch of six entries runmingetty

on the first six virtual consoles.mingettyprovides the login prompts you see on the consoles, and invokeslogin

for you. The final one runsprefdm, which is (indirectly) responsible for automatically starting the X server and a graphical login program.

1. Edit/etc/inittabto add amingettyontty8

2. Askinitto re-read/etc/inittab: #telinit q

3. Check that you can now login on virtual console 8 (use [Ctrl] + [Alt] + [F8]) 4. Remove the line frominittabandtelinit q

Slide 126: Exercise: Changing the number of text consoles

More secure consoles

Note that if you want to enable logins on virtual consoles abovetty11, you’ll need to edit/etc/securettyto include them. Therootuser is only allowed to log in on terminals mentioned in this file (though only certain login mechanisms homour this), and by default it only containstty1–tty11.