Linux boot process in Red Hat Enterprise Linux 5
Environment
- Red Hat Enterprise Linux 5
Issue
- Linux boot process in Red Hat Enterprise Linux 5
Resolution
- This Article explains the
bootprocess and important scripts participating in it, which is based onRHEL5.
Road Map
Power-On Self Test---> Master Boot Record---> Boot Loader---> GRUB---> Kernel---> INIT---> Runlevels---> Login
Power-On Self Test
- The first thing to occur in the boot process is the Power-On Self Test (POST) that is performed by the computer's
Basic Input/Output System (BIOS), when it is powered on. - This self test is just that -- an internal check of the system's internal components. Other architectures may use different ways to accomplish this, but the purpose is identical.
- This checking normally involves the system
RAM, CPUs, video card, hard drives, and other motherboardcomponents. - Many other types of internal cards such as
RAIDcards and various other types of controller cards perform their own self-testing. If errors occur, some form of alert will be generated or sent to a panel or console; thePOSTuses a series of beeps to indicate the specific error encountered. - If all goes well, the
BIOSreads themaster boot recordon the hard disk and loads the program that is found there into memory.
Master Boot Record
- The
Master Boot Record (MBR)is the first512 bytesof the boot drive that is read into memory by theBIOS. (This is assuming we are using an x86 architecture.) - The first
446 bytes of that 512will normally contain a low-level boot code that points to aboot loadersomewhere else on the disk - it can even point to another hard disk. - The next 64 bytes contain the partition table for the disk (four 16-byte entries known as the Partition Table Scheme). Finally, the remaining 2 bytes are the "Magic Number" (used for error detection).
Boot Loader
- The purpose of a
bootloaderis to load the operating system. Manyboot loadersare available; however,LILOandGRUBare the most common for Linux. - Windows has its own, called the
New Technology Loader (NTLDR). You may have at one time or another seen the message "NTLDRis missing"; this is a Windowsbootloadererror. - Regardless of the
bootloaderbeing used, it is important to keep in mind thatbootloadersare very complex and easily rendered inoperative by inexperienced users with root access.
GRUB
GRUBworks in stages calledStage1, Stage1_5, and Stage 2.
Stage1
- The
stage1code ofGRUBis written within the512 bytesof themaster boot record. Because of the limited size of themaster boot recordarea,GRUBstage1 will usually point to the next stage ofGRUB, stage1_5 or stage2.GRUBmay or may not need to loadstage1_5depending on the types offile systemspresent.
[root@localhost grub]# file stage1
stage1: x86 boot sector, code offset 0x48
Stage1_5
Stage1_5is the intermediate stage betweenstage1andstage2. If you look at your/boot/grubarea, you should see variousstage1_5files with names of various types offilesystems.Stage1_5deals with specific types offilesystems, and is named after them. This code will allow thefilesystemto be interpreted appropriately.- These are the
stage1_5files located in my/boot/grubdirectory:
e2fs_stage1_5
fat_stage1_5
ffs_stage1_5
iso9660_stage1_5
jfs_stage1_5
minix_stage1_5
reiserfs_stage1_5
ufs2_stage1_5
vstafs_stage1_5
xfs_stage1_5
Stage2
- This is the main
GRUBimage, and will usually reside on the filesystem in the/bootpartition at/boot/grub/stage2. It reads the/boot/grub/grub.conffile for configuration information that details how it will load the kernel. It also has an interactive interface that will allow you to troubleshoot, re-install, or modify howGRUBworks. - Stage2 will present the user with a graphical boot menu entry. If the appropriate key is not pressed in the time allotted to enter the hidden menu, or nothing is selected in the time frame allowed, GRUB will boot the default entry.
GRUBhas a really helpful tab-completion feature that you can use to assist you in getting your system up and running if, for example, you have a non-bootable system due to a improperly specified line in yourgrub.conffile. I have used it in this way many times to get unbootable systems working again.- This file specifies the kernel to load and the
initrdimage file with all of the modules to load for your system.
Here is a typical grub.conf file for Red Hat Linux:
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/hdb9
# initrd /initrd-version.img
#boot=/dev/hdb
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-53.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet
initrd /initrd-2.6.18-53.el5.img
Kernel
- When
GRUBtransfers control to the kernel and the kernel is booting, you may see a lot of text output. On Red Hat-based systems, the line in grub.conf specifying the kernel may have anrhgb quietappended to it that prevents this. - It stands for "Red Hat graphical boot quiet" and will suppress kernel boot messages. When I need to see boot messages, I interrupt the
GRUBcycle by hitting the escape key, hitting 'e' for edit (the procedure to modify the grub kernel arguments is at the bottom of the GRUB screen), and editing the kernel line. - Removing the
rhgb quietallows us to see the kernel messages so we can determine if any of them are relevant to, e.g., akernel panicor a similar problem. - What is actually going on at this time is that the kernel is probing your hardware and configuring itself for your hardware.
- The kernel is also loading modules in the
initrdimage that it needs to operate your hardware. Note that the information will scroll very quickly up your screen - If you believe your problem exists at the kernel level, be sure to watch closely, as this is sure to give you a clue about where your problem lies. Once the kernel is done with its initialization, it starts the system's first process, which is/sbin/init.
INIT
Initis the first process running on the system. It reads the/etc/inittabfile, executes/etc/rc.d/rc.sysinit, then boots into the runlevel as defined in/etc/inittab.- Init starts out with a Process ID (PID) of 1. In the image above, there's a line saying
INIT: version 2.86 booting; this is/sbin/inittaking over at this point in the boot process. - On the line right after that one, you see the messages being displayed by the
/etc/rc.d/rc.sysinitshell script; as a matter of fact, the entire screen contains messages from that script, so you can get an idea of some of the functions it performs. Initwill also normally start several instances of/sbin/gettyor/sbin/mingetty, which are your virtual terminals. This is why you can hit<Ctrl> + <Alt> + < F1> through <F6>and come up with a virtual terminal.- Next, we will look at the
/etc/inittabfile, specifically, at the configuration file for Init:
#
# inittab This file describes how the INIT process should set up
# the system in a certain run-level.
#
# Author: Miquel van Smoorenburg,
# Modified for RHS Linux by Marc Ewing and Donnie Barnes
#
# 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:5:initdefault:
# System initialization.
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
# Trap CTRL-alt-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
# When our UPS tells us power has failed, assume we have a few minutes
# of power left. Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
# Run gettys in standard runlevels
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
# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon
-
Depending on what Init is doing, you may see typical Init script verification messages getting printed to the screen, i.e.,
[ OK ] or [Failed]to aid in troubleshooting.
Runlevels -
When you enter your runlevel, you will see further Init messages being printed to the screen (depending if your machine is configured to do so), again ending with a
[ OK ] or [Failed]depending on whether it started successfully or not. -
These are your startup services within your runlevels. The runlevel is decided from
/etc/inittabfile. The line which looks likeid:5:initdefault:decides the runlevel. this is your default runlevel. -
To get an overview of what processes get started or stopped for any particular runlevel, we should look within the
/etc/rcX.d(where X is runlevel) directory. -
Inside these directories, you will see symbolic links to the files in your
/etc/init.d/directory. The file names will be prefixed with either a'K' or an 'S'(signifying kill or start) for the given daemon at that runlevel. -
The number immediately after the letter positions the script in the start order, because the processes are started alphabetically.
Login
- That sums up the outline of the boot process for a typical Linux machine.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.