Kernel Swapping and In-Memory OS Transition via kexec
# Comprehensive Guide: Kernel Swapping and In-Memory OS Transition via `kexec`
## Executive Summary
`kexec` (Kernel Execute) is a powerful Linux kernel mechanism that enables booting a new kernel directly from the currently running operating system, bypassing the traditional BIOS/UEFI firmware, POST (Power-On Self-Test), and bootloader (GRUB) stages.
When modernizing or migrating legacy operating systems—such as transitioning an end-of-life **CentOS 7** environment to **Debian 10** or **Rocky Linux 9**—traditional re-provisioning usually requires physical media, remote IPMI/iDRAC virtual media mounting, or provider-specific rescue disks. `kexec` streamlines this by allowing an administrator to load a temporary installation kernel directly into RAM, stop running user-space services, and seamlessly pivot into a fresh environment to execute a clean installation or in-place upgrade.
—
## Architecture of `kexec`
### How `kexec` Works
1. **Two-Stage Process:**
– **Stage 1 (Loading):** The user-space tool `kexec -l` reads a target kernel and its associated initial RAM disk (`initrd`) and loads them into reserved, safe memory regions of the running kernel.
– **Stage 2 (Execution):** Upon trigger (`kexec -e` or `systemctl kexec`), the current kernel disables interrupts, shuts down hardware devices cleanly, and transfers CPU execution directly to the entry point of the pre-loaded secondary kernel.
2. **Advantages over Traditional Reboots:**
– **Zero Firmware Delay:** Bypasses hardware initialization and memory checks, reducing downtime drastically.
– **Remote Safety:** Ideal for headless cloud instances or remote data centers where physical console access is unavailable or expensive. If the remote network configuration is handled correctly in the initrd, connectivity can be maintained.
—
## Prerequisites & Planning
Before executing a kernel swap, ensure your environment meets the following requirements:
* **OS / Kernel Support:** CentOS 7 with a standard Linux kernel (`3.10.x` or later) equipped with `kexec-load` system call support (enabled by default in RHEL/CentOS kernels).
* **Root Privileges:** Full `root` or `sudo` access.
* **Console Access:** IPMI, iDRAC, iLO, serial console, or cloud provider serial logs (mandatory as a safety net in case network configurations fail during the swap).
* **Storage Backup:** Full file-level or block-level backups of critical application data.
—
## Step-by-Step Implementation Guide
### Step 1: Install and Verify `kexec-tools`
On your existing CentOS 7 system, install the management utilities required to interact with the kernel’s `kexec` subsystem.
“`bash
sudo yum install kexec-tools -y
“`
Verify that the tool is operational:
“`bash
kexec -v
“`
### Step 2: Acquire Target Installer Assets
To transition to a new OS like Debian 10 or Rocky Linux 9, you need the installer’s kernel (`vmlinuz`) and ramdisk (`initrd.img`). These can be extracted from netboot images or official distribution mirrors.
For example, to stage a network installer or minimal boot environment, place the files into `/boot`:
“`bash
# Example directory structure check
ls -la /boot/
# Expected outputs: vmlinuz-installer, initrd.img-installer
“`
### Step 3: Stage the Kernel into Memory
Load the target kernel into memory using `kexec`. You must pass the correct kernel command-line parameters (`–append`) required by the installer, such as automated installation scripts, IP configurations, or repository mirrors.
“`bash
sudo kexec -l /boot/vmlinuz-installer \
–initrd=/boot/initrd.img-installer \
–append=”auto=true priority=critical ip=dhcp netdevice=eth0 inst.repo=http://mirror.centos.org/…”
“`
Verify that the kernel has been successfully staged:
“`bash
sudo kexec -s
“`
*(The `-s` flag checks the loaded kernel status).*
### Step 4: Execute the Kernel Swap
Trigger the transition. Using `systemctl kexec` ensures that systemd gracefully stops active services, unmounts filesystems cleanly (or switches them to read-only where appropriate), and invokes the kernel execution jump.
“`bash
sudo systemctl kexec
“`
> **Warning:** Once executed, the old CentOS 7 user space ceases to exist. Monitor your IPMI console or cloud serial log to watch the new kernel boot.
### Step 5: Complete the OS Installation
1. Once the installer kernel boots, access it via your remote serial console, VNC, or SSH (if configured in the initrd).
2. Repartition your primary root disks, wiping the legacy CentOS 7 installation.
3. Complete the installation of Debian 10 or Rocky Linux 9.
4. Reboot the machine normally into your brand-new operating system.
Verification post-reboot:
“`bash
cat /etc/os-release
“`
—
## Alternative Approach: In-Place Migration (CentOS 7 to Rocky Linux 9)
If a clean reinstallation via `kexec` netboot is not required, tools like `leapp` or specialized containerized upgrade paths are often used for direct in-place upgrades (though jumping from CentOS 7 straight to Rocky Linux 9 typically requires an intermediate upgrade to Rocky Linux 8 using `leapp`).
* **`leapp` Utility:** Developed by Red Hat/The Rocky Linux community to handle complex binary incompatibilities, Python 2-to-3 transitions, and package shifts between major OS versions.
—
## Troubleshooting & Safety Best Practices
* **Watchdog Timers:** Configure a hardware watchdog if available, so that if the kernel swap hangs or network connectivity is lost indefinitely, the hardware power cycles back to a known good state.
* **Serial Console Redirection:** Always ensure `console=ttyS0,115200n8` (or your hardware’s matching serial parameters) is appended to the `kexec` command line if you lack graphical IPMI.
About the author
Greg Phillips is the founder and principal engineer of Think IT Data Solutions LLC. He has been building and operating physical infrastructure since 1984 — industrial control systems, the U.S. Navy Advanced Electronics Program, satellite communications at Comsat World Systems and Intelsat, and terrestrial transport and video distribution networks since. He founded Think IT Data Solutions in 2013.
Think IT Data Solutions builds and operates the layers underneath AI workloads — power and facilities, network transport, and GPU compute — for organizations that can’t run their most sensitive work on shared infrastructure.
Building something like this and want a second set of eyes on it? greg@thinkitdata.com · 678-643-4065