Terry : Fast boot using kexec

Fast reboot using kexec

Debian GNU/Linux and Ubuntu

Install kexec-tools

apt-get install kexec-tools

DESCRIPTION

kexec is a system call that enables you to load and boot into another kernel from the currently running kernel. kexec performs the function of the boot loader from within the kernel. The primary difference between a standard system boot and a kexec boot is that the hardware initialization normally performed by the BIOS or firmware (depending on architecture) is not performed during a kexec boot. This has the effect of reducing the time required for a reboot.

Make sure you have selected CONFIG_KEXEC=y when configuring the kernel. The CONFIG_KEXEC option enables the kexec system call.

How to use kexec

Using kexec consists of

(1) loading the kernel to be rebooted to into memory, and

(2) actually rebooting to the pre-loaded kernel.

To load a kernel, the syntax is as follows:

kexec -l kernel-image --append="command-line-options" --initrd=initrd-image

where kernel-image is the kernel file that you intend to reboot to.

Insert the command-line parameters that must be passed to the new kernel into command-line-options. Passing the exact contents of /proc/cmdline into command-line-options is the safest way to ensure that correct values are passed to the rebooting kernel. The optional initrd-image is the initrd image to be used during boot.

Note that on different distributions the contents of /proc/cmdline varies.

  1. Ubuntu Server
    root=UUID=8850aec2-d10a-43b4-8c86-10c8d350a12e ro quiet splash
  2. Ubuntu 9.10 fresh installation, with GRUB2 and LVM configuration
    BOOT_IMAGE=/vmlinuz-2.6.31-16-generic root=/dev/mapper/ubuntu-root ro quiet splash
  3. Ubuntu 11.04, with GRUB2 and LVM
    In /boot/grub/grub.cfg

    linux /vmlinuz-2.6.38-13-generic root=/dev/mapper/ubuntu-root ro quiet splash vt.handoff=7

    /proc/cmdline="root=/dev/mapper/ubuntu-root ro quiet splash vt.handoff=7"

  4. Debian GNU/Linux squeeze/sid with GRUB2
    root=/dev/sda1
  5. Arch Linux x86_64

It's also possible to invoke kexec without an option parameter. In that case, kexec loads the specified kernel and then invokes shutdown. If the shutdown scripts of your Linux distribution support kexec-based rebooting, they then call kexec -e just before actually rebooting the machine. That way, the machine does a clean shutdown including all shutdown scripts.

Benifits of kexec

Systems with high availability requirements and kernel developers who have to constantly reboot their systems will benefit most from kexec. Because kexec skips the most time-consuming parts of system reboot, namely the firmware stage, reboots are extremely quick and availability is increased.

Example

For example, if the kernel image you want to reboot to is /boot/vmlinuz, the contents of /proc/cmdline is root=/dev/sda1, and the path to the initrd is /boot/initrd, then you would use the following command to load the kernel:

kexec -l /boot/vmlinuz --append=root=/dev/sda1 --initrd=/boot/initrd.img

After this kernel is loaded, it can be booted to at any time using the command:

kexec -e

On Ubuntu Server

Assume that we installed the new 2.6.31-16-generic-pae kernel, currently running 2.6.31-15-generic-pae.

In a Terminal

kexec -l /vmlinuz --append=root=/dev/sda1 --initrd=/initrd.img

or

kexec -l /boot/vmlinuz-2.6.31-16-generic-pae --append=root=/dev/sda1 --initrd=/boot/initrd.img-2.6.31-16-generic-pae

Result
Run kexec from

  1. ssh client, it froze and nothing can be done.
  2. pseudo console, background turn blue and freezes.
  3. X - the Desktop Environment or Window Manager desktop freezes (background may turn green) and you can't do anything until you see gdm (kdm, xdm or slim) again.

Reboot sequence

  1. The current running system will be shutdown. This involves terminating running processes, shutting down system services, writing back cache buffers to disk, deactivating swap, unmounting file systems, and performing a hardware reset
  2. Reboot into the pre-loaded kernel, bootloader stage will be skipped and it goes directly to the kernel stage.
    In kernel stage, where the kernel takes control. It sets up the necessary data structures, probes the devices present on the system, loads the necessary device drivers, and initializes the devices. The last stage of the booting process involves user-level initialization. In this stage, the kernel checks the integrity of file systems, mounts file systems, sets up swap partitions (or swap files), starts system services, sets up system terminals, and sets up a whole lot of other things.

What has been skipped?

Bootloader stage is skipped. It consists of hardware stage, the firmware stage (BIOS), the first-level bootloader (MBR), and the second-level bootloader (GRUB/GRUB2). This will approximately save 5-10 seconds, it varies and depends on different hardware.

Reference

http://www.ibm.com/developerworks/linux/library/l-kexec.html

http://www.ibm.com/developerworks/cn/linux/l-kexec

http://www.ibm.com/developerworks/library/l-linuxboot/