仅重新启动用户空间

Fra*_*ois 5 linux kernel btrfs

是否可以仅重新启动用户空间?比如关闭内核之前的所有内容,然后从 PID 1 重新启动?

我想对我的根 btrfs 文件系统进行快照,并在该快照上快速启动。

Cos*_*ușă 2

您可能受到 macOSlaunchctl reboot userspace功能的启发;不幸的是,systemd缺少此功能(systemd可以使用 重新启动自身,但这不会终止所有子项),但是您可以通过使用systemctl daemon-reexec获得比 macOS 更好的结果reboot userspacekexecLinux 内核的功能,

\n

它的作用是终止所有用户空间进程以及所有内核线程,然后重新启动当前加载的(甚至用户指定的备用)内核,从而有效地从引导加载程序将初始化传递给内核的那一刻开始,减去通过重新启动所需的硬件重置阶段。

\n

来自kexec(8)手册页:

\n
DESCRIPTION\n       kexec  is  a system call that enables you to load and boot into another\n       kernel from the currently running kernel.  kexec performs the  function\n       of  the  boot loader from within the kernel. The primary difference be\xe2\x80\x90\n       tween a standard system boot and a kexec boot is that the hardware ini\xe2\x80\x90\n       tialization  normally  performed  by the BIOS or firmware (depending on\n       architecture) is not performed during a kexec boot. This has the effect\n       of reducing the time required for a reboot.\n
Run Code Online (Sandbox Code Playgroud)\n

如果您不想学习 kexec 参数,有一个ruby​​ 脚本可以简化使用,它会解析 grub 配置文件并允许您选择甚至不同的内核,但请注意,它似乎不理解 Fedora 的新功能引导加载程序规范配置文件,在这种情况下,您必须使用裸工具执行操作。kexeckexec

\n

作为参考,以下是在 EFI 上的 Ubuntu 22.04 上运行 kexec-reboot 的输出

\n
$ sudo kexec-reboot -iv\nRead GRUB configuration from /boot/grub/grub.cfg\n\nSelect a kernel to stage:\n\n1: Ubuntu\n2: Ubuntu, with Linux 5.19.0-35-generic\n3: Ubuntu, with Linux 5.19.0-35-generic (recovery mode)\n4: Ubuntu, with Linux 5.15.0-67-generic\n5: Ubuntu, with Linux 5.15.0-67-generic (recovery mode)\n6: Ubuntu, with Linux 5.15.0-58-generic\n7: Ubuntu, with Linux 5.15.0-58-generic (recovery mode)\n\nYour selection: 1\nStaging Ubuntu\nStaging kernel Ubuntu\nUnloading previous kexec target, if any\nRunning /sbin/kexec -l /boot/vmlinuz-5.19.0-35-generic \n--append=\'root=UUID=9e994b93-047b-46a6-9a71-51dfcb4e9598 ro intel_iommu=on iommu=pt \ni915.enable_gvt=1 zswap.enabled=1 zswap.compressor=zstd resume=/dev/disk/by-\nuuid/01b394fe-b29e-499c-a722-5f8d56cec3cd quiet splash $vt_handoff\' --initrd=/boot\n/initrd.img-5.19.0-35-generic\n
Run Code Online (Sandbox Code Playgroud)\n

添加-r到同时重新启动

\n

API 调用本身声明文件系统未卸载,但是 kexec 工具似乎还调用关闭序列减去重新启动计算机的部分,从而同步文件系统,因此您的服务和进程应该像正常关闭一样优雅地终止。

\n