boot-tree: live systemd boot dependency tree on a dedicated VT

This commit is contained in:
Willem Vanhulle committed 2026-10-11 15:54:28 +02:00
commit 097caf81a2
24 files changed
+4817

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env nu
def user-unit-dir []: nothing -> path {
$env.XDG_RUNTIME_DIR | path join systemd user
}
def unit-names [units: path]: nothing -> list<string> {
ls $units | get name | path basename
}
def install-units [units: path] {
let target_dir = user-unit-dir
mkdir $target_dir
unit-names $units | each {|name| ^ln -sfn ($units | path join $name) ($target_dir | path join $name) } | ignore
^systemctl --user daemon-reload
}
def "main start" [--units: path, --target: string] {
install-units $units
^systemctl --user stop ...(unit-names $units)
^systemctl --user start --no-block $target
print $"started ($target); watch it with: boot-tree --user --target ($target) --hold-ms 3000"
}
def "main remove" [--units: path, --target: string] {
^systemctl --user stop ...(unit-names $units)
unit-names $units | each {|name| rm -f (user-unit-dir | path join $name) } | ignore
^systemctl --user daemon-reload
}
def main [--units: path, --target: string] {
print "usage: boot-tree-demo-units (start | remove)"
}
+56
View File
@@ -0,0 +1,56 @@
{
demoUnits,
lib,
pkgs,
...
}:
let
demoService = spec: {
after = map (dependency: "${dependency}.service") (spec.after or [ ]);
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
ExecStart = [
"${pkgs.coreutils}/bin/sleep ${spec.seconds}"
]
++ lib.optional (spec.fails or false) "${pkgs.coreutils}/bin/false";
};
};
in
{
networking.hostName = "boot-tree-demo";
system.stateVersion = "26.05";
boot.initrd.systemd = {
enable = true;
services.demo-initrd-probe = {
description = "boot-tree demo: slow initrd step";
wantedBy = [ "initrd.target" ];
before = [ "initrd.target" ];
unitConfig.DefaultDependencies = false;
serviceConfig = {
Type = "oneshot";
ExecStart = "/bin/sleep ${demoUnits.initrdProbeSeconds}";
};
};
};
services.boot-tree = {
enable = true;
consoleFont = "ter-v20n";
};
systemd.services = lib.mapAttrs (_: demoService) demoUnits.services // {
display-manager.after = [ "multi-user.target" ];
};
services.greetd = {
enable = true;
settings.default_session.command = "${pkgs.greetd}/bin/agreety --cmd ${pkgs.bashInteractive}/bin/bash";
};
users.users.demo = {
isNormalUser = true;
password = "demo";
};
}
+18
View File
@@ -0,0 +1,18 @@
{
boot.loader.grub.enable = false;
services.getty.autologinUser = "root"; fileSystems."/" = {
device = "/dev/disk/by-label/nixos";
fsType = "ext4";
};
virtualisation.vmVariant.virtualisation = {
memorySize = 2048;
cores = 2;
graphics = true;
qemu.options = [
"-vga none"
"-device virtio-vga,xres=1920,yres=1080"
];
};
}
+41
View File
@@ -0,0 +1,41 @@
{ demoHost }:
{
name = "boot-tree";
enableOCR = true;
nodes.machine = {
imports = [ demoHost ];
virtualisation = {
memorySize = 2048;
qemu.options = [
"-vga none"
"-device virtio-vga,xres=1280,yres=800"
];
};
};
testScript = ''
machine.start()
machine.wait_for_text("initrd")
machine.screenshot("initrd")
machine.wait_for_text("demo-slow")
machine.screenshot("stage-2")
machine.wait_for_unit("greetd.service")
machine.wait_until_succeeds("systemctl show -p ActiveState --value boot-tree | grep -qx inactive")
journal = machine.succeed("journalctl -b -u boot-tree -o cat")
print(journal)
watched = [line for line in journal.splitlines() if line.startswith("boot-tree: watching")]
assert any(" in initrd " in line for line in watched), "never ran in the initrd"
assert any(" in stage 2 " in line for line in watched), "never reconnected after the root switch"
pids = {line.split(" as pid ")[1].split()[0] for line in watched}
assert len(pids) == 1, f"the initrd process did not survive the root switch: {pids}"
assert "handing the console over" in journal, "never handed the console over"
machine.succeed("test \"$(systemctl show -p Result --value boot-tree)\" = success")
machine.screenshot("handed-over")
'';
}
+32
View File
@@ -0,0 +1,32 @@
{
target = "boot-tree-demo";
initrdProbeSeconds = "6";
services = {
demo-disk.seconds = "2";
demo-db = {
seconds = "3";
after = [ "demo-disk" ];
};
demo-cache.seconds = "1.5";
demo-web = {
seconds = "2";
after = [
"demo-db"
"demo-cache"
];
};
demo-fetch.seconds = "4";
demo-flaky = {
seconds = "1";
fails = true;
};
demo-slow.seconds = "8";
demo-report = {
seconds = "1";
after = [
"demo-web"
"demo-fetch"
];
};
};
}
+55
View File
@@ -0,0 +1,55 @@
{
demoUnits,
lib,
coreutils,
linkFarm,
nushell,
writeShellApplication,
writeText,
}:
let
toUnitFile = lib.generators.toINI { listsAsDuplicateKeys = true; };
serviceFile =
name: spec:
writeText "${name}.service" (toUnitFile {
Unit = {
Description = "boot-tree demo: ${name}";
After = map (dependency: "${dependency}.service") (spec.after or [ ]);
};
Service = {
Type = "oneshot";
ExecStart = [
"${coreutils}/bin/sleep ${spec.seconds}"
]
++ lib.optional (spec.fails or false) "${coreutils}/bin/false";
};
});
serviceNames = map (name: "${name}.service") (lib.attrNames demoUnits.services);
targetFile = writeText "${demoUnits.target}.target" (toUnitFile {
Unit = {
Description = "boot-tree demo target";
Wants = serviceNames;
After = serviceNames;
};
});
units = linkFarm "boot-tree-demo-units" (
lib.mapAttrsToList (name: spec: {
name = "${name}.service";
path = serviceFile name spec;
}) demoUnits.services
++ [
{
name = "${demoUnits.target}.target";
path = targetFile;
}
]
);
in
writeShellApplication {
name = "boot-tree-demo-units";
text = ''exec ${lib.getExe nushell} ${./demo-units.nu} "$@" --units ${units} --target ${demoUnits.target}.target'';
}