Kexec: Difference between revisions

From Alpine Linux
(Added category, minor formatting and template changes)
m (Re-added accidentally trimmed content.)
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[https://en.wikipedia.org/wiki/Kexec Kexec] is a system call that enables loading and booting into another kernel. This is useful for fast reboots that skip the BIOS or UEFI initialisation process.
{{DISPLAYTITLE:kexec}}
[https://en.wikipedia.org/wiki/Kexec 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 {{cmd|apk add {{pkg|kexec-tools}} {{pkg|kexec-tools-doc}}}} But note that not all kernels are compiled with the kexec syscall enabled. You should check the {{path|/boot/config-*}} file for {{codeline|CONFIG_KEXEC=y}}
Most Alpine kernels have been [https://gitlab.alpinelinux.org/alpine/aports/-/commit/909d020b81c09bf0df649f8aa6b7da10377a0667 hardened] and return
<code>kexec_load failed: Operation not permitted</code>
if kexec is called without being unlocked with the kernel boot parameter
<code>kexec_load_disabled=0</code>.
Without it the [https://linux.die.net/man/8/sysctl sysctl setting] <code>kernel.kexec_load_disabled</code> defaults to 1 and it can't be turned off in the running kernel, so you need to add the parameter to your [[Bootloaders|bootloader]] configuration and reboot.
==Usage==
===Manually===
{{note|Currently multiple initrd (e.g. for loading CPU microcode) is not supported}}
On a typical Alpine setup, it can be used via:
On a typical Alpine setup, it can be used via:


{{Cmd|# kexec -l /boot/vmlinuz-edge --initrd \
{{Cmd|# kexec -l /boot/vmlinuz-edge --initrd \
     /boot/initramfs-edge --reuse-cmdline
     /boot/initramfs-edge --reuse-cmdline \
&& openrc shutdown
&#35; kexec -e}}
&#35; kexec -e}}


{{Warning|Running <code>kexec -e</code> does not unmount any filesystems or gracefully shut down any services!}}
===Automatically on every reboot/shutdown===
kexec can be set to run automatically for faster rebooting. This is very useful on servers.


There are no Alpine-specific considerations for Kexec. Please review the man page and existing references below of more details. This page is deliberately kept short in order to avoid duplicating existing documentation.
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|{{:Kexec/kexec-load}}}}
{{cat|/etc/init.d/kexec-exec|{{:Kexec/kexec-exec}}}}
 
Now give these services execute permission and assign them to the appropriate runlevels:
{{cmd|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 <code>reboot</code> or <code>poweroff</code> enjoy!
 
{{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 default <code>poweroff</code> or <code>reboot</code> behavior, simply run <code>rc-service kexec-load stop</code> beforehand.}}


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


* [https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/log/ kexec-tools changelog]
* [https://wiki.gentoo.org/wiki/Kexec Gentoo Wiki: Kexec]
* [https://wiki.gentoo.org/wiki/Kexec Gentoo Wiki: Kexec]
* [https://wiki.archlinux.org/title/Kexec ArchWiki: Kexec]
* [https://wiki.archlinux.org/title/Kexec ArchWiki: Kexec]
[[Category: Booting]]
[[Category: Booting]]
[[Category: Kernel]]
[[Category: Kernel]]

Latest revision as of 22:41, 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

Note: Currently multiple initrd (e.g. for loading CPU microcode) is not supported

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

#!/sbin/openrc-run description="kexec for faster reboot" # Define defaults : "${BOOTPART:=/boot}" : "${KERNEL:=vmlinuz-$(uname -r | awk -F \- '{print $NF }')}" : "${INITRD:=initramfs-$(uname -r | awk -F \- '{print $NF}')}" 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 default poweroff or reboot behavior, simply run rc-service kexec-load stop beforehand.

See also