LinuxCommandLibrary

kexec

Reboot quickly into another kernel

TLDR

Load a new kernel

$ kexec [[-l|--load]] [path/to/kernel] --initrd=[path/to/initrd] --command-line=[arguments]
copy

Load a new kernel with current boot parameters
$ kexec [[-l|--load]] [path/to/kernel] --initrd=[path/to/initrd] --reuse-cmdline
copy

Execute a currently loaded kernel
$ kexec [[-e|--exec]]
copy

Unload current kexec target kernel
$ kexec [[-u|--unload]]
copy

SYNOPSIS

kexec [options] {command} [arguments]

Common commands:
kexec -l kernel-image [--initrd=initrd-file] [--append=cmdline-string] [options]
kexec -e
kexec -u
kexec -p kernel-image [--initrd=initrd-file] [--append=cmdline-string] [options]
kexec -s
kexec -x
kexec -d

PARAMETERS

-l, --load
    Loads a new kernel image and its associated initial RAM disk (initrd) into memory, preparing it for execution. This does not immediately boot the new kernel.

-e, --exec
    Executes the kernel image that was previously loaded using -l. This command initiates the boot of the new kernel.

-u, --unload
    Unloads the kernel image previously loaded by -l, freeing up the memory it occupied.

-p, --load-panic
    Loads a kernel image specifically designated as a 'panic kernel' or 'dump kernel' for use with the kdump mechanism. This kernel is executed automatically in the event of a system crash.

-s, --force-sync
    Forces a sync operation before executing the loaded kernel, ensuring all buffered data is written to disk. Useful for preventing data loss during an immediate reboot.

-x, --no-ifdown
    Prevents kexec from bringing down network interfaces before executing the new kernel. Useful in specific network configurations or virtual environments.

-d, --debug
    Enables verbose debug output, which can be helpful for troubleshooting issues during kernel loading or execution.

--append=string
    Appends the specified string to the kernel command line for the new kernel. This is equivalent to setting console=ttyS0 or root=/dev/sda1.

--initrd=file
    Specifies the initial RAM disk (initrd) file to be used with the new kernel. The initrd provides a temporary root filesystem during the early boot process.

--real-mode
    Attempts to boot the new kernel in real mode. This option is typically used for older systems or specific debugging scenarios.

--elf64-core-headers
    Generates 64-bit ELF core headers. Primarily used in conjunction with --load-panic for kdump setups to ensure proper crash dump capture on 64-bit systems.

--reuse-console
    Attempts to reuse the current console for the new kernel, which can help maintain continuous output during the kexec transition. Behaviour can vary based on kernel versions and console types.

DESCRIPTION

kexec is a Linux system call and a utility that allows you to load and boot a new kernel from the currently running kernel, bypassing the traditional boot process (BIOS/UEFI, bootloader). This means it performs a "warm reboot" or "soft reboot" where the hardware remains powered on and initialized, significantly reducing the time it takes to restart the system compared to a full power cycle.

It's primarily used for several critical purposes:

  1. Faster Reboots: Especially beneficial in large server environments or cloud infrastructures where uptime is crucial and minimizing downtime for kernel updates is desired.
  2. Kernel Development and Testing: Allows developers to quickly test new kernel versions without waiting for a full system restart.
  3. Crash Recovery (kdump): It's a fundamental component of the kdump mechanism, which allows the system to capture a memory dump of a crashed kernel by booting into a separate, lightweight "dump kernel." This dump kernel can then save the memory contents for later analysis.

CAVEATS

Using kexec requires root privileges due to its direct interaction with kernel memory and hardware.

While offering faster reboots, kexec bypasses the typical system initialization performed by BIOS/UEFI and bootloaders. This can lead to various issues:

  • Hardware State: Not all hardware components are fully reinitialized, which might cause instability or unexpected behavior with certain drivers or devices, especially after a prolonged uptime or if the new kernel requires a different hardware state.
  • Memory Fragmentation: The currently running kernel might have fragmented memory, making it difficult for kexec to find contiguous memory regions large enough to load the new kernel and its initrd. This can lead to "Not enough memory" errors.
  • Firmware Issues: Some firmware or BIOS versions might not fully support the kexec mechanism, leading to hang-ups or failed reboots.
  • Security: As it allows booting arbitrary kernels, it presents a potential security risk if an attacker gains root access, as they could load a malicious kernel.
  • Debugging Complexity: Issues during a kexec boot can be harder to diagnose than a traditional boot, as there's less console output from the initial boot stages.

<B>KDUMP INTEGRATION</B>

kdump is a kernel crash dumping mechanism that relies heavily on kexec. When a kernel panic occurs, kexec is used to boot into a pre-loaded, separate 'dump kernel' (loaded via kexec -p). This dump kernel then collects the memory contents of the crashed system, typically saving it to disk for later analysis. This allows administrators to diagnose the cause of kernel panics without having to manually reboot and re-create the crash conditions.

The kdump configuration often involves reserving a specific region of memory for the dump kernel and its associated data structures using the crashkernel= boot parameter.

<B>USE CASES AND BENEFITS</B>

kexec offers significant advantages over traditional reboots in specific scenarios:

  • Reduced Downtime: Eliminates the lengthy BIOS/UEFI POST sequence, speeding up system restarts. This is invaluable for critical services and continuous integration/delivery pipelines.
  • Kernel Patching: Allows applying kernel security patches or updates with minimal service interruption.
  • Debugging: Provides a rapid way for kernel developers to test new kernel builds or debug issues without the overhead of a full power cycle.
  • Virtualization: In some virtualized environments, kexec can offer a more graceful way to restart guest kernels.

HISTORY

The kexec system call and utility were primarily developed to address the need for faster kernel reboots, especially in server environments where minimizing downtime for kernel updates is critical. Its origins trace back to the early 2000s, with significant development occurring around the Linux 2.6 kernel series.

A major driving force behind its adoption and continued development has been its role in the kdump mechanism. kdump relies on kexec to load and execute a dedicated "dump kernel" in the event of a system crash, allowing for reliable collection of kernel core dumps for post-mortem analysis.

Over the years, its usage has expanded from enterprise servers to cloud environments and even embedded systems, whenever quick and reliable kernel transitions are desired without a full system power cycle.

SEE ALSO

reboot(8), shutdown(8), systemctl(1), init(8), grub(8), bootctl(1)

Copied to clipboard