Kexec: Difference between revisions

From Alpine Linux
m (Expanded rationale for using kexec and make it less x86* specific.)
m (→‎Automatically on every reboot/shutdown: Updated the kexec-load script: make it easier to temporarily restore poweroff/reboot default behavior. Documentation.)
Line 25: Line 25:
: "${BOOTPART:=/boot}"
: "${BOOTPART:=/boot}"
: "${KERNEL:=vmlinuz-edge}"
: "${KERNEL:=vmlinuz-edge}"
: "${INITRD:=initramfs-edge}"  
: "${INITRD:=initramfs-edge}"


depend() {  
depend() {
need localmount
need localmount
}
}


start() {  
start() {
:
:
}
}


stop() {
stop() {
ebegin "kexec: loading kernel for faster reboot"
if ! yesno ${RC_GOINGDOWN}; then
einfo "kexec-load: Not rebooting or powering off; not loading kernel"
exit
fi
ebegin "kexec-load: loading kernel for faster reboot"
kexec -l "${BOOTPART}/${KERNEL}" \
kexec -l "${BOOTPART}/${KERNEL}" \
--initrd "${BOOTPART}/${INITRD}" \
--initrd "${BOOTPART}/${INITRD}" \
Line 48: Line 52:
}
}
start() {
start() {
ebegin "Using kexec for faster reboot"  
ebegin "kexec-exec: Using kexec for faster reboot"  
kexec -e
kexec -e
ewend $? No kernel loaded.
ewend $? "kexec-exec No kernel loaded."
return 0
return 0
}</nowiki>}}
}</nowiki>}}
Line 61: Line 65:
}}
}}


Reboot and enjoy!
kexec will run on your next <code>reboot</code> or <code>poweroff</code> enjoy!
{{Note|With both of the above service enabled, it's difficult to poweroff the system as it will reboot unless an unsafe shutdown with <code>poweroff -f</code> is used... To get around this temporarily disable either one of the services (e.g. <code>rc-update del kexec-load default</code>)}}
 
{{Note|With both of the above service enabled, the system will reboot via kexec even if you are attempting to <code>poweroff</code>. To temporarily restore <code>poweroff</code> or <code>reboot</code> behavior, simply run <code>rc-service kexec-load stop</code> beforehand.}}


== See also ==
== See also ==

Revision as of 09:23, 6 September 2023

kexec is a system call that enables loading and booting into another kernel. This is useful for faster reboots that skip the firmware initialisation process and the bootloader.

Installing kexec-tools

The userspace tools required to use it can be installed via

apk add kexec-tools kexec-tools-doc

The tools are not available on all flavors of Alpine, additionally not all kernels are compiled with the kexec syscall enabled. You will most likely want to check your /boot/config-* file for CONFIG_KEXEC=y

Usage

Manually

On a typical Alpine setup, it can be used via:

# kexec -l /boot/vmlinuz-edge --initrd \ /boot/initramfs-edge --reuse-cmdline \ && openrc shutdown # kexec -e

There are no Alpine-specific considerations for Kexec. Please review the man page and existing references below for more details. This page is deliberately kept short in order to avoid duplicating existing documentation.

Automatically on every reboot/shutdown

kexec can be set to run automatically for faster rebooting. This is very useful on servers.

First create two openrc services and edit the BOOTPART, KERNEL, and INITRD variables if not using the defaults:

Contents of /etc/init.d/kexec-load

#!/sbin/openrc-run description="kexec for faster reboot" # Define defaults : "${BOOTPART:=/boot}" : "${KERNEL:=vmlinuz-edge}" : "${INITRD:=initramfs-edge}" depend() { need localmount } start() { : } stop() { if ! yesno ${RC_GOINGDOWN}; then einfo "kexec-load: Not rebooting or powering off; not loading kernel" exit fi ebegin "kexec-load: loading kernel for faster reboot" kexec -l "${BOOTPART}/${KERNEL}" \ --initrd "${BOOTPART}/${INITRD}" \ --reuse-cmdline ewend $? Failed. }

Contents of /etc/init.d/kexec-exec

#! /sbin/openrc-run description="kexec for faster reboot" depend() { after killprocs savecache mount-ro } start() { ebegin "kexec-exec: Using kexec for faster reboot" kexec -e ewend $? "kexec-exec No kernel loaded." return 0 }

Now give these services execute permission and assign them to the appropriate runlevels:

chmod a+x /etc/init.d/kexec-load chmod a+x /etc/init.d/kexec-exec rc-update add kexec-load default rc-update add kexec-exec shutdown rc-service kexec-load start

kexec will run on your next reboot or poweroff enjoy!

Note: With both of the above service enabled, the system will reboot via kexec even if you are attempting to poweroff. To temporarily restore poweroff or reboot behavior, simply run rc-service kexec-load stop beforehand.

See also