Linux Kernel Quick Boot with Kexec


by Ramses Soto-Navarro ramses@sotosystems.com, 7/12/2024

Linux Kernel Quick Boot with Kexec


Overview

Kexec quickly boots a Linux kernel and initramfs without resetting the hardware. Typically the reboot command also resets the hardware which takes longer for the entire reboot process to finish. The Kexec process is very convenient for rebooting a system very fast in order to troubleshoot or diagnose a quick reboot; it saves time, especially for servers that take very long during the BIOS post, in order to check memory and very the hardware. This technique is useful for test purposes or during times when rebooting quickly during an emergency is extremely necessary. Literally it can reboot your system in seconds. Here we run Kexec through a simple bash script which will run it in the background and then exit the script; otherwize your remote SSH remote terminal gets stuck in La La Land. The audience is experienced Linux administrators.


Install Kexec

Debian:

$ sudo apt-get install kexec-tools

Red Hat:

$ sudo dnf install kexec

The Script

Create a simple bash script which will wait 3 seconds, then reboot the kernel and initramfs. Here I am using an LVM volume as my root partition. Simply look for the kernel and initramfs that you would like to boot, then change the variables accordingly. The scripts confirms if you really want to quick reboot. Follow the logic.

cat >>/boot/mykexec.sh<<EOF
#!/bin/bash

export K=/boot/vmlinuz-6.1.0-22-amd64
export I=/boot/initrd.img-6.1.0-22-amd64
export R=/dev/mapper/vg1-lvroot
export S=3

f_reboot () {
kexec -l $K --initrd=$I --append=root=$R
( sleep $S ; kexec -e ) &
echo "Rebooting $K in $S seconds..."
exit 
}

echo -ne "
Quick reboot kernel $K? 
Are you sure? [y/N]: " 

read CHOICE

case $CHOICE in
	y|Y)	f_reboot ;; 
	*) 	echo "Nothing done." ;;
esac
EOF

Run It

Change file attributes then run it.

$ sudo chmod 0700 /boot/mykexec.sh

$ sudo /boot/mykexec.sh

Your system reboot should only take a couple of seconds, rather than many seconds or even a couple of minutes. All memory content will be reset. Try it out.


The End.