linux

7

Click here to load reader

Upload: chs360

Post on 18-Sep-2015

216 views

Category:

Documents


0 download

DESCRIPTION

boot process linux

TRANSCRIPT

  • Introduction to Linux:Prev Chapter 4. Processes Next

    4.2. Boot process, Init andshutdown4.2.1. IntroductionOne of the most powerful aspects of Linux concerns its open method of startingand stopping the operating system, where it loads specied programs usingtheir particular congurations, permits you to change those congurations tocontrol the boot process, and shuts down in a graceful and organized way.Beyond the question of controlling the boot or shutdown process, the opennature of Linux makes it much easier to determine the exact source of mostproblems associated with starting up or shutting down your system. A basicunderstanding of this process is quite benecial to everybody who uses a Linuxsystem.A lot of Linux systems use lilo, the LInux LOader for booting operating systems.We will only discuss GRUB, however, which is easier to use and more exible.Should you need information about lilo, refer to the man pages and HOWTOs.Both systems support dual boot installations, we refer to the HOWTOs on thissubject for practical examples and background information.

    4.2.2. The boot processWhen an x86 computer is booted, the processor looks at the end of the systemmemory for the BIOS (Basic Input/Output System) and runs it. The BIOSprogram is written into permanent read-only memory and is always available foruse. The BIOS provides the lowest level interface to peripheral devices andcontrols the rst step of the boot process.The BIOS tests the system, looks for and checks peripherals, and then looks fora drive to use to boot the system. Usually it checks the oppy drive (or CD-ROMdrive on many newer systems) for bootable media, if present, and then it looksto the hard drive. The order of the drives used for booting is usually controlledby a particular BIOS setting on the system. Once Linux is installed on the harddrive of a system, the BIOS looks for a Master Boot Record (MBR) starting atthe rst sector on the rst hard drive, loads its contents into memory, thenpasses control to it.

    Boot process, Init and shutdown http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    1 of 7 06/01/2015 05:31 PM

  • This MBR contains instructions on how to load the GRUB (or LILO) boot-loader,using a pre-selected operating system. The MBR then loads the boot-loader,which takes over the process (if the boot-loader is installed in the MBR). In thedefault Red Hat Linux conguration, GRUB uses the settings in the MBR todisplay boot options in a menu. Once GRUB has received the correctinstructions for the operating system to start, either from its command line orconguration le, it nds the necessary boot le and hands o control of themachine to that operating system.

    4.2.3. GRUB featuresThis boot method is called direct loading because instructions are used todirectly load the operating system, with no intermediary code between theboot-loaders and the operating system's main les (such as the kernel). The bootprocess used by other operating systems may dier slightly from the above,however. For example, Microsoft's DOS and Windows operating systemscompletely overwrite anything on the MBR when they are installed withoutincorporating any of the current MBR's conguration. This destroys any otherinformation stored in the MBR by other operating systems, such as Linux. TheMicrosoft operating systems, as well as various other proprietary operatingsystems, are loaded using a chain loading boot method. With this method, theMBR points to the rst sector of the partition holding the operating system,where it nds the special les necessary to actually boot that operating system.GRUB supports both boot methods, allowing you to use it with almost anyoperating system, most popular le systems, and almost any hard disk yourBIOS can recognize.GRUB contains a number of other features; the most important include:

    GRUB provides a true command-based, pre-OS environment on x86machines to allow maximum exibility in loading operating systems withcertain options or gathering information about the system.GRUB supports Logical Block Addressing (LBA) mode, needed to accessmany IDE and all SCSI hard disks. Before LBA, hard drives could encountera 1024-cylinder limit, where the BIOS could not nd a le after that point.GRUB's conguration le is read from the disk every time the system boots,preventing you from having to write over the MBR every time you changethe boot options.

    A full description of GRUB may be found by issuing the info grub command or atthe GRUB site. The Linux Documentation Project has a Multiboot with GRUBMini-HOWTO.

    Boot process, Init and shutdown http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    2 of 7 06/01/2015 05:31 PM

  • 4.2.4. InitThe kernel, once it is loaded, nds init in sbin and executes it.When init starts, it becomes the parent or grandparent of all of the processesthat start up automatically on your Linux system. The rst thing init does, isreading its initialization le, /etc/inittab. This instructs init to read an initialconguration script for the environment, which sets the path, starts swapping,checks the le systems, and so on. Basically, this step takes care of everythingthat your system needs to have done at system initialization: setting the clock,initializing serial ports and so forth.Then init continues to read the /etc/inittab le, which describes how the systemshould be set up in each run level and sets the default run level. A run level is aconguration of processes. All UNIX-like systems can be run in dierent processcongurations, such as the single user mode, which is referred to as run level 1or run level S (or s). In this mode, only the system administrator can connect tothe system. It is used to perform maintenance tasks without risks of damagingthe system or user data. Naturally, in this conguration we don't need to oeruser services, so they will all be disabled. Another run level is the reboot runlevel, or run level 6, which shuts down all running services according to theappropriate procedures and then restarts the system.Use the who to check what your current run level is:

    willy@ubuntu:~$ who -rrun-level 2 2006-10-17 23:22 last=S

    More about run levels in the next section, see Section 4.2.5.After having determined the default run level for your system, init starts all ofthe background processes necessary for the system to run by looking in theappropriate rc directory for that run level. init runs each of the kill scripts (theirle names start with a K) with a stop parameter. It then runs all of the startscripts (their le names start with an S) in the appropriate run level directory sothat all services and applications are started correctly. In fact, you can executethese same scripts manually after the system is nished booting with acommand like /etc/init.d/httpd stop or service httpd stop logged in as root, inthis case stopping the web server.

    Special caseNote that on system startup, the scripts in rc2.d and rc3.d are usuallyexecuted. In that case, no services are stopped (at least not permanently).

    Boot process, Init and shutdown http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    3 of 7 06/01/2015 05:31 PM

  • There are only services that are started.None of the scripts that actually start and stop the services are located in/etc/rc.d. Rather, all of the les in /etc/rc.d are symbolic links that point tothe actual scripts located in /etc/init.d. A symbolic link is nothing more than ale that points to another le, and is used in this case because it can be createdand deleted without aecting the actual scripts that kill or start the services.The symbolic links to the various scripts are numbered in a particular order sothat they start in that order. You can change the order in which the servicesstart up or are killed by changing the name of the symbolic link that refers tothe script that actually controls the service. You can use the same numbermultiple times if you want a particular service started or stopped right before orafter another service, as in the example below, listing the content of /etc/rc5.d,where crond and xfs are both started from a linkname starting with "S90". Inthis case, the scripts are started in alphabetical order.

    [jean@blub /etc/rc5.d] lsK15httpd@ K45named@ S08ipchains@ S25netfs@ S85gpm@K16rarpd@ K46radvd@ S08iptables@ S26apmd@ S90crond@K20nfs@ K61ldap@ S09isdn@ S28autofs@ S90xfs@K20rstatd@ K65identd@ S10network@ S30nscd@ S95anacron@K20rusersd@ K74ntpd@ S12syslog@ S55sshd@ S95atd@K20rwalld@ K74ypserv@ S13portmap@ S56rawdevices@ S97rhnsd@K20rwhod@ K74ypxfrd@ S14nfslock@ S56xinetd@ S99local@K25squid@ K89bcm5820@ S17keytable@ S60lpd@K34yppasswdd@ S05kudzu@ S20random@ S80sendmail@

    After init has progressed through the run levels to get to the default run level,the /etc/inittab script forks a getty process for each virtual console (loginprompt in text mode). getty opens tty lines, sets their modes, prints the loginprompt, gets the user's name, and then initiates a login process for that user.This allows users to authenticate themselves to the system and use it. Bydefault, most systems oer 6 virtual consoles, but as you can see from the inittable, this is congurable./etc/inittab can also tell init how it should handle a user pressingCtrl+Alt+Delete at the console. As the system should be properly shut downand restarted rather than immediately power-cycled, init is told to execute thecommand /sbin/shutdown -t3 -r now, for instance, when a user hits those keys.In addition, /etc/inittab states what init should do in case of power failures, ifyour system has a UPS unit attached to it.On most RPM-based systems the graphical login screen is started in run level 5,where /etc/inittab runs a script called /etc/X11/prefdm. The prefdm script runs thepreferred X display manager, based on the contents of the /etc/sysconfig/desktop

    Boot process, Init and shutdown http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    4 of 7 06/01/2015 05:31 PM

  • directory. This is typically gdm if you run GNOME or kdm if you run KDE, butthey can be mixed, and there's also the xdm that comes with a standard Xinstallation.But there are other possibilities as well. On Debian, for instance, there is aninitscript for each of the display managers, and the content of the /etc/X11/default-display-manager is used to determine which one to start. More about thegraphical interface can be read in Section 7.3. Ultimately, your systemdocumentation will explain the details about the higher level aspects of init.The /etc/default and/or /etc/sysconfig directories contain entries for a range offunctions and services, these are all read at boot time. The location of thedirectory containing system defaults might be somewhat dierent depending onyour Linux distribution.Besides the graphical user environment, a lot of other services may be startedas well. But if all goes well, you should be looking at a login prompt or loginscreen when the boot process has nished.

    Other proceduresWe explained how SysV init works on x86 based machines. Startupprocedures may vary on other architectures and distributions. Othersystems may use the BSD-style init, where startup les are not split upinto multiple /etc/rc.d directories. It might also be possible that yoursystem uses /etc/rc.d/init.d instead of /etc/init.d.

    4.2.5. Init run levelsThe idea behind operating dierent services at dierent run levels essentiallyrevolves around the fact that dierent systems can be used in dierent ways.Some services cannot be used until the system is in a particular state, or mode,such as being ready for more than one user or having networking available.There are times in which you may want to operate the system in a lower mode.Examples are xing disk corruption problems in run level 1 so no other userscan possibly be on the system, or leaving a server in run level 3 without an Xsession running. In these cases, running services that depend upon a highersystem mode to function does not make sense because they will not workcorrectly anyway. By already having each service assigned to start when itsparticular run level is reached, you ensure an orderly start up process, and youcan quickly change the mode of the machine without worrying about whichservices to manually start or stop.Available run levels are generally described in /etc/inittab, which is partially

    Boot process, Init and shutdown http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    5 of 7 06/01/2015 05:31 PM

  • shown below:

    ## inittab This file describes how the INIT process should set up# the system in a certain run-level.

    # Default run level. The run levels 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:5:initdefault:

    Feel free to congure unused run levels (commonly run level 4) as you see t.Many users congure those run levels in a way that makes the most sense forthem while leaving the standard run levels as they are by default. This allowsthem to quickly move in and out of their custom conguration withoutdisturbing the normal set of features at the standard run levels.If your machine gets into a state where it will not boot due to a bad /etc/inittabor will not let you log in because you have a corrupted /etc/passwd le (or if youhave simply forgotten your password), boot into single-user mode.

    No graphics?When you are working in text mode because you didn't get presented agraphical login screen on the console of your machine, you can normallyswitch to console 7 or up to have a graphical login. If this is not the case,check the current run level using the command who -r. If it is set tosomething else than the original default from /etc/inittab, chances are thatthe system does not start up in graphical mode by default. Contact yoursystem administrator or read man init in that case. Note that switchingrun levels is done preferably using the telinit command; switching from atext to a graphical console or vice versa does not involve a run levelswitch.

    The discussion of run levels, scripts and congurations in this guide tries to beas general as possible. Lots of variations exist. For instance, Gentoo Linux storesscripts in /etc/run levels. Other systems might rst run through (a) lower runlevel(s) and execute all the scripts in there before arriving at the nal run level

    Boot process, Init and shutdown http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    6 of 7 06/01/2015 05:31 PM

  • and executing those scripts. Refer to your system documentation for moreinformation. You might also read through the scripts that are refered to in/etc/inittab to get a better comprehension of what happens on your system.4.2.5.1. ToolsThe chkcong or update-rc.d utilities, when installed on your system, providea simple command-line tool for maintaining the /etc/init.d directory hierarchy.These relieve system administrators from having to directly manipulate thenumerous symbolic links in the directories under /etc/rc[x].d.In addition, some systems oer the ntsysv tool, which provides a text-basedinterface; you may nd this easier to use than chkcong's command-lineinterface. On SuSE Linux, you will nd the yast and insserv tools. ForMandrake easy conguration, you may want to try DrakConf, which allowsamong other features switching between run levels 3 and 5. In Mandriva thisbecame the Mandriva Linux Control Center.Most distributions provide a graphical user interface for conguring processes,check with your system documentation.All of these utilities must be run as root. The system administrator may alsomanually create the appropriate links in each run level directory in order tostart or stop a service in a certain run level.

    4.2.6. ShutdownUNIX was not made to be shut down, but if you really must, use the shutdowncommand. After completing the shutdown procedure, the -h option will halt thesystem, while -r will reboot it.The reboot and halt commands are now able to invoke shutdown if run whenthe system is in run levels 1-5, and thus ensure proper shutdown of thesystem,but it is a bad habit to get into, as not all UNIX/Linux versions have thisfeature.If your computer does not power itself down, you should not turn o thecomputer until you see a message indicating that the system is halted ornished shutting down, in order to give the system the time to unmount allpartitions. Being impatient may cause data loss.

    Prev Home NextProcesses inside out Up Managing processes

    Boot process, Init and shutdown http://www.tldp.org/LDP/intro-linux/html/sect_04_...

    7 of 7 06/01/2015 05:31 PM