Fast reboot using kexec

Why ?

In a virtual environment you are unlikely to need this, since the "hardware" initialization there happens very quickly.

However, rebooting a physical server can take a long time depending on the hardware installed. I have seen servers take about 20 minutes to initialize. Skipping this initialization can save a lot of time in deployment.

Prerequisites

Kexec must be installed. It usually comes with kexec-tools and is installed automatically as part of kdump preparation.

Using kexec is built from two phases, load and exec. Let's look at some usage scenarios.

Load phase

Load current kernel with its initrd and same kernel cmdline:

# kexec -l $(find /boot -type f -name vmlinuz*$(uname -r)*) --initrd=$(find /boot -type f -name initr*$(uname -r)*) --reuse-cmdline

The last parameter copies all other /proc/cmdline arguments to save typing. Of course, you can replace or add any of your own arguments.

An example of how to play with parameters:

# kexec -l /boot/mykernel --initrd=/boot/myinitrd --append="mem=1G root=/dev/mapper/rootvg-slash rd.break=mount"

Reboot phase

# kexec -e

This command simulates pressing the RESET button. But if you want to shut down normally, you can use the regular commands. They will call kexec instead of the hard reset command.

# reboot
# shutdown -r

This means that you can put the following line in /etc/rc.local file or your distribution's equivalent.

/sbin/kexec -l $(find /boot -type f -name vmlinuz*$(uname -r)*) --initrd=$(find /boot -type f -name initr*$(uname -r)*) --reuse-cmdline

However, this line should not be forgotten during kernel updates.


Updated on Wed Aug 14 15:55:49 IDT 2024 More documentations here