From 1482d4437a76bf8420787b68a8482c64eca08c54 Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Sun, 11 Oct 2026 14:38:57 +0200 Subject: [PATCH] boot-tree: portable unit, dracut and mkinitcpio hooks, self-quieting console, minimal NixOS module --- README.md | 51 +++++++- demo/host.nix | 8 +- demo/test.nix | 25 ++-- flake.nix | 17 ++- nixos-module.nix | 118 +++---------------- packaging/dracut/.shellcheckrc | 1 + packaging/dracut/90boot-tree/module-setup.sh | 16 +++ packaging/mkinitcpio/install/sd-boot-tree | 15 +++ packaging/systemd/boot-tree.service | 20 ++++ src/console.rs | 23 ++++ src/feed.rs | 15 +++ src/run.rs | 9 +- src/systemd.rs | 11 ++ 13 files changed, 208 insertions(+), 121 deletions(-) create mode 100644 packaging/dracut/.shellcheckrc create mode 100755 packaging/dracut/90boot-tree/module-setup.sh create mode 100755 packaging/mkinitcpio/install/sd-boot-tree create mode 100644 packaging/systemd/boot-tree.service diff --git a/README.md b/README.md index 74969dc..a542f2e 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,27 @@ A live dependency tree of systemd jobs on the boot console, in the spirit of nix-output-monitor. It replaces the scrolling kernel and systemd lines with a tree of pending units and a timeline bar per row. +## Requirements + +- Linux with **systemd ≥ 246 as PID 1** (private socket `/run/systemd/private`, + `Manager.SetShowStatus`). +- Runs as **root**, from the shipped `boot-tree.service`. +- **libdbus-1** at runtime. +- A **virtual terminal** (`CONFIG_VT`); the unit draws on `/dev/tty12`. +- Optional: a **systemd-based initrd** (NixOS `boot.initrd.systemd`, dracut, or + mkinitcpio's `systemd` hook). Without one the tree starts in stage 2, once the + real root is mounted. +- Optional: a large console font on HiDPI screens, e.g. Terminus `ter-v32n` + through the distro's console font setting. + +No kernel parameters are needed: while it runs, boot-tree lowers the kernel +console log level and turns systemd's status output off, and restores both when +it hands the console over. + ## How it works -- Starts as an initrd unit (needs `boot.initrd.systemd.enable`), about a second - after the kernel. +- Starts as an initrd unit when the initrd runs systemd, about a second after + the kernel. - Talks to PID 1 over the private socket `/run/systemd/private` with libdbus: `Subscribe`, `ListJobs`, unit properties, and the `JobNew`/`JobRemoved`/ `PropertiesChanged` signals. @@ -18,17 +35,39 @@ tree of pending units and a timeline bar per row. previous one when the display manager starts or the default target is reached, so gettys and greeters on tty1 cannot draw over it. -## Usage +## Installing + +The package ships everything a system needs under `lib/`: + +| File | Purpose | +| --- | --- | +| `lib/systemd/system/boot-tree.service` | the unit, used in both the initrd and stage 2 | +| `lib/dracut/modules.d/90boot-tree/` | dracut module | +| `lib/initcpio/install/sd-boot-tree` | mkinitcpio hook | + +### NixOS ```nix inputs.boot-tree.url = "git+file:/home/wvhulle/Code/boot-tree?ref=refs/heads/main"; -# in a host's modules: +# in the host's modules: boot-tree.nixosModules.default { services.boot-tree.enable = true; } ``` -Options: `tty`, `holdMs`, `quietConsole` (silences kernel/systemd console -output and loads a Terminus font in the initrd), `consoleFont`. +With `boot.initrd.systemd.enable` the unit is added to the initrd as well. + +### Other distributions + +1. Install `boot-tree` to `/usr/bin` and the files above under `/usr/lib`. +2. `systemctl enable boot-tree.service`. +3. To start it in the initrd: + - dracut: `add_dracutmodules+=" boot-tree "` in `/etc/dracut.conf.d/boot-tree.conf`, + then `dracut --force`; + - mkinitcpio: add `sd-boot-tree` after `systemd` in `HOOKS`, then + `mkinitcpio -P`. + +To draw on another terminal, override `TTYPath=` with +`systemctl edit boot-tree.service`. ## Testing diff --git a/demo/host.nix b/demo/host.nix index c3f3ca0..e27b929 100644 --- a/demo/host.nix +++ b/demo/host.nix @@ -35,9 +35,11 @@ in }; }; - services.boot-tree = { - enable = true; - consoleFont = "ter-v20n"; + services.boot-tree.enable = true; + + console = { + earlySetup = true; + font = "${pkgs.terminus_font}/share/consolefonts/ter-v20n.psf.gz"; }; systemd.services = lib.mapAttrs (_: demoService) demoUnits.services // { diff --git a/demo/test.nix b/demo/test.nix index 9c30160..c66087a 100644 --- a/demo/test.nix +++ b/demo/test.nix @@ -3,16 +3,19 @@ name = "boot-tree"; enableOCR = true; - nodes.machine = { - imports = [ demoHost ]; - virtualisation = { - memorySize = 2048; - qemu.options = [ - "-vga none" - "-device virtio-vga,xres=1280,yres=800" - ]; + nodes.machine = + { lib, ... }: + { + imports = [ demoHost ]; + systemd.settings.Manager.ShowStatus = lib.mkForce true; + virtualisation = { + memorySize = 2048; + qemu.options = [ + "-vga none" + "-device virtio-vga,xres=1280,yres=800" + ]; + }; }; - }; testScript = '' machine.start() @@ -36,6 +39,10 @@ assert "handing the console over" in journal, "never handed the console over" machine.succeed("test \"$(systemctl show -p Result --value boot-tree)\" = success") + console_level = machine.succeed("cut -f1 /proc/sys/kernel/printk").strip() + assert console_level != "1", f"kernel console level was not restored: {console_level}" + show_status = machine.succeed("systemctl show -p ShowStatus --value").strip() + assert show_status == "yes", f"systemd status output was not restored: {show_status}" machine.screenshot("handed-over") ''; } diff --git a/flake.nix b/flake.nix index a67f948..31599bf 100644 --- a/flake.nix +++ b/flake.nix @@ -67,8 +67,23 @@ { default = craneLib.buildPackage { inherit (buildInputsFor pkgs) nativeBuildInputs buildInputs; - src = craneLib.cleanCargoSource ./.; + src = lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + (craneLib.fileset.commonCargoSources ./.) + ./packaging + ]; + }; strictDeps = true; + postInstall = '' + install -Dm644 packaging/systemd/boot-tree.service $out/lib/systemd/system/boot-tree.service + substituteInPlace $out/lib/systemd/system/boot-tree.service \ + --replace-fail /usr/bin/boot-tree $out/bin/boot-tree + install -Dm755 packaging/dracut/90boot-tree/module-setup.sh \ + $out/lib/dracut/modules.d/90boot-tree/module-setup.sh + install -Dm644 packaging/mkinitcpio/install/sd-boot-tree \ + $out/lib/initcpio/install/sd-boot-tree + ''; meta.mainProgram = "boot-tree"; meta.platforms = lib.platforms.linux; }; diff --git a/nixos-module.nix b/nixos-module.nix index 2763556..1305953 100644 --- a/nixos-module.nix +++ b/nixos-module.nix @@ -7,120 +7,36 @@ }: let cfg = config.services.boot-tree; - - unit = { - description = "Live dependency tree of boot jobs on ${cfg.tty}"; - wantedBy = [ "sysinit.target" ]; - after = [ "systemd-vconsole-setup.service" ]; - before = [ - "display-manager.service" - "shutdown.target" - ]; - conflicts = [ "shutdown.target" ]; - unitConfig = { - DefaultDependencies = false; - IgnoreOnIsolate = true; - }; - serviceConfig = { - Type = "simple"; - ExecStart = "@${lib.getExe cfg.package} @boot-tree --hold-ms ${toString cfg.holdMs}"; - StandardInput = "tty"; - StandardOutput = "tty"; - StandardError = "journal"; - TTYPath = cfg.tty; - TTYReset = true; - TTYVTDisallocate = false; - }; - }; in { options.services.boot-tree = { enable = lib.mkEnableOption '' - a live dependency tree of systemd jobs on the boot console. It starts in - the systemd initrd, survives the switch to the real root, and hands the - console over once the display manager starts or the default target is - reached + a live dependency tree of systemd jobs on the boot console. With a + systemd initrd it starts there and survives the switch to the real root; + otherwise it starts in stage 2. It hands the console back once the + display manager starts or the default target is reached ''; package = lib.mkOption { type = lib.types.package; default = self.packages.${pkgs.stdenv.hostPlatform.system}.default; defaultText = lib.literalExpression "boot-tree.packages.\${system}.default"; - description = "The boot-tree package."; - }; - - tty = lib.mkOption { - type = lib.types.str; - default = "/dev/tty12"; - description = '' - Virtual terminal the tree is drawn on. boot-tree switches to it while - it runs, so gettys, greeters and kernel output on other terminals - cannot draw over the tree, and switches back to the previously active - terminal when it hands the console over. - ''; - }; - - holdMs = lib.mkOption { - type = lib.types.ints.unsigned; - default = 0; - description = "Milliseconds the final frame stays on screen before the console is handed over."; - }; - - quietConsole = lib.mkOption { - type = lib.types.bool; - default = true; - description = '' - Silence kernel, udev and systemd status output on the console so it - does not draw over the tree, and load a large Terminus console font in - the initrd. - ''; - }; - - consoleFont = lib.mkOption { - type = lib.types.str; - default = "ter-v32n"; - example = "ter-v20n"; - description = "Terminus console font loaded when {option}`services.boot-tree.quietConsole` is set."; + description = "The boot-tree package, which ships the systemd unit."; }; }; - config = lib.mkIf cfg.enable ( - lib.mkMerge [ - { - assertions = [ - { - assertion = config.boot.initrd.systemd.enable; - message = "services.boot-tree needs boot.initrd.systemd.enable to start before the root switch."; - } - ]; + config = lib.mkIf cfg.enable { + environment.systemPackages = [ cfg.package ]; - environment.systemPackages = [ cfg.package ]; + systemd = { + packages = [ cfg.package ]; + services.boot-tree.wantedBy = [ "sysinit.target" ]; + }; - systemd.services.boot-tree = unit; - - boot.initrd.systemd = { - services.boot-tree = unit; - storePaths = [ (lib.getExe cfg.package) ]; - }; - } - - (lib.mkIf cfg.quietConsole { - boot = { - consoleLogLevel = lib.mkDefault 3; - kernelParams = [ - "quiet" - "systemd.show_status=error" - "rd.systemd.show_status=error" - "udev.log_level=3" - "rd.udev.log_level=3" - ]; - }; - - console = { - earlySetup = true; - font = lib.mkDefault "${pkgs.terminus_font}/share/consolefonts/${cfg.consoleFont}.psf.gz"; - }; - }) - ] - ); + boot.initrd.systemd = lib.mkIf config.boot.initrd.systemd.enable { + packages = [ cfg.package ]; + storePaths = [ (lib.getExe cfg.package) ]; + services.boot-tree.wantedBy = [ "sysinit.target" ]; + }; + }; } diff --git a/packaging/dracut/.shellcheckrc b/packaging/dracut/.shellcheckrc new file mode 100644 index 0000000..026ee64 --- /dev/null +++ b/packaging/dracut/.shellcheckrc @@ -0,0 +1 @@ +disable=SC2154 diff --git a/packaging/dracut/90boot-tree/module-setup.sh b/packaging/dracut/90boot-tree/module-setup.sh new file mode 100755 index 0000000..872ed83 --- /dev/null +++ b/packaging/dracut/90boot-tree/module-setup.sh @@ -0,0 +1,16 @@ +#!/usr/bin/bash + +check() { + require_binaries boot-tree || return 1 + return 255 +} + +depends() { + echo systemd +} + +install() { + inst_binary boot-tree + inst_simple "$systemdsystemunitdir/boot-tree.service" + $SYSTEMCTL -q --root "$initdir" enable boot-tree.service +} diff --git a/packaging/mkinitcpio/install/sd-boot-tree b/packaging/mkinitcpio/install/sd-boot-tree new file mode 100755 index 0000000..b77549d --- /dev/null +++ b/packaging/mkinitcpio/install/sd-boot-tree @@ -0,0 +1,15 @@ +#!/usr/bin/bash + +build() { + add_binary boot-tree + add_systemd_unit boot-tree.service + add_symlink /usr/lib/systemd/system/sysinit.target.wants/boot-tree.service \ + /usr/lib/systemd/system/boot-tree.service +} + +help() { + cat < Option { + let levels = fs::read_to_string(KERNEL_PRINTK).ok()?; + let previous_level = levels.split_whitespace().next()?.to_owned(); + fs::write(KERNEL_PRINTK, ALERTS_ONLY).ok()?; + Some(Self { previous_level }) + } +} + +impl Drop for QuietKernelConsole { + fn drop(&mut self) { + let _ = fs::write(KERNEL_PRINTK, &self.previous_level); + } +} #[repr(C)] struct VtState { diff --git a/src/feed.rs b/src/feed.rs index f1e19ce..ac213f6 100644 --- a/src/feed.rs +++ b/src/feed.rs @@ -18,6 +18,8 @@ use crate::{ const RECONNECT_PATIENCE: Duration = Duration::from_secs(60); const SWITCH_ROOT_UNIT: &str = "initrd-switch-root.service"; +const SILENT_STATUS: &str = "no"; +const CONFIGURED_STATUS: &str = ""; pub struct Feed { scope: Scope, @@ -91,6 +93,9 @@ impl Feed { fn connect(&mut self, state: &mut BootState) -> Result { let session = Session::open(self.scope)?; session.subscribe()?; + if self.scope == Scope::System { + session.set_show_status(SILENT_STATUS).ok(); + } let requested_root = match &self.target { Some(target) => target.clone(), None => session.default_target()?, @@ -176,3 +181,13 @@ impl Feed { } } } + +impl Drop for Feed { + fn drop(&mut self) { + if let Some(session) = &self.session + && self.scope == Scope::System + { + session.set_show_status(CONFIGURED_STATUS).ok(); + } + } +} diff --git a/src/run.rs b/src/run.rs index 0304cae..f3d1113 100644 --- a/src/run.rs +++ b/src/run.rs @@ -21,7 +21,11 @@ use rustix::system::uname; use signal_hook::{consts::SIGTERM, flag}; use crate::{ - clock::monotonic_now, console::ForegroundVt, error::Error, feed::Feed, sysroot::INITRD_RELEASE, + clock::monotonic_now, + console::{ForegroundVt, QuietKernelConsole}, + error::Error, + feed::Feed, + sysroot::INITRD_RELEASE, systemd::Scope, }; @@ -44,6 +48,9 @@ pub fn run(options: Options) -> Result<(), Error> { }; let mut state = BootState::new(phase); let mut feed = Feed::new(options.scope, options.target); + let _quiet_kernel = (options.scope == Scope::System) + .then(QuietKernelConsole::take) + .flatten(); let _foreground = ForegroundVt::take(); let mut terminal = ratatui::try_init()?; let watched = watch( diff --git a/src/systemd.rs b/src/systemd.rs index c2c6e96..0870893 100644 --- a/src/systemd.rs +++ b/src/systemd.rs @@ -129,6 +129,17 @@ impl Session { Ok(()) } + pub fn set_show_status(&self, mode: &str) -> Result<(), Error> { + self.call( + DESTINATION, + MANAGER_PATH, + MANAGER_INTERFACE, + "SetShowStatus", + (mode,), + )?; + Ok(()) + } + pub fn default_target(&self) -> Result { let reply = self.call( DESTINATION,