Understanding Linux System Initialization

The BIOS is the initialization stage of the boot process that performs POST operations and determines what peripherals are available and which device to boot from. The BIOS obtains hardware configuration information from the CMOS which is where boot device order and other BIOS settings are stored. After the POST the selected boot device or the first boot device configured in CMOS will be booted from using the first sector of the boot media (Hard Disk, CDROM, network-adapter, floppy, usb device or removeable media).

In the case of the default RedHat installation using the GRUB bootloader, during Stage 1 of the boot process the BIOS passes control to the IPL installed in the MBR or boot sector. The second stage of the boot process involves loading the initrd image and kernel from the /boot partition based upon the /boot/grub/grub.conf configuration.

Example grub.conf:

title Red Hat Enterprise Linux AS (2.6.9-67.0.20.EL)
root (hd0,0)
kernel /vmlinuz-2.6.9-67.0.20.EL ro root=/dev/sda2 rhgb quiet clock=pit noapic
initrd /initrd-2.6.9-67.0.20.EL.img
title Red Hat Enterprise Linux AS (2.6.9-67.0.4.EL)
root (hd0,0)
kernel /vmlinuz-2.6.9-67.0.4.EL ro root=/dev/sda2 rhgb quiet clock=pit noapic
initrd /initrd-2.6.9-67.0.4.EL.img
title Red Hat Enterprise Linux AS (2.6.9-42.0.10.EL)
root (hd0,0)
kernel /vmlinuz-2.6.9-42.0.10.EL ro root=/dev/sda2 rhgb quiet clock=pit noapic
initrd /initrd-2.6.9-42.0.10.EL.img
title Red Hat Enterprise Linux AS (2.6.9-42.0.3.EL)
root (hd0,0)
kernel /vmlinuz-2.6.9-42.0.3.EL ro root=/dev/sda2 rhgb quiet clock=pit noapic
initrd /initrd-2.6.9-42.0.3.EL.img
title Red Hat Enterprise Linux AS (2.6.9-34.EL)
root (hd0,0)
kernel /vmlinuz-2.6.9-34.EL ro root=LABEL=/ rhgb quiet clock=pit
initrd /initrd-2.6.9-34.EL.img

To boot a Windows partition you could add an entry as follows provided you have a windows installation on Disk 0, Partition 1:

title Windows
rootnoverify (hd0, 1)
chainloader +1

To include a bootloader password:

 generate a password with - grub-md5-crypt
add password --md5 <results of output from command above> to the top of grub.conf under hiddenmenu

To repair a corrupted bootloader in MBR on /dev/sda reinstall with:

 /sbin/grub-install /dev/sda

If grub-install fails try the following:

root (hd0, 0)
setup (hd0)

The kernel initialization now performs device detection, driver initialization, mounts the root filesystem and loads init which reads inittab and runs rc.sysinit. All the output from the kernel initialization can be viewed using /var/log/dmesg. If special drivers are required for boot and have been compiled as modules they must be included in the initrd image so that the kernel can mount the RAM disk to make the required modules available. For example, boot from SAN requires hba drivers to access the /boot filesytem on the SAN. Once the init process is loaded kernel passes control to it and the OS is now accessible.

To define the default runlevel for the system to boot edit the /etc/inittab file as follows:

change the line id:5:initdefault: substituting the desired run-level
2 multiuser no NFS
3 multiuser text, most common for servers
4 undefined
5 graphical login

To shutdown system: init 0

To reboot system: init 6

Single User Mode: s, S, or single

Emergency: bypasses rc.sysinit and prompts for root pw

Show current and recent run-levels: /sbin/runlevel

/etc/rc.d/rc.sysinit performs the following tasks - see the script for detailed info:

Activates udev (devices created dynamically) and selinux (security)
reads /etc/sysctl.conf for kernel parameters
sets the clock
loads keymaps
enables swap partitions
sets hostname
checks and remounts root filesystem rw
activates any raid or lvm devices that are present
enables disk storage quotas
checks and mounts any other local filesystems
cleans up locks and PID files

/etc/rc.d/rc defines services that should start based upon the default runlevel defined in /etc/inittab. If runlevel 3 is the default then rc will use /etc/rc.d/rc3.d/ directory as the basis for starting services. The service startup scripts are located in /etc/rc.d/init.d and each respective runlevel directory contains symbolic links to the files in the /etc/init.d directory beginning with S (Start) or K (Kill-Stop) and a number which identifies the Start/Kill order.

Required entries to create an init script:

First copy an existing init script in /etc/init.d as a baseline and edit the following
# chkconfig: - 85 15 # Runlevel (- for default) StartNumber KillNumber
Manage a lock file with (example):
stop() {
echo -n $"Stopping $prog: "
killproc $proc
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
and verify the file contains:
# Source function library.
. /etc/rc.d/init.d/functions

/etc/rc.d/rc.local can be used for customizations that do not require a start and stop command but this is not recommended.

The xinetd service acts as a traffic cop for less used services. Xinetd provides host based access, logging, time based access, and address redirection. All port definitions are located in /etc/services and the default config file is in /etc/xinetd.conf which can be overridden by service specific files in the /etc/xinetd.d/ directory. See man 5 xinetd.conf for more detail.

chkconfig is a command line utility for managing services and acts similar to service for xinetd services - most commonly used
system-config-services is a graphical utility for managing services
ntsysv is an ncurses based utility
service command line to to start,stop,restart,reload,status init script services located in /etc/init.d

To change runlevel from the grub bootloader:

  1. Reboot the server and hit the space bar when the GRUB screen appears
  2. Use arrow key to highlight the desired boot kernel and press the e key to edit the grub.conf
  3. Use arrow key to highlight the kernel line and press the e key to edit the line
  4. Hit the space bar and enter the desired runlevel 2,3,5 or s and hit Enter
  5. Press the b key to boot using the new boot option

No comments: