如何在CentOS 7上正确使用kexec与systemd?

我想用kexec来加速CentOS 7机器的重新启动。 我怎么能以一种很好地集成现有的关机/重启系统目标的方式做到这一点? 什么是适当的(官方)方式来做到这一点?

我想出了一个方法来使一个kexec加载脚本运行良好,并将在grub中加载默认的内核,这意味着它应该在内核更新后加载新的内核。

文件:/ 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" 

文件:/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 

这很简单。

第一阶段内核启动:

 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" 

这些选项已经从生成的grubconfiguration中删除。

现在告诉systemd做它的魔法。

 systemctl start kexec.target 

几秒钟后,你将会进入新的内核。


我最近写了一个分发不可知的脚本来帮助自动化(错误报告欢迎)。