Tuesday, November 9, 2010

Linux Boot Process

The Boot Process

When a computer boots the BIOS is the first program that is run. Once loaded the BIOS will begin to test the system through the power on self test (POST), and then begin loading peripherials. The BIOS will then look to whatever is contained in the first sector of this device. This usually contains the master boot record (MBR) which will start to boot the machine via the boot loader. Once the boot loader has been loaded, the BIOS yields control to it. The default boot loader for most systems these days is GRUB. From here GRUB will look to boot into the kernel that is labeled as the "default". Finally the Kernel will call the init process which boots up the rest of the system. Understanding how the system boots is essential to troubleshooting should the system ever have a problem booting, or if you are working with a kernel.


GRUB

GRUB has become the default boot loader for Red Hat & Debian as well. GRUB can be used to boot into different operating systems (usually called a multiboot system), for system recovery, and to boot the kernel using special parameters. When GRUB loads you are given a list of kernels from which you can choose to boot from. There is also a list of commands which you can select from.

e -> edit the commands before booting
a -> modify the kernel arguments before booting
c -> grub command line

Although working with GRUB can save you time to diagnose system boot issues make sure that you record all changes to /boot/grub/grub.conf otherwise your changes will be lost on next boot. On Red Hat the /etc/grub.conf is actually a link to /boot/grub/grub.conf so all changes will be made to the same file.

The Kernel

After GRUB has loaded the kernel takes over and begins to initialize and configure the computer's hardware. depending on how your GRUB configuration file is setup you may or may not see anything during this process. The "quiet" parameter in GRUB hides this process from the user (usually a gaphic of some sort is shown in its place). All the information gathered during this processes is logged to /var/log/dmesg. You can also use the dmesg tool to query information about bootup messages. Once the systems drivers are in place the kernel will execute the /sbin/init program.

The init Program

The init program is the first process that is created by the kernel. It is responsible for the rest of the boot process and setting up the environment for the user. First it will call /etc/rd.d/rc.sysinit which is responsible for a number of tasks including setting the system clock, networking, user environment, and others. After is it finished, the init command then runs the /etc/inittab script. This script defines how all services should be handle in each runlevel as well as which run level to boot the system into. In Red Hat and CentOs the default runlevel is 5 while Debian is 2. The last thing you will see is the screen to login, which will either be a virtual terminal (if no X window is configured) or a GUI login.

Runlevels

The runlevel system helps organize services which create a standard for controlling system services. There are six runlevels which can be found in /etc/inittab.

0 - Halt
1 - Singe-user mode
2 - Multi-user with partial services
3 - Full multi-user with networking (text mode)
4 - Not used
5 - Full multi-user graphical mode (provides a GUI desktop login)
6 - Reboot

Located in the /etc/rc.d/ directory there are different directories containing which services should be started or stoped at each runlevel. There are three utilities that are provided to manage services during system boot. The first is the chkconfig command.

chkconfig --list [service-name]

If you specify no service it will list all services and their status at each run level. You can narrow this down and look at a particular service by specifing the service name. You will see that each service is either "on" or "off" at each runlevel respresenting a started or not started state. This utility can also be used to enable or disable services at each runlevel.

chkconfig --level

The second utility is ntsysv. Instead of modifying each indivual service at the command line you can use this command to modify multiple services at multiple runlevels at once.

ntsysv [--level ]

For example if I wanted to modify all services at runlevels 3 & 4 I would type:

ntsysv --level 34

This utility produces a list of all services which can be checked or unchecked to either start or stop that at the runlevels that you specified. That last utility that you can use is system-config-services. This utility is a GUI program that can't be launch from a system that without a GUI. Similar to ntsysv it provides a listing of services which can be easily modified to each runlevel. You should now be pretty comfortable with the boot process and have an idea of how you can go about troubleshooting should your system not boot correctly.

No comments:

Post a Comment