Kexec

From ArchWiki

Jump to: navigation, search

Kexec is a system call that enables you to load and boot into another kernel from the currently running kernel. This is useful for kernel developers or other people who need to reboot very quickly without waiting for the whole BIOS boot process to finish. Note that there may appear some problems and kexec may not work correctly for you because the devices won't fully reinitiate when using this method.

Contents

Installation

To install kexec, type:

 pacman -Sy kexec-tools

Configuration

Arch rc scripts already have kexec support, so it's enough to just call reboot after loading the kernel and it'll get (re)started.

Tips & Tricks

Rebooting your system using kexec

Here is a simple script that reads kernel information from the GRUB menu, loads the kernel using kexec, and then reboots. Save the script as, e.g., /usr/local/sbin/kexec-reboot.sh and make it executable using chmod +x.

Note: The script supports only GRUB Legacy menu files, not grub2.

Script usage

The script takes an item number as its first parameter. E.g., to boot the first item in your GRUB menu, type:

 /usr/local/sbin/kexec-reboot.sh 1

The script

#!/bin/sh
# This script loads a kernel from the GRUB menu and reboots.
# The error checking is very basic.
# By Přemysl Janouch, 2008

if [ $UID -ne 0 ]; then echo "Error: Only a superuser can do this" >&2; exit 1; fi

# Choose the #$1 kernel from the GRUB menu or the first one if none was specified.
if [ -z "$1" ]; then N=1
elif awk 'BEGIN{exit("'"$1"'"!~/^[[:digit:]]+$/)}'; then N=$1
else echo "Error: The parameter is not a number" >&2; exit 1; fi

# This returns just the #N item in the GRUB menu.
ITEM=$(awk -v i="$N" '/^title/{if(c>i)exit;else c++}c==i{print}' /boot/grub/menu.lst)
if [ -z "$ITEM" ]; then echo "Error: No such item" >&2; exit 1; fi

KERNEL=$(echo "$ITEM" | awk '/^kernel/{print $2;exit}')
PARAM=$(echo "$ITEM" | awk '/^kernel/{print substr($0,index($0,$3));exit}')
INITRD=$(echo "$ITEM" | awk '/^initrd/{print $2;exit}')
if [ -z "$KERNEL" ]; then echo "Error: No kernel" >&2; exit 1; fi

# Load the kernel and reboot.
kexec -l "$KERNEL" --initrd="$INITRD" --append="$PARAM"
reboot

More Resources

Personal tools