System Startup & Shutdown • Objectives • to interpret the Unix startup and shutdown configuration files • to be able to create a customised run level • Contents • bootstrap procedure • single and multi-user run levels • system startup files (rc files) • standard system processes • clean shutdown procedure • maintenance mode • Practicals • to modify the startup sequence of a Unix system • Summary
Standard Boot Process • The system boot process is hardware-specific • often an automatic boot from hardware into multi-user Unix • high security systems require PROM password for manual boot • In order to boot a system boot loader is needed • SysLINUX • LILO (ctrl + x = boot prompt) • GRUB (select kernel and press e = edit boot) • Boot prompt arguments is optional • Used to boot into single user mode whenever needed • To set kernel arguments at boot like boot disks, network, kernelfile • Once loaded the Unix kernel starts running • initialise devices, virtual memory, etc. • initialise internal tables (processes, files, etc.) • creates scheduler process (number 0 invisable in SuSE and RedHat) • runs the first process (number 1) /sbin/init • System initialisation performed by /sbin/init is user configurable boot: linux root=/dev/hda1 boot: linux single
Startup Flow Control run level 0 boot run level 3 multi-user & DFS run level 1 single-user maintenance run level 4 multi-user user defined run level 2 multi-user
SuSE / RedHat Run-Levels • Run levels are standard under RedHat Unix: 0 System HALT 1 Single user mode or administration mode 2 Local multiuser without remote network (e.g. NFS) 3 Full multi-user with networking 4 not used 5 Full multi-user with networking xdm (X-Windows) 6 System reboot s/S single-user mode a/b/c pseudo states (rarely used) • Use the -r option to who to get the current run level # who -r run-level 5 Sep 25 10:35 last=S # shutdown -h +10 "Memory upgrade. Please log off ” clean HALT in 10 minutes with warning message: Memory upgrade….
The init Control file: /etc/inittab • The init process uses /etc/inittab as its control file # Default runlevel. The runlevels used by RHS are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 – unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # #id:3:initdefault: # Console Text Mode id:5:initdefault: # Console GUI Mode • Processes run from /etc/inittab are daemons • standard I/O attached to /dev/null • I/O usually be redirected to a log file or perhaps the console
Structure of /etc/inittab • Each line has 4 fields separated by colons id : level : action : process id unique identifier for line - up to four alphanumerics level run level(s) to activate process action keyword for how to run process command full pathname and parameters of command to be executed • Main inittab action keywords are: off don't run the command wait run command and wait for completion once run command, but don't wait respawn run command. If process exits, then repeat the command sysinit run command at first init boot run command at boot-time but don't wait bootwait like boot, but wait for completion initdefault defines default boot-level
Exercise - /etc/inittab • What processes are run at system startup • What processes are run at system shutdown # more /etc/inittab id:5:initdefault: si::bootwait:/etc/init.d/boot 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 ls:S:wait:/etc/init.d/rc S ~~:S:respawn:/sbin/sulogin ca::ctrlaltdel:/sbin/shutdown -r -t 4 now pf::powerwait:/etc/init.d/powerfail pn::powerfailnow:/etc/init.d/powerfail now po::powerokwait:/etc/init.d/powerfail 1:2345:respawn:/sbin/mingetty tty1 2:2345:respawn:/sbin/mingetty tty2
Run Command Scripts • The run command scripts invoked by init via /etc/inittab • Each rc script in /etc/rc.d/ controls changes to named run level • Each script runs startup programs in directory /etc/rc?.d rc 0 looks in /etc/rc0.d for levels 0 rc 1 looks in /etc/rc1.d for level 1 rc 2 looks in /etc/rc2.d for levels 2 & 3 rc 3 looks in /etc/rc3.d for level 3 • Startup program names is formed like: sNNname s S to start process or K to kill process at this run level NN sequence number name name of script in /etc/init.d • All startup programs in runlevels are links to /etc/init.d
RC Script Details (Start and Stop) # more /etc/init.d/xinet case "$1" in start) ... ...;; stop) ... ...;;esac # Adding xinetd # ln init.d/xinet rc3.d/K08xinet# ln init.d/xinet rc3.d/S14xinet # init 3 # init 0 # sh S14xinet start # sh K08halt stop
Set Programs Run At Each runlevel • Check, set and move program’s to various runlevels • Switch Off SuSEfirewall2_setup Starting Up In Levels 3 and 5 # chkconfig --level 35 SuSEfirewall2_setup off • Switch On SuSEfirewall2_setup Starting Up In Levels 3 and 5 # chkconfig --level 35 SuSEfirewall2_setup on # chkconfig --list SuSEfirewall2_setup 0:off 1:off 2:off 3:on 4:on 5:on 6:off atd 0:off 1:off 2:off 3:off 4:off 5:off 6:off cron 0:off 1:off 2:on 3:on 4:off 5:on 6:off network 0:off 1:off 2:on 3:on 4:off 5:on 6:off syslog 0:off 1:off 2:on 3:on 4:off 5:on 6:off gpm 0:off 1:off 2:on 3:on 4:off 5:off 6:off cups 0:off 1:off 2:on 3:on 4:off 5:on 6:off nfs 0:off 1:off 2:off 3:on 4:off 5:on 6:off portmap 0:off 1:off 2:off 3:on 4:off 5:on 6:off xinetd 0:off 1:off 2:off 3:on 4:off 5:on 6:off . . .
Manually start and stop services • Check if service is running • Stop service • Start service • Reload service after reconfiguring # /etc/init.d/xinetd status Checking for service xinetd: running # /etc/init.d/xinetd stop Shutting down xinetd: done # /etc/init.d/xinetd start Starting INET services. (xinetd)done # /etc/init.d/xinetd reload Reload INET services (xinetd). done
Exercise - System Startup • What subsystems are started up at run level 3 • What subsystems are stopped going from level 3 to 2 # grep '' /etc/inittab id:3:initdefault: l2:2:wait:/etc/rc.d/rc 2 l3:3:wait:/etc/rc.d/rc 3 ca::ctrlaltdel:/sbin/shutdown -t3 -r now pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure!" pr:12345:powerokwait:/sbin/shutdown -c "Power Restored" 1:2345:respawn:/sbin/mingetty tty1 2:2345:respawn:/sbin/mingetty tty2 # ls rc.d rc2.d: K20nfs K55xdm S40lp K33oracle S05cron S33acct . . . . rc3.d: S22nfs S56xinetd S30oracle . . .
Changing run levels • Use init to change run levels if users not logged • use to switch to multi-user from single user • use to switch between multi-user levels • Use shutdown to close down the system • warns users what is happening • performes a lot of housekeeping routines • issues the init command # init 2 switch to run level 2 # init 3 now switch to run level 3 # shutdown default system shutdown
System Shutdown • Use /sbin/shutdown when closing down a Unix system shutdown [-tN ] [+M] [-h] [-r] [-c] time message +M wait period in seconds (now=0) -tNwait period in seconds between message and action -h shutdown to halt -c Cancel shutdown -r shutdown and reboot time absolute time hh:mm message shutdown message • wall is used to send broadcasts at set intervals until system shutdown • shutdown is started by init changing to new run level • any process left running are sent a TERM signal • any still running are sent a KILL signal • WARNING: databases cannot normally close down quickly enoughwhen receiving a TERM signal during shutdown # shutdown –r now reboot the system now # shutdown +3600 -r halt system in one hour
BSD Derived Systems (AIX, OSF/1) • Only one multi user level • boot up direct to multi-user • boot to single user and go to multi-user on exit from single user root shell • cannot use init to change levels • Use shutdown and specify time to exit from multi user • Startup/shutdown controlled by command scripts in /etc • scripts must be modified using a text editor • require knowledge of script programming • look for files: rc, rc.local, rc.single, rc.tcpip, rc.boot, rc.shutdown # shutdown -r now reboot the system now # shutdown -h 17:00 halt system at 5pm # shutdown now password: # sync # sync # halt # halt # poweroff # reboot
Maintenance mode • Maintenance mode is used to get out of trouble • the system won't boot due to password/kernel/init problems • If the system is alive and well, use shutdown to run level 1 • Otherwise reboot to single user mode • if you have to power cycle the system wait for the disk activity lights to stop blinking • If the system won’t boot, insert the installation (boot) media, and boot from this instead • ensure that you do not attempt to reinstall the system at this point • this will give you a system where Unix is running in memory using the installation media as a filesystem. The hard disk may now be able to be fixed (if you are lucky!)
Summary • Unix systems start up through a clearly defined sequence • The first configurable stage is with the init process using /etc/inittab • Run levels provide additional configuration through the run command scripts • Link and remove files from the /etc/init.d/rc*.d directories to enable/disable sub-systems for different run levels • Shutdown Unix using the shutdown command • AIX and OSF/1 (BSD) use a different mechanism to SVR4 (SVR3.2) systems