How to properly use kexec with systemd on CentOS 7?
I figured out a way to make a kexec loading script that works well and will load the default kernel in grub, meaning it should load the new kernel after a kernel update.
File: /usr/bin/kexec-load
#!/usr/bin/env bash
GRUBBY_FILE="/var/log/grubby"
TMP=$(mktemp)
# Command "grubby --default-kernel" has a bug/feature that fsyncs
# after writting each line to a debug log file, making it slow (several seconds).
# Workaround is to write to /dev/null instead.
if [ -e $GRUBBY_FILE ]
then rm -f $GRUBBY_FILE
fi
ln -s /dev/null $GRUBBY_FILE
KERNEL_IMG=$(grubby --default-kernel)
unlink $GRUBBY_FILE
# Get the detailed information of the default kernel (as seen by grub)
# This will create a temporary file in /tmp
grubby --info=$KERNEL_IMG | grep -v title > $TMP
source $TMP
rm $TMP
# Simple log to see if this script gets executed
date --rfc-3339=seconds >> /var/log/kexec
# Load (prepare) the kernel for execution
kexec -l $kernel --initrd=$initrd --command-line="root=$root $args"
File: /etc/systemd/system/kexec-load.service
[Unit]
Description=loads the kernel
Documentation=man:kexec(8)
DefaultDependencies=no
Before=shutdown.target umount.target final.target
[Service]
Type=oneshot
ExecStart=/usr/bin/kexec-load
[Install]
WantedBy=kexec.target
$ chmod +x /usr/bin/kexec-load
$ systemctl enable kexec-load.service
$ systemctl kexec
This is pretty straightforward.
First stage the kernel to be booted:
kexec -l /boot/vmlinuz-3.10.0-123.6.3.el7.x86_64 \
--initrd=/boot/initramfs-3.10.0-123.6.3.el7.x86_64.img \
--command-line="root=/dev/mapper/centos-root ro rd.lvm.lv=centos/swap vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos/root crashkernel=auto vconsole.keymap=us rhgb quiet LANG=en_US.UTF-8"
These options having been swiped out of the generated grub configuration.
Now tell systemd to do its magic.
systemctl start kexec.target
Or on more recent versions of systemd:
systemctl kexec
A few seconds later, you will be up in your new kernel.
I've recently written a distribution-agnostic script to help automate this (bug reports welcome).