Difference between revisions of "Kexec"
(Add a unit file that works when /boot is on a different partition)
|Line 75:||Line 75:|
Revision as of 20:28, 10 August 2013
Kexec is a system call that enables you to load and boot into another kernel from the currently running kernel. This is useful for kernel developers or other people who need to reboot very quickly without waiting for the whole BIOS boot process to finish. Note that there may appear some problems and kexec may not work correctly for you because the devices won't fully reinitiate when using this method.
Rebooting using kexec
You will need to create a new unit file,
kexec-load@.service, that will load the specified kernel to be kexec'ed.
# vim /etc/systemd/system/kexec-load@.service
[Unit] Description=load %i kernel into the current kernel Documentation=man:kexec(8) DefaultDependencies=no Before=shutdown.target umount.target final.target [Service] Type=oneshot ExecStart=/usr/bin/kexec -l /boot/vmlinuz-%i --initrd=/boot/initramfs-%i.img --reuse-cmdline [Install] WantedBy=kexec.target
Then enable the service file for the kernel you want to load, for example simply the default kernel
# systemctl enable kexec-load@linux
ln -s '/etc/systemd/system/kexec-load@.service' '/email@example.com'
Ensure that the shutdown hook is not part of your initramfs image by removing it from the
HOOKS array in
/etc/mkinitcpio.conf. If it is, remove it and rebuild your initrd image with
mkinitcpio -p linux.
Then to kexec
# systemctl kexec
If you wish to load a different kernel for the next kexec, for example
linux-lts, disable the service for the current kernel and enable the one for the new kernel:
# systemctl disable kexec-load@linux # systemctl enable kexec-load@linux-lts
Separate /boot partition
The above systemd unit file will fail if /boot is not on the root file system, as systemd will likely unmount /boot before it runs the kexec-load unit file. An alternative approach is to load a "hook" unit file that does nothing on startup and invokes kexec upon termination. By making this unit file conflict with kexec.target and only kexec.target, you can ensure the new kernel gets loaded early enough and only after a "systemctl kexec" command. Here is an alternate
/etc/systemd/system/kexec-load@.service file that follows this strategy:
[Unit] Description=hook to load vmlinuz-%i kernel upon kexec Documentation=man:kexec(8) DefaultDependencies=no RequiresMountsFor=/boot/vmlinuz-%i Conflicts=kexec.target [Service] Type=oneshot ExecStart=-/usr/bin/true RemainAfterExit=yes ExecStop=/usr/bin/kexec -l /boot/vmlinuz-%i --initrd=/boot/initramfs-%i.img --reuse-cmdline [Install] WantedBy=basic.target
Unfortunately, while the above file works reliably on some machines, the ExecStop command seems to fail to run on others. Hence, at present there does not appear to be a known reliable way to make "systemctl kexec" do the right thing on all machines. Until then, it may be best to call kexec manually before invoking "systemctl kexec" (or even "reboot", which will do the right thing when a kernel is loaded).
It is also perfectly legal to invoke kexec manually:
# kexec -l /boot/vmlinuz-linux --initrd=/boot/initramfs-linux.img --reuse-cmdline # kexec -e