Booting - Linux Administration (2016)

Linux Administration (2016)

Booting

As a system administrator you need to understand the Linux boot process. In this chapter, you will learn about the BIOS, the boot loader, the Linux kernel and runlevels.

The BIOS

BIOS stands for Basic Input/Output System. It’s a special type of firmware used in the booting process and it’s the first piece of software that is run when a computer is powered on. The BIOS is operating system independent. Its primary purpose is to test the underlying hardware components and to load a boot loader or operating system.

The BIOS performs a POST, which stands for Power-On Self Test. The POST performs some basic checks of various hardware components such as the CPU, memory, and storage devices. Only if the POST succeeds will the BIOS attempt to load the boot loader.

The BIOS contains a list of boot devices such as hard disks, a DVD drive, USB devices, and others depending on the hardware being used. The BIOS searches that list for a bootable device in the order specified. You can change this order by interrupting the boot sequence and entering into the configuration for the BIOS. The key combination used to do this will vary from one hardware manufacturer to another.

Once a bootable device has been found, the BIOS will run the boot loader. Typically the GRUB boot loader will be used, but you may run into older Linux systems that use the LILO boot loader. LILO stands for LInux LOader, while GRUB stands for GRand Unified Bootloader. In any case, the primary purpose of the boot loader is to start the operating system. You will typically see a message or series of messages from the boot loader which will allow you to interrupt the boot process and interact with the boot loader. If there are multiple operating systems installed, you can tell the boot loader which operating system to run. You can also instruct the boot loader to pass different boot options to the operating system.

The Initial RAM Disk

The initial RAM disk, abbreviated as “initrd,” is a temporary file system that’s loaded into memory when the system boots. This file system can contain helpers that perform hardware detection and load the necessary modules, sometimes called drivers, to get the actual file system mounted. For example, if the root file filesystem is stored on an LVM (Logical Volume Manager) volume, the initrd image will contain the kernel modules required to mount that logical volume as the root file system. Once the initrd mounts the actual root file system its job is done and the operating system continues loading from the real root file system.

Kernel and Initial RAM Disk Location

The Linux kernel, the initial RAM disk, and other files needed to boot the operating system are stored in /boot. Here is a listing of the /boot directory for an Ubuntu system.

$ ls -1F /boot

abi-3.13.0-46-generic

config-3.13.0-46-generic

grub/

initrd.img-3.13.0-46-generic

System.map-3.13.0-46-generic

vmlinuz-3.13.0-46-generic

The Linux kernel is typically named vmlinux or vmlinuz. If the kernel is compressed its name is in the vmlinuz format. In this example, the kernel is vmlinuz-3.13.0-46-generic. The initial RAM disk in this example is initrd.img-3.13.0-46-generic.

The Kernel Ring Buffer

The kernel ring buffer contains messages related to the Linux kernel. A ring buffer is a data structure that is always the same size. Once the buffer is completely full, old messages are discarded when new messages arrive. To see the contents of the kernel ring buffer, use the dmesgcommand.

$ dmesg

[ 0.000000] Initializing cgroup subsys cpuset

[ 0.000000] Initializing cgroup subsys cpu

[ 0.000000] Initializing cgroup subsys cpuacct

[ 0.000000] Linux version 3.13.0-46-generic (buildd@orlo) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #79-Ubuntu SMP Tue Mar 10 20:06:50 UTC 2015 (Ubuntu 3.13.0-46.79-generic 3.13.11-ckt15)

[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-3.13.0-46-generic root=UUID=736134da-c6b8-4a97-911c-650146a68b3c ro console=tty1 console=ttyS0

[ 0.000000] KERNEL supported cpus:

[ 0.000000] Intel GenuineIntel

[ 0.000000] AMD AuthenticAMD

[ 0.000000] Centaur CentaurHauls

[ 0.000000] e820: BIOS-provided physical RAM map:

...

On most Linux distributions these messages are also stored on disk in the /var/log/dmesg file. Between the dmesg command and the /var/log/dmesg log file, you will be able to see the messages the kernel is generating, even during the earliest stages of the boot process when those messages can quickly fly by your screen.

Runlevels and Targets

Linux uses runlevels to determine what processes and services to start. Each distribution can be configured differently, but, in general, runlevel 0 is used to power off a system, runlevel 1 is single-user mode, runlevels 2-5 are for normal system operations, and runlevel 6 is used to reboot a system.

Runlevel Description

0 Shuts down the system

1, S, s Single user mode. Used for maintenance.

2 Multi-user mode with graphical interface.

(Debian/Ubuntu)

3 Multi-user text mode (RedHat/CentOS)

4 Undefined

5 Multi-user mode with graphical interface.

(RedHat/CentOS)

6 Reboot

Historically, runlevels were controlled by the init program. The init configuration was stored in /etc/inittab. To change the default runlevel using init, you would edit the /etc/inittab file and set the runlevel number on the initdefault line. Here is an example of setting runlevel 3 to be the default runlevel.

id:3:initdefault:

However, init alternatives such as systemd and upstart are quickly taking the place of init with systemd currently being the most widely adopted replacement.

Instead of runlevels, systemd has the concepts of targets. These targets are roughly equivalent to runlevels. To see a list of available targets, look in /lib/systemd/system. You’ll notice the runlevel targets are actually symlinks to the real targets being used. For example, runlevel5.target is a symlink to graphical.target.

# cd /lib/systemd/system

# ls -l runlevel*target

lrwxrwxrwx. 1 root root 15 Jul 17 2014 runlevel0.target -> poweroff.target

lrwxrwxrwx. 1 root root 13 Jul 17 2014 runlevel1.target -> rescue.target

lrwxrwxrwx. 1 root root 17 Jul 17 2014 runlevel2.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 Jul 17 2014 runlevel3.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 Jul 17 2014 runlevel4.target -> multi-user.target

lrwxrwxrwx. 1 root root 16 Jul 17 2014 runlevel5.target -> graphical.target

lrwxrwxrwx. 1 root root 13 Jul 17 2014 runlevel6.target -> reboot.target

To change the default runlevel, or target, with systemd, use the systemctl command followed by set-default and finally the desired target. Optionally, you can manually create a symlink to the desired target from the /etc/systemd/system/default.target file. This example sets the default target to be graphical, which is equivalent to runlevel 5.

# set-default multi-user.target

rm '/etc/systemd/system/default.target'

ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/default.target'

#

Changing Runlevels or Targets

With the init system, you can change runlevels using the telinit command. Simply supply the runlevel you want to change to as an argument to the telinit command.

# telinit 5

To change the target, the runlevel equivalent for systemd, use the systemctl command followed by isolate and finally the desired target. Here is how to change to the graphical target.

systemctl isolate graphical.target

Rebooting a System

Even though there is a runlevel/target if you’re using systemd, for rebooting you can also use the reboot or shutdown commands.

Here’s how to reboot with init.

# telinit 6

To reboot using systemd use the systemctl command.

# systemctl isolate reboot.target

To reboot using the reboot command simply execute reboot.

# reboot

The format of the shutdown command is as follows.

shutdown [options] time [message]

The option to tell shutdown to perform a reboot is -r. You can specify the time to shutdown using the “HH:MM” format. You can also use +N where N represents the number of minutes to wait before performing the action. Finally, you can use the now keyword to start immediately. Optionally, you may specify a message that will be broadcast to all users logged into the system.

# shutdown -r now

Powering Off a System

To power off a system, use runlevel 0, the poweroff target, or the poweroff command.

Here is how to issue a power-off with init.

# telinit 0

Here is how to power off a system with systemctl.

# systemctl isolate poweroff.target

Finally, you can use the poweroff command.

# poweroff

Summary

In this chapter, you learned about the Linux boot process. You learned that the job of the BIOS is to perform basic hardware checks and to start the boot loader from a bootable device. You also learned about the two most commonly used bootloaders, LILO and Grub. The primary job of any boot loader is to start the operating system.

You also learned that the files required to boot a Linux system are stored in the /boot directory. The initial ram disk, or initrd, is a tempory file system that is loaded into memory. Its main job is to mount the file system where the operating system is stored. The Linux kernel is typically named vmlinux, or, if it is compressed, vmlinuz.

You learned how to view the messages in the kernel ring buffer by using the dmesg command or by examining the /var/log/dmesg file.

You also learned about run levels and how systemd has an equivalent concept known as targets. You learned how to change the runlevel with the telinit command for the traditional init system, and the systemctl command for systems the use system. Finally, you learned about the shutdown, reboot, and poweroff commands.

Quiz

1. The BIOS begins the computer's boot process and passes control to the boot loader.

1. True

2. False

2. Which of the following are Linux boot loaders?

1. LILO

2. GRUB

3. Both LILO and GRUB

4. BIOS

3. Which of the following is a temporary file system which is loaded into memory when the system boots.

1. kernel

2. vmlinux

3. vmlinuz

4. initrd

4. The /linux directory contains the files required to boot Linux.

1. True

2. False

5. Which command displays the contents of the kernel ring buffer?

1. ringbuf

2. ringb

3. rmesg

4. dmesg

Quiz Answers

1. A

2. C

3. D

4. B

5. D