kexec

On this page 7

kexec is a Linux kernel mechanism that loads a second kernel into memory and jumps into it directly from the running kernel, without passing through firmware initialization or a bootloader such as GRUB. In the context of system initialization, it shortens the path between shutting down one kernel and starting the next, which makes reboots faster and enables reliable crash capture.

The name combines kernel and exec: just as the execve system call replaces a running program with a new one, kexec replaces the running kernel with a new kernel image. The mechanism consists of two parts, the kexec_load and kexec_file_load system calls in the kernel and the kexec-tools user-space utility that prepares and triggers the switch.

How kexec replaces the running kernel

kexec works in two distinct phases: loading and execution. In the loading phase, the user-space tool reads a kernel image, an optional initramfs and a kernel command line, then asks the running kernel to place these segments into memory. Nothing visible happens yet; the system keeps running normally with the new kernel staged in reserved pages.

In the execution phase, triggered by kexec -e or by systemctl kexec, the running kernel stops devices, disables interrupts and copies the staged segments to their final addresses. A small piece of code called the purgatory then verifies checksums of the loaded segments and transfers control to the new kernel entry point. The new kernel starts as if a bootloader had just handed it control, but the BIOS or UEFI firmware never runs.

The following table summarizes the main components involved in a kexec transition and what each one does.

ComponentLocationRole
kexec_loadKernel system callAccepts segments prepared by user space
kexec_file_loadKernel system callLoads kernel and initramfs from file descriptors, supports signature checks
kexec-toolsUser spaceParses images, builds segments, triggers execution
PurgatorySmall code blob in memoryVerifies segment checksums and jumps to the new kernel
InitramfsLoaded segmentProvides the early root file system for the new kernel
Components of a kexec transition

The kexec_file_load call, added in kernel 3.17, matters for secured systems. Because the kernel itself reads the files, it can enforce kernel image signature verification, which is required when UEFI Secure Boot or kernel lockdown is active. The older call trusts whatever buffers user space supplies and is therefore restricted under lockdown.

Main uses of kexec in Linux

kexec serves two main purposes: fast reboots and crash dump capture. Both rely on the same loading mechanism but differ in when and how the second kernel is started.

Use caseTriggerMain benefit
Fast rebootsystemctl kexec or kexec -eSkips firmware and bootloader, often saving tens of seconds to minutes
Crash dump with kdumpKernel panicBoots a capture kernel that saves memory of the failed kernel
Kernel update without full reboot cyclePlanned maintenanceShortens downtime when activating a patched kernel
Boot from an embedded loaderMinimal Linux used as bootloaderUses Linux drivers to fetch and start the final kernel
Typical kexec use cases

With kdump, a separate capture kernel is loaded in advance into a memory region reserved at boot with the crashkernel= parameter on the kernel command line. When the main kernel panics, it jumps directly into the capture kernel, which still sees the memory of the crashed system and exposes it as /proc/vmcore. Tools such as makedumpfile then write a filtered dump to disk for later analysis with the crash utility.

Projects such as LinuxBoot use the same idea at the firmware level: a small Linux kernel replaces most of the firmware boot logic and then uses kexec to start the production kernel, which gives administrators Linux drivers and scripting during boot.

How kexec compares with a conventional reboot

A kexec reboot differs from a normal reboot mainly in which stages of the boot chain are skipped. A conventional reboot resets the CPU, reruns firmware tests, reinitializes hardware and loads a bootloader before the kernel starts. kexec keeps the machine powered and jumps from kernel to kernel.

Boot stageConventional rebootkexec reboot
Firmware POST and initializationRuns fullySkipped
Option ROMs and RAID controller setupRuns, often slow on serversSkipped
Bootloader (GRUB, systemd-boot)Selects and loads the kernelNot involved
Kernel and initramfs loadingDone by the bootloaderDone by kexec-tools before the switch
Hardware resetFull reset of devicesOnly driver shutdown, no full reset
User-space shutdownClean shutdown by the init systemClean shutdown when started through systemd
Conventional reboot compared with kexec reboot

On large servers, firmware initialization alone can take several minutes, especially with many memory modules or storage controllers. kexec removes that delay, which is why it is valued in high-availability environments where every minute of downtime counts. On a laptop or small virtual machine, the saving is usually only a few seconds.

Using kexec with systemd and kexec-tools

The safe way to perform a kexec reboot is to let the init system stop services first. On distributions using systemd, the kexec.target unit handles the clean shutdown, and systemctl kexec reboots into the kernel that was previously loaded or into the default boot entry.

  1. Load the kernel: kexec -l /boot/vmlinuz-6.8.0 --initrd=/boot/initramfs-6.8.0.img --reuse-cmdline
  2. Confirm that a kernel is staged by reading /sys/kernel/kexec_loaded, which returns 1
  3. Reboot cleanly with systemctl kexec, which stops units and file systems before the switch
  4. Avoid plain kexec -e on a production system, because it switches immediately without shutting down services

For kdump, distributions provide services such as kdump.service that load the capture kernel automatically at boot with kexec -p. The administrator mainly needs to size the crashkernel= reservation, commonly between 128 MB and 512 MB depending on memory size, architecture and the drivers needed by the capture kernel.

Limitations and common problems

The main weakness of kexec is that hardware is not fully reset. Devices are left in whatever state their drivers placed them during shutdown, and a driver without a proper shutdown routine can leave a device unusable for the new kernel. Graphics adapters, some network cards and devices with active DMA are frequent sources of trouble.

SymptomLikely causeTypical fix
Black screen after switchGraphics device not reinitializedUse a serial or network console, or add nomodeset for testing
Permission denied on loadKernel lockdown or Secure BootUse kexec_file_load (-s) with a signed kernel
kdump never captures a dumpMissing crashkernel reservationAdd crashkernel= to the kernel command line and reboot
New kernel hangs on device probeDriver left hardware in a bad stateUpdate the driver or use a full reboot
Microcode or firmware update ignoredFirmware stage skippedPerform a cold reboot after firmware changes
Common kexec problems and fixes

A further point is security. Because kexec can replace the kernel at will, it is a powerful privilege for an attacker with root access. Hardened systems can disable it with the kernel.kexec_load_disabled sysctl, which is a one-way switch until the next boot, or allow only signed images through lockdown policies.

A non-technical view

Restarting a computer normally means going back to the very beginning: the hardware checks itself, the startup menu appears and only then does the operating system load. kexec lets Linux skip that whole warm-up. The running system hands over directly to a fresh copy of the system core, much like a relay runner passing the baton without stopping.

The result is a much faster restart, especially on large servers that take minutes to check their hardware. The same trick also helps when the system crashes, because a small backup system can start instantly and save a record of what went wrong.

A concrete example

An operations team runs a database cluster on physical servers with 1 TB of memory each. A full reboot of one node takes about seven minutes, and more than five of those are spent in firmware memory tests and storage controller setup. Each monthly kernel security update therefore costs noticeable failover time.

The team installs the patched kernel, stages it with kexec -l and the current command line, and then runs systemctl kexec after draining the node. The services stop cleanly, the new kernel starts directly, and the node returns to the cluster in under two minutes. The same servers also reserve 512 MB with crashkernel=, so when a faulty storage driver later causes a kernel panic, kdump saves a memory dump that lets engineers identify the failing driver within a day.