1

How to use kexec in Fedora Linux to fast-reboot automatically to the newest installed kernel?

kexec --load command line usage is rather verbose — you have to provide:

  • kernel image, like /boot/vmlinuz-3.18.8-201.fc21.x86_64,
  • corresponding initrd file, like --initrd=/boot/initramfs-3.18.8-201.fc21.x86_64.img,
  • kernel command-line arguments, like --append="root=/dev/sda1 ro rhgb quiet LANG=en_US.UTF-8", from /proc/cmdline.

It is tedious to type it all, as even tab completion does not help too much, because there are usually multiple kernels installed.

With UEFI systems and Windows 8 dual-boot laptop it is easy to miss just a right moment to interrupt reboot to select non-default system. If you miss then you have to wait for Windows 8 to start, maybe automatically login, reboot and try again.

0

2 Answers 2

1

I don't think it's a good idea to re-use the current command-line arguments like you have now. I recommend checking the GRUB configuration instead, assuming you have GRUB set up. The grubby command can get you the required info. The exact output format of grubby --info is a bit inconsistent, but it's enough to be usable.

# cat /usr/local/bin/kexec-load
#!/bin/sh
set -eu
index=$(grubby --default-index)
grubby --info="$index" | {
  IFS==
  while read field value
  do
    case $value in
    \"*\")
      eval "$field=$value" ;;
    *)
      eval "$field=\$value" ;;
    esac
  done
  unset IFS

  set -x
  kexec -l --initrd="$initrd" --append="root=$root $args" -- "$kernel"
}

This doesn't use the latest available kernel, it uses whichever one is configured in Grub as the kernel to use by default. You already have code to determine the latest installed kernel, and you can easily change the --info="$index" to --info=$latestkernel if that's what you want to use instead.

0

I've solved this by creating a script /usr/local/bin/kexec-load-latest that automatically selects the most recent kernel and loads it to kexec.

#!/bin/bash

set -x

NEWEST_KERNEL_VERSION="$(
  rpm --query --queryformat='%{VERSION}-%{RELEASE}.%{ARCH}\n' kernel \
  | sort --version-sort --reverse \
  | head --lines=1
)"
kexec --load --append="`cat /proc/cmdline`" \
  --initrd="/boot/initramfs-$NEWEST_KERNEL_VERSION.img" \
  "/boot/vmlinuz-$NEWEST_KERNEL_VERSION"

Set it to executable chmod a+x /usr/local/bin/kexec-load-latest so I can simply:

  • run sudo /usr/local/bin/kexec-load-latest,
  • then sudo reboot or use my desktop environment's reboot function.

Tested on Fedora 21. This should also work in other, recent rpm based distributions like RHEL or CentOS.

1
  • I wonder should not cat /proc/cmdline be updated, typically by sed, to include the new version of the kernel? Jul 20, 2015 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .