Appearance
2.1.3 — How a Computer Boots
You press the power button on the server that runs our photo-upload service. About ten seconds later, the service is running and accepting photos. In between, something strange has happened. The kernel was a file sitting on a disk. To load a file from a disk, you need a file system. To have a file system, you need a kernel. So how did the machine get the kernel running without already having a kernel?
The answer is a chain of small programs. Each one is just clever enough to find and start the next, slightly cleverer one. That chain is called booting. The word comes from bootstrapping, from the old joke about lifting yourself off the ground by pulling on your own boot straps. It is a fair picture of a machine that has to load the software it needs to load software.
1. The whole chain in one picture
Here are the stages first, so the details have somewhere to hang.
Notice the pattern before reading the details. The firmware cannot read your files. The bootloader can read them, but only well enough to find one file, the kernel. The kernel can do everything, but it does not start your applications itself. It starts one program, and that program starts the rest. Each stage is small and limited, and its only real skill is launching the next stage.
2. Stage 1: the firmware
When power reaches the CPU, it has no program and no memory contents. RAM is empty, because RAM forgets everything when the power goes off. So the CPU is designed to start running instructions from one fixed address. That address does not point into RAM. It points into a small memory chip on the motherboard that keeps its contents without power. The program stored on that chip is the firmware.
The name means software that is "firm": it is not part of the hardware, but it is not changed often either. It is written for one specific motherboard and comes from the motherboard's maker.
The firmware does three jobs, in this order.
- Check the hardware. This is the POST, the power-on self-test. The firmware checks that RAM is present and working, that a keyboard and a screen are there, and that the CPU is sane. If something basic is missing, it cannot show a message on a screen, so it often beeps a pattern instead. Different beep patterns mean different faults.
- Set up the basic hardware. It switches on the memory controller, finds the disks, and finds the other devices plugged into the motherboard.
- Find something to boot. It looks through the disks, in an order you can change in its settings menu, for a bootloader.
BIOS and UEFI: the old firmware and the new
There are two kinds of PC firmware, and the difference explains settings you have probably seen.
BIOS, the Basic Input/Output System, is the original. It dates from the first IBM PC in 1981. It is very limited. It starts the CPU in an old 16-bit mode from the 1970s. It finds a bootloader by reading the very first sector of a disk, 512 bytes, and running whatever code is there. That first sector is called the Master Boot Record, or MBR. Only about 440 of its 512 bytes can hold code, which is far too little to do anything clever. So the code in the MBR does one thing: it loads a bigger second piece of code from elsewhere on the disk, and runs that.
The MBR also holds the disk's partition table, the list of the regions the disk is divided into. The MBR format allows only four main partitions and disks no bigger than 2 TB. In 1981, nobody imagined a 2 TB disk.
UEFI, the Unified Extensible Firmware Interface, replaced BIOS on almost every PC sold since about 2012. It is much more capable:
- It runs the CPU in a modern 32-bit or 64-bit mode from the start.
- It can read a simple file system itself. It does not run a raw 512-byte sector. It reads ordinary files from a small partition called the EFI System Partition (ESP), which is formatted with the FAT32 file system (2.6.3 explains FAT32). The bootloader is just a file there, such as
\EFI\ubuntu\grubx64.efior\EFI\Microsoft\Boot\bootmgfw.efi. - It uses a new partition table called GPT, the GUID Partition Table. GPT allows 128 partitions by default and disks far beyond any size made today.
- It can check that the bootloader is genuine before running it. That is Secure Boot, in section 3.
| BIOS | UEFI | |
|---|---|---|
| Since | 1981 | about 2012 on PCs |
| Finds the bootloader by | running the first 512 bytes | reading a file from the ESP |
| Partition table | MBR | GPT |
| Largest disk | 2 TB | effectively unlimited |
| Checks the bootloader | no | yes, with Secure Boot |
Many UEFI machines can pretend to be a BIOS for very old operating systems. That pretending is called a compatibility mode, or CSM. When an install guide tells you to switch between "Legacy" and "UEFI" boot, this is the switch it means.
3. Secure Boot: making sure the first program is genuine
The boot chain has a weak point. The firmware runs whatever bootloader it finds, and the bootloader runs whatever kernel it finds. If an attacker replaces the bootloader on the disk with their own, their code runs before the kernel even exists. Nothing you install later, such as antivirus software, can see it, because it is running underneath the kernel that the antivirus depends on. This kind of attack is called a bootkit.
Secure Boot closes that gap with digital signatures. A digital signature is a short block of data that only the holder of a secret key can produce, and anyone can check it using the matching public key. 8.2.3 explains how that works. Here is how Secure Boot uses it.
- The firmware comes from the factory holding a short list of public keys it trusts. On most PCs these include Microsoft's key.
- Before running a bootloader, the firmware checks its signature against that list. A bootloader that has been changed, even by one byte, fails the check, and the firmware refuses to run it.
- The signed bootloader then checks the signature of the kernel before running it.
Each stage checks the next before handing over control. This is called a chain of trust. Each link is only as trustworthy as the one before it, and the first link is the firmware chip itself.
Linux distributions fit into this with a small extra program called a shim. Microsoft signs the shim. The shim carries the distribution's own key, and it checks the distribution's bootloader and kernel with that key. This is why Ubuntu boots on a Secure Boot PC without you turning anything off.
4. Stage 2: the bootloader
The firmware has found a bootloader and handed over control. The bootloader has exactly one job: find the kernel on the disk, load it into memory, and jump to it.
Why is this a separate stage at all? Why can't the firmware load the kernel itself? Three reasons:
- The firmware knows only simple file systems. UEFI can read FAT32, but the kernel usually lives on a partition formatted with a richer file system, such as ext4 on Linux. The bootloader understands those file systems.
- The firmware does not know about operating systems. It does not know that one partition holds Windows and another holds Linux, or that Linux needs certain settings passed to it. The bootloader does.
- People want a choice. If you have two operating systems on one machine, something has to show you a menu and let you pick. That menu is the bootloader.
On Linux the common bootloader is GRUB, the GRand Unified Bootloader. On Windows it is the Windows Boot Manager. On Macs it is Apple's own firmware and boot process.
GRUB loads two files into memory, not one:
- The kernel itself, in a file usually named
/boot/vmlinuz-.... Thezat the end tells you the file is compressed, to make it smaller and faster to read from disk. - A small starter file system called the initramfs, the initial RAM file system, in a file such as
/boot/initrd.img-.... Section 5 explains why this is needed.
It also passes the kernel a line of settings, called the kernel command line. The most important setting says which disk partition holds the real root file system. A typical one looks like root=UUID=3f2a… ro quiet, meaning "the root file system is on the partition with this unique ID, start by mounting it read-only, and print fewer messages".
Then GRUB jumps to the start of the kernel, and its job is done. It never runs again until the next boot.
5. Stage 3: the kernel starts itself
Now the kernel is in memory and running, in kernel mode, with the whole machine to itself. It has to turn a bare machine into the shared, protected system 2.1.1 described. It works through a long list. These are the steps that matter most:
- Unpack itself. The kernel file was compressed, so a small piece of code at its start decompresses the rest into memory.
- Set up memory management. It builds the tables that give each program its own private memory (2.5.1) and switches the CPU into using them.
- Set up the ways into the kernel. It writes the address of its system call entry point into the CPU's special register, the step 2.1.2 said happened "at boot". It also sets up the table that says where to go for each kind of exception and interrupt.
- Start the scheduler and the timer. From now on, the timer interrupt will tap the CPU many times a second, and the kernel can share the CPU between programs.
- Wake the other CPU cores. The machine starts with only one core running. The kernel now starts the others and gives each one its own scheduler.
- Find the hardware and load drivers. It finds the disks, the network cards and everything else, and starts a driver for each.
- Mount the root file system. It attaches the real disk partition named on the command line at
/, the top of the file tree. From this moment, every file on the system can be found.
Why the initramfs exists
Step 7 hides a chicken-and-egg problem. To mount the root file system, the kernel needs the driver for the disk it lives on. But kernel drivers are often stored as files on the root file system, as loadable modules (2.1.1). So to read the disk, the kernel needs a driver that is stored on the disk.
The same trap appears in other forms. The root disk might be encrypted, so the kernel must first ask you for a password. It might be spread across several disks in a RAID array, or reached over a network. Each of those needs extra programs before the real root can be mounted.
The initramfs breaks the loop. It is a tiny file system that the bootloader loaded into RAM alongside the kernel. It contains just the drivers and small programs needed to reach the real root disk. The kernel mounts the initramfs first, as a temporary /. Programs inside it load the right disk driver, unlock the encryption if needed, and mount the real root file system. Then the kernel swaps the real root in place of the temporary one, and frees the memory the initramfs used.
6. Stage 4: the first program, PID 1
The kernel is now fully running, but it has not started a single ordinary program. Its last step is to start exactly one, in user mode. It is the first process, so it gets process ID 1, and it is always called PID 1. Historically this program was called init, short for initialisation.
PID 1 is the ancestor of every other program on the machine. Every program that ever runs is started by PID 1, or by a program that PID 1 started, or by one of their children, and so on. 2.2.2 explains how one program starts another, and why PID 1 also has to clean up after programs whose parents have died.
On most Linux systems today, PID 1 is systemd. Its job is to bring the system up to the state you asked for:
- mount the remaining file systems;
- start the network;
- start background services, such as the program that accepts remote logins and the program that keeps the clock right;
- start your applications, including our photo-upload service;
- and show a login screen, if the machine has one.
Each of these is described by a small text file called a unit. A unit says what to run and what it depends on. For example, it might say "the photo-upload service needs the network to be up first". systemd reads all the units, works out the order, and starts as many as it can at the same time, which makes booting much faster than starting them one by one. 2.8.4 shows how to write a unit for the upload service.
Seeing where the time went
On a Linux machine with systemd, one command shows how long each stage took. Here is the kind of output it gives (the numbers are illustrative):
text
$ systemd-analyze
Startup finished in 3.112s (firmware) + 1.884s (loader) + 1.402s (kernel) + 4.215s (userspace) = 10.613sEvery stage from the figure is there. Firmware is stage 1, the hardware checks. Loader is stage 2, GRUB. Kernel is stage 3. Userspace is stage 4, systemd starting everything. On servers, the firmware stage is often the slowest, because server firmware checks large amounts of memory and many devices. A second command, systemd-analyze blame, lists every service with the time it took to start, which is how you find the one slowing the boot down.
7. When the boot fails, which stage failed?
Knowing the chain turns a frightening black screen into a short list of suspects. The message on the screen usually tells you which stage stopped.
| What you see | Stage that failed | Likely cause |
|---|---|---|
| beeps, no picture | firmware | RAM or graphics card |
| "no bootable device" | firmware | disk missing, or wrong boot order |
a grub> prompt | bootloader | GRUB cannot find its settings |
| "Secure Boot violation" | firmware | unsigned bootloader or kernel |
| kernel panic, "unable to mount root" | kernel | wrong root=, missing driver in initramfs |
| boots, but a service is missing | PID 1 | that service's unit failed |
The table follows the chain from left to right in time. A beep means the firmware could not even get going. A grub> prompt means the firmware worked and found GRUB, but GRUB could not find its menu. "Unable to mount root" means the kernel ran fine but step 7 of section 5 failed, often because the initramfs is missing the right disk driver. And if the machine boots but our upload service is not running, the problem is in stage 4, and systemctl status will show its error.
What the interviewer will push on
"What happens when you turn on a computer?" This is a classic opening question, and they are checking whether you can tell it as a chain with a reason for each link. A good answer names the four stages and says why each exists: the firmware lives on a chip because RAM is empty at power-on, the bootloader exists because the firmware cannot read the kernel's file system, and PID 1 exists because the kernel starts only one program. The common weak answer lists stages without the reasons.
"What is the difference between BIOS and UEFI?" They want the practical differences. BIOS runs the first 512 bytes of the disk, uses MBR with a 2 TB limit, and cannot check signatures. UEFI reads a bootloader file from the EFI System Partition, uses GPT, and supports Secure Boot. The tell is knowing that UEFI understands FAT32, which is why the ESP is formatted that way.
"What is PID 1, and why is it special?" They are checking whether you know it is the first user-mode program and the ancestor of all others. The strong answer adds its duty of collecting finished children whose parents died, and the container trap that follows from it, which 2.2.3 covers.
"Why does Linux need an initramfs?" This separates people who have fixed a broken boot from people who have read about one. The answer is the chicken-and-egg problem: the driver needed to read the root disk may be stored on the root disk, or the disk may be encrypted or spread across several drives.
Something to volunteer that nobody asks for: Secure Boot is a chain of trust, and it is only as strong as its first link, the firmware. This is why firmware updates matter for security, and why a bootkit is so dangerous: it runs before the kernel that every other defence relies on.
Recall
- Booting is a chain where each small stage loads and starts the next: firmware → bootloader → kernel → PID 1.
- The CPU starts at a fixed address on a chip because RAM is empty at power-on. The firmware runs the POST, sets up basic hardware, and finds a bootloader.
- BIOS runs the 512-byte Master Boot Record and is limited to 2 TB disks. UEFI reads a bootloader file from the FAT32 EFI System Partition, uses GPT, and supports Secure Boot, a chain of trust in which each stage checks the next stage's signature.
- The bootloader (GRUB) loads the compressed kernel and the initramfs, a tiny RAM file system that holds what the kernel needs to reach the real root disk.
- The kernel sets up memory, the system call entry, the scheduler and drivers, mounts root, and starts PID 1, today usually systemd, which starts every service including our upload service.
Self-test: Why can the CPU not start by running code from RAM? What can UEFI do that BIOS cannot? What attack does Secure Boot stop, and why can antivirus not stop it? Why does the kernel need an initramfs? A server shows "unable to mount root". Which stage failed, and what would you check?
Quiz Bank
FoundationalWhat are the stages of booting a computer, in order?
- Firmware (UEFI or BIOS), stored on a chip on the motherboard, runs first. It checks the hardware with the power-on self-test and finds a bootloader.
- The bootloader, such as GRUB, loads the kernel and the initramfs into memory and passes the kernel its settings.
- The kernel unpacks itself, sets up memory management, the system call entry point, the scheduler and the drivers, then mounts the root file system.
- PID 1, usually systemd, is the first ordinary program. It starts every service, the network and the login screen.
Each stage is limited and exists only to start the next.
FoundationalWhy does the CPU start by running firmware from a chip instead of a program in RAM?
Because RAM loses its contents when the power is off, so at power-on it holds nothing. The CPU is built to start running instructions from one fixed address, and that address points into a small chip on the motherboard that keeps its contents without power. The program stored there is the firmware. Its job is to get the machine far enough along to load the next stage from a disk into RAM.
AppliedWhat are the practical differences between BIOS and UEFI?
BIOS dates from 1981. It starts the CPU in an old 16-bit mode. It runs whatever code is in the first 512-byte sector of the disk, the Master Boot Record. It uses the MBR partition table, with at most four main partitions and a 2 TB size limit. And it cannot check that the boot code is genuine.
UEFI replaced it on PCs from about 2012. It starts in a modern 32-bit or 64-bit mode, can read the FAT32 file system itself, and loads the bootloader as an ordinary file from the EFI System Partition. It uses the GPT partition table, which has no practical size limit, and it supports Secure Boot, which checks the bootloader's signature before running it.
InterviewWhat is PID 1 and why does it matter?
PID 1 is the first ordinary program the kernel starts at the end of booting, so it gets process ID 1. Historically it was called init, and on most Linux systems today it is systemd. Every other program on the machine descends from it.
It matters for two reasons. First, it brings the system up: it starts the services, the network and the applications in the right order. Second, it adopts any program whose parent has died and collects it when it finishes, so dead programs do not pile up. In a container, whatever program you start becomes PID 1 inside the container, and if it does not do that collecting job, finished programs can pile up there. 2.2.3 covers that trap.
InterviewWhat problem does the initramfs solve?
A chicken-and-egg problem. To mount the root file system, the kernel needs the driver for the disk it is on, but drivers are often stored as module files on that root file system. The root disk might also be encrypted, or spread across several disks, or reached over the network, and each case needs extra programs before it can be mounted.
The initramfs is a tiny file system the bootloader loads into RAM next to the kernel. It holds exactly the drivers and small programs needed to reach the real root. The kernel mounts it first as a temporary root, its programs load the drivers and unlock or assemble the disk, and then the kernel swaps in the real root and frees the initramfs.
StaffExplain Secure Boot as a chain of trust, and name where the chain can still be broken.
Each stage checks the next stage's digital signature before handing over control. The firmware holds a list of trusted public keys from the factory and refuses any bootloader whose signature does not match. The signed bootloader, or on Linux a Microsoft-signed shim carrying the distribution's key, then checks the kernel. So a changed bootloader or kernel never runs.
The chain is only as strong as its links. If the firmware itself is compromised, every later check is worthless, which is why firmware updates matter. If a signed bootloader has a bug, an attacker can use the genuine signed file to run unsigned code, which is why trusted keys and bad bootloaders sometimes have to be revoked. And Secure Boot stops at the kernel. Once the kernel runs, protecting the rest of the system is the kernel's job.
Scenario Drill
DrillAfter a kernel update, a Linux server that runs the photo-upload service stops at a message saying it is unable to mount root. Using this page, explain what probably went wrong and how you would recover.
"Unable to mount root" means the firmware and the bootloader both worked, because the kernel is running and printing messages. The failure is in stage 3, at the moment the kernel tries to attach the real root file system.
The likely causes, in order:
- The new initramfs is missing the disk driver, or was not rebuilt for the new kernel. Without that driver, the kernel cannot see the disk at all.
- The kernel command line names the wrong partition, for example a wrong
root=UUID=…after a disk change. - The root disk is encrypted or in a RAID array, and the tools to unlock or assemble it are missing from the initramfs.
To recover, reboot and pick the previous kernel from the GRUB menu. Distributions keep the last one or two kernels for exactly this reason. Once the server is running on the old kernel, rebuild the initramfs for the new kernel, check that it includes the right disk driver, and check the root= setting. Then reboot into the new kernel again.
The chain is what makes this quick: the message tells you which stage failed, and you only have to look at that one stage.
Next: the kernel has started PID 1, and PID 1 has started our photo-upload service. But what is a running program, exactly, from the kernel's point of view? Page 2.2.1 defines the process: what the kernel keeps about each one, what its memory looks like, and how the kernel pauses one program and resumes another many times a second.