Linux startup process
||This article's lead section may not adequately summarize key points of its contents. (October 2011)|
Overview of typical process
In Linux, the flow of control during a boot is from BIOS, to boot loader, to kernel. The kernel then starts the scheduler (to allow multi-tasking) and runs the first userland (i.e. outside kernel space) program Init (which is mostly responsible to run startup scripts for each runlevel), at which point the kernel goes idle unless called externally.
init (short for initialization) is a program for Unix-based computer operating systems that spawns all other processes. It runs as a daemon and typically has PID 1. The boot loader starts the kernel and the kernel starts init. If one were to delete init without a replacement, the system would encounter a kernel panic on the next reboot.
- The BIOS performs hardware-platform specific startup tasks
- Once the hardware is recognized and started correctly, the BIOS loads and executes the partition boot code from the designated boot device, which contains phase 1 of a Linux boot loader. Phase 1 loads phase 2 (the bulk of the boot loader code). Some loaders may use an intermediate phase (known as phase 1.5) to achieve this since modern large disks may not be fully readable without further code.
- The boot loader often presents the user with a menu of possible boot options. It then loads the operating system, which decompresses into memory, and sets up system functions such as essential hardware and memory paging, before calling
start_kernel()then performs the majority of system setup (interrupts, the rest of memory management, device initialization, drivers, etc.) before spawning separately, the idle process and scheduler, and the Init process (which is executed in user space).
- The Init process executes scripts as needed that set up all non-operating system services and structures in order to allow a user environment to be created, and then presents the user with a login screen.
On shutdown, Init is called to close down all user space functionality in a controlled manner, again via scripted directions, following which Init terminates and the Kernel executes its own shutdown.
Early user space
Early user space has been available since version 2.5.46 of the Linux kernel, with the intent to replace as many functions as possible that previously the kernel would have performed during the start-up process. Typical uses of early user space are to detect what device drivers are needed to load the main user space file system and load them from a temporary filesystem.
Boot loader phase
The boot loader phase varies by platform. Since the earlier phases are not specific to the OS, the boot process is considered to start, for x86 or x86-64, when the partition boot sector code is executed in real mode and loads the first stage boot loader (typically a part of LILO or GRUB). From that point, the boot process continues as follows:
The first stage boot loader (in the MBR or the volume boot record) loads the remainder of the boot loader, which typically gives a prompt asking which operating system (or type of session) the user wishes to initialize. LILO and GRUB differ in some ways:
- Under LILO, the map installer,
lilo, reads the configuration file
/etc/lilo.confto identify the available bootable systems (it is executed from a running Linux system). The configuration file can include data such as boot partition and kernel pathname for each, as well as customized options if needed. To be precise,
/etc/lilo.confis prior-parsed[clarification needed] and used to create fixed-offset information saved in the boot sector and the map file, which will be used at the next boot. This information is discovered by asking the Linux kernel, at map installer time, where (e.g. on which disk sectors) the object of interest (such as an initrd, a kernel image file, or the like) is stored. At boot time, the default or selected operating system is loaded into RAM, a minimal initial file system is possibly set up in RAM from an image file ("initrd"), and along with the appropriate parameters, control is passed to the newly loaded kernel. LILO does not "understand" file systems, so it uses raw disk offsets and the BIOS to load any needed code or data based on data in the boot sector and map file. It cannot "find"
/etc/lilo.confat boot time because it does not understand file systems; instead it searches fixed locations on the disk memorized the last time the LILO map installer (lilo) was run to generate new offsets in the boot sector and the map file image. Boot time LILO logic loads the menu code, and then, depending on the
lilo.confdirectives used to make the map file, along with any user interaction, loads either the boot sector for another system such as Microsoft Windows, or the kernel image for Linux.
- GRUB by contrast does have understanding of the common ext2, ext3 and ext4 file systems. Because GRUB stores its data in a configuration file rather than the MBR and contains a command-line interface, it is often easier to rectify or modify GRUB if misconfigured or corrupt.
- The first stage loader (stage1) is loaded and executed either by the BIOS from the Master boot record (MBR) or by another boot loader from the partition boot sector.
- If necessary, an intermediate stage loader (stage1.5) is loaded and executed by the first stage loader. This may be necessary if the second stage loader is not contiguous, or if the filesystem or hardware requires special handling in order to access the second stage loader.
- The second stage loader (stage2) is then loaded and executed. This displays the GRUB startup menu, which allows the user to choose an operating system or to examine and edit startup parameters.
- After an operating system is chosen, it is loaded and executed.
LILO, the older of the two boot loaders, is almost identical to GRUB in process, except that its command line interface allows only selection of options previously recorded in the boot sector and map file. Thus all changes must be made to its configuration and written to the boot sector and map file, and then the system restarted. An error in configuration can therefore leave a disk unable to be booted without use of a separate boot device (floppy disk etc.) containing a program capable of fixing this. Additionally, it does not understand filesystems. Instead, locations of image files are stored within the boot sector and map file directly and the BIOS is used to access them directly.
Yet another way to boot Linux is from DOS or Windows 9x, where the Linux kernel completely replaces the running copy of this operating system. This can be useful in the case of hardware which needs to be switched on via software and for which such configuration programs are only available for DOS, whereas not for Linux, those being proprietary to the manufacturer and kept an industry secret. This tedious booting method is less necessary nowadays, as Linux has drivers for a multitude of hardware devices, but it has seen some use in mobile devices.
Another case is when the Linux is located on a storage device which is not available to the BIOS for booting: DOS or Windows can load the appropriate drivers to make up for the BIOS limitation, and boot Linux from there.
The kernel in Linux handles all operating system processes, such as memory management, task scheduling, I/O, interprocess communication, and overall system control. This is loaded in two stages - in the first stage the kernel (as a compressed image file) is loaded into memory and decompressed, and a few fundamental functions such as basic memory management are set up. Control is then switched one final time to the main kernel start process. Once the kernel is fully operational – and as part of its startup, upon being loaded and executing – the kernel looks for an init process to run, which (separately) sets up a user space and the processes needed for a user environment and ultimate login. The kernel itself is then allowed to go idle, subject to calls from other processes.
Kernel loading stage
The kernel as loaded is typically an image file, compressed into either zImage or bzImage formats with zlib. A routine at the head of it does a minimal amount of hardware setup, decompresses the image fully into high memory, and takes note of any RAM disk if configured. It then executes kernel startup via
./arch/i386/boot/head and the
startup_32 () (for x86 based processors) process.
Kernel startup stage
The startup function for the kernel (also called the swapper or process 0) establishes memory management (paging tables and memory paging), detects the type of CPU and any additional functionality such as floating point capabilities, and then switches to non-architecture specific Linux kernel functionality via a call to
start_kernel executes a wide range of initialization functions. It sets up interrupt handling (IRQs), further configures memory, starts the Init process (the first user-space process), and then starts the idle task via
cpu_idle(). Notably, the kernel startup process also mounts the initial RAM disk ("initrd") that was loaded previously as the temporary root file system during the boot phase. The initrd allows driver modules to be loaded directly from memory, without reliance upon other devices (e.g. a hard disk) and the drivers that are needed to access them (e.g. an SATA driver). This split of some drivers statically compiled into the kernel and other drivers loaded from initrd allows for a smaller kernel. The root file system is later switched via a call to
pivot_root() which unmounts the temporary root file system and replaces it with the use of the real one, once the latter is accessible. The memory used by the temporary root file system is then reclaimed.
Thus, the kernel initializes devices, mounts the root filesystem specified by the boot loader as read only, and runs Init (
/sbin/init) which is designated as the first process run by the system (PID = 1). A message is printed by the kernel upon mounting the file system, and by Init upon starting the Init process. It may also optionally run Initrd[clarification needed] to allow setup and device related matters (RAM disk or similar) to be handled before the root file system is mounted.
- "When the kernel is loaded, it immediately initializes and configures the computer's memory and configures the various hardware attached to the system, including all processors, I/O subsystems, and storage devices. It then looks for the compressed initrd image in a predetermined location in memory, decompresses it, mounts it, and loads all necessary drivers. Next, it initializes virtual devices related to the file system, such as LVM or software RAID before unmounting the initrd disk image and freeing up all the memory the disk image once occupied. The kernel then creates a root device,[clarification needed] mounts the root partition read-only, and frees any unused memory. At this point, the kernel is loaded into memory and operational. However, since there are no user applications that allow meaningful input to the system, not much can be done with it." An initramfs-style boot is similar, but not identical to the described initrd boot.
At this point, with interrupts enabled, the scheduler can take control of the overall management of the system, to provide pre-emptive multi-tasking, and the init process is left to continue booting the user environment in user space.
Init is the father of all processes. Its primary role is to create processes from a script stored in the file
/etc/inittab. This file usually has entries which cause init to spawn gettys on each line that users can log in. It also controls autonomous processes required by any particular system. A run level is a software configuration of the system which allows only a selected group of processes to exist. The processes spawned by init for each of these run levels are defined in the /etc/inittab file.— manual page for Init
Init's job is "to get everything running the way it should be" once the kernel is fully running. Essentially it establishes and operates the entire user space. This includes checking and mounting file systems, starting up necessary user services, and ultimately switching to a user-environment when system startup is completed. It is similar to the Unix and BSD init processes, from which it derived, but in some cases has diverged or became customized. In a standard Linux system, Init is executed with a parameter, known as a runlevel, that takes a value from 1 to 6, and that determines which subsystems are to be made operational. Each runlevel has its own scripts which codify the various processes involved in setting up or leaving the given runlevel, and it is these scripts which are referenced as necessary in the boot process. Init scripts are typically held in directories with names such as
"/etc/rc...". The top level configuration file for init is at
During system boot, it checks whether a default runlevel is specified in /etc/inittab, and requests the runlevel to enter via the system console if not. It then proceeds to run all the relevant boot scripts for the given runlevel, including loading modules, checking the integrity of the root file system (which was mounted read-only) and then remounting it for full read-write access, and sets up the network.
After it has spawned all of the processes specified, init goes dormant, and waits for one of three events to happen:- processes it started to end or die, a power failure signal,[clarification needed] or a request via
/sbin/telinit to further change the runlevel.
This applies to SysV-style init.
- "Initramfs arrives". Retrieved 14 November 2011.
- http://oldfield.wattle.id.au/luv/boot.html Linux Boot Process - by Kim Oldfield (2001)
- IBM description of Linux boot process
- From Power Up To Bash Prompt: Init