Kexec: Difference between revisions
m (→Automatically on every reboot/shutdown: transclude the kexec-load file instead of doing it inline.) |
Tag: Undo |
||
Line 28: | Line 28: | ||
First create two openrc services and edit the <code>BOOTPART</code>, <code>KERNEL</code>, and <code>INITRD</code> variables if not using the defaults: | First create two openrc services and edit the <code>BOOTPART</code>, <code>KERNEL</code>, and <code>INITRD</code> variables if not using the defaults: | ||
{{cat|/etc/init.d/kexec-load|{{: | {{cat|/etc/init.d/kexec-load|<nowiki>#!/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. | |||
}</nowiki>}} | |||
{{cat|/etc/init.d/kexec-exec|<nowiki>#! /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 | |||
}</nowiki>}} | |||
Now give these services execute permission and assign them to the appropriate runlevels: | Now give these services execute permission and assign them to the appropriate runlevels: | ||
{{cmd|chmod a+x /etc/init.d/kexec-load | {{cmd|chmod a+x /etc/init.d/kexec-load |
Revision as of 19:04, 2 January 2024
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.
Preparing
In Alpine 3.19 and up, the userspace tools required to use it can be installed via
apk add kexec-tools kexec-tools-doc
But note that not all kernels are compiled with the kexec syscall enabled. You should check the /boot/config-* file for CONFIG_KEXEC=y
Most Alpine kernels have been hardened and return
kexec_load failed: Operation not permitted
if kexec is called without being unlocked with the kernel boot parameter
kexec_load_disabled=0
.
Without it the sysctl setting kernel.kexec_load_disabled
defaults to 1 and it can't be turned off in the running kernel, so you need to add the parameter to your bootloader configuration and reboot.
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
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
Contents of /etc/init.d/kexec-exec
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!
poweroff
. To temporarily restore default poweroff
or reboot
behavior, simply run rc-service kexec-load stop
beforehand.