Kexec: Optimize Your Boot Times
Introduction
Kexec is a tool that allows rebooting a machine without going through the hardware layer. It will stop all services, shut down the init processes (sysV) to reach the bootloader. Then it will start normally, bypassing the hardware reboot phase.
This technique can be very useful on High Availability systems where downtime is critical.
Installation
Debian
On Debian, you need to install this package:
aptitude install kexec-tools
Red Hat
On Red Hat, you need to have this package installed:
yum install kexec-tools
Configuration
Debian
There is a configuration file that allows you to make some modifications such as loading a newer kernel if one exists. This technique is not particularly recommended as some applications may not handle it well, though this is relatively rare. This option is not enabled by default, however, you can activate it if needed:
# Defaults for kexec initscript
# sourced by /etc/init.d/kexec and /etc/init.d/kexec-load
# Load a kexec kernel (true/false)
LOAD_KEXEC=true
# Kernel and initrd image
KERNEL_IMAGE="/vmlinuz"
INITRD="/initrd.img"
# If empty, use current /proc/cmdline
APPEND=""
# Load the default kernel from grub config (true/false)
USE_GRUB_CONFIG=false
Red Hat
Like Debian, Red Hat has its own configuration for kexec:
# Kernel Version string for the -kdump kernel, such as 2.6.13-1544.FC5kdump
# If no version is specified, then the init script will try to find a
# kdump kernel with the same version number as the running kernel.
KDUMP_KERNELVER=""
# The kdump commandline is the command line that needs to be passed off to
# the kdump kernel. This will likely match the contents of the grub kernel
# line. For example:
# KDUMP_COMMANDLINE="ro root=LABEL=/"
# If a command line is not specified, the default will be taken from
# /proc/cmdline
KDUMP_COMMANDLINE=""
# This variable lets us append arguments to the current kdump commandline
# As taken from either KDUMP_COMMANDLINE above, or from /proc/cmdline
KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory"
# Any additional /sbin/mkdumprd arguments required.
MKDUMPRD_ARGS=""
# Any additional kexec arguments required. In most situations, this should
# be left empty
#
# Example:
# KEXEC_ARGS="--elf32-core-headers"
KEXEC_ARGS=""
#Where to find the boot image
KDUMP_BOOTDIR="/boot"
#What is the image type used for kdump
KDUMP_IMG="vmlinuz"
#What is the images extension. Relocatable kernels don't have one
KDUMP_IMG_EXT=""
The default options do not need to be modified.
Utilization
Debian
When kexec is installed, the reboot command calls kexec and therefore natively reboots via kexec. Here are the useful commands:
- reboot: executes a fast reboot of the machine via kexec
- coldreboot: performs a standard reboot including hardware reboot
Red Hat
For Red Hat, run this command:
kexec -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname -r`.img --command-line="`sed 's/ rhgb\| quiet//g' /proc/cmdline`"
Now, to launch a fast reboot, use the standard reboot command.
I made a small script to do all of this:
#!/bin/sh
# Made by Pierre Mavro (Deimosfr) | 15/03/2012
echo "Do you want to perform a fast reboot (without hardware reboot) ? (y/n)"
read ans
if [ $ans = 'y' ] ; then
echo "Fast reboot in progress..."
kexec -l /boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname -r`.img --command-line="`sed 's/ rhgb\| quiet//g' /proc/cmdline`"
reboot
exit 0
fi
echo "Fast reboot cancelled"
Resources
Last updated 15 Mar 2012, 13:41 +0200.