Download presentation
Presentation is loading. Please wait.
1
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 This chapter looks at the starting up and closing down of Unix systems. While describing system startup (known as bootstrapping), we cover the various system run levels (boot, single-user and multi-user) and the control files associated with each level (run command files). System shutdown covers standard shutdown procedures and why a clean shutdown is required.
2
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 all linux dialects) 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 Unix systems have to have a fairly standard bootstrap interface. Obviously, the lowest-level machine bootstrap is hardware-dependent. On Unix-specific systems, the hardware bootstrap is the Unix bootstrap; other systems load the Unix bootstrap from the default boot device. The bootstrap program has to be able to boot Unix to single-user or multi-user mode and be able to boot different configurations of the operating system. This information is read in from a user-supplied command line. Most systems allow default bootstrap information to be defined and permanently stored to permit an automatic bootstrap whenever the system is powered up. SVR4 systems store bootable programs in the /stand directory, which is usually a separate disk partition. Older versions of Unix store bootable programs in the root directory. The Unix operating system is traditionally called Unix (i.e. /stand/unix). After the bootstrap program has loaded the Unix operating system, or kernel, into memory, it relinquishes control to this system. The Unix system initialises the physical devices, virtual memory controller and its internal control tables for processes, files, etc. The Unix kernel contains process-scheduling code and this is turned into the first process to be run; process zero (0). The second process is always the initialisation process which takes over from the kernel and controls the remainder of the initialisation process. Before handing control over to init, the kernel will create any other system-level processes that are required. Typical processes include the virtual memory handler (vhand) and the process swapper (swapper). AIX
3
Startup Flow Control run level 0 boot run level 3 run level 1
single-user maintenance run level 3 multi-user & DFS run level 4 multi-user user defined run level 2 multi-user All Unix systems follow the same general startup procedure. Beginning with the hardware boot prom, the first stage is to load in the basic Unix operating system, device drivers and system processes. At this point, the system is able to run in single-user mode, enabling administrative and maintenance tasks to be carried out. Usually the boot process goes straight to a multi-user level to initiate the system background processes, called daemons, which support multi-user operation. At this point, the system is up and running and other users can log in and use the system. An SVR4 Unix system can be configured to have three different multi-user run levels to enable different layers of software availability. It usual to run Unix systems at level 3. On most Unix systems, the bootstrap process can be automated so that the system comes up into multi-user mode without manual intervention. Some systems require the operator to supervise the bootstrap process. AIX uses the BSD startup technique, whereby the system only use single user and one multi-user run level. AIX
4
Gentoo / SuSE / RedHat Run-Levels
Run levels are standard under Gentoo 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, gentoo also x-windows 4 not used 5 Full multi-user with networking xdm (X-Windows), exept gentoo 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 When Gentoo boots, the boot process will run a number of scripts located in subdirectories under directory /etc/runlevels. The boot process first runs rc sysinit (an internal runlevel) to bring up filesystems, then the scripts found in /etcrunlevels/boot will be executed, which provides only the most basic functionality, and the ability to only handle a single user. This stage is known as "single user mode". After completing this first phase, the boot process will run scripts in only one of the other directories depending on the startup mode or "run level". These are listed above. The init loads the init default runlevel, usally runlevel 2, 3 or 5. Usally ONLY ROOT can run shutdown commands, to allow others add them in: /etc/shutdown.allow The gentoo way to allow regular users do a system shutdown is to use sudo: 1. Add group boot and add users to it. #newgrp boot 2. With visudo add: %boot ALL=(root) NOPASSWD: /sbin/shutdown %boot ALL=(root) NOPASSWD: /sbin/halt %boot ALL=(root) NOPASSWD: /sbin/reboot %boot ALL=(root) NOPASSWD: /sbin/poweroff The general unix way allowing non-root users to shutdown a Linux box. 1. As root create a group shutdown. #addgroup shutdown 2. Put /sbin/shutdown into the group shutdown. #chown root:shutdown /sbin/shutdown 3. Change accessrights to make /sbin/shutdown setuid and disallow other users from executing shutdown. #chmod 4754 /sbin/shutdown 4. Make a link from /bin/shutdown to /sbin/shutdown #ln -s /sbin/shutdown /bin/shutdown Now anyone belonging to the group shutdown can execute shutdown but I just will allow the person in control of the keyboard to be able to issue the shutdown command. You might prefer something else. # who -r run-level 5 Sep 25 10: last=S # shutdown -h +10 "Memory upgrade. Please log off ” clean HALT in 10 minutes with warning message: Memory upgrade….
5
The init Control file: /etc/inittab
The init process uses /etc/inittab as its control file # Default runlevel. The runlevels used by LINUX 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, exept gentoo 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 The init process is controlled via the /etc/inittab file. The inittab file contains one line for every process to be run for every initialisation file. This file can be very big on large Unix systems. Init reads this file every time the run level is changed, and changes made to the file only apply at this time. Init can be told to reread the inittab file without changing levels by the command: # init q This tells init to query its configuration file without changing run levels. The command # telinit q can also be used on some systems to achieve the same effect. Telinit is linked to init on SVR4 and AIX4 systems, but is a separate program on older Unix systems. Note that the id field can be a maximum of 4 characters in length.
6
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 The action keywords in the inittab file are shown on the slide. Each action has a particular use as detailed below. wait Typically used for commands that control changing levels. s2:23:wait:/sbin/rc2 >/dev/console 2>&1 <&1. once Typically used to run a command when changing levels, but the command is not necessary for the new level. Daemon processes are often started this way. respawn Used for commands that have to start again after completion, like getty & ttymon. off Used to retain an entry, but to disable it. Often used to stop the login process on a faulty terminal line. The boot and bootwait actions are boot-time equivalents of once and wait. The initdefault entry is used to tell init the default system boot-level. Note that the difference between boot, bootwait and sysinit actions is that sysinit actions will be done even if the state to be entered is single-user, whereas boot and bootwait actions are done on entering run levels 1,2,3 and 4. The inittab file in AIX can be interrogated and modified through a couple of utilities. This is a nice add-on feature, helping to prevent file corruption through bad editing. The utilities are: lsitab -a, to list the entries in the table (without the comment lines) and chitab to add or modify an entry. AIX
7
Exercise - /etc/inittab
What processes are run at system startup What processes are run at system shutdown # more /etc/inittab si::sysinit:/sbin/rc sysinit rc::bootwait:/sbin/rc boot id:3:initdefault: l0:0:wait:/sbin/rc shutdown l1:S1:wait:/sbin/rc single l2:2:wait:/sbin/rc nonetwork l3:3:wait:/sbin/rc default l4:4:wait:/sbin/rc default l5:5:wait:/sbin/rc default l6:6:wait:/sbin/rc reboot 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 c1:12345:respawn:/sbin/agetty tty1 linux c2:12345:respawn:/sbin/agetty tty2 linux X:a:once:/etc/X11/startDM.sh Simplified /etc/inittab without all text comments Mingetty is the virtual terminal server, inthis case it is instructed to handle two, tty1 and tty2 which is same as Alt + F1 and Alt + F2
8
Run Command Scripts The run command scripts invoked by init via /etc/inittab Each rc attribute controls changes to named run level rc sysinit internal runlevel at boot is activated rc boot looks in /etc/runlevel/boot at booting rc shutdown looks in /etc/runlevel/shutdown for level 0 rc single looks in /etc/runlevel/single for level 1 & S rc nonetwork looks in /etc/runlevel/nonetwork for level 2 rc default looks in /etc/runlevel/default for levels 3, 4 & 5 rc reboot looks in /etc/runlevel/reboot for level 6 Script runs startup programs in subdirectory of /etc/runlevel Startup program names is formed like: Startup in alphanumeric order or by script internal logics. All startup programs in runlevels are links to scripts in /etc/init.d Gentoo comes with a simplified runlevel control system, real runlevels have been hidden for the user, in good and bad. This way runlevels as numbers have been renamed to something that could be called run-modes. The run command scripts are kept in /sbin and are initiated by init under directions from /etc/inittab. Each significantly different run level has its own script. The user-defined run level (4) does not have a script, but if this level is used, the system administrator would normally create a /sbin/rc4 script. The scripts look in subdirectories /etc/runlevels for scripts to run for the required run level. Each of these directories contain links to files kept in /sbin/init.d. The link names are formatted so that they are executed in a specific order determined by their names in alphanumeric order or internal script logics whilst executed. When the script is invoked by the rc program, the first parameter is set to the word start or stop. The script in /sbin/init.d must look at the value of this parameter to determine which action to take (start or kill).
9
RC Script Details (Start and Stop)
# more /etc/init.d/sshd case "$1" in start) ... ...;; stop) ... ...;; esac Adding sshd # cd /etc # ln init.d/sshd runlevels/default/sshd # init 3 # init 0 # sh sshd start In this example, the rc script (here simplified) is kept in /etc/init.d/sshd. This script is linked to /etc/runlevels/default/sshd so that the sshd service is started up when switching to multi-user mode. The same script is also executed by run control /sbin/rc so that the internet service is stopped when leaving runlevel 3 to 0. The sshd script contains the necessary code to start or stop the sshd service. The start or stop function is passed in as a keyword when the script is called, so a typical format for all rc scripts is to use a shell case statement to choose between the start and the stop functionality. The command: init 3 invokes the /sbin/rc default script which ultimately performs the command: sh sshd start init 0 command invokes the /sbin/rc shutdown script which ultimately performs the command: sh sshd stop Note that there is no requirement for the sequence number of the linked filenames to be the same in different rc directories. In most other Unixes or linuxes name is formed as sequence numbers in order to control stop and start in a more effective manner. # sh sshd stop
10
Set Programs Run At Each runlevel
Check, set and move program’s to various runlevels Switch Off sshd Starting Up In gentoo runlevel default # rc-update –d sshd default Switch On sshd Starting Up In gentoo runlevel default # rc-update –a sshd default # rc-status -a Runlevel: boot keymaps [ started ] . . . Runlevel: default sshd [ stopped ] Local [ started ] Runlevel: nonetwork Runlevel: single Runlevel: UNASSIGNED Most Gentoo packages place a startup script in the directory /etc/init.d and place symbolic links (pointers) to this script in the appropriate /etc/runlevels/ directory. The typical home/SOHO user doesn't have to be a scripting / symbolic linking guru to make sure everything works right because Gentoo comes with two nifty utility called ”rc-status" and ”rc-update” to do it for you. rc-update works for most applications having start/stop script in /etc/init.d Final Tips On rc-update Don't add/remove anything to other runlevels unless you absolutely know what you are doing. Don't experiment. rc-update doesn't start the programs in the /etc/init.d directory, it just configures them to be started or ignored when the system boots up. The commands for starting and stopping the programs covered in this book are covered in each respective chapter. rc-update –d sshd would unlink the sshd from all runlevels. rc-status –l will show you all available runlevels
11
Manually start and stop services
Check if service is running Stop service Start service Reload service after reconfiguring Also note that other start/stop scripts can have: restart condrestart graceful # /etc/init.d/sshd status * status: started # /etc/init.d/sshd stop Stopping sshd [ok] # /etc/init.d/sshd start Starting sshd [ok] Most services and some applications can be manually stopped and started with the start scripts in /etc/init.d Fx. XINETD is Gentoo, SuSE and RedHat internet daemon, controlling tcp-wrappers and other servers. In Gentoo proftpd is preferred standard. It is default dependent on XINETD Manipulating with services at runlevel system is not making all of them be ready for action directly. There are two kind of services, stand alone and xinetd dependent. All stand alone services need to be started with /etc/init.d/<name> start after installed, if you dont intend to reboot the server before production. The other catogery of services only need to be rc-updated’ed and the xinetd service need to be reloaded: /etc/init.d/xinetd reload They will then be in production direcly. Also note that Gentoo services for comfort optionally can use /etc/conf.d for settings when services are started. # /etc/init.d/sshd reload Reloading sshd [ok]
12
Exercise - System Startup
What subsystems are started up at run level 3 What subsystems are stopped going from level 3 to 2 # grep '[23]' /etc/inittab id:3:initdefault: l2:2:wait:/sbin/rc nonetwork l3:3:wait:/sbin/rc default ca:12345: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/agetty tty1 linux 2:2345:respawn:/sbin/agetty tty2 linux # ls /etc/runlevels/nonetwork /etc/runlevels/default /etc/runlevels/default: local net.eth0 netmount syslog-ng vixie-cron /etc/runlevels/nonetwork: local
13
Changing run levels Use init to change run levels if users not logged on 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 The init process looks after the Unix system. It reads information from a control file called /etc/inittab and uses this information to manage the operation of the system at the appropriate run level. Init is also used to change between different run levels. The init process is found in /sbin/init on SVR4 systems and /etc/init on older systems. The init command is used to specify the current run level. Init performs the required actions to put the system into the new level from the old level. The inittab file is used to determine which commands should be executed at each level. The change to a different level is controlled by individual run command (rc) files, which are described on the next slide. The rc files to run for each level are defined in the inittab file. Init will accept a parameter which is either a new run level or one of the letters described below: init s single-user mode (S also recognised) init q reread inittab file for current level (Q also recognised) If a new run level is specified as an option, init will change the system's run level accordingly. Although it is reasonable to change to a higher run level using init, it is less acceptable to change down to a lower run level. Using "init 0" to halt the system is very unfriendly when other users are logged in, as they will be logged out without warning. The shutdown command should be used to close down the system from multi-user to single-user or powerdown levels.
14
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) -tN wait 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 enough when receiving a TERM signal during shutdown # shutdown –r now reboot the system now # shutdown r halt system in one hour Use shutdown to close down the system from multi-user levels. All logged in users will be warned of the impending shutdown and those that fail to log off voluntarily will be logged off automatically when the shutdown occurs. On BSD-derived Unix systems, the shutdown command accepts different parameters, but performs the same function: shutdown [-hr] time [message] Shutdown is to single mode unless the -r (reboot) or -h (halt) options are specified. The shutdown time parameter is an absolute time of day (e.g. 19:00 is 7pm), an incremental time (e.g. +10 for 10 minutes hence) or the keyword now for immediate shutdown. The second parameter to shutdown is a message which is output with the shutdown warning messages. BSD-derived systems do not use the init process for changing run levels, and there is no inittab file; only firmware, single-user and multi-user levels are provided. To change from single-user to multi-user is achieved by logging out (typing exit) from the single-user shell.
15
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 BSD-derived Unix systems differ in the way the run levels are handled. They only have single-user and multi-user levels and consequently have a simpler approach to changing run levels. The run command scripts are simple shell scripts which have to be edited to modify the startup procedures. BSD-derived systems do not use the init process for changing run levels, and there is no inittab file; only firmware, single-user and multi-user levels are provided. To change from single-user to multi-user is achieved by logging out (typing exit) from the single-user shell. The way AIX 4 handles the startup procedure is currently half way between SVR4 and BSD. AIX does use the /etc/inittab file. The initdefault is defined there to the value of 2, which is the only multiuser level used. The init process will recognise other multiuser states, but implementation of those, without the rc?.d directory structure would be rather complex. All services are started directly from the inittab table, with the startup scripts located within the /etc directory. Rather like with straight BSD systems, startup script names begin with "rc.", ie. /etc/rc.boot, /etc/rc.tcpip, etc. # shutdown now password: # sync # halt # halt # poweroff AIX # reboot
16
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!) Gentoo uses the live CD or DVD Most unix serves has a boot prompt, in INTEL plateform it does not exist, there you have bios settings and grub or lilo or syslinux Maintenance mode is a catch all phrase for what to do if things go wrong. The simplest and best solution is to shut down to single user (maintenance) mode and try and fix the problem. If this doesn't work try rebooting and interrupting the normal boot sequence to force the system into single user mode. A typical boot command would be: boot -s If all else fails go to the original installation media (typically a CD-ROM on modern systems) and boot from this. Make sure you answer no when asked to install the system and you should be given a Unix miniroot system. The miniroot runs in available memory without the need for a swapfile on disk. A suit of commands will be provided on the installation media and these are usually sufficient to fix most repairable problems.
17
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.