5

Is it possible to restart only the userspace? Like shutdown everything up to the kernel and then restart from PID 1?

I would like to snapshot my root btrfs filesystem and quickly boot on that snapsnot.

3
  • Pressing ctrl-alt-backspace will reboot the GUI. Get you to a login prompt in less than a second. Next you have to restart any services that are running outdated code. Commented Oct 2, 2020 at 18:02
  • But I want to reboot the whole userspace on another partition/subvolume and restart everything there including systemd.
    – Francois
    Commented Oct 2, 2020 at 18:36
  • Basically change root= from CMDLINE
    – Francois
    Commented Oct 2, 2020 at 18:37

2 Answers 2

2

You are probably inspired by macOS launchctl reboot userspace feature; unfortunately systemd lacks this feature (systemd can re-launch itself with systemctl daemon-reexec, but this does not terminate all children), however you can get an even better result than macOS' reboot userspace by using the kexec feature of the Linux kernel.

What it does it terminates all userspace processes as well as all kernel threads, then restarts the currently loaded (or even user-specified alternate) kernel, effectively resulting in a start from the moment the bootloader would pass the initialization to the kernel, minus going through the hardware reset stage that a reboot would do.

from kexec(8) manual page:

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

There is a ruby script that eases the use of kexec if you do not want to learn kexec arguments, it parses the grub configuration files and allows you to choose even a different kernel, however note that it does not seem to understand Fedora's new bootloader spec configuration files, in which case you must perform the actions using bare kexec tool.

For reference, here is output from running kexec-reboot on Ubuntu 22.04 on EFI

$ sudo kexec-reboot -iv
Read GRUB configuration from /boot/grub/grub.cfg

Select a kernel to stage:

1: Ubuntu
2: Ubuntu, with Linux 5.19.0-35-generic
3: Ubuntu, with Linux 5.19.0-35-generic (recovery mode)
4: Ubuntu, with Linux 5.15.0-67-generic
5: Ubuntu, with Linux 5.15.0-67-generic (recovery mode)
6: Ubuntu, with Linux 5.15.0-58-generic
7: Ubuntu, with Linux 5.15.0-58-generic (recovery mode)

Your selection: 1
Staging Ubuntu
Staging kernel Ubuntu
Unloading previous kexec target, if any
Running /sbin/kexec -l /boot/vmlinuz-5.19.0-35-generic 
--append='root=UUID=9e994b93-047b-46a6-9a71-51dfcb4e9598 ro intel_iommu=on iommu=pt 
i915.enable_gvt=1 zswap.enabled=1 zswap.compressor=zstd resume=/dev/disk/by-
uuid/01b394fe-b29e-499c-a722-5f8d56cec3cd quiet splash $vt_handoff' --initrd=/boot
/initrd.img-5.19.0-35-generic

Add -r to also reboot

The API call itself states that filesystems are not umounted, however the kexec tool appears to also invoke a shutdown sequence minus the part where it reboots the machine, thus syncing the filesystems, so your services and processes should terminate gracefully like in a normal shutdown.

0

Remember what you're running (to be honest it doesn't actually tell you what you're really running, for that refer to systemd : how to get the running target

systemctl get-default 

Switch to single-user mode:

systemctl isolate rescue.target

You may want to kill some remaining processes.

Switch back to whatever mode you had in the beginning (normally it's graphical.target):

systemctl isolate graphical.target

You may also use init 1 and then init 5 which are still supported.

You must log in to answer this question.

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