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

Mat*_*rix 14 centos systemd

我想使用 kexec 来加速我的 CentOS 7 机器的重启。我怎样才能以与现有关闭/重启 systemd 目标很好地集成的方式做到这一点?这样做的正确(官方)方法是什么?

Mat*_*rix 12

我想出了一种方法来制作一个运行良好的 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"
Run Code Online (Sandbox Code Playgroud)

文件:/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 
Run Code Online (Sandbox Code Playgroud)
$ chmod +x /usr/bin/kexec-load
$ systemctl enable kexec-load.service
$ systemctl kexec
Run Code Online (Sandbox Code Playgroud)

  • 为了简单起见,我去掉了脚本文件,而是使用 `ExecStart=/bin/sh -c "kexec -l $$(grubby --default-kernel) --initrd=$$(grubby --default-内核 | sed 's!vmlinuz!initramfs!;s/$/.img/') --reuse-cmdline"` (3认同)

Mic*_*ton 7

这很简单。

启动内核的第一阶段:

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"
Run Code Online (Sandbox Code Playgroud)

这些选项已从生成的 grub 配置中删除。

现在告诉 systemd 发挥它的魔力。

systemctl start kexec.target
Run Code Online (Sandbox Code Playgroud)

或者在更新版本的 systemd 上:

systemctl kexec
Run Code Online (Sandbox Code Playgroud)

几秒钟后,您将进入新内核。


我最近编写了一个与分发无关的脚本来帮助自动化(欢迎报告错误)。