systemd soft reboot

Table of Contents

systemd soft reboot

I wanted write about a brand new feature in systemd 254 that has got me very excited, this feature is soft reboot which is described as:

systemd-soft-reboot.service is a system service that is pulled in by soft-reboot.target and is responsible for performing a userspace-only reboot operation. When invoked, it will send the SIGTERM signal to any processes left running (but does not follow up with SIGKILL, and does not wait for the processes to exit). If the /run/nextroot/ directory exists (which may be a regular directory, a directory mount point or a symlink to either) then it will switch the file system root to it. It then reexecutes the service manager off the (possibly now new) root file system, which will enqueue a new boot transaction as in a normal reboot.

man systemd-soft-reboot.service.

So in short it replaces your root file system while keeping your current kernel. Why do I find that exciting, well for a while I’ve been enthralled when Wes Payne from Linux Unplugged talks about how he uses kexec to switch into an operating system running entirely on RAM, that’s right the entire / running in ram which is a whole other magnitude of fast. But when I have tried to do this myself I struggled building a kernel (Linux from Scratch) would help but that’s a big chunk of time) and then trying to figure out exactly what is needed and would just hit wall after wall of kexecing into a black screen. The only time I had success is using nixos-generate -f kexec to build a kexec image that gets mounted in RAM as a loop back device and this is good but I find it slightly restricting and the time it takes to build the squashfs image on my laptop is just a bit too long for the time I have to tinker with it.

What this new soft reboot brings is I don’t need to worry about the kernel, I’ll just use the my existing one that works perfectly fine. All I need to do is create a new file system and mount it to /run/nextroot and we are off and running. Now this can be done with a simple block RAM-disk that is super fast and ephemeral, perfect for testing and I have a few ideas to see how this could augment some of the great file systems available on Linux.

Now let me show you how to get started with this on Arch Linux which just got the systemd update last night.

Soft reboot into RAM

Here are steps to create your very own RAM-disk and soft reboot into it using Arch Linux. Note any distro that has systemd >= 254 will be able to do something similar (Debian based distro’s have debootstrap in place of pacstrap).

#Create RAM-disk of 4GB
modprobe brd rd_nr=1 rd_size=$((4*1024*1024))
#Make a file system on the RAM-disk
mkfs.ext4 /dev/ram0
#Create directory to mount RAM-disk
mkdir /run/nextroot
#Mount the RAM-disk
mount /dev/ram0 /run/nextroot
#Create a new Arch Linux environment on the RAM-disk
pacstrap /run/nextroot base neofetch
#chroot into the new environment
arch-chroot /run/nextroot
#Set a password for root
passwd
#Exit the chroot with Ctrl+ D
#Soft reboot into your RAM-disk as your root directory
systemctl soft-reboot

Now you are running Arch Linux in RAM, let’s test the file system speed

#Write a 2GB file
dd if=/dev/zero of=ramdisk_is_fast bs=1M count=2048 status=progress
#Write a 2GB file in 1KB  blocks
dd if=/dev/zero of=ramdisk_is_fast bs=1K count=$((1024*2048)) status=progress
#Read that 2GB file
dd of=/dev/zero if=ramdisk_is_fast  status=progress

On my Laptop sporting an i5-7300U with a single channel of DDR4 RAM was able to get 1.5GB/s, 388MB/s and 340MB/s on the above respectively.

Wow that is awesome! My only problem now is I need more RAM!