Wednesday, March 25, 2009

Linux Startup Sequence

To trace the full startup sequence from hardware powerup to shell, we'll assume: x86 hardware, the boot manager is LILO installed in the MBR, the kernel is on the hard disk, the system normally starts in runlevel 3, the example user's shell is bash, and the system is not using X Windows. Other possibilities are indicated where appropriate.

Hardware
  1. When the computer power is turned on, a special circuit signals the RESET pin of the CPU.
  2. The CPU resets its registers and executes the code at a fixed address (0xFFFFFFF0), the starting point of the BIOS (Basic Input/Output System).
BIOS
  1. Executes the POST (Power-On Self-Test) and displays various banners on the monitor screen.
  2. Initializes the hardware devices and maps IRQs for PCI devices.
  3. Searches for an operating system to load (usually floppy disk then CD-ROM then hard disk).
  4. Copies first sector of operating system device into memory and begins execution.
LILO Bootloader
  1. Only part of LILO fits into the MBR (the first sector loaded by the BIOS) so its role is to load the rest of the bootloader code into memory and begin execution of that code. (If the system was booted from a floppy, a portion of the kernel itself is located in the first sector--enough to load the rest of the compressed kernel image into memory.)
  2. Displays a timed prompt for the user to choose a kernel to load.
  3. Loads the full kernel image into memory and begins execution at the setup() function entry point.
Kernel
  1. setup(): determines amount of RAM, initializes keyboard and mouse, initializes video adapter, looks up hard disk partitions, resets the FPU, maps the IRQ lines, switches the CPU from Real to Protected mode, and finally jumps to Part I of the startup_32() function.
  2. startup_32() Part I: decompresses the kernel, and jumps to the now uncompressed Part II of the startup_32() function.
  3. startup_32() Part II: initializes internal kernel tables, identifies the processor type, and then jumps to the start_kernel() function.
  4. start_kernel(): initializes the memory management tables, finalizes the interrupt tables, spawns the kernel threads (which are responsible for the dmesg messages as they complete the system initialization), and finally launches init as process ID (PID) 1.
init
  1. Reads the /etc/inittab configuration file.
  2. Sets the default runlevel.
  3. Runs the rc.sysinit script which: enables disk swapping, checks and mounts filesystems, and synchronizes the system time with the CMOS clock. The rc.sysinit script is responsible for the OK messages that appear during the startup before entering interactive/non-interactive mode.
  4. Runs the rc script, passing the runlevel as a parameter. The rc script in turn starts or stops all of the services in the approprate rcN.d directory in numerical order. Usually the rc.local script is run last. The rc script is responsible for the OK messages that appear during the startup after entering interactive/non-interactive mode.
  5. Installs the Ctrl+Alt+Del interrupt handler (usually a form of shutdown ).
  6. Starts the virtual terminals using a variant of the getty program which in turn run login to accept user logins.
login
  1. User logs in through one of the tty processes (virtual terminals).
  2. Authenticates the login and password against the /etc/passwd file.
  3. Runs the /etc/profile script to set global user settings.
  4. Sets the current working directory to the user's home directory (from /etc/passwd).
  5. Launches the user's preferred shell (from /etc/passwd).
bash
  1. Runs the .bash_profile script to set personalized shell settings. Typically .bash_profile in turn calls .bashrc for the bulk of the settings. On subsequent opening of subshells, only the .bashrc script is run.
  2. The command line prompt signals to the user that the shell is ready to accept commands.
 
Things You Should Know About Linux !!!