Skip to content

crosbreaker/kexec

Repository files navigation

kexec-mod

Kexec as a loadable kernel module for Linux kernels based on kexec-module.

Purpose

This loadable kernel module enables users of Linux kernels without built-in Kexec support to still make use of Kexec functionality. For instance, you may use this module to boot to a more recent kernel if you are unable to replace the boot image.

Support status

Kernel Arch Status
Linux 4.14 x86_64 / ARCH=x86 Builds as kernel/kexec_mod.ko
Linux 5.x arm64 / ARCH=arm64 Original upstream support, not reverified in this fork yet

The current x86_64 path targets the classic kexec_load flow. Crash-kexec and kexec_file_load are not wired up in this module path yet.

Kernel Build Requirements

This project builds an external kernel module. You do not need to rebuild the whole kernel, but you do need a prepared build directory for the exact target kernel. A usable build directory contains generated files such as:

include/generated/autoconf.h
include/generated/utsrelease.h
include/generated/asm-offsets.h
Module.symvers

Module.symvers is especially important when CONFIG_MODVERSIONS is enabled. The module version string comes from include/generated/utsrelease.h. If UTS_RELEASE does not match the target kernel's uname -r, insmod can reject the module.

This repo provides a KERNEL_VERSION convenience flag for already-built target kernels. It updates include/generated/utsrelease.h and include/config/kernel.release in the prepared build directory before building the module. Use it only when you know the exact target uname -r.

KERNEL_COMPAT selects which kernel internal API/ABI implementation to compile. It is separate from KERNEL_VERSION: compatibility controls source code paths, while version controls module vermagic.

Current compatibility values:

ARCH KERNEL_COMPAT Meaning
x86 3.18 Linux 3.18 x86_64 kexec internals
x86 4.x Linux 4.4/4.14 x86_64 kexec internals
x86 5.4 Linux 5.4 x86_64 kexec internals
x86 5.15 Linux 5.15 x86_64 kexec internals
arm64 5.x Original upstream Linux 5.x arm64 path

On installed systems /lib/modules/$(uname -r)/build is often the right KERNEL_BUILD path, but extracted root filesystems may contain a broken absolute symlink. In that case, use the real build directory or recreate one from matching source and config.

Building

The project is comprised of two parts:

  • kernel/ contains the Linux kernel module that exposes Kexec functionality via /dev/kexec.
  • user/ contains a helper library that allows the use of an unpatched kexec-tools.
  • bin/ contains static helper binaries, including a patched x86_64 kexec that talks to /dev/kexec directly.

Pre-requisites

Make sure you have installed the necessary packages for building Linux kernel modules on your system (e.g., make and gcc).

Kernel module

Enter the kernel directory:

cd kernel/

Then build the module against a prepared kernel build directory. KERNEL_BUILD means the prepared output directory containing generated headers and, ideally, Module.symvers:

make KERNEL_BUILD=/path/to/kernel-build ARCH=x86 KERNEL_COMPAT=4.x

You must specify both the target architecture and the kernel-internals compatibility profile:

make KERNEL_BUILD=/path/to/kernel-build ARCH=x86 KERNEL_COMPAT=3.18
make KERNEL_BUILD=/path/to/kernel-build ARCH=x86 KERNEL_COMPAT=4.x
make KERNEL_BUILD=/path/to/kernel-build ARCH=x86 KERNEL_COMPAT=5.4
make KERNEL_BUILD=/path/to/kernel-build ARCH=x86 KERNEL_COMPAT=5.15
make KERNEL_BUILD=/path/to/kernel-build ARCH=arm64 KERNEL_COMPAT=5.x

This builds kexec_mod.ko. Some architectures may also build an arch-specific companion module while that support is being consolidated.

For split source/output kernel builds, use KERNEL_SRC for the kernel source tree and KERNEL_BUILD for the prepared generated output directory:

make KERNEL_SRC=/path/to/linux-source KERNEL_BUILD=/path/to/linux-build \
  ARCH=x86 KERNEL_COMPAT=4.x

For ChromiumOS 4.14 x86_64, use ARCH=x86, not ARCH=x86_64:

make KERNEL_BUILD=/path/to/prepared-4.14-build ARCH=x86 KERNEL_COMPAT=4.x

To force the exact vermagic for an already-built ChromeOS kernel:

make KERNEL_SRC=/path/to/linux-source KERNEL_BUILD=/path/to/linux-build \
  ARCH=x86 \
  KERNEL_COMPAT=4.x \
  KERNEL_VERSION=4.14.91-18023-g2ab161c540baf

If you prefer native Kbuild behavior, provide just the suffix with LOCALVERSION, then refresh the kernel release files before building:

make -C /path/to/linux-source O=/path/to/linux-build ARCH=x86 \
  LOCALVERSION=-18023-g2ab161c540baf \
  include/generated/utsrelease.h include/config/kernel.release

For arm64:

make KERNEL_BUILD=/path/to/prepared-arm64-build ARCH=arm64 KERNEL_COMPAT=5.x

The ChromiumOS 4.14 x86_64 source tree used for initial verification is:

/home/appleflyer/chromiumos/src/third_party/kernel/v4.14

The module was built successfully against a prepared x86_64 output directory:

make -C kernel KERNEL_BUILD=/tmp/opencode/kexec-kernel-4.14-build ARCH=x86 \
  KERNEL_COMPAT=4.x

User-space helper

Enter the user directory and build the helper as follows:

make

This will build redir.so that acts as an LD_PRELOAD interposer for Kexec syscalls, allowing the use of unpatched kexec-tools.

If the target does not have kexec-tools, use the static patched binary instead:

bin/kexec-static-x86_64-devkexec -c -l /path/to/bzImage --reuse-cmdline
bin/kexec-static-x86_64-devkexec -e

The static binary does not require LD_PRELOAD because it calls /dev/kexec directly.

Usage

Make sure you have built the module and user-space helper. Also check whether you have installed kexec-tools. Then, you can use kexec-tools as follows:

LD_PRELOAD=/root/redir.so kexec -l /boot/vmlinuz --reuse-cmdline
LD_PRELOAD=/root/redir.so kexec -e

License

The code is released under the GPLv2 license. See COPYING.txt.

About

vibecoded kernel module to provide /dev/kexec in chromeOS shims

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors