From 89eec1ddafe585f30c917c540a3ed2b666f287e5 Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Wed, 4 Nov 2026 18:59:06 +0100 Subject: [PATCH] fix: keep systemd status off the display manager until the boot finishes bootvine restored systemd's status output as soon as a display manager took the console. greetd's Type=idle only waits five seconds, so jobs still running then wrote status lines over the login screen. Restore the output when StartupFinished arrives instead, through a single private path that owns the console watch. A quit key or stopping the service still restores it at once, so a stuck boot stays visible. --- bootvine/src/console_session/watch_loop.rs | 23 +++-- bootvine/src/systemd_link/boot_feed.rs | 3 +- bootvine/src/systemd_link/systemd_bus.rs | 97 +++++++++++++++++----- docs/architecture.md | 9 +- docs/guide.md | 10 +++ vm-tests/scripted-initrd/check.nix | 17 +++- vm-tests/scripted-initrd/check.py | 24 +++++- vm-tests/slow-services/check.nix | 1 + vm-tests/slow-services/check.py | 26 +++++- 9 files changed, 169 insertions(+), 41 deletions(-) diff --git a/bootvine/src/console_session/watch_loop.rs b/bootvine/src/console_session/watch_loop.rs index e825949..20d238f 100644 --- a/bootvine/src/console_session/watch_loop.rs +++ b/bootvine/src/console_session/watch_loop.rs @@ -19,7 +19,7 @@ use crate::{ boot_feed::{RootRequest, bus_inputs}, link_error::Error, switch_root::INITRD_RELEASE, - systemd_bus::{Scope, Session, StatusOutput}, + systemd_bus::{Scope, Session, StatusRelease, restore_status_after_watch}, }, }; @@ -33,16 +33,6 @@ pub struct Settings { const FULL_REPAINT_INTERVAL: Duration = Duration::from_secs(5); -struct ConfiguredStatusOnExit; - -impl Drop for ConfiguredStatusOnExit { - fn drop(&mut self) { - if let Ok(session) = Session::open(Scope::System) { - session.set_show_status(StatusOutput::Configured).ok(); - } - } -} - fn boot_already_finished(settings: &Settings) -> bool { settings.scope == Scope::System && !settings.shutdown @@ -73,8 +63,14 @@ pub fn run(settings: &Settings) -> Result<(), Error> { } async fn watch_live_boot(settings: &Settings, phase: Phase) -> Result<(), Error> { + match settings.scope { + Scope::System => restore_status_after_watch(watch_on_console(settings, phase)).await, + Scope::User => watch_on_console(settings, phase).await.map(drop), + } +} + +async fn watch_on_console(settings: &Settings, phase: Phase) -> Result { let for_system = settings.scope == Scope::System; - let _restores_status = for_system.then_some(ConfiguredStatusOnExit); let requests = RequestWatch::open(); let prompt_check: fn() -> ScreenInput = if requests.is_some() { || ScreenInput::PromptCheck @@ -117,6 +113,7 @@ async fn watch_live_boot(settings: &Settings, phase: Phase) -> Result<(), Error> ratatui::restore(); match ending { WatchEnding::Failed(error) => Err(error), - WatchEnding::BootCompleted | WatchEnding::ViewerLeft => Ok(()), + WatchEnding::BootCompleted => Ok(StatusRelease::AfterBoot), + WatchEnding::ViewerLeft => Ok(StatusRelease::Immediately), } } diff --git a/bootvine/src/systemd_link/boot_feed.rs b/bootvine/src/systemd_link/boot_feed.rs index d3eef6c..5ddfb8f 100644 --- a/bootvine/src/systemd_link/boot_feed.rs +++ b/bootvine/src/systemd_link/boot_feed.rs @@ -25,7 +25,6 @@ use crate::{ switch_root::Sysroot, systemd_bus::{ BusConnection, FetchedUnit, JobListing, Scope, Session, Signal, SignalBatch, - StatusOutput, }, }, }; @@ -327,7 +326,7 @@ impl BootTracker { ) -> Result, Error> { connection.subscribe().await?; if self.scope == Scope::System { - connection.set_show_status(StatusOutput::Silent).await.ok(); + connection.silence_status_output().await.ok(); } let shutting_down = self.phase == Phase::Shutdown; let jobs: Vec = connection diff --git a/bootvine/src/systemd_link/systemd_bus.rs b/bootvine/src/systemd_link/systemd_bus.rs index 1042a38..b1fe8bc 100644 --- a/bootvine/src/systemd_link/systemd_bus.rs +++ b/bootvine/src/systemd_link/systemd_bus.rs @@ -1,5 +1,5 @@ use std::{ - future::ready, + future::{Future, pending, ready}, io, iter, mem, os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd}, pin::pin, @@ -11,9 +11,13 @@ use dbus::{ arg::{AppendAll, PropMap, RefArg, Variant}, channel::{BusType, Channel}, }; -use futures::{TryStreamExt, stream}; +use futures::{Stream, StreamExt, TryStreamExt, future::select, stream}; use rustix::event::{PollFd, PollFlags, Timespec, poll}; -use tokio::{io::unix::AsyncFd, time::timeout}; +use tokio::{ + io::unix::AsyncFd, + signal::unix::{Signal as OsSignal, SignalKind, signal}, + time::timeout, +}; use crate::{ boot_model::{Activity, JobKind, JobResult, UnitFacts, UnitKind, UnitTimeouts}, @@ -40,6 +44,8 @@ const PRIVATE_SOCKET: &str = "unix:path=/run/systemd/private"; const SIGNAL_RULE: &str = "type='signal',path_namespace='/org/freedesktop/systemd1'"; const CALL_TIMEOUT: Duration = Duration::from_secs(1); const UNLIMITED: Duration = Duration::from_micros(u64::MAX); +const STATUS_SILENT_MODE: &str = "no"; +const STATUS_RESET_MODE: &str = ""; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Scope { @@ -48,18 +54,9 @@ pub enum Scope { } #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum StatusOutput { - Silent, - Configured, -} - -impl StatusOutput { - fn bus_mode(self) -> &'static str { - match self { - Self::Silent => "no", - Self::Configured => "", - } - } +pub enum StatusRelease { + AfterBoot, + Immediately, } pub enum SignalBatch { @@ -157,6 +154,41 @@ fn read_boot_in_progress(reply: &Message) -> Result { Ok(finished.0 == 0) } +async fn boot_finish_or_lost_connection() -> Result<(), Error> { + let mut connection = BusConnection::register(Session::open(Scope::System)?)?; + connection.subscribe().await?; + if connection.boot_in_progress().await? { + connection + .into_signals() + .any(|signal| ready(matches!(signal, Signal::BootFinished))) + .await; + } + Ok(()) +} + +async fn termination_request(listener: io::Result) { + match listener { + Ok(mut listener) => { + listener.recv().await; + } + Err(_) => pending().await, + } +} + +pub async fn restore_status_after_watch( + watch: impl Future>, +) -> Result<(), Error> { + let terminated = termination_request(signal(SignalKind::terminate())); + let watched = watch.await; + if !matches!(watched, Ok(StatusRelease::Immediately)) { + select(pin!(terminated), pin!(boot_finish_or_lost_connection())).await; + } + if let Ok(session) = Session::open(Scope::System) { + session.restore_configured_status().ok(); + } + watched.map(drop) +} + impl Session { pub fn open(scope: Scope) -> Result { if scope == Scope::System @@ -193,8 +225,8 @@ impl Session { Ok(self.channel.send_with_reply_and_block(call, CALL_TIMEOUT)?) } - pub fn set_show_status(&self, output: StatusOutput) -> Result<(), Error> { - self.blocking_call(manager_call("SetShowStatus", (output.bus_mode(),))?)?; + fn restore_configured_status(&self) -> Result<(), Error> { + self.blocking_call(manager_call("SetShowStatus", (STATUS_RESET_MODE,))?)?; Ok(()) } @@ -297,8 +329,8 @@ impl BusConnection { Ok(()) } - pub async fn set_show_status(&mut self, output: StatusOutput) -> Result<(), Error> { - self.call(manager_call("SetShowStatus", (output.bus_mode(),))?) + pub async fn silence_status_output(&mut self) -> Result<(), Error> { + self.call(manager_call("SetShowStatus", (STATUS_SILENT_MODE,))?) .await?; Ok(()) } @@ -372,6 +404,10 @@ impl BusConnection { if self.early_signals.is_empty() { readable_or_timeout(&self.session, patience).await; } + self.take_signals() + } + + fn take_signals(&mut self) -> SignalBatch { let still_connected = self.drain_replies().is_ok(); let signals = mem::take(&mut self.early_signals); if still_connected { @@ -380,6 +416,29 @@ impl BusConnection { SignalBatch::Closed(signals) } } + + async fn next_signals(&mut self) -> SignalBatch { + if self.early_signals.is_empty() { + let Ok(mut readiness) = self.session.readable().await else { + return SignalBatch::Closed(Vec::new()); + }; + readiness.clear_ready(); + } + self.take_signals() + } + + fn into_signals(self) -> impl Stream { + unfold_carrying(self, |mut connection| async move { + match connection.next_signals().await { + SignalBatch::Open(signals) => Some(CarriedItem { + item: signals, + state: connection, + }), + SignalBatch::Closed(_) => None, + } + }) + .flat_map(stream::iter) + } } fn text_property(properties: &PropMap, property: &str) -> Option { diff --git a/docs/architecture.md b/docs/architecture.md index c9f2419..68aa665 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -275,8 +275,13 @@ While it runs, bootvine: writes to the console and resets it while it re-executes, and drawing only the cells that changed would leave that on screen. -It restores the log level and the status output when it hands the console -back. Kernel messages from before it starts are already on screen; see +It restores the log level when it hands the console back. It keeps running +without touching the console until the boot finishes and only then restores the +status output, so jobs still running cannot write over a display manager that +started before them. This holds for a handover and for an error. Pressing a +[quit key](guide.md#leaving-the-tree) or stopping the service restores the +output at once, so a stuck boot stays visible; status lines can then reach a +display manager that starts before the boot finishes. Kernel messages from before it starts are already on screen; see [early kernel messages](configuration.md#early-kernel-messages). ### Resuming from hibernation diff --git a/docs/guide.md b/docs/guide.md index 532cdf9..9586b18 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -173,6 +173,16 @@ Failures from the initrd stay in the panel after the switch to the real root. The recording is the `failures` machine, where every kind of failure systemd reports happens one after another. +## Leaving the tree + +`q`, `Ctrl-C` or `Esc` closes the tree during a boot or shutdown and gives the +console back with systemd's status lines, for when bootvine misbehaves or you +want to see what a stuck boot is doing. While a passphrase prompt is open, keys +go to the prompt instead. + +These keys cannot help if bootvine itself has hung, because it reads them in +the same loop that draws the screen. + ## Trying it Neither of these touches your system's boot. diff --git a/vm-tests/scripted-initrd/check.nix b/vm-tests/scripted-initrd/check.nix index f588fd4..53ad7da 100644 --- a/vm-tests/scripted-initrd/check.nix +++ b/vm-tests/scripted-initrd/check.nix @@ -12,9 +12,20 @@ machine ../shared/recorded-display.nix ]; - systemd.services = { - sample-database.serviceConfig.ExecStop = lib.mkForce (waitFor "release-sample-database"); - backdoor.before = [ "sample-database.service" ]; + systemd = { + settings.Manager.ShowStatus = lib.mkForce true; + services = { + sample-database.serviceConfig.ExecStop = lib.mkForce (waitFor "release-sample-database"); + backdoor.before = [ "sample-database.service" ]; + late-sensor-probe = { + wantedBy = [ "graphical.target" ]; + after = [ "greetd.service" ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = waitFor "release-late-sensor-probe"; + }; + }; + }; }; }; diff --git a/vm-tests/scripted-initrd/check.py b/vm-tests/scripted-initrd/check.py index c4cb06c..6dda5bc 100644 --- a/vm-tests/scripted-initrd/check.py +++ b/vm-tests/scripted-initrd/check.py @@ -10,8 +10,30 @@ wait_for_screen("Stage 2", "sample-database") assert_bootvine_in_front() assert_text_visible(font_has_512_glyphs=True) +machine.succeed("timeout 10 systemctl stop bootvine") +machine.succeed("systemctl start --no-block bootvine") +retry(lambda _: foreground_terminal() == f"tty{BOOTVINE_TERMINAL}", timeout_seconds=30) +wait_for_screen("Stage 2") + +machine.send_chars("q") +retry(lambda _: unit_state("bootvine") == "inactive", timeout_seconds=10) +assert foreground_terminal() != f"tty{BOOTVINE_TERMINAL}", "the quit key left bootvine in front" +show_status = machine.succeed("systemctl show -p ShowStatus --value").strip() +assert show_status == "yes", f"the quit key did not restore the status output: {show_status}" + +machine.succeed("systemctl start --no-block bootvine") +retry(lambda _: foreground_terminal() == f"tty{BOOTVINE_TERMINAL}", timeout_seconds=30) + machine.wait_for_unit("greetd.service") -retry(lambda _: unit_state("bootvine") == "inactive", timeout_seconds=60) +retry(lambda _: "another program took the console" in bootvine_journal(), timeout_seconds=60) +assert unit_state("late-sensor-probe") == "activating", "the boot finished before the display manager took over" +assert unit_state("bootvine") == "active", "bootvine left before the boot finished" +machine.succeed("timeout 10 systemctl stop bootvine") +assert unit_state("bootvine") == "inactive", "stopping bootvine during the boot did not end it" +show_status = machine.succeed("systemctl show -p ShowStatus --value").strip() +assert show_status == "yes", f"stopping bootvine did not restore the status output: {show_status}" +machine.succeed("touch /run/release-late-sensor-probe") +machine.wait_for_unit("graphical.target") journal = bootvine_journal() print(journal) watched = [line for line in journal.splitlines() if line.startswith("bootvine: watching")] diff --git a/vm-tests/slow-services/check.nix b/vm-tests/slow-services/check.nix index 29c7e01..b0074cc 100644 --- a/vm-tests/slow-services/check.nix +++ b/vm-tests/slow-services/check.nix @@ -13,6 +13,7 @@ ../shared/recorded-display.nix ]; testing.initrdBackdoor = true; + boot.kernelParams = lib.mkAfter [ "console=tty0" ]; boot.initrd.systemd.services.initrd-hardware-probe.serviceConfig.ExecStart = lib.mkForce ( waitFor "release-initrd-hardware-probe" ); diff --git a/vm-tests/slow-services/check.py b/vm-tests/slow-services/check.py index 24cecc4..7224285 100644 --- a/vm-tests/slow-services/check.py +++ b/vm-tests/slow-services/check.py @@ -6,6 +6,17 @@ def unit_state(unit): return machine.succeed(f"systemctl is-active {unit} || true").strip() +def foreground_console_text(): + terminal_number = foreground_terminal().removeprefix("tty") + return machine.succeed(f"tr -cd '[:print:]' < /dev/vcs{terminal_number}") + + +def assert_unit_start_stays_off_console(description): + machine.succeed(f"systemd-run --wait --description='{description}' /run/current-system/sw/bin/true") + console_text = foreground_console_text() + assert description not in console_text, f"systemd status reached the login screen:\n{console_text}" + + recording = ScreenRecording(machine) machine.start() recording.start() @@ -24,11 +35,17 @@ assert_text_visible(font_has_512_glyphs=True) assert console_level() == "1", f"stage 2 sysctl reopened the kernel console: {console_level()}" machine.wait_for_unit("greetd.service") -retry(lambda _: unit_state("bootvine") == "inactive", timeout_seconds=60) +retry(lambda _: "another program took the console" in bootvine_journal(), timeout_seconds=60) assert unit_state("late-sensor-probe") == "activating", "the boot finished before the display manager took over" assert foreground_terminal() != f"tty{BOOTVINE_TERMINAL}", "bootvine took the console back from the display manager" +machine.wait_until_tty_matches("1", "login: ") +assert_unit_start_stays_off_console("Mid boot status probe") +show_status = machine.succeed("systemctl show -p ShowStatus --value").strip() +assert show_status == "no", f"systemd status output came back before the boot finished: {show_status}" +assert unit_state("bootvine") == "active", "bootvine left before the boot finished" machine.succeed("touch /run/release-late-sensor-probe") machine.wait_for_unit("graphical.target") +retry(lambda _: unit_state("bootvine") == "inactive", timeout_seconds=60) journal = bootvine_journal() print(journal) @@ -43,6 +60,13 @@ machine.succeed('test "$(systemctl show -p Result --value bootvine)" = success') 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}" +assert_unit_start_stays_off_console("Post boot status probe") + +machine.wait_until_tty_matches("1", "login: ") +machine.send_chars("sample-user\n") +machine.wait_until_tty_matches("1", "Password: ") +machine.send_chars("sample-user\n") +machine.wait_until_succeeds("pgrep -u sample-user bash", timeout=30) foreground = foreground_terminal() assert foreground != f"tty{BOOTVINE_TERMINAL}", "bootvine kept the console after handing it over"