From 3ae06e91240b3e08c4b3f07751153c3e43098bb8 Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 1 Oct 2026 15:54:32 -0700 Subject: [PATCH 01/62] feature: Add autorun installer crate and ISO console Add HC and COSI autorun, ISO media discovery, recovery, shell and result actions, and a safe local demo. Wire the optional installer RPM and preserve caller-managed reboot policy. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Cargo.lock | 241 +++++- Cargo.toml | 4 + Makefile | 16 +- crates/installer/Cargo.toml | 39 + crates/installer/README.md | 127 +++ crates/installer/src/client.rs | 532 ++++++++++++ crates/installer/src/config.rs | 307 +++++++ crates/installer/src/demo.rs | 106 +++ crates/installer/src/main.rs | 114 +++ crates/installer/src/source.rs | 524 ++++++++++++ crates/installer/src/ui.rs | 847 ++++++++++++++++++++ crates/osutils/Cargo.toml | 2 +- crates/osutils/src/blkid.rs | 33 +- crates/osutils/src/dependencies.rs | 67 +- crates/osutils/src/lib.rs | 1 + crates/osutils/src/systemd.rs | 22 + crates/osutils/src/terminal.rs | 109 +++ packaging/docker/Dockerfile.azl | 1 + packaging/installer.toml | 8 + packaging/rpm/trident.spec | 29 +- packaging/systemd/trident-installer.service | 18 + 21 files changed, 3134 insertions(+), 13 deletions(-) create mode 100644 crates/installer/Cargo.toml create mode 100644 crates/installer/README.md create mode 100644 crates/installer/src/client.rs create mode 100644 crates/installer/src/config.rs create mode 100644 crates/installer/src/demo.rs create mode 100644 crates/installer/src/main.rs create mode 100644 crates/installer/src/source.rs create mode 100644 crates/installer/src/ui.rs create mode 100644 crates/osutils/src/terminal.rs create mode 100644 packaging/installer.toml create mode 100644 packaging/systemd/trident-installer.service diff --git a/Cargo.lock b/Cargo.lock index cdac464052..3044fc7e51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -318,6 +318,21 @@ version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" +[[package]] +name = "cassowary" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" + +[[package]] +name = "castaway" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a" +dependencies = [ + "rustversion", +] + [[package]] name = "cc" version = "1.4.0" @@ -436,6 +451,20 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "compact_str" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fd622ebbb56a5b2ccb651b32b911cdeb2a9b4b11776b2473bf26a26a286244e" +dependencies = [ + "castaway", + "cfg-if", + "itoa", + "rustversion", + "ryu", + "static_assertions", +] + [[package]] name = "configparser" version = "3.1.0" @@ -564,6 +593,31 @@ version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" +[[package]] +name = "crossterm" +version = "0.28.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6" +dependencies = [ + "bitflags", + "crossterm_winapi", + "mio", + "parking_lot", + "rustix 0.38.41", + "signal-hook 0.3.18", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -1651,6 +1705,46 @@ version = "2.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" +[[package]] +name = "instability" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6778b0196eefee7df739db78758e5cf9b37412268bfa5650bfeed028aed20d9c" +dependencies = [ + "darling", + "indoc", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "installer" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "crossterm", + "env_logger 0.11.5", + "indoc", + "log", + "osutils", + "ratatui", + "reqwest", + "serde", + "serde_json", + "serde_yaml", + "tar", + "tempfile", + "tokio", + "tokio-stream", + "toml", + "tonic", + "trident-proto", + "trident_api", + "url", +] + [[package]] name = "instant" version = "0.1.13" @@ -1945,6 +2039,15 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "lru" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" +dependencies = [ + "hashbrown", +] + [[package]] name = "maplit" version = "1.0.2" @@ -1994,6 +2097,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" dependencies = [ "libc", + "log", "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.52.0", ] @@ -2336,6 +2440,12 @@ dependencies = [ "regex", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pem" version = "3.0.6" @@ -2758,6 +2868,27 @@ dependencies = [ "zerocopy 0.8.27", ] +[[package]] +name = "ratatui" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabd94c2f37801c20583fc49dd5cd6b0ba68c716787c2dd6ed18571e1e63117b" +dependencies = [ + "bitflags", + "cassowary", + "compact_str", + "crossterm", + "indoc", + "instability", + "itertools", + "lru", + "paste", + "strum 0.26.3", + "unicode-segmentation", + "unicode-truncate", + "unicode-width 0.2.0", +] + [[package]] name = "rayon" version = "1.10.0" @@ -3160,6 +3291,15 @@ dependencies = [ "serde_core", ] +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -3241,6 +3381,16 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +[[package]] +name = "signal-hook" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2" +dependencies = [ + "libc", + "signal-hook-registry", +] + [[package]] name = "signal-hook" version = "0.4.3" @@ -3251,6 +3401,17 @@ dependencies = [ "signal-hook-registry", ] +[[package]] +name = "signal-hook-mio" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b75a19a7a740b25bc7944bdee6172368f988763b744e3d4dfe753f6b4ece40cc" +dependencies = [ + "libc", + "mio", + "signal-hook 0.3.18", +] + [[package]] name = "signal-hook-registry" version = "1.4.7" @@ -3364,6 +3525,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "strsim" version = "0.10.0" @@ -3381,6 +3548,9 @@ name = "strum" version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" +dependencies = [ + "strum_macros 0.26.4", +] [[package]] name = "strum" @@ -3582,7 +3752,7 @@ checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057" dependencies = [ "smawk", "unicode-linebreak", - "unicode-width", + "unicode-width 0.2.0", ] [[package]] @@ -3733,6 +3903,47 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "toml_write", + "winnow", +] + +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + [[package]] name = "tonic" version = "0.14.2" @@ -3987,7 +4198,7 @@ dependencies = [ "serde_json", "serde_yaml", "sha2", - "signal-hook", + "signal-hook 0.4.3", "sqlite", "strum 0.26.3", "strum_macros 0.26.4", @@ -4229,6 +4440,23 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" +[[package]] +name = "unicode-truncate" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf" +dependencies = [ + "itertools", + "unicode-segmentation", + "unicode-width 0.1.14", +] + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "unicode-width" version = "0.2.0" @@ -4765,6 +4993,15 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" +[[package]] +name = "winnow" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" +dependencies = [ + "memchr", +] + [[package]] name = "winsafe" version = "0.0.19" diff --git a/Cargo.toml b/Cargo.toml index 1483194d00..5d53d8ea74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ default-members = ["crates/trident"] members = [ "crates/docbuilder", + "crates/installer", "crates/trident-acl-agent", "crates/trident-client", "crates/osmodifier", @@ -34,6 +35,7 @@ chrono = { version = "0.4.38", features = ["serde"] } clap = { version = "4.4.18", features = ["derive", "wrap_help"] } configparser = { version = "3.1.0", features = ["indexmap"] } const_format = "0.2.33" +crossterm = "0.28.1" derive_more = "0.99.19" docker_credential = "1.0.1" documented = "0.6.0" @@ -78,6 +80,7 @@ prost = "0.14.1" prost-types = "0.14.1" quote = "1.0.37" rand = "0.9.3" +ratatui = "0.29.0" rayon = "1.10" regex = "1.11.1" reqwest = { version = "0.12.9", features = [ @@ -114,6 +117,7 @@ tonic = "0.14.2" tonic-middleware = "0.4.0" tonic-prost = "0.14.2" tonic-prost-build = "0.14.2" +toml = "0.8.23" tower = "0.5.2" tracing = "0.1.41" tracing-journald = "0.3.0" diff --git a/Makefile b/Makefile index e26b33e6c8..6f9a3150a8 100644 --- a/Makefile +++ b/Makefile @@ -184,12 +184,12 @@ clean-coverage: rm -rf target/coverage/profraw rm -rf target/lcov.info -target/release/trident target/release/trident-acl-agent: .cargo/config | version-vars +target/release/trident target/release/trident-acl-agent target/release/trident-installer: .cargo/config | version-vars @OPENSSL_STATIC=1 \ OPENSSL_LIB_DIR=$(shell dirname `whereis libssl.a | cut -d" " -f2`) \ OPENSSL_INCLUDE_DIR=/usr/include/openssl \ TRIDENT_VERSION="$(LOCAL_BUILD_TRIDENT_VERSION)" \ - cargo build --release --features dangerous-options,grpc-preview -p trident -p trident-acl-agent + cargo build --release --features dangerous-options,grpc-preview -p trident -p trident-acl-agent -p installer ARTIFACTS_DIR="artifacts" @@ -212,7 +212,7 @@ clean-builder-image: azl-version-vars @echo "Removing local image $(BUILDER_IMAGE)..." @docker rmi $(BUILDER_IMAGE) || echo "Image $(BUILDER_IMAGE) not found locally." -target/$(DISTRO)/release/trident target/$(DISTRO)/release/trident-acl-agent: azl-version-vars version-vars builder-image +target/$(DISTRO)/release/trident target/$(DISTRO)/release/trident-acl-agent target/$(DISTRO)/release/trident-installer: azl-version-vars version-vars builder-image @mkdir -p bin/ @mkdir -p target/$(DISTRO)/ @echo "Building Trident for Azure Linux ($(DISTRO)) using Docker image $(BUILDER_IMAGE)..." @@ -225,10 +225,11 @@ target/$(DISTRO)/release/trident target/$(DISTRO)/release/trident-acl-agent: azl --release \ --features dangerous-options,grpc-preview \ -p trident \ - -p trident-acl-agent + -p trident-acl-agent \ + -p installer # This will do a proper build on azl3, exactly as the pipelines would, with the custom registry and all. -bin/trident-rpms-%.tar.gz: packaging/docker/Dockerfile.full packaging/systemd/*.service packaging/rpm/trident.spec packaging/selinux-policy-trident/* LICENSE NOTICE version-vars azl-version-vars +bin/trident-rpms-%.tar.gz: packaging/docker/Dockerfile.full packaging/installer.toml packaging/systemd/*.service packaging/rpm/trident.spec packaging/selinux-policy-trident/* LICENSE NOTICE version-vars azl-version-vars @case "$@" in \ *$(DISTRO).tar.gz) ;; \ *) echo "Invalid target '$@' for DISTRO '$(DISTRO)'. Expected target ending in '$(DISTRO).tar.gz'."; exit 1 ;; \ @@ -262,7 +263,7 @@ bin/trident-rpms-%.tar.gz: packaging/docker/Dockerfile.full packaging/systemd/*. # backward compatibility; other distros use the in-container distro build. TRIDENT_RPM_BIN_DIR := $(if $(filter azl3,$(DISTRO)),target/release,target/$(DISTRO)/release) -bin/trident-rpms.tar.gz: azl-version-vars packaging/docker/Dockerfile.azl packaging/systemd/*.service packaging/rpm/trident.spec $(TRIDENT_RPM_BIN_DIR)/trident $(TRIDENT_RPM_BIN_DIR)/trident-acl-agent packaging/selinux-policy-trident/* LICENSE NOTICE +bin/trident-rpms.tar.gz: azl-version-vars packaging/docker/Dockerfile.azl packaging/installer.toml packaging/systemd/*.service packaging/rpm/trident.spec $(TRIDENT_RPM_BIN_DIR)/trident $(TRIDENT_RPM_BIN_DIR)/trident-acl-agent $(TRIDENT_RPM_BIN_DIR)/trident-installer packaging/selinux-policy-trident/* LICENSE NOTICE @mkdir -p bin/ @if [ ! -f bin/trident ] || ! cmp -s $(TRIDENT_RPM_BIN_DIR)/trident bin/trident; then \ cp $(TRIDENT_RPM_BIN_DIR)/trident bin/trident; \ @@ -270,6 +271,9 @@ bin/trident-rpms.tar.gz: azl-version-vars packaging/docker/Dockerfile.azl packag @if [ ! -f bin/trident-acl-agent ] || ! cmp -s $(TRIDENT_RPM_BIN_DIR)/trident-acl-agent bin/trident-acl-agent; then \ cp $(TRIDENT_RPM_BIN_DIR)/trident-acl-agent bin/trident-acl-agent; \ fi + @if [ ! -f bin/trident-installer ] || ! cmp -s $(TRIDENT_RPM_BIN_DIR)/trident-installer bin/trident-installer; then \ + cp $(TRIDENT_RPM_BIN_DIR)/trident-installer bin/trident-installer; \ + fi @docker build -t trident/trident-build:latest \ --build-arg TRIDENT_VERSION="$(LOCAL_BUILD_TRIDENT_VERSION)" \ --build-arg RPM_VER="$(TRIDENT_CARGO_VERSION)" \ diff --git a/crates/installer/Cargo.toml b/crates/installer/Cargo.toml new file mode 100644 index 0000000000..8b7ef0d5b6 --- /dev/null +++ b/crates/installer/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "installer" +version = "0.1.0" +edition = "2024" +rust-version = "1.85.0" +publish = false +license = "MIT" + +[[bin]] +name = "trident-installer" +path = "src/main.rs" + +[dependencies] +anyhow = { workspace = true } +clap = { workspace = true } +crossterm = { workspace = true } +env_logger = { workspace = true } +log = { workspace = true } +ratatui = { workspace = true } +reqwest = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +serde_yaml = { workspace = true } +tar = { workspace = true } +tokio = { workspace = true } +toml = { workspace = true } +tonic = { workspace = true } +url = { workspace = true } + +# Local Crate Dependencies +osutils = { path = "../osutils" } +trident_api = { path = "../trident_api" } +trident-proto = { path = "../trident-proto", features = ["grpc-preview"] } + +[dev-dependencies] +indoc = { workspace = true } +tempfile = { workspace = true } +tokio-stream = { workspace = true } +trident-proto = { path = "../trident-proto", features = ["server", "grpc-preview"] } diff --git a/crates/installer/README.md b/crates/installer/README.md new file mode 100644 index 0000000000..15dc3405dd --- /dev/null +++ b/crates/installer/README.md @@ -0,0 +1,127 @@ +# Trident installer + +`installer` builds the `trident-installer` binary and optional RPM. It is an +autorun wrapper around Trident's gRPC API, not a partitioning engine. + +## Local demo + +```console +cargo run -p installer -- --demo +cargo run -p installer -- --demo failure +cargo run -p installer -- --demo no-images +cargo run -p installer -- --demo already-present +cargo run -p installer -- --demo disconnect +``` + +The demo never mounts media, inspects disks, contacts Trident, or changes machine +power state. Press `Q` to quit. Shell opens as the invoking user. Use `--plain` +for line-oriented output; without a terminal the demo prints its events and +exits when complete. + +## Configuration + +Read `/etc/trident/installer.toml` or the path supplied with `--config`. +`mode` is mandatory. All other fields are optional: + +```toml +mode = "autorun" + +[media] +cdromLabel = "TRIDENT_INSTALL" +cosiDirectory = "cosi/" +# Instead of cdromLabel, supply mountPath for already-mounted media. + +[autorun] +# If omitted, use /etc/trident/config.yaml when that file exists. +# hostConfiguration = "installer/host-configuration.yaml" +reboot = true +``` + +Unknown fields and conflicting media selectors are errors. `interactive` is +reserved for later work, not an alias for autorun. COSI directory paths must +stay beneath the media root. + +Media is mounted read-only at `/run/trident/installer-media`. Duplicate labels +are rejected. Optional `installer/installer.toml` on the mounted media can +override mode, image directory, and autorun options, but not bootstrap media +discovery. Relative HC paths are media-relative; absolute paths and `file://` +refer to the live filesystem. HTTP(S) HC sources are supported. + +With an HC, use Install. Otherwise stream the first regular `.cosi` file in +the configured folder, sorted by filename, without recursing or following +image symlinks. Invalid HC or selected-image input is an error, not permission +to choose another operation or image. + +An HC can use the installer-only image reference: + +```yaml +image: + url: installer://image + sha384: ignored +``` + +The marker becomes the selected image's encoded `file://` URL before validation +and submission. Authored checksum settings are preserved. An explicit HC image +URL stays unchanged and needs no ISO image. Configure a checksum for production +images rather than copying the deliberately unverified example above. +HC disk paths are used as authored. The installer does not resolve legacy disk +placeholders or automatically choose a disk for an HC. + +## Console and recovery + +On the ISO, `--system-console` prefers the graphical virtual console for local +keyboard/monitor and BMC KVM, independent of `console=` order. An active serial +console is the fallback. The chosen getty is stopped; other active consoles +receive plain status once and identify the interactive console. + +No ordinary source picker or confirmation precedes autorun. Missing sources +and failures show scrollable error details. Continue opens recovery: Shell, +Stream COSI URL, remote HC URL, or Shutdown. Shell `exit` returns to the +originating screen. Installation continues while a shell is open; automatic +reboot waits. The success screen offers Reboot and Shell. + +Display filtering never removes daemon log records. All received responses are +also appended to the private `/var/log/trident-installer.log`. `V` toggles +detailed logs; PgUp/PgDn scroll. A missing final Completed response means unknown +outcome, not success. Another write requires a successful Trident status query +establishing that the daemon is idle. + +Streaming checks COSI filesystem UUIDs before writing. A complete match opens +the already-present screen with a separately confirmed Force reinstall action. +It is an identity heuristic, not proof of image contents or OS health. Mounted +or read-only potential stream targets are never force-overwritten. +**HC installation intentionally has no repeat-install/Force guard:** the HC is +the operator's authority to overwrite its selected disks. Remove ISO media or +correct boot order to avoid repeated HC-driven installations. + +`autorun.reboot` defaults to true. Successful installations requiring reboot +show a short countdown, then reboot; false leaves the success screen. Errors, +unknown outcomes, already-present images, and no-servicing results never +automatically reboot. + +## ISO integration + +Install `trident-installer`, then explicitly enable `trident-installer.service` +and `tridentd.socket` in the ISO recipe. Installing the RPM alone does not enable +the installer. Do not also enable `trident-install.service`. The daemon must be +built with `grpc-preview` for regular Install; repository RPM builds enable it. +Replace older installer startup units and autologin entrypoints rather than +running both installers on the same ISO. + +The ISO volume label must match configuration. Image Customizer's existing +`CDROM` label requires an override or relabelling. With full-OS initramfs, keep +SELinux disabled and include USB storage/UAS, CD-ROM/SCSI, and USB host-controller +drivers. QEMU's IDE CD-ROM does not validate BMC virtual-media support. + +## Validation + +```console +cargo test -p installer +cargo test -p osutils terminal::tests +cargo clippy -p installer --all-targets --no-deps -- -D warnings +cargo fmt --all -- --check +``` + +Interactive disk-selection strategies and broader HC generation are follow-ups. +ISO boot, serial-terminal behaviour, RPM builds, and physical BMC virtual media +need separate integration validation. diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs new file mode 100644 index 0000000000..43866b6bb2 --- /dev/null +++ b/crates/installer/src/client.rs @@ -0,0 +1,532 @@ +use std::time::Duration; + +use anyhow::{anyhow, bail, ensure, Context, Error}; +use tokio::{sync::mpsc::Sender, task}; +use tonic::{ + transport::{Channel, Endpoint}, + Code, Status, Streaming, +}; +use url::Url; + +use trident_proto::{ + v1::{ + servicing_response::Response as ResponseBody, + streaming_service_client::StreamingServiceClient, Completed, HostConfiguration, + RebootHandling, RebootManagement, RebootStatus, ServicingKind, ServicingResponse, + StatusCode, StreamDiskRequest, + }, + v1preview::{ + install_service_client::InstallServiceClient, status_service_client::StatusServiceClient, + FinalizeInstallRequest, GetServicingStateRequest, InstallRequest, StageInstallRequest, + }, + TRIDENT_DEFAULT_SOCKET_URI, +}; + +use crate::{ + config::{Config, DEFAULT_HOST_CONFIGURATION}, + source::{self, Plan, Request}, +}; + +const CONNECTION_TIMEOUT: Duration = Duration::from_secs(30); +const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); + +#[derive(Debug)] +pub(super) enum Event { + Prepared { + description: String, + reboot: bool, + stream_image: Option, + }, + Response(ServicingResponse), + Error { + details: String, + uncertain: bool, + }, + AlreadyPresent(String), +} + +#[derive(Debug, PartialEq, Eq)] +pub(super) enum Completion { + Success { + reboot_required: bool, + performed: bool, + }, + Failure(String), +} + +pub(super) fn completion(completed: Completed) -> Result { + match StatusCode::try_from(completed.status).context("Invalid final status code")? { + StatusCode::Success => { + ensure!( + completed.error.is_none(), + "Success response unexpectedly includes an error" + ); + let reboot = + RebootStatus::try_from(completed.reboot_status).context("Invalid reboot status")?; + ensure!( + matches!( + reboot, + RebootStatus::RebootRequired | RebootStatus::RebootNotRequired + ), + "Daemon did not honour caller-managed reboot: {reboot:?}" + ); + let performed = match completed.servicing_kind { + Some(kind) => { + ServicingKind::try_from(kind).context("Invalid servicing kind")? + != ServicingKind::NoneRequired + } + None => true, + }; + Ok(Completion::Success { + reboot_required: reboot == RebootStatus::RebootRequired, + performed, + }) + } + StatusCode::Failure => Ok(Completion::Failure(match completed.error { + Some(error) => format!("{}\n\nReturned error details:\n{error:#?}", error.message), + None => "Trident reported failure without error details".into(), + })), + StatusCode::Unspecified => bail!("Daemon returned an unspecified final status"), + } +} + +pub(super) async fn execute( + settings: Config, + request: Request, + force: bool, + require_idle: bool, + tx: Sender, +) -> Result<(), Error> { + let description = match &request { + Request::Autorun => format!( + "Autorun: HC '{}' or first ISO COSI in '{}'", + settings + .autorun + .host_configuration + .as_deref() + .unwrap_or(DEFAULT_HOST_CONFIGURATION), + settings.media.cosi_directory.display() + ), + Request::HostConfiguration(url) => format!("Install from {url}"), + Request::Stream(url) => format!("StreamDisk from {url}"), + }; + send( + &tx, + Event::Prepared { + description, + reboot: settings.autorun.reboot, + stream_image: None, + }, + ) + .await?; + let prepared = task::spawn_blocking(move || source::prepare(&settings, &request)).await?; + let (settings, plan) = match prepared { + Ok(prepared) => prepared, + Err(error) => { + return send( + &tx, + Event::Error { + details: format!("{error:#}"), + uncertain: require_idle, + }, + ) + .await + } + }; + send( + &tx, + Event::Prepared { + description: plan.description(), + reboot: settings.autorun.reboot, + stream_image: match &plan { + Plan::Stream { image } => Some(image.clone()), + Plan::Install { .. } => None, + }, + }, + ) + .await?; + if let Plan::Stream { image } = &plan { + let image = image.clone(); + match task::spawn_blocking(move || source::stream_preflight(&image, force)).await? { + Ok(true) => return send(&tx, Event::AlreadyPresent( + "Every COSI filesystem UUID was found on an attached disk.\nNo installation was started.\nThis is an identity guard, not a health check.".into() + )).await, + Ok(false) => {} + Err(error) => return send(&tx, Event::Error { details: format!("{error:#}"), uncertain: false }).await, + } + } + let channel = match connect(TRIDENT_DEFAULT_SOCKET_URI).await { + Ok(channel) => channel, + Err(error) => { + return send( + &tx, + Event::Error { + details: format!("{error:#}"), + uncertain: require_idle, + }, + ) + .await + } + }; + if require_idle { + if let Err(error) = StatusServiceClient::new(channel.clone()) + .get_servicing_state(GetServicingStateRequest {}) + .await + { + return send(&tx, Event::Error { + details: format!("The previous operation's outcome is unknown. Cannot establish that Trident is idle; refusing another write.\n{error}"), + uncertain: true, + }).await; + } + } + let started = tokio::time::timeout(REQUEST_TIMEOUT, start(channel, plan)) + .await + .unwrap_or_else(|_| { + Err(Status::deadline_exceeded( + "Timed out waiting for Trident to accept the request; outcome is unknown", + )) + }); + let response = match started { + Ok(response) => response, + Err(error) => { + let uncertain = !matches!( + error.code(), + Code::InvalidArgument | Code::Unimplemented | Code::FailedPrecondition + ); + return send( + &tx, + Event::Error { + details: format!("gRPC request failed: {error}"), + uncertain, + }, + ) + .await; + } + }; + receive(response, &tx).await +} + +async fn connect(address: &str) -> Result { + Endpoint::new(address.to_owned())? + .connect_timeout(CONNECTION_TIMEOUT) + .connect() + .await + .with_context(|| format!("Failed to connect to Trident at '{address}'")) +} + +async fn start(channel: Channel, plan: Plan) -> Result, Status> { + let reboot = Some(RebootManagement { + handling: RebootHandling::CallerHandlesReboot.into(), + }); + match plan { + Plan::Stream { image } => Ok(StreamingServiceClient::new(channel) + .stream_disk(StreamDiskRequest { + image_url: image.to_string(), + image_hash: None, + reboot, + }) + .await? + .into_inner()), + Plan::Install { config, .. } => { + let config = serde_yaml::to_string(&config).map_err(|error| { + Status::internal(format!("Failed to serialize Host Configuration: {error}")) + })?; + Ok(InstallServiceClient::new(channel) + .install(InstallRequest { + stage: Some(StageInstallRequest { + config: Some(HostConfiguration { config }), + }), + finalize: Some(FinalizeInstallRequest { reboot }), + }) + .await? + .into_inner()) + } + } +} + +async fn receive( + mut stream: Streaming, + tx: &Sender, +) -> Result<(), Error> { + loop { + let response = match stream.message().await { + Ok(Some(response)) => response, + Ok(None) => return send(tx, Event::Error { + details: "The gRPC stream ended without a final Completed response. Installation outcome is unknown; it may still be running.".into(), + uncertain: true, + }).await, + Err(error) => return send(tx, Event::Error { + details: format!("The gRPC stream failed; installation outcome is unknown.\n{error}"), + uncertain: true, + }).await, + }; + let completed = matches!(response.response, Some(ResponseBody::Completed(_))); + if response.response.is_none() { + return send( + tx, + Event::Error { + details: "gRPC response has no body; installation outcome is unknown".into(), + uncertain: true, + }, + ) + .await; + } + send(tx, Event::Response(response)).await?; + if completed { + return Ok(()); + } + } +} + +pub(super) async fn send(tx: &Sender, event: Event) -> Result<(), Error> { + tx.send(event) + .await + .map_err(|_| anyhow!("Installer event receiver closed")) +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::{ + sync::{Arc, Mutex}, + vec::IntoIter, + }; + + use tempfile::TempDir; + use tokio::{net::UnixListener, sync::mpsc}; + use tokio_stream::{wrappers::UnixListenerStream, Iter}; + use tonic::{async_trait, transport::Server, Request as TonicRequest, Response}; + use url::Url; + + use trident_api::config::{HostConfiguration as ApiHostConfiguration, ImageSha384, OsImage}; + use trident_proto::{ + v1::{ + streaming_service_server::{StreamingService, StreamingServiceServer}, + Log, LogLevel, Started, + }, + v1preview::install_service_server::{InstallService, InstallServiceServer}, + }; + + type ResponseStream = Iter>>; + + fn successful() -> Completed { + Completed { + status: StatusCode::Success.into(), + reboot_status: RebootStatus::RebootRequired.into(), + servicing_kind: Some(ServicingKind::CleanInstall.into()), + ..Default::default() + } + } + + #[test] + fn final_response_is_strict() { + assert_eq!( + completion(successful()).unwrap(), + Completion::Success { + reboot_required: true, + performed: true + } + ); + completion(Completed::default()).unwrap_err(); + completion(Completed { + status: 999, + ..successful() + }) + .unwrap_err(); + completion(Completed { + reboot_status: RebootStatus::RebootStarted.into(), + ..successful() + }) + .unwrap_err(); + assert_eq!( + completion(Completed { + servicing_kind: Some(ServicingKind::NoneRequired.into()), + ..successful() + }) + .unwrap(), + Completion::Success { + reboot_required: true, + performed: false + } + ); + } + + #[derive(Clone)] + struct Fake { + complete: bool, + } + + #[async_trait] + impl StreamingService for Fake { + type StreamDiskStream = ResponseStream; + + async fn stream_disk( + &self, + request: TonicRequest, + ) -> Result, Status> { + assert_eq!( + request.into_inner().reboot.unwrap().handling, + i32::from(RebootHandling::CallerHandlesReboot) + ); + let mut responses = vec![ + ServicingResponse { + response: Some(ResponseBody::Started(Started {})), + ..Default::default() + }, + ServicingResponse { + response: Some(ResponseBody::Log(Log { + level: LogLevel::Trace.into(), + message: "full detail".into(), + ..Default::default() + })), + ..Default::default() + }, + ]; + if self.complete { + responses.push(ServicingResponse { + response: Some(ResponseBody::Completed(successful())), + ..Default::default() + }); + } + + Ok(Response::new(tokio_stream::iter( + responses.into_iter().map(Ok).collect::>(), + ))) + } + } + + #[derive(Clone)] + struct FakeInstall { + request: Arc>>, + } + + #[async_trait] + impl InstallService for FakeInstall { + type InstallStream = ResponseStream; + type InstallStageStream = ResponseStream; + type InstallFinalizeStream = ResponseStream; + + async fn install( + &self, + request: TonicRequest, + ) -> Result, Status> { + *self.request.lock().unwrap() = Some(request.into_inner()); + Ok(Response::new(tokio_stream::iter(vec![Ok( + ServicingResponse { + response: Some(ResponseBody::Completed(successful())), + ..Default::default() + }, + )]))) + } + + async fn install_stage( + &self, + _: TonicRequest, + ) -> Result, Status> { + Err(Status::unimplemented("test only supports full install")) + } + + async fn install_finalize( + &self, + _: TonicRequest, + ) -> Result, Status> { + Err(Status::unimplemented("test only supports full install")) + } + } + + #[tokio::test] + async fn install_sends_full_configuration_and_caller_managed_reboot() { + let root = TempDir::new().unwrap(); + let path = root.path().join("trident.sock"); + let listener = UnixListener::bind(&path).unwrap(); + let captured = Arc::new(Mutex::new(None)); + let server = tokio::spawn( + Server::builder() + .add_service(InstallServiceServer::new(FakeInstall { + request: captured.clone(), + })) + .serve_with_incoming(UnixListenerStream::new(listener)), + ); + let image = OsImage { + url: Url::parse("file:///media/cosi/selected.cosi").unwrap(), + sha384: ImageSha384::new(&format!("sha384:{}", "ab".repeat(48))).unwrap(), + }; + let config = ApiHostConfiguration { + image: Some(image.clone()), + ..Default::default() + }; + let channel = connect(&format!("unix://{}", path.display())) + .await + .unwrap(); + let stream = start( + channel, + Plan::Install { + config: Box::new(config), + source: "test".into(), + }, + ) + .await + .unwrap(); + let (tx, mut rx) = mpsc::channel(4); + receive(stream, &tx).await.unwrap(); + assert!(matches!(rx.recv().await.unwrap(), Event::Response(_))); + let request = captured.lock().unwrap().take().unwrap(); + assert_eq!( + request.finalize.unwrap().reboot.unwrap().handling, + i32::from(RebootHandling::CallerHandlesReboot) + ); + let received: ApiHostConfiguration = + serde_yaml::from_str(&request.stage.unwrap().config.unwrap().config).unwrap(); + assert_eq!(received.image, Some(image)); + server.abort(); + } + + #[tokio::test] + async fn real_unix_transport_preserves_trace_and_detects_missing_completion() { + for complete in [true, false] { + let root = TempDir::new().unwrap(); + let path = root.path().join("trident.sock"); + let listener = UnixListener::bind(&path).unwrap(); + let server = tokio::spawn( + Server::builder() + .add_service(StreamingServiceServer::new(Fake { complete })) + .serve_with_incoming(UnixListenerStream::new(listener)), + ); + let channel = connect(&format!("unix://{}", path.display())) + .await + .unwrap(); + let stream = start( + channel, + Plan::Stream { + image: Url::parse("https://example.com/image.cosi").unwrap(), + }, + ) + .await + .unwrap(); + let (tx, mut rx) = mpsc::channel(8); + receive(stream, &tx).await.unwrap(); + drop(tx); + let mut trace = false; + let mut completed = false; + let mut uncertain = false; + while let Some(event) = rx.recv().await { + match event { + Event::Response(response) => match response.response.unwrap() { + ResponseBody::Log(log) => trace = log.level() == LogLevel::Trace, + ResponseBody::Completed(_) => completed = true, + _ => {} + }, + Event::Error { + uncertain: value, .. + } => uncertain = value, + _ => {} + } + } + assert!(trace); + assert_eq!(completed, complete); + assert_eq!(uncertain, !complete); + server.abort(); + } + } +} diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs new file mode 100644 index 0000000000..5f9bcc5e6e --- /dev/null +++ b/crates/installer/src/config.rs @@ -0,0 +1,307 @@ +use std::{ + fs, + io::ErrorKind, + path::{Component, Path, PathBuf}, +}; + +use anyhow::{bail, ensure, Context, Error}; +use serde::{Deserialize, Serialize}; +use toml::Value as TomlValue; + +pub(super) const DEFAULT_CONFIG: &str = "/etc/trident/installer.toml"; +pub(super) const DEFAULT_HOST_CONFIGURATION: &str = "/etc/trident/config.yaml"; +pub(super) const MEDIA_CONFIG: &str = "installer/installer.toml"; +pub(super) const MEDIA_MOUNT: &str = "/run/trident/installer-media"; +pub(super) const DEFAULT_MEDIA_LABEL: &str = "TRIDENT_INSTALL"; + +#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub(super) struct Config { + pub mode: Mode, + #[serde(default)] + pub media: Media, + #[serde(default)] + pub autorun: Autorun, +} + +#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub(super) enum Mode { + #[default] + Autorun, + Interactive, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub(super) struct Media { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub cdrom_label: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mount_path: Option, + #[serde(default = "default_cosi_directory")] + pub cosi_directory: PathBuf, +} + +impl Default for Media { + fn default() -> Self { + Self { + cdrom_label: None, + mount_path: None, + cosi_directory: default_cosi_directory(), + } + } +} + +fn default_cosi_directory() -> PathBuf { + "cosi".into() +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub(super) struct Autorun { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub host_configuration: Option, + #[serde(default = "default_reboot")] + pub reboot: bool, +} + +impl Default for Autorun { + fn default() -> Self { + Self { + host_configuration: None, + reboot: default_reboot(), + } + } +} + +fn default_reboot() -> bool { + true +} + +impl Config { + pub fn read(path: impl AsRef) -> Result { + let path = path.as_ref(); + let text = fs::read_to_string(path).with_context(|| { + format!( + "Failed to read installer configuration '{}'", + path.display() + ) + })?; + Self::parse(&text) + } + + pub fn parse(text: &str) -> Result { + let config: Self = toml::from_str(text).context("Invalid installer TOML configuration")?; + config.validate()?; + Ok(config) + } + + fn validate(&self) -> Result<(), Error> { + ensure!( + self.media.cdrom_label.is_none() || self.media.mount_path.is_none(), + "media.cdromLabel and media.mountPath are mutually exclusive" + ); + if let Some(label) = &self.media.cdrom_label { + ensure!( + !label.trim().is_empty(), + "media.cdromLabel must not be empty" + ); + } + if let Some(path) = &self.media.mount_path { + ensure!(path.is_absolute(), "media.mountPath must be absolute"); + } + ensure!( + !self.media.cosi_directory.as_os_str().is_empty() + && self.media.cosi_directory.components().all(|component| { + matches!(component, Component::Normal(_) | Component::CurDir) + }), + "media.cosiDirectory must be a nonempty relative path beneath the media root" + ); + if let Some(source) = &self.autorun.host_configuration { + ensure!( + !source.trim().is_empty(), + "autorun.hostConfiguration must not be empty" + ); + } + Ok(()) + } + + pub fn require_autorun(&self) -> Result<(), Error> { + ensure!( + self.mode == Mode::Autorun, + "Interactive installation is not implemented yet; choose mode = \"autorun\"" + ); + Ok(()) + } + + pub fn overlay(&self, root: impl AsRef) -> Result { + let path = root.as_ref().join(MEDIA_CONFIG); + let text = match fs::read_to_string(&path) { + Ok(text) => text, + Err(error) if error.kind() == ErrorKind::NotFound => return Ok(self.clone()), + Err(error) => { + return Err(error).with_context(|| format!("Failed to read '{}'", path.display())) + } + }; + let overlay: TomlValue = toml::from_str(&text) + .with_context(|| format!("Invalid media configuration '{}'", path.display()))?; + if let Some(media) = overlay.get("media") { + ensure!( + media.get("cdromLabel").is_none() && media.get("mountPath").is_none(), + "Media configuration cannot change bootstrap cdromLabel or mountPath" + ); + } + let mut base = TomlValue::try_from(self)?; + merge(&mut base, overlay); + let config: Self = base.try_into()?; + config.validate()?; + Ok(config) + } +} + +fn merge(base: &mut TomlValue, overlay: TomlValue) { + match (base, overlay) { + (TomlValue::Table(base), TomlValue::Table(overlay)) => { + for (key, value) in overlay { + match base.get_mut(&key) { + Some(base_value) => merge(base_value, value), + None => { + base.insert(key, value); + } + } + } + } + (base, overlay) => *base = overlay, + } +} + +pub(super) fn discover_images( + root: impl AsRef, + directory: impl AsRef, +) -> Result, Error> { + let root = root + .as_ref() + .canonicalize() + .context("Failed to resolve media root")?; + let directory = root.join(directory); + let entries = match fs::read_dir(&directory) { + Ok(entries) => entries, + Err(error) if error.kind() == ErrorKind::NotFound => return Ok(Vec::new()), + Err(error) => { + return Err(error).with_context(|| format!("Failed to scan '{}'", directory.display())) + } + }; + ensure!( + directory.canonicalize()?.starts_with(&root), + "COSI directory escapes the media root" + ); + let mut images = Vec::new(); + for entry in entries { + let entry = entry?; + let path = entry.path(); + if entry.file_type()?.is_file() + && path + .extension() + .is_some_and(|extension| extension == "cosi") + { + images.push(path); + } + } + images.sort(); + Ok(images) +} + +pub(super) fn choose_media(devices: &[PathBuf]) -> Result, Error> { + match devices { + [] => Ok(None), + [device] => Ok(Some(device.as_path())), + _ => bail!("Multiple devices match the installer media label: {devices:?}"), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::os::unix::fs as unix_fs; + + use indoc::indoc; + use tempfile::TempDir; + + #[test] + fn explicit_mode_and_strict_fields() { + Config::parse("").unwrap_err(); + Config::parse("mode = 'autorun'\nunknown = true").unwrap_err(); + let config = Config::parse("mode = 'autorun'").unwrap(); + assert!(config.autorun.reboot); + assert_eq!(config.media.cosi_directory, Path::new("cosi")); + Config::parse("mode = 'interactive'") + .unwrap() + .require_autorun() + .unwrap_err(); + } + + #[test] + fn packaged_configuration_matches_the_runtime_schema() { + let config = Config::parse(include_str!("../../../packaging/installer.toml")).unwrap(); + config.require_autorun().unwrap(); + assert_eq!( + config.media.cdrom_label.as_deref(), + Some(DEFAULT_MEDIA_LABEL) + ); + assert!(config.autorun.reboot); + } + + #[test] + fn media_selector_and_path_validation() { + Config::parse(indoc! {" + mode = 'autorun' + [media] + cdromLabel = 'TEST' + mountPath = '/media' + "}) + .unwrap_err(); + for directory in ["/cosi", "../cosi", "cosi/../../outside", ""] { + Config::parse(&format!( + "mode = 'autorun'\n[media]\ncosiDirectory = '{directory}'" + )) + .unwrap_err(); + } + Config::parse("mode = 'autorun'\n[media]\nmountPath = '/media'").unwrap(); + choose_media(&[PathBuf::from("/dev/sr0"), PathBuf::from("/dev/sr1")]).unwrap_err(); + } + + #[test] + fn catalog_is_sorted_nonrecursive_and_ignores_symlinks() { + let root = TempDir::new().unwrap(); + let directory = root.path().join("cosi"); + fs::create_dir(&directory).unwrap(); + for filename in ["z.cosi", "a.cosi", "readme.txt"] { + fs::write(directory.join(filename), "").unwrap(); + } + fs::create_dir(directory.join("nested")).unwrap(); + fs::write(directory.join("nested/hidden.cosi"), "").unwrap(); + unix_fs::symlink(directory.join("z.cosi"), directory.join("0.cosi")).unwrap(); + assert_eq!( + discover_images(root.path(), "cosi").unwrap(), + [directory.join("a.cosi"), directory.join("z.cosi")] + ); + } + + #[test] + fn media_overlay_preserves_defaults_and_bootstrap() { + let root = TempDir::new().unwrap(); + fs::create_dir(root.path().join("installer")).unwrap(); + let path = root.path().join(MEDIA_CONFIG); + fs::write(&path, "[autorun]\nreboot = false").unwrap(); + let config = Config::parse("mode = 'autorun'") + .unwrap() + .overlay(root.path()) + .unwrap(); + assert!(!config.autorun.reboot); + assert_eq!(config.media.cosi_directory, Path::new("cosi")); + fs::write(&path, "[media]\ncdromLabel = 'OTHER'").unwrap(); + config.overlay(root.path()).unwrap_err(); + } +} diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs new file mode 100644 index 0000000000..ed2991601f --- /dev/null +++ b/crates/installer/src/demo.rs @@ -0,0 +1,106 @@ +use std::time::Duration; + +use anyhow::Error; +use clap::ValueEnum; +use tokio::{sync::mpsc::Sender, time}; + +use trident_proto::v1::{ + servicing_response::Response as ResponseBody, Completed, Log, LogLevel, RebootStatus, + ServicingKind, ServicingResponse, Started, StatusCode, TridentError, +}; + +use crate::client::{self, Event}; + +const DEMO_STEP: Duration = Duration::from_millis(500); + +#[derive(Debug, Clone, Copy, ValueEnum)] +pub(super) enum Scenario { + Success, + Failure, + NoImages, + AlreadyPresent, + Disconnect, +} + +pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Error> { + client::send( + &tx, + Event::Prepared { + description: "DEMO: StreamDisk from ISO image".into(), + reboot: false, + stream_image: None, + }, + ) + .await?; + match scenario { + Scenario::NoImages => return client::send(&tx, Event::Error { + details: "DEMO: No Host Configuration and no COSI images in cosi/.\nOpen Shell to configure networking, then select a remote source.".into(), + uncertain: false, + }).await, + Scenario::AlreadyPresent => return client::send(&tx, Event::AlreadyPresent( + "DEMO: Matching COSI filesystem UUIDs found. No writes performed.".into() + )).await, + _ => {} + } + response(&tx, ResponseBody::Started(Started {})).await?; + for (level, message) in [ + (LogLevel::Info, "Loading image metadata"), + (LogLevel::Debug, "Inspecting available block devices"), + (LogLevel::Info, "Preparing storage"), + (LogLevel::Trace, "Example detailed diagnostic record"), + (LogLevel::Info, "Streaming the OS image"), + ] { + time::sleep(DEMO_STEP).await; + response( + &tx, + ResponseBody::Log(Log { + level: level.into(), + message: message.into(), + ..Default::default() + }), + ) + .await?; + } + time::sleep(DEMO_STEP).await; + if matches!(scenario, Scenario::Disconnect) { + return client::send(&tx, Event::Error { + details: "DEMO: Connection lost before Completed. Outcome unknown; another write must wait until Trident is confirmed idle.".into(), + uncertain: true, + }).await; + } + let failed = matches!(scenario, Scenario::Failure); + response( + &tx, + ResponseBody::Completed(Completed { + status: if failed { + StatusCode::Failure + } else { + StatusCode::Success + } + .into(), + error: failed.then(|| TridentError { + message: + "DEMO: Installation failed\nFailed to stream the OS image\nConnection timed out" + .into(), + subkind: "demo-network-failure".into(), + error_message: "Connection timed out".into(), + ..Default::default() + }), + reboot_status: RebootStatus::RebootRequired.into(), + servicing_kind: Some(ServicingKind::CleanInstall.into()), + ..Default::default() + }), + ) + .await +} + +async fn response(tx: &Sender, response: ResponseBody) -> Result<(), Error> { + client::send( + tx, + Event::Response(ServicingResponse { + response: Some(response), + ..Default::default() + }), + ) + .await +} diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs new file mode 100644 index 0000000000..23efa0f256 --- /dev/null +++ b/crates/installer/src/main.rs @@ -0,0 +1,114 @@ +use std::{ + fs::{self, OpenOptions, Permissions}, + os::unix::fs::{OpenOptionsExt, PermissionsExt}, + path::PathBuf, + process::ExitCode, +}; + +use anyhow::{Context, Error}; +use clap::Parser; +use log::LevelFilter; +use tokio::runtime::Builder; + +use osutils::{systemd, terminal}; + +use crate::{config::Config, demo::Scenario, ui::Output}; + +mod client; +mod config; +mod demo; +mod source; +mod ui; + +const LOG_PATH: &str = "/var/log/trident-installer.log"; +const PRIVATE_FILE_MODE: u32 = 0o600; +const WORKER_THREADS: usize = 2; + +#[derive(Debug, Parser)] +#[command( + name = "trident-installer", + version, + about = "Autorun OS installer for Trident installer media" +)] +struct Args { + #[arg(long, default_value = config::DEFAULT_CONFIG)] + config: PathBuf, + #[arg(long, num_args = 0..=1, default_missing_value = "success", value_enum)] + demo: Option, + #[arg(long)] + plain: bool, + #[arg(long, conflicts_with = "demo")] + system_console: bool, +} + +fn main() -> ExitCode { + env_logger::Builder::from_default_env() + .filter_level(LevelFilter::Info) + .init(); + let args = Args::parse(); + let result = Builder::new_multi_thread() + .worker_threads(WORKER_THREADS) + .enable_all() + .build() + .context("Failed to create installer async runtime") + .and_then(|runtime| runtime.block_on(run(args))); + match result { + Ok(()) => ExitCode::SUCCESS, + Err(error) => { + eprintln!("Installer failed: {error:#}"); + ExitCode::FAILURE + } + } +} + +async fn run(args: Args) -> Result<(), Error> { + let mut output = Output { + log: None, + mirrors: Vec::new(), + control: "current terminal".into(), + }; + if args.demo.is_none() { + output.log = Some( + OpenOptions::new() + .create(true) + .append(true) + .mode(PRIVATE_FILE_MODE) + .open(LOG_PATH) + .context("Failed to open private installer diagnostic log; run as root")?, + ); + fs::set_permissions(LOG_PATH, Permissions::from_mode(PRIVATE_FILE_MODE))?; + if args.system_console { + let consoles = terminal::active_consoles()?; + let primary = terminal::preferred_console(&consoles)?; + let name = primary + .file_name() + .context("Console has no device name")? + .to_string_lossy(); + let unit = + if name.starts_with("tty") && name.chars().skip(3).all(|c| c.is_ascii_digit()) { + format!("getty@{name}.service") + } else { + format!("serial-getty@{name}.service") + }; + systemd::stop_unit(&unit)?; + let _terminal = terminal::attach(primary)?; + output.control = primary.display().to_string(); + for path in consoles.iter().filter(|path| path.as_path() != primary) { + output + .mirrors + .push(OpenOptions::new().write(true).open(path).with_context(|| { + format!("Failed to open active console '{}'", path.display()) + })?); + } + } + } + let (settings, initial_error) = if args.demo.is_some() { + (Config::default(), None) + } else { + match Config::read(&args.config) { + Ok(settings) => (settings, None), + Err(error) => (Config::default(), Some(format!("{error:#}"))), + } + }; + ui::run(settings, initial_error, args.demo, args.plain, output).await +} diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs new file mode 100644 index 0000000000..2f350cab81 --- /dev/null +++ b/crates/installer/src/source.rs @@ -0,0 +1,524 @@ +use std::{ + collections::HashSet, + fs::{self, File}, + io::{ErrorKind, Read}, + path::{Path, PathBuf}, + time::Duration, +}; + +use anyhow::{anyhow, bail, ensure, Context, Error}; +use reqwest::blocking::Client; +use serde::Deserialize; +use tar::Archive; +use url::Url; + +use osutils::{ + blkid, + filesystems::MountFileSystemType, + findmnt::FindMnt, + lsblk::{self, BlockDevice, BlockDeviceType}, + mount, mountpoint, +}; +use trident_api::{config::HostConfiguration, constants::MOUNT_OPTION_READ_ONLY}; + +use crate::config::{self, Config}; + +pub(super) const IMAGE_MARKER: &str = "installer://image"; +const SOURCE_TIMEOUT: Duration = Duration::from_secs(30); +const MAX_HOST_CONFIGURATION_BYTES: u64 = 2 * 1024 * 1024; +const MAX_METADATA_BYTES: u64 = 16 * 1024 * 1024; +const METADATA_PATH: &str = "metadata.json"; +const STREAM_DISK_PREFIXES: &[&str] = &["sd", "nvme", "vd", "hd", "mmcblk"]; + +#[derive(Debug, Clone)] +pub(super) enum Request { + Autorun, + HostConfiguration(String), + Stream(Url), +} + +#[derive(Debug, Clone)] +pub(super) enum Plan { + Install { + config: Box, + source: String, + }, + Stream { + image: Url, + }, +} + +impl Plan { + pub fn description(&self) -> String { + match self { + Self::Install { source, .. } => format!("Install from {source}"), + Self::Stream { image } => format!("StreamDisk from {image}"), + } + } +} + +pub(super) fn prepare(settings: &Config, request: &Request) -> Result<(Config, Plan), Error> { + let root = media_root(settings)?; + prepare_from_media(settings, request, root.as_deref()) +} + +fn prepare_from_media( + settings: &Config, + request: &Request, + root: Option<&Path>, +) -> Result<(Config, Plan), Error> { + let settings = match root { + Some(root) => settings.overlay(root)?, + None => settings.clone(), + }; + settings.require_autorun()?; + let images = match root { + Some(root) => config::discover_images(root, &settings.media.cosi_directory)?, + None => Vec::new(), + }; + let source = match request { + Request::HostConfiguration(source) => { + remote_url(source)?; + Some(source.clone()) + } + Request::Stream(source) => { + return Ok(( + settings, + Plan::Stream { + image: source.clone(), + }, + )); + } + Request::Autorun => match &settings.autorun.host_configuration { + Some(source) => Some(source.clone()), + None => match fs::metadata(config::DEFAULT_HOST_CONFIGURATION) { + Ok(_) => Some(config::DEFAULT_HOST_CONFIGURATION.into()), + Err(error) if error.kind() == ErrorKind::NotFound => None, + Err(error) => { + return Err(error).context("Failed to inspect default Host Configuration") + } + }, + }, + }; + let plan = match source { + Some(source) => { + let url = source_url(&source, root)?; + let text = read_text(&url)?; + let config = resolve_host_configuration(&text, images.first().map(PathBuf::as_path))?; + Plan::Install { + config: Box::new(config), + source, + } + } + None => { + let path = images.first().with_context(|| { + format!( + "No Host Configuration at '{}' and no COSI images in '{}'. Configure networking in Shell, then choose a remote source.", + config::DEFAULT_HOST_CONFIGURATION, + settings.media.cosi_directory.display() + ) + })?; + Plan::Stream { + image: file_url(path)?, + } + } + }; + Ok((settings, plan)) +} + +fn media_root(settings: &Config) -> Result, Error> { + if let Some(path) = &settings.media.mount_path { + ensure!( + mountpoint::check_is_mountpoint(path)?, + "media.mountPath is not a mount point: {}", + path.display() + ); + return Ok(Some(path.canonicalize()?)); + } + let label = settings + .media + .cdrom_label + .as_deref() + .unwrap_or(config::DEFAULT_MEDIA_LABEL); + let devices = blkid::devices_by_label(label)?; + let Some(device) = config::choose_media(&devices)? else { + return Ok(None); + }; + let target = Path::new(config::MEDIA_MOUNT); + fs::create_dir_all(target)?; + if mountpoint::check_is_mountpoint(target)? { + let mounts = FindMnt::run()? + .root() + .context("findmnt did not report a root mount")?; + let existing = mounts + .find_mount_point_for_path(target) + .context("Mounted installer media is not visible in findmnt")?; + ensure!( + existing.target == target + && existing.options.contains(MOUNT_OPTION_READ_ONLY) + && existing.source.as_ref().map(fs::canonicalize).transpose()? + == Some(device.canonicalize()?), + "Existing installer mount does not match the requested read-only media" + ); + } else { + mount::mount( + device, + target, + MountFileSystemType::Auto, + &[ + MOUNT_OPTION_READ_ONLY.into(), + "nosuid".into(), + "nodev".into(), + "noexec".into(), + ], + )?; + } + Ok(Some(target.into())) +} + +pub(super) fn remote_url(source: &str) -> Result { + let url = Url::parse(source).context("Enter an absolute http:// or https:// URL")?; + ensure!( + matches!(url.scheme(), "http" | "https"), + "Remote source must use HTTP or HTTPS" + ); + ensure!( + url.username().is_empty() && url.password().is_none(), + "Credentials in source URLs are not supported" + ); + Ok(url) +} + +fn source_url(source: &str, root: Option<&Path>) -> Result { + if source.starts_with('/') { + return file_url(Path::new(source)); + } + if source.contains("://") { + let url = Url::parse(source)?; + ensure!( + matches!(url.scheme(), "file" | "http" | "https"), + "Unsupported source URL scheme" + ); + return Ok(url); + } + let root = root.context("A media-relative Host Configuration requires installer media")?; + let path = root + .join(source) + .canonicalize() + .context("Failed to resolve media-relative Host Configuration")?; + ensure!( + path.starts_with(root.canonicalize()?), + "Host Configuration escapes the media root" + ); + file_url(&path) +} + +fn file_url(path: &Path) -> Result { + Url::from_file_path(path) + .map_err(|()| anyhow!("Cannot convert '{}' to a file URL", path.display())) +} + +fn reader(url: &Url) -> Result, Error> { + match url.scheme() { + "file" => { + let path = url + .to_file_path() + .map_err(|()| anyhow!("Invalid local file URL"))?; + Ok(Box::new(File::open(&path).with_context(|| { + format!("Failed to open '{}'", path.display()) + })?)) + } + "http" | "https" => Ok(Box::new( + Client::builder() + .timeout(SOURCE_TIMEOUT) + .build()? + .get(url.clone()) + .send()? + .error_for_status()?, + )), + _ => bail!("Unsupported source scheme '{}'", url.scheme()), + } +} + +fn read_text(url: &Url) -> Result { + let mut text = String::new(); + reader(url)? + .take(MAX_HOST_CONFIGURATION_BYTES + 1) + .read_to_string(&mut text)?; + ensure!( + text.len() as u64 <= MAX_HOST_CONFIGURATION_BYTES, + "Host Configuration exceeds the size limit" + ); + Ok(text) +} + +pub(super) fn resolve_host_configuration( + text: &str, + selected: Option<&Path>, +) -> Result { + let mut config: HostConfiguration = + serde_yaml::from_str(text).context("Invalid Host Configuration YAML")?; + if let Some(image) = &mut config.image { + if image.url.as_str() == IMAGE_MARKER { + image.url = file_url(selected.context( + "Host Configuration uses installer://image but no COSI was discovered", + )?)?; + } + } + config + .validate() + .context("Host Configuration validation failed")?; + Ok(config) +} + +// Only the repeat-install preflight needs these fields; Trident validates the full COSI. +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +struct Metadata { + version: String, + disk: Option, + images: Vec, +} + +#[derive(Debug, Deserialize)] +struct DiskMetadata { + size: u64, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +struct ImageMetadata { + fs_uuid: Option, +} + +fn metadata(reader: impl Read) -> Result { + let mut archive = Archive::new(reader.take(MAX_METADATA_BYTES)); + for entry in archive.entries().context("Invalid COSI archive")? { + let mut entry = entry?; + if entry.path()?.as_ref() == Path::new(METADATA_PATH) { + ensure!( + entry.header().entry_type().is_file(), + "COSI metadata must be a regular file" + ); + ensure!( + entry.size() <= MAX_METADATA_BYTES, + "COSI metadata is too large" + ); + let metadata: Metadata = + serde_json::from_reader(&mut entry).context("Invalid COSI metadata")?; + ensure!( + metadata.disk.as_ref().is_some_and(|disk| disk.size > 0), + "StreamDisk requires COSI disk metadata" + ); + ensure!( + !metadata.images.is_empty(), + "COSI metadata contains no images" + ); + let (major, minor) = metadata + .version + .split_once('.') + .context("Invalid COSI metadata version")?; + ensure!( + major == "1" && minor.parse::()? >= 2, + "StreamDisk requires COSI v1.2 or newer" + ); + return Ok(metadata); + } + } + bail!("COSI metadata.json was not found within the metadata read limit") +} + +pub(super) fn stream_preflight(image: &Url, force: bool) -> Result { + let metadata = metadata(reader(image)?)?; + let devices = lsblk::list().context("Failed to inspect attached disks")?; + let required = metadata + .disk + .as_ref() + .context("COSI disk metadata is missing")? + .size; + let candidates = devices + .iter() + .filter(|device| device.blkdev_type == BlockDeviceType::Disk) + .filter(|device| { + STREAM_DISK_PREFIXES + .iter() + .any(|prefix| device.name.starts_with(prefix)) + }) + .filter(|device| device.size >= required) + .collect::>(); + let minimum = candidates + .iter() + .map(|device| device.size) + .min() + .context("No disk fits the COSI image")?; + // Until StreamDisk accepts an explicit target, any minimum-sized candidate may win. + for device in candidates.iter().filter(|device| device.size == minimum) { + ensure!( + !device.readonly && device.get_all_mountpoints_recursive().is_empty(), + "StreamDisk could select a read-only or mounted disk '{}'; refusing to overwrite live media", + device.device_path().display() + ); + } + if force { + return Ok(false); + } + let expected = metadata + .images + .iter() + .filter_map(|image| image.fs_uuid.as_deref()) + .filter(|uuid| !uuid.is_empty()) + .map(str::to_ascii_lowercase) + .collect::>(); + ensure!( + !expected.is_empty(), + "COSI has no filesystem UUIDs for repeat-install protection" + ); + Ok(contains_image(&devices, &expected)) +} + +fn contains_image(devices: &[BlockDevice], expected: &HashSet) -> bool { + fn collect(devices: &[BlockDevice], present: &mut HashSet) { + for device in devices { + if let Some(uuid) = &device.fsuuid { + present.insert(uuid.to_string().to_ascii_lowercase()); + } + collect(&device.children, present); + } + } + let mut present = HashSet::new(); + collect(devices, &mut present); + expected.iter().all(|uuid| present.contains(uuid)) +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::io::Cursor; + + use indoc::indoc; + use tar::Builder as TarBuilder; + use tempfile::TempDir; + + #[test] + fn remote_sources_are_explicit_and_limited() { + remote_url("https://example.com/image.cosi").unwrap(); + for invalid in [ + "file:///image.cosi", + "ftp://example.com/image.cosi", + "image.cosi", + "https://user:secret@example.com/image.cosi", + ] { + remote_url(invalid).unwrap_err(); + } + } + + #[test] + fn marker_requires_an_image_and_does_not_touch_other_urls() { + let yaml = valid_host_configuration(); + resolve_host_configuration(yaml, None).unwrap_err(); + let resolved = + resolve_host_configuration(yaml, Some(Path::new("/media/cosi/an image.cosi"))).unwrap(); + assert_eq!( + resolved.image.unwrap().url.as_str(), + "file:///media/cosi/an%20image.cosi" + ); + let remote = yaml.replace(IMAGE_MARKER, "https://example.com/image.cosi"); + assert_eq!( + resolve_host_configuration(&remote, None) + .unwrap() + .image + .unwrap() + .url + .as_str(), + "https://example.com/image.cosi" + ); + } + + fn valid_host_configuration() -> &'static str { + indoc! {" + storage: + disks: + - id: os + device: /dev/test + partitionTableType: gpt + partitions: + - id: esp + type: esp + size: 192M + - id: root + type: root + size: 1G + filesystems: + - deviceId: esp + mountPoint: + path: /boot/efi + - deviceId: root + mountPoint: + path: / + image: + url: installer://image + sha384: ignored + "} + } + + #[test] + fn host_configuration_wins_and_invalid_hc_never_falls_back() { + let root = TempDir::new().unwrap(); + fs::create_dir(root.path().join("cosi")).unwrap(); + fs::write(root.path().join("cosi/z.cosi"), "").unwrap(); + fs::write(root.path().join("cosi/a.cosi"), "").unwrap(); + let path = root.path().join("host.yaml"); + fs::write(&path, valid_host_configuration()).unwrap(); + let mut settings = Config::default(); + settings.autorun.host_configuration = Some(path.display().to_string()); + let (_, plan) = + prepare_from_media(&settings, &Request::Autorun, Some(root.path())).unwrap(); + let Plan::Install { config, .. } = plan else { + panic!("HC must select Install") + }; + assert_eq!( + config.image.unwrap().url, + file_url(&root.path().join("cosi/a.cosi")).unwrap() + ); + fs::write(&path, "unknown: field").unwrap(); + prepare_from_media(&settings, &Request::Autorun, Some(root.path())).unwrap_err(); + } + + #[test] + fn configuration_read_limit_is_enforced() { + let root = TempDir::new().unwrap(); + let path = root.path().join("oversized.yaml"); + fs::write( + &path, + vec![b'x'; usize::try_from(MAX_HOST_CONFIGURATION_BYTES + 1).unwrap()], + ) + .unwrap(); + let error = read_text(&file_url(&path).unwrap()).unwrap_err(); + assert!(error.to_string().contains("size limit"), "{error:#}"); + } + + #[test] + fn truncated_and_missing_metadata_are_errors() { + metadata(Cursor::new(b"not a tar file")).unwrap_err(); + let mut builder = TarBuilder::new(Vec::new()); + builder.finish().unwrap(); + metadata(Cursor::new(builder.into_inner().unwrap())).unwrap_err(); + } + + #[test] + fn repeat_guard_requires_the_complete_nonempty_uuid_set() { + let devices = vec![BlockDevice { + children: vec![BlockDevice { + fsuuid: Some("uuid-1".into()), + ..Default::default() + }], + ..Default::default() + }]; + assert!(contains_image(&devices, &HashSet::from(["uuid-1".into()]))); + assert!(!contains_image( + &devices, + &HashSet::from(["uuid-1".into(), "uuid-2".into()]) + )); + } +} diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs new file mode 100644 index 0000000000..37a2b42c3b --- /dev/null +++ b/crates/installer/src/ui.rs @@ -0,0 +1,847 @@ +use std::{ + collections::VecDeque, + fs::{File, OpenOptions}, + io::{self, IsTerminal, Write}, + time::{Duration, Instant}, +}; + +use anyhow::{anyhow, Context, Error}; +use crossterm::{ + event::{self, Event as TerminalEvent, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}, + execute, + terminal::{self, EnterAlternateScreen, LeaveAlternateScreen}, +}; +use log::error; +use ratatui::{ + backend::CrosstermBackend, + layout::{Constraint, Layout}, + style::{Color, Modifier, Style}, + symbols::border::Set as BorderSet, + text::{Line, Span}, + widgets::{Block, Borders, List, ListItem, Paragraph, Wrap}, + Frame, Terminal, +}; +use tokio::{ + signal::unix::{self, SignalKind}, + sync::mpsc, + task::{self, JoinHandle}, + time, +}; +use url::Url; + +use osutils::{systemd, terminal as os_terminal}; +use trident_proto::v1::{servicing_response::Response as ResponseBody, LogLevel}; + +use crate::{ + client::{self, Completion, Event}, + config::Config, + demo::{self, Scenario}, + source::{self, Request}, +}; + +const FRAME_INTERVAL: Duration = Duration::from_millis(100); +const REBOOT_DELAY: Duration = Duration::from_secs(5); +const EVENT_CAPACITY: usize = 64; +const LOG_CAPACITY: usize = 500; +const DISPLAY_LOG_CHARACTERS: usize = 4096; +const ASCII_BORDER: BorderSet = BorderSet { + top_left: "+", + top_right: "+", + bottom_left: "+", + bottom_right: "+", + vertical_left: "|", + vertical_right: "|", + horizontal_top: "-", + horizontal_bottom: "-", +}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Screen { + Progress, + Error, + Recovery, + Success, + AlreadyPresent, + Force, + StreamUrl, + HostConfigUrl, + Menu, +} + +#[derive(Debug)] +struct Model { + screen: Screen, + source: String, + activity: String, + details: String, + logs: VecDeque<(LogLevel, String)>, + verbose: bool, + selected: usize, + scroll: u16, + url: String, + uncertain: bool, + reboot: bool, + reboot_at: Option, + stream_image: Option, + started: Instant, + failed: bool, +} + +impl Model { + fn new() -> Self { + Self { + screen: Screen::Progress, + source: String::new(), + activity: "Preparing installer".into(), + details: String::new(), + logs: VecDeque::new(), + verbose: false, + selected: 0, + scroll: 0, + url: String::new(), + uncertain: false, + reboot: true, + reboot_at: None, + stream_image: None, + started: Instant::now(), + failed: false, + } + } + + fn fail(&mut self, details: String, uncertain: bool) { + self.details = details; + self.uncertain |= uncertain; + self.reboot_at = None; + self.failed = true; + self.scroll = 0; + self.selected = 0; + self.screen = Screen::Error; + } + + fn event(&mut self, event: Event) -> Option { + match event { + Event::Prepared { + description, + reboot, + stream_image, + } => { + self.source = description; + self.reboot = reboot; + self.stream_image = stream_image; + Some(self.source.clone()) + } + Event::AlreadyPresent(details) => { + self.details = details; + self.reboot_at = None; + self.screen = Screen::AlreadyPresent; + self.selected = 0; + Some(self.details.clone()) + } + Event::Error { details, uncertain } => { + self.fail(details, uncertain); + Some(self.details.clone()) + } + Event::Response(response) => match response.response { + Some(ResponseBody::Started(_)) => { + self.activity = "Servicing started".into(); + Some(self.activity.clone()) + } + Some(ResponseBody::Log(log)) => { + let level = log.level(); + let mut characters = log.message.chars(); + let mut message = display_text( + &characters + .by_ref() + .take(DISPLAY_LOG_CHARACTERS) + .collect::(), + ); + if characters.next().is_some() { + message.push_str("\n[Display shortened; the full record is preserved in the diagnostic log]"); + } + if level == LogLevel::Info { + self.activity = message.clone(); + } + if self.logs.len() == LOG_CAPACITY { + self.logs.pop_front(); + } + self.logs.push_back((level, message.clone())); + if matches!( + level, + LogLevel::Error | LogLevel::Warn | LogLevel::Info | LogLevel::Unspecified + ) { + Some(format!("{level}: {message}")) + } else { + None + } + } + Some(ResponseBody::Completed(completed)) => match client::completion(completed) { + Ok(Completion::Success { + reboot_required, + performed, + }) => { + self.uncertain = false; + self.failed = false; + self.screen = Screen::Success; + self.selected = 0; + self.details = if performed { + "Installation completed successfully.\nRemove the installation media before booting the installed OS.".into() + } else { + "Trident reported that no servicing was performed.".into() + }; + self.reboot_at = (self.reboot && reboot_required && performed) + .then(|| Instant::now() + REBOOT_DELAY); + Some(self.details.clone()) + } + Ok(Completion::Failure(details)) => { + self.uncertain = false; + self.fail(details, false); + Some(self.details.clone()) + } + Err(error) => { + self.fail(format!("{error:#}"), true); + Some(self.details.clone()) + } + }, + None => { + self.fail( + "Response has no body; installation outcome is unknown".into(), + true, + ); + Some(self.details.clone()) + } + }, + } + } + + fn choices(&self) -> Vec<&'static str> { + match self.screen { + Screen::Recovery => vec![ + "Shell", + "Stream COSI from URL", + "Install from Host Configuration URL", + "Shutdown", + ], + Screen::Success => vec!["Reboot", "Shell"], + Screen::AlreadyPresent => vec!["Reboot", "Force reinstall", "Shell"], + Screen::Force => vec!["Cancel", "Force reinstall and erase"], + Screen::Menu => vec!["Return to installation", "Shell"], + Screen::Error => vec!["Continue to recovery", "Shell"], + _ => Vec::new(), + } + } + + fn key(&mut self, key: KeyEvent, demo: bool) -> Option { + if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + return None; + } + if matches!(self.screen, Screen::StreamUrl | Screen::HostConfigUrl) { + return match key.code { + KeyCode::Esc => { + self.screen = Screen::Recovery; + None + } + KeyCode::Backspace => { + self.url.pop(); + None + } + KeyCode::Char(c) if !c.is_control() => { + self.url.push(c); + None + } + KeyCode::Enter => match source::remote_url(self.url.trim()) { + Ok(url) => Some(Action::Start( + if self.screen == Screen::StreamUrl { + Request::Stream(url) + } else { + Request::HostConfiguration(url.to_string()) + }, + false, + )), + Err(error) => { + self.fail(format!("{error:#}"), false); + None + } + }, + _ => None, + }; + } + if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') { + self.screen = if self.screen == Screen::Progress { + Screen::Menu + } else { + Screen::Recovery + }; + self.selected = 0; + return None; + } + let count = self.choices().len(); + match key.code { + KeyCode::Char(digit) if digit.is_ascii_digit() => { + if let Some(index) = digit.to_digit(10).and_then(|value| value.checked_sub(1)) { + if (index as usize) < count { + self.selected = index as usize; + } + } + } + KeyCode::Char('q') if demo => return Some(Action::Quit), + KeyCode::Char('s') => return Some(Action::Shell), + KeyCode::Char('v') => { + self.verbose = !self.verbose; + return None; + } + KeyCode::PageUp => { + self.scroll = if matches!(self.screen, Screen::Progress | Screen::Menu) { + self.scroll.saturating_add(5) + } else { + self.scroll.saturating_sub(5) + }; + return None; + } + KeyCode::PageDown => { + self.scroll = if matches!(self.screen, Screen::Progress | Screen::Menu) { + self.scroll.saturating_sub(5) + } else { + self.scroll.saturating_add(5) + }; + return None; + } + KeyCode::Esc => { + self.screen = match self.screen { + Screen::Progress => Screen::Menu, + Screen::Menu => Screen::Progress, + Screen::Force => Screen::AlreadyPresent, + Screen::Error => Screen::Recovery, + other => other, + }; + self.selected = 0; + return None; + } + _ => {} + } + if count == 0 { + return None; + } + match key.code { + KeyCode::Up | KeyCode::BackTab => self.selected = (self.selected + count - 1) % count, + KeyCode::Down | KeyCode::Tab => self.selected = (self.selected + 1) % count, + KeyCode::Enter => { + return match (self.screen, self.selected) { + (Screen::Recovery, 0) + | (Screen::Success, 1) + | (Screen::AlreadyPresent, 2) + | (Screen::Menu, 1) + | (Screen::Error, 1) => Some(Action::Shell), + (Screen::Recovery, 1) => { + self.url.clear(); + self.screen = Screen::StreamUrl; + None + } + (Screen::Recovery, 2) => { + self.url.clear(); + self.screen = Screen::HostConfigUrl; + None + } + (Screen::Recovery, 3) => Some(Action::Poweroff), + (Screen::Success, 0) | (Screen::AlreadyPresent, 0) => Some(Action::Reboot), + (Screen::AlreadyPresent, 1) => { + self.screen = Screen::Force; + self.selected = 0; + None + } + (Screen::Force, 0) => { + self.screen = Screen::AlreadyPresent; + self.selected = 0; + None + } + (Screen::Force, 1) => Some(Action::Start( + self.stream_image + .clone() + .map(Request::Stream) + .unwrap_or(Request::Autorun), + true, + )), + (Screen::Menu, 0) => { + self.screen = Screen::Progress; + None + } + (Screen::Error, 0) => { + self.screen = Screen::Recovery; + self.selected = 0; + None + } + _ => None, + } + } + _ => {} + } + None + } +} + +enum Action { + Start(Request, bool), + Shell, + Reboot, + Poweroff, + Quit, +} + +pub(super) struct Output { + pub log: Option, + pub mirrors: Vec, + pub control: String, +} + +impl Output { + fn record(&mut self, event: &Event) -> Result<(), Error> { + if let Some(log) = &mut self.log { + writeln!(log, "{event:#?}").context("Failed to preserve installer diagnostics")?; + log.flush()?; + } + Ok(()) + } + + fn announce(&mut self, message: &str) -> Result<(), Error> { + for mirror in &mut self.mirrors { + writeln!( + mirror, + "\r\n[installer; control {}] {}", + self.control, + display_text(message) + ) + .context("Failed to mirror installer output to an active console")?; + } + Ok(()) + } +} + +struct Display { + terminal: Terminal>, + active: bool, +} + +impl Display { + fn new() -> Result { + let mut display = Self { + terminal: Terminal::new(CrosstermBackend::new(io::stdout()))?, + active: false, + }; + display.resume()?; + Ok(display) + } + + fn resume(&mut self) -> Result<(), Error> { + terminal::enable_raw_mode()?; + self.active = true; + execute!(self.terminal.backend_mut(), EnterAlternateScreen)?; + self.terminal.clear()?; + Ok(()) + } + + fn suspend(&mut self) -> Result<(), Error> { + terminal::disable_raw_mode()?; + execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; + self.terminal.show_cursor()?; + self.active = false; + Ok(()) + } +} + +impl Drop for Display { + fn drop(&mut self) { + if self.active { + if let Err(error) = self.suspend() { + error!("Failed to restore terminal: {error:#}"); + } + } + } +} + +fn spawn_operation( + settings: &Config, + request: Request, + force: bool, + uncertain: bool, + scenario: Option, + tx: mpsc::Sender, +) -> JoinHandle> { + let settings = settings.clone(); + task::spawn(async move { + match scenario { + Some(scenario) => demo::run(if force { Scenario::Success } else { scenario }, tx).await, + None => client::execute(settings, request, force, uncertain, tx).await, + } + }) +} + +pub(super) async fn run( + settings: Config, + initial_error: Option, + scenario: Option, + plain: bool, + mut output: Output, +) -> Result<(), Error> { + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let mut interrupts = unix::signal(SignalKind::interrupt())?; + let mut termination = unix::signal(SignalKind::terminate())?; + let mut model = Model::new(); + let mut worker = if let Some(details) = initial_error { + model.fail(details, false); + None + } else { + Some(spawn_operation( + &settings, + Request::Autorun, + false, + false, + scenario, + tx.clone(), + )) + }; + let interactive = io::stdin().is_terminal() && io::stdout().is_terminal(); + let mut display = if interactive && !plain { + Some(Display::new()?) + } else { + None + }; + let mut shell: Option>> = None; + let mut printed_screen = None; + loop { + for _ in 0..EVENT_CAPACITY { + let Ok(event) = rx.try_recv() else { break }; + output.record(&event)?; + if let Some(message) = model.event(event) { + output.announce(&message)?; + if display.is_none() && shell.is_none() { + println!("{}", display_text(&message)); + } + } + } + if worker.as_ref().is_some_and(JoinHandle::is_finished) { + if let Some(finished) = worker.take() { + match finished.await { + Ok(Ok(())) => {} + Ok(Err(error)) => { + model.fail(format!("Installer worker failed: {error:#}"), true) + } + Err(error) => model.fail(format!("Installer worker terminated: {error}"), true), + } + } + } + if shell.as_ref().is_some_and(JoinHandle::is_finished) { + if let Some(finished) = shell.take() { + match finished.await { + Ok(Ok(())) => {} + Ok(Err(error)) => model.fail(format!("Shell failed: {error:#}"), false), + Err(error) => model.fail(format!("Shell task terminated: {error}"), false), + } + } + if let Some(display) = &mut display { + display.resume()?; + } + printed_screen = None; + } + if shell.is_some() { + tokio::select! { + _ = time::sleep(FRAME_INTERVAL) => {} + _ = termination.recv() => return Err(anyhow!("Installer stopped; servicing may continue in Trident")), + } + continue; + } + if model + .reboot_at + .is_some_and(|deadline| Instant::now() >= deadline) + { + model.reboot_at = None; + if scenario.is_none() { + match systemd::reboot() { + Ok(()) => return Ok(()), + Err(error) => model.fail(format!("Failed to reboot: {error:#}"), false), + } + } + } + if !interactive { + if worker.is_none() && rx.is_empty() { + if model.failed { + return Err(anyhow!( + "{}\nRecovery requires an interactive terminal.", + model.details + )); + } + return Ok(()); + } + } else { + if let Some(display) = &mut display { + display + .terminal + .draw(|frame| render(frame, &model, scenario.is_some()))?; + } else if printed_screen != Some(model.screen) { + println!( + "\n{}\n{}", + title(model.screen), + display_text(&model.details) + ); + for (index, choice) in model.choices().iter().enumerate() { + println!("{}: {choice}", index + 1); + } + if matches!(model.screen, Screen::StreamUrl | Screen::HostConfigUrl) { + println!("Enter an http:// or https:// URL, then press Enter"); + } + println!("Number + Enter: select; S + Enter: shell; Esc: menu; V: details"); + printed_screen = Some(model.screen); + } + if event::poll(Duration::ZERO)? { + let event = event::read()?; + if let TerminalEvent::Key(key) = event { + if let Some(action) = model.key(key, scenario.is_some()) { + match action { + Action::Quit => return Ok(()), + Action::Shell => { + if let Some(display) = &mut display { + display.suspend()?; + } + println!("\nShell: type exit to return. Installation continues; automatic reboot waits."); + let tty = + OpenOptions::new().read(true).write(true).open("/dev/tty")?; + shell = + Some(task::spawn_blocking(move || os_terminal::shell(&tty))); + } + Action::Start(request, force) => { + if worker.is_some() { + model.fail("An installer operation is still active. Wait for it to finish.".into(), true); + } else { + model.screen = Screen::Progress; + model.activity = "Preparing installation".into(); + model.started = Instant::now(); + model.reboot_at = None; + model.scroll = 0; + model.selected = 0; + let recovery_demo = scenario.map(|_| Scenario::Success); + worker = Some(spawn_operation( + &settings, + request, + force, + model.uncertain, + recovery_demo, + tx.clone(), + )); + } + } + Action::Reboot | Action::Poweroff if scenario.is_some() => { + model.reboot_at = None; + model.details = "DEMO: Machine power action suppressed".into(); + model.screen = Screen::Success; + model.selected = 0; + } + Action::Reboot => match systemd::reboot() { + Ok(()) => return Ok(()), + Err(error) => { + model.fail(format!("Failed to reboot: {error:#}"), false) + } + }, + Action::Poweroff => match systemd::poweroff() { + Ok(()) => return Ok(()), + Err(error) => { + model.fail(format!("Failed to shut down: {error:#}"), false) + } + }, + } + } + } + } + } + tokio::select! { + _ = time::sleep(FRAME_INTERVAL) => {} + signal = interrupts.recv() => { + if signal.is_none() { + return Err(anyhow!("Installer interrupt handler closed")); + } + model.screen = if worker.is_some() { Screen::Menu } else { Screen::Recovery }; + model.selected = 0; + } + _ = termination.recv() => return Err(anyhow!("Installer stopped; servicing may continue in Trident")), + } + } +} + +fn display_text(text: &str) -> String { + text.chars() + .filter(|c| !c.is_control() || *c == '\n' || *c == '\t') + .collect::() + .replace('\t', " ") +} + +fn title(screen: Screen) -> &'static str { + match screen { + Screen::Progress => "INSTALLING", + Screen::Error => "INSTALLATION ERROR", + Screen::Recovery => "RECOVERY", + Screen::Success => "INSTALLATION COMPLETE", + Screen::AlreadyPresent => "IMAGE ALREADY PRESENT", + Screen::Force => "CONFIRM FORCE REINSTALL", + Screen::StreamUrl => "STREAM COSI FROM URL", + Screen::HostConfigUrl => "INSTALL FROM HOST CONFIGURATION URL", + Screen::Menu => "INSTALLER MENU", + } +} + +fn block(title: &str) -> Block<'_> { + Block::default() + .borders(Borders::ALL) + .border_set(ASCII_BORDER) + .border_style(Style::default().fg(Color::DarkGray)) + .title(title) +} + +fn render(frame: &mut Frame, model: &Model, demo: bool) { + let areas = Layout::vertical([ + Constraint::Length(2), + Constraint::Length(3), + Constraint::Min(4), + Constraint::Length(if model.choices().is_empty() { 0 } else { 6 }), + Constraint::Length(2), + ]) + .split(frame.area()); + let header = format!( + " TRIDENT INSTALLER{} {}", + if demo { " / DEMO" } else { "" }, + title(model.screen) + ); + frame.render_widget( + Paragraph::new(header).style( + Style::default() + .fg(Color::Cyan) + .add_modifier(Modifier::BOLD), + ), + areas[0], + ); + frame.render_widget( + Paragraph::new(display_text(&model.source)).wrap(Wrap { trim: false }), + areas[1], + ); + let content = match model.screen { + Screen::Progress | Screen::Menu => { + let mut lines = vec![Line::from(Span::styled(model.activity.clone(), Style::default().fg(Color::Cyan))), Line::default()]; + let logs = model.logs.iter().filter(|(level, _)| model.verbose || matches!(level, LogLevel::Error | LogLevel::Warn | LogLevel::Info | LogLevel::Unspecified)).collect::>(); + let visible = usize::from(areas[2].height.saturating_sub(4)); + let end = logs.len().saturating_sub(usize::from(model.scroll).min(logs.len().saturating_sub(1))); + let start = end.saturating_sub(visible); + lines.extend(logs[start..end].iter().map(|(level, message)| Line::from(format!("{level} {message}")))); + Paragraph::new(lines).block(block("Activity / logs")).wrap(Wrap { trim: false }) + } + Screen::StreamUrl | Screen::HostConfigUrl => Paragraph::new(format!("Enter an http:// or https:// URL:\n\n{}\n\nEnter: start Esc: back", display_text(&model.url))) + .block(block("Remote source")).wrap(Wrap { trim: false }), + Screen::Force => Paragraph::new("Force reinstall will erase the disk selected by Trident.\n\nExisting partitions and files will be lost.\nCancel is selected by default.") + .block(block("Destructive action")).wrap(Wrap { trim: false }), + _ => Paragraph::new(display_text(&model.details)) + .block(block(if model.screen == Screen::Error { "Full error details / PgUp, PgDn to scroll" } else { "Result" })) + .wrap(Wrap { trim: false }).scroll((model.scroll, 0)), + }; + frame.render_widget(content, areas[2]); + let choices = model + .choices() + .iter() + .enumerate() + .map(|(index, choice)| { + ListItem::new(format!( + "{} {choice}", + if index == model.selected { ">" } else { " " } + )) + .style(if index == model.selected { + Style::default() + .fg(Color::Cyan) + .add_modifier(Modifier::BOLD) + } else { + Style::default() + }) + }) + .collect::>(); + frame.render_widget(List::new(choices).block(block("Actions")), areas[3]); + let countdown = model + .reboot_at + .map(|deadline| { + format!( + "Automatic reboot in {}s; Shell holds reboot", + deadline.saturating_duration_since(Instant::now()).as_secs() + ) + }) + .unwrap_or_else(|| format!("Elapsed {}s", model.started.elapsed().as_secs())); + frame.render_widget( + Paragraph::new(format!( + "Up/Down + Enter: select S: shell V: details Esc: menu{}\n{countdown}", + if demo { " Q: quit" } else { "" } + )) + .style(Style::default().fg(Color::Cyan)), + areas[4], + ); +} + +#[cfg(test)] +mod tests { + use super::*; + + use ratatui::backend::TestBackend; + + fn enter() -> KeyEvent { + KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) + } + + #[test] + fn error_continue_and_shell_keep_the_originating_screen() { + let mut model = Model::new(); + model.fail("Complete diagnostic information".into(), false); + assert!(model.key(enter(), false).is_none()); + assert_eq!(model.screen, Screen::Recovery); + assert!(matches!(model.key(enter(), false), Some(Action::Shell))); + assert_eq!(model.screen, Screen::Recovery); + assert_eq!(model.details, "Complete diagnostic information"); + model.screen = Screen::Success; + model.selected = 1; + assert!(matches!(model.key(enter(), false), Some(Action::Shell))); + assert_eq!(model.screen, Screen::Success); + model.selected = 0; + assert!(matches!(model.key(enter(), false), Some(Action::Reboot))); + } + + #[test] + fn force_is_never_the_default_and_requires_confirmation() { + let mut model = Model::new(); + model.screen = Screen::AlreadyPresent; + model.selected = 1; + assert!(model.key(enter(), false).is_none()); + assert_eq!(model.screen, Screen::Force); + assert_eq!(model.selected, 0); + assert!(model.key(enter(), false).is_none()); + assert_eq!(model.screen, Screen::AlreadyPresent); + } + + #[test] + fn screens_render_at_80_by_24_with_visible_actions() { + for screen in [ + Screen::Recovery, + Screen::Success, + Screen::Error, + Screen::AlreadyPresent, + ] { + let mut model = Model::new(); + model.screen = screen; + model.details = "Diagnostic or result details".into(); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains("Shell"), "{text}"); + if screen == Screen::Success { + assert!(text.contains("Reboot"), "{text}"); + } + if screen == Screen::Recovery { + assert!(text.contains("Shutdown"), "{text}"); + } + } + } +} diff --git a/crates/osutils/Cargo.toml b/crates/osutils/Cargo.toml index 9480233ee2..d59ea7898f 100644 --- a/crates/osutils/Cargo.toml +++ b/crates/osutils/Cargo.toml @@ -19,7 +19,7 @@ indoc = {workspace = true} inventory = {workspace = true} log = {workspace = true, features = ["std"]} netplan-types = {workspace = true} -nix = {workspace = true, features = ["fs"]} +nix = {workspace = true, features = ["fs", "process", "term"]} once_cell = {workspace = true} openssl = {workspace = true} serde = {workspace = true} diff --git a/crates/osutils/src/blkid.rs b/crates/osutils/src/blkid.rs index 7132ef8a23..38a988cd6c 100644 --- a/crates/osutils/src/blkid.rs +++ b/crates/osutils/src/blkid.rs @@ -1,6 +1,6 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; -use anyhow::{Context, Error}; +use anyhow::{ensure, Context, Error}; use uuid::Uuid; use crate::dependencies::Dependency; @@ -48,6 +48,35 @@ pub fn get_filesystem_label(device_path: impl AsRef) -> Result) -> Result, Error> { + let label = label.as_ref(); + ensure!(!label.is_empty(), "Filesystem label must not be empty"); + let output = Dependency::Blkid + .cmd() + .args([ + "-c", + "/dev/null", + "-t", + &format!("LABEL={label}"), + "-o", + "device", + ]) + .output()?; + // blkid uses status 2 when its search has no matches. + if output.code() == Some(2) { + return Ok(Vec::new()); + } + output.check()?; + let mut devices = output + .output() + .lines() + .map(PathBuf::from) + .collect::>(); + devices.sort(); + devices.dedup(); + Ok(devices) +} + #[cfg(feature = "functional-test")] #[cfg_attr(not(test), allow(unused_imports, dead_code))] mod functional_test { diff --git a/crates/osutils/src/dependencies.rs b/crates/osutils/src/dependencies.rs index 61b4b6d445..f96d87760d 100644 --- a/crates/osutils/src/dependencies.rs +++ b/crates/osutils/src/dependencies.rs @@ -1,13 +1,18 @@ use std::{ borrow::Cow, ffi::{OsStr, OsString}, + fs::File, io::{self, Write}, - os::unix::process::ExitStatusExt, + os::unix::process::{CommandExt, ExitStatusExt}, path::PathBuf, process::{Command as StdCommand, Output, Stdio}, }; use log::trace; +use nix::{ + sys::signal::{self, SigSet, SigmaskHow, Signal}, + unistd::{self, Pid}, +}; use strum_macros::IntoStaticStr; use trident_api::error::{ @@ -88,6 +93,7 @@ impl DependencyResultExt for Result> { #[derive(Debug, Clone, Copy, IntoStaticStr)] #[strum(serialize_all = "lowercase")] pub enum Dependency { + Bash, Blkid, Chown, Chpasswd, @@ -223,11 +229,70 @@ pub struct Command { } impl Command { + pub fn run_interactive(&self, terminal: &File) -> Result<(), Box> { + let binary = self.dependency.path()?; + let execute = || -> Result { + if self.stdin_data.is_some() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Interactive commands cannot pipe stdin", + )); + } + let parent_group = unistd::tcgetpgrp(terminal)?; + let mut command = StdCommand::new(&binary); + command + .args(&self.args) + .envs(self.envs.clone()) + .stdin(terminal.try_clone()?) + .stdout(terminal.try_clone()?) + .stderr(terminal.try_clone()?) + .process_group(0); + let mut child = command.spawn()?; + let child_group = Pid::from_raw(i32::try_from(child.id()).map_err(io::Error::other)?); + let foreground = Self::set_foreground_group(terminal, child_group); + if let Err(error) = foreground { + child.kill()?; + child.wait()?; + return Err(error); + } + let continued = signal::killpg(child_group, Signal::SIGCONT); + let status = child.wait(); + let restored = Self::set_foreground_group(terminal, parent_group); + restored?; + continued?; + Ok(Output { + status: status?, + stdout: Vec::new(), + stderr: Vec::new(), + }) + }; + CommandOutput { + rendered_command: self.render_command(), + dependency: self.dependency, + inner: execute().map_err(|inner| DependencyError::CouldNotExecute { + dependency: self.dependency, + inner, + })?, + } + .check() + } + pub fn arg>(&mut self, arg: S) -> &mut Self { self.args.push(arg.as_ref().to_os_string()); self } + fn set_foreground_group(terminal: &File, group: Pid) -> io::Result<()> { + let mut blocked = SigSet::empty(); + blocked.add(Signal::SIGTTOU); + let mut previous = SigSet::empty(); + signal::pthread_sigmask(SigmaskHow::SIG_BLOCK, Some(&blocked), Some(&mut previous))?; + let result = unistd::tcsetpgrp(terminal, group); + signal::pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&previous), None)?; + result?; + Ok(()) + } + pub fn with_arg>(mut self, arg: S) -> Self { self.arg(arg); self diff --git a/crates/osutils/src/lib.rs b/crates/osutils/src/lib.rs index 6efde3b846..3a39e4c404 100644 --- a/crates/osutils/src/lib.rs +++ b/crates/osutils/src/lib.rs @@ -40,6 +40,7 @@ pub mod sfdisk; pub mod swap; pub mod systemd; pub mod tabfile; +pub mod terminal; pub mod tune2fs; pub mod udevadm; pub mod uki; diff --git a/crates/osutils/src/systemd.rs b/crates/osutils/src/systemd.rs index 28e6bf5731..1602d26331 100644 --- a/crates/osutils/src/systemd.rs +++ b/crates/osutils/src/systemd.rs @@ -42,6 +42,28 @@ where .with_context(|| format!("Failed to restart unit: {}", unit.as_ref())) } +pub fn stop_unit(unit: impl AsRef) -> Result<(), Error> { + let unit = unit.as_ref(); + Dependency::Systemctl + .cmd() + .args(["stop", unit]) + .run_and_check() + .with_context(|| format!("Failed to stop unit '{unit}'")) +} + +pub fn reboot() -> Result<(), Error> { + Dependency::Systemctl.cmd().arg("reboot").run_and_check()?; + Ok(()) +} + +pub fn poweroff() -> Result<(), Error> { + Dependency::Systemctl + .cmd() + .arg("poweroff") + .run_and_check()?; + Ok(()) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/osutils/src/terminal.rs b/crates/osutils/src/terminal.rs new file mode 100644 index 0000000000..79f6df7271 --- /dev/null +++ b/crates/osutils/src/terminal.rs @@ -0,0 +1,109 @@ +use std::{ + fs::{self, File, OpenOptions}, + io::{self, IsTerminal}, + os::{fd::AsRawFd, unix::fs::FileTypeExt}, + path::{Path, PathBuf}, +}; + +use anyhow::{ensure, Context, Error}; +use nix::{libc, unistd}; + +use crate::dependencies::Dependency; + +const ACTIVE_CONSOLES_PATH: &str = "/sys/class/tty/console/active"; +const GRAPHICAL_CONSOLE: &str = "tty1"; + +pub fn active_consoles() -> Result, Error> { + let active = fs::read_to_string(ACTIVE_CONSOLES_PATH) + .context("Failed to read active kernel consoles")?; + console_paths(&active) +} + +fn console_paths(active: &str) -> Result, Error> { + let mut consoles = Vec::new(); + for name in active.split_whitespace() { + ensure!( + name.chars().all(|c| c.is_ascii_alphanumeric()), + "Invalid console name '{name}'" + ); + let name = if name == "tty0" { + GRAPHICAL_CONSOLE + } else { + name + }; + let path = Path::new("/dev").join(name); + if !consoles.contains(&path) { + consoles.push(path); + } + } + ensure!(!consoles.is_empty(), "No active kernel consoles found"); + Ok(consoles) +} + +pub fn preferred_console(consoles: &[PathBuf]) -> Result<&Path, Error> { + consoles + .iter() + .find(|path| { + path.file_name() + .is_some_and(|name| name == GRAPHICAL_CONSOLE) + }) + .or_else(|| consoles.first()) + .map(PathBuf::as_path) + .context("No console available") +} + +pub fn attach(path: impl AsRef) -> Result { + let path = path.as_ref(); + let terminal = OpenOptions::new() + .read(true) + .write(true) + .open(path) + .with_context(|| format!("Failed to open terminal '{}'", path.display()))?; + ensure!( + terminal.metadata()?.file_type().is_char_device() && terminal.is_terminal(), + "Not a terminal: {}", + path.display() + ); + if unistd::getsid(None)? != unistd::getpid() { + unistd::setsid()?; + } + // The caller has stopped competing getty units before claiming the terminal. + if unsafe { libc::ioctl(terminal.as_raw_fd(), libc::TIOCSCTTY, 1) } < 0 { + return Err(io::Error::last_os_error()).context("Failed to claim controlling terminal"); + } + for target in [libc::STDIN_FILENO, libc::STDOUT_FILENO] { + // dup2 redirects the inherited stdio descriptors without transferring ownership. + if unsafe { libc::dup2(terminal.as_raw_fd(), target) } < 0 { + return Err(io::Error::last_os_error()).context("Failed to attach terminal"); + } + } + Ok(terminal) +} + +pub fn shell(terminal: &File) -> Result<(), Error> { + Dependency::Bash + .cmd() + .args(["--noprofile", "--norc", "-i"]) + .run_interactive(terminal)?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn graphical_console_wins_and_aliases_are_deduplicated() { + let consoles = console_paths("ttyS0 tty0 tty1").unwrap(); + assert_eq!( + consoles, + [PathBuf::from("/dev/ttyS0"), PathBuf::from("/dev/tty1")] + ); + assert_eq!( + preferred_console(&consoles).unwrap(), + Path::new("/dev/tty1") + ); + console_paths("../tty1").unwrap_err(); + console_paths("").unwrap_err(); + } +} diff --git a/packaging/docker/Dockerfile.azl b/packaging/docker/Dockerfile.azl index b1806bc901..db3380d9b1 100644 --- a/packaging/docker/Dockerfile.azl +++ b/packaging/docker/Dockerfile.azl @@ -23,6 +23,7 @@ COPY packaging ./packaging COPY LICENSE NOTICE ./ COPY bin/trident ./target/release/trident COPY bin/trident-acl-agent ./target/release/trident-acl-agent +COPY bin/trident-installer ./target/release/trident-installer ARG TRIDENT_VERSION=dev-build ARG RPM_VER=0.1.0 diff --git a/packaging/installer.toml b/packaging/installer.toml new file mode 100644 index 0000000000..4f1234fe94 --- /dev/null +++ b/packaging/installer.toml @@ -0,0 +1,8 @@ +mode = "autorun" + +[media] +cdromLabel = "TRIDENT_INSTALL" +cosiDirectory = "cosi/" + +[autorun] +reboot = true diff --git a/packaging/rpm/trident.spec b/packaging/rpm/trident.spec index ee37f3a83c..46acb8cbdc 100644 --- a/packaging/rpm/trident.spec +++ b/packaging/rpm/trident.spec @@ -251,6 +251,30 @@ The Trident ACL components required to orchestrate servicing of ACL images. # ------------------------------------------------------------------------------ +%package installer +Summary: Trident installer console for installer media +Requires: %{name} = %{version}-%{release} +Requires: bash +Conflicts: %{name}-install-service + +%description installer +Autorun installer, recovery console, and local demonstration UI. +Enable trident-installer.service explicitly in the installer ISO recipe. + +%files installer +%license LICENSE NOTICE +%{_bindir}/%{name}-installer +%{_unitdir}/%{name}-installer.service +%config(noreplace) /etc/%{name}/installer.toml + +%preun installer +%systemd_preun %{name}-installer.service + +%postun installer +%systemd_postun %{name}-installer.service + +# ------------------------------------------------------------------------------ + %if %{undefined rpm_ver} # Use cargo with source and vendor tarballs for distro build %prep @@ -279,7 +303,7 @@ export TRIDENT_VERSION="%{version}-%{release}" # Use %{trident_version} for Trident repo build export TRIDENT_VERSION="%{trident_version}" %endif -cargo build --release -p trident -p trident-acl-agent +cargo build --release --features trident/grpc-preview -p trident -p trident-acl-agent -p installer mkdir selinux cp -p packaging/selinux-policy-trident/trident.fc selinux/ @@ -310,7 +334,10 @@ cargo test --all --no-fail-fast -- --skip test_run_systemd_check --skip test_pre %install install -D -m 755 target/release/%{name} %{buildroot}/%{_bindir}/%{name} install -D -m 755 target/release/%{name}-acl-agent %{buildroot}/%{_bindir}/%{name}-acl-agent +install -D -m 755 target/release/%{name}-installer %{buildroot}/%{_bindir}/%{name}-installer install -D -m 644 packaging/systemd/%{name}-acl-agent.service %{buildroot}%{_unitdir}/%{name}-acl-agent.service +install -D -m 644 packaging/systemd/%{name}-installer.service %{buildroot}%{_unitdir}/%{name}-installer.service +install -D -m 644 packaging/installer.toml %{buildroot}/etc/%{name}/installer.toml # Copy Trident SELinux policy module to /usr/share/selinux/packages install -D -m 0644 %{name}.pp.bz2 %{buildroot}%{_datadir}/selinux/packages/%{selinuxtype}/%{name}.pp.bz2 diff --git a/packaging/systemd/trident-installer.service b/packaging/systemd/trident-installer.service new file mode 100644 index 0000000000..6a18bbc964 --- /dev/null +++ b/packaging/systemd/trident-installer.service @@ -0,0 +1,18 @@ +[Unit] +Description=Trident Installer +Wants=tridentd.socket systemd-udev-settle.service +After=tridentd.socket systemd-udev-settle.service network.target +Conflicts=trident-install.service + +[Service] +Type=simple +ExecStart=/usr/bin/trident-installer --system-console +StandardInput=null +StandardOutput=journal +StandardError=journal +TimeoutStartSec=infinity +Restart=no +SendSIGHUP=yes + +[Install] +WantedBy=multi-user.target From fbfa5eeb8f29683613948284c6bc32b07b6375db Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 1 Oct 2026 16:04:12 -0700 Subject: [PATCH 02/62] fix: Reload installer settings before recovery attempts Do not supply implicit settings after a configuration failure. Keep URL-entry controls accurate and installer method visibility scoped to the crate. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 3 ++ crates/installer/src/config.rs | 8 ++-- crates/installer/src/main.rs | 15 ++---- crates/installer/src/source.rs | 2 +- crates/installer/src/ui.rs | 84 ++++++++++++++++++++++++---------- 5 files changed, 72 insertions(+), 40 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 15dc3405dd..50778413b0 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -79,6 +79,9 @@ and failures show scrollable error details. Continue opens recovery: Shell, Stream COSI URL, remote HC URL, or Shutdown. Shell `exit` returns to the originating screen. Installation continues while a shell is open; automatic reboot waits. The success screen offers Reboot and Shell. +Before each recovery attempt the baked configuration is reloaded, so shell +edits take effect. Invalid or missing configuration never supplies implicit +defaults for an installation. Display filtering never removes daemon log records. All received responses are also appended to the private `/var/log/trident-installer.log`. `V` toggles diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index 5f9bcc5e6e..4cf49c2c87 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -80,7 +80,7 @@ fn default_reboot() -> bool { } impl Config { - pub fn read(path: impl AsRef) -> Result { + pub(super) fn read(path: impl AsRef) -> Result { let path = path.as_ref(); let text = fs::read_to_string(path).with_context(|| { format!( @@ -91,7 +91,7 @@ impl Config { Self::parse(&text) } - pub fn parse(text: &str) -> Result { + pub(super) fn parse(text: &str) -> Result { let config: Self = toml::from_str(text).context("Invalid installer TOML configuration")?; config.validate()?; Ok(config) @@ -127,7 +127,7 @@ impl Config { Ok(()) } - pub fn require_autorun(&self) -> Result<(), Error> { + pub(super) fn require_autorun(&self) -> Result<(), Error> { ensure!( self.mode == Mode::Autorun, "Interactive installation is not implemented yet; choose mode = \"autorun\"" @@ -135,7 +135,7 @@ impl Config { Ok(()) } - pub fn overlay(&self, root: impl AsRef) -> Result { + pub(super) fn overlay(&self, root: impl AsRef) -> Result { let path = root.as_ref().join(MEDIA_CONFIG); let text = match fs::read_to_string(&path) { Ok(text) => text, diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index 23efa0f256..ae9b8f944f 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -12,7 +12,7 @@ use tokio::runtime::Builder; use osutils::{systemd, terminal}; -use crate::{config::Config, demo::Scenario, ui::Output}; +use crate::{config::DEFAULT_CONFIG, demo::Scenario, ui::Output}; mod client; mod config; @@ -31,7 +31,7 @@ const WORKER_THREADS: usize = 2; about = "Autorun OS installer for Trident installer media" )] struct Args { - #[arg(long, default_value = config::DEFAULT_CONFIG)] + #[arg(long, default_value = DEFAULT_CONFIG)] config: PathBuf, #[arg(long, num_args = 0..=1, default_missing_value = "success", value_enum)] demo: Option, @@ -102,13 +102,6 @@ async fn run(args: Args) -> Result<(), Error> { } } } - let (settings, initial_error) = if args.demo.is_some() { - (Config::default(), None) - } else { - match Config::read(&args.config) { - Ok(settings) => (settings, None), - Err(error) => (Config::default(), Some(format!("{error:#}"))), - } - }; - ui::run(settings, initial_error, args.demo, args.plain, output).await + let config_path = args.demo.is_none().then_some(args.config); + ui::run(config_path, args.demo, args.plain, output).await } diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 2f350cab81..97a3a8327d 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -49,7 +49,7 @@ pub(super) enum Plan { } impl Plan { - pub fn description(&self) -> String { + pub(super) fn description(&self) -> String { match self { Self::Install { source, .. } => format!("Install from {source}"), Self::Stream { image } => format!("StreamDisk from {image}"), diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 37a2b42c3b..fba2467e9b 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -2,6 +2,7 @@ use std::{ collections::VecDeque, fs::{File, OpenOptions}, io::{self, IsTerminal, Write}, + path::PathBuf, time::{Duration, Instant}, }; @@ -458,25 +459,39 @@ impl Drop for Display { } fn spawn_operation( - settings: &Config, + config_path: Option, request: Request, force: bool, uncertain: bool, scenario: Option, tx: mpsc::Sender, ) -> JoinHandle> { - let settings = settings.clone(); task::spawn(async move { match scenario { Some(scenario) => demo::run(if force { Scenario::Success } else { scenario }, tx).await, - None => client::execute(settings, request, force, uncertain, tx).await, + None => { + let path = config_path + .context("Live installation requires an explicit configuration path")?; + match Config::read(path) { + Ok(settings) => client::execute(settings, request, force, uncertain, tx).await, + Err(error) => { + client::send( + &tx, + Event::Error { + details: format!("{error:#}"), + uncertain, + }, + ) + .await + } + } + } } }) } pub(super) async fn run( - settings: Config, - initial_error: Option, + config_path: Option, scenario: Option, plain: bool, mut output: Output, @@ -485,19 +500,14 @@ pub(super) async fn run( let mut interrupts = unix::signal(SignalKind::interrupt())?; let mut termination = unix::signal(SignalKind::terminate())?; let mut model = Model::new(); - let mut worker = if let Some(details) = initial_error { - model.fail(details, false); - None - } else { - Some(spawn_operation( - &settings, - Request::Autorun, - false, - false, - scenario, - tx.clone(), - )) - }; + let mut worker = Some(spawn_operation( + config_path.clone(), + Request::Autorun, + false, + false, + scenario, + tx.clone(), + )); let interactive = io::stdin().is_terminal() && io::stdout().is_terminal(); let mut display = if interactive && !plain { Some(Display::new()?) @@ -618,7 +628,7 @@ pub(super) async fn run( model.selected = 0; let recovery_demo = scenario.map(|_| Scenario::Success); worker = Some(spawn_operation( - &settings, + config_path.clone(), request, force, model.uncertain, @@ -766,12 +776,16 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { ) }) .unwrap_or_else(|| format!("Elapsed {}s", model.started.elapsed().as_secs())); - frame.render_widget( - Paragraph::new(format!( - "Up/Down + Enter: select S: shell V: details Esc: menu{}\n{countdown}", + let controls = if matches!(model.screen, Screen::StreamUrl | Screen::HostConfigUrl) { + "Enter: start Esc: return to recovery".to_owned() + } else { + format!( + "Up/Down + Enter: select S: shell V: details Esc: menu{}", if demo { " Q: quit" } else { "" } - )) - .style(Style::default().fg(Color::Cyan)), + ) + }; + frame.render_widget( + Paragraph::new(format!("{controls}\n{countdown}")).style(Style::default().fg(Color::Cyan)), areas[4], ); } @@ -780,12 +794,34 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { mod tests { use super::*; + use std::fs; + use ratatui::backend::TestBackend; + use tempfile::TempDir; fn enter() -> KeyEvent { KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) } + #[tokio::test] + async fn recovery_reloads_configuration_without_using_defaults_on_error() { + let root = TempDir::new().unwrap(); + let path = root.path().join("installer.toml"); + for (text, diagnostic) in [("", "mode"), ("mode = 'not-a-mode'", "not-a-mode")] { + fs::write(&path, text).unwrap(); + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let worker = + spawn_operation(Some(path.clone()), Request::Autorun, false, false, None, tx); + let Event::Error { details, uncertain } = rx.recv().await.unwrap() else { + panic!("Invalid settings must fail before preparing a source"); + }; + assert!(!uncertain); + assert!(details.contains(diagnostic), "{details}"); + worker.await.unwrap().unwrap(); + rx.try_recv().unwrap_err(); + } + } + #[test] fn error_continue_and_shell_keep_the_originating_screen() { let mut model = Model::new(); From 104a488a2b4577b2712228ef74573a456aa70cb7 Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 1 Oct 2026 16:37:08 -0700 Subject: [PATCH 03/62] fix: Fully simulate demo actions and format installer errors Replace the demo's real shell with an inert command screen. Keep reboot and shutdown mocked, render readable structured error details, and avoid duplicating headless errors. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 3 +- crates/installer/src/client.rs | 56 ++++++++++- crates/installer/src/demo.rs | 5 +- crates/installer/src/ui.rs | 179 +++++++++++++++++++++++++++++---- 4 files changed, 215 insertions(+), 28 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 50778413b0..a3447a5a49 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -14,7 +14,8 @@ cargo run -p installer -- --demo disconnect ``` The demo never mounts media, inspects disks, contacts Trident, or changes machine -power state. Press `Q` to quit. Shell opens as the invoking user. Use `--plain` +power state. Reboot, Shutdown and Shell are all simulated; no real shell or +command is launched. Press `Q` to quit. Use `--plain` for line-oriented output; without a terminal the demo prints its events and exits when complete. diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index 43866b6bb2..4afa588f66 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -13,7 +13,8 @@ use trident_proto::{ servicing_response::Response as ResponseBody, streaming_service_client::StreamingServiceClient, Completed, HostConfiguration, RebootHandling, RebootManagement, RebootStatus, ServicingKind, ServicingResponse, - StatusCode, StreamDiskRequest, + StatusCode, StreamDiskRequest, TridentError as RemoteError, + TridentErrorKind as ErrorCategory, }, v1preview::{ install_service_client::InstallServiceClient, status_service_client::StatusServiceClient, @@ -83,13 +84,40 @@ pub(super) fn completion(completed: Completed) -> Result { }) } StatusCode::Failure => Ok(Completion::Failure(match completed.error { - Some(error) => format!("{}\n\nReturned error details:\n{error:#?}", error.message), + Some(error) => format_error(&error), None => "Trident reported failure without error details".into(), })), StatusCode::Unspecified => bail!("Daemon returned an unspecified final status"), } } +fn format_error(error: &RemoteError) -> String { + let category = match ErrorCategory::try_from(error.kind) { + Ok(ErrorCategory::Unspecified) => "Unspecified", + Ok(ErrorCategory::ExecutionEnvironmentMisconfigurationError) => "Execution environment", + Ok(ErrorCategory::HealthChecksError) => "Health checks", + Ok(ErrorCategory::InitializationError) => "Initialization", + Ok(ErrorCategory::InternalError) => "Internal error", + Ok(ErrorCategory::InvalidInputError) => "Invalid input", + Ok(ErrorCategory::ServicingError) => "Servicing", + Ok(ErrorCategory::UnsupportedConfigurationError) => "Unsupported configuration", + Err(_) => "Unknown", + }; + let mut details = format!( + "{}\n\nCategory: {category} ({})\nCode: {}", + error.message.trim(), + error.kind, + error.subkind + ); + if !error.error_message.is_empty() && !error.message.contains(&error.error_message) { + details.push_str(&format!("\nCause: {}", error.error_message)); + } + if let Some(location) = &error.location { + details.push_str(&format!("\nLocation: {}:{}", location.path, location.line)); + } + details +} + pub(super) async fn execute( settings: Config, request: Request, @@ -303,7 +331,7 @@ mod tests { use trident_proto::{ v1::{ streaming_service_server::{StreamingService, StreamingServiceServer}, - Log, LogLevel, Started, + FileLocation, Log, LogLevel, Started, }, v1preview::install_service_server::{InstallService, InstallServiceServer}, }; @@ -352,6 +380,28 @@ mod tests { ); } + #[test] + fn errors_are_readable_without_debug_dumps() { + let error = RemoteError { + kind: ErrorCategory::ServicingError.into(), + subkind: "download-image".into(), + message: "Installation failed\n Failed to download the image".into(), + error_message: "Connection timed out".into(), + location: Some(FileLocation { + path: "source.rs".into(), + line: 42, + }), + }; + let formatted = format_error(&error); + assert!(formatted.contains("Installation failed\n Failed to download the image")); + assert!(formatted.contains("Category: Servicing")); + assert!(formatted.contains("Code: download-image")); + assert!(formatted.contains("Cause: Connection timed out")); + assert!(formatted.contains("Location: source.rs:42")); + assert!(!formatted.contains("TridentError {")); + assert!(!formatted.contains("\\n")); + } + #[derive(Clone)] struct Fake { complete: bool, diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs index ed2991601f..b7b7cc32c0 100644 --- a/crates/installer/src/demo.rs +++ b/crates/installer/src/demo.rs @@ -6,7 +6,7 @@ use tokio::{sync::mpsc::Sender, time}; use trident_proto::v1::{ servicing_response::Response as ResponseBody, Completed, Log, LogLevel, RebootStatus, - ServicingKind, ServicingResponse, Started, StatusCode, TridentError, + ServicingKind, ServicingResponse, Started, StatusCode, TridentError, TridentErrorKind, }; use crate::client::{self, Event}; @@ -34,7 +34,7 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err .await?; match scenario { Scenario::NoImages => return client::send(&tx, Event::Error { - details: "DEMO: No Host Configuration and no COSI images in cosi/.\nOpen Shell to configure networking, then select a remote source.".into(), + details: "DEMO: No Host Configuration and no COSI images in cosi/.\nTry the simulated shell or a remote-source recovery action.\nNo network requests or machine operations will be performed.".into(), uncertain: false, }).await, Scenario::AlreadyPresent => return client::send(&tx, Event::AlreadyPresent( @@ -79,6 +79,7 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err } .into(), error: failed.then(|| TridentError { + kind: TridentErrorKind::ServicingError.into(), message: "DEMO: Installation failed\nFailed to stream the OS image\nConnection timed out" .into(), diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index fba2467e9b..7beb3edc92 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -45,6 +45,8 @@ const REBOOT_DELAY: Duration = Duration::from_secs(5); const EVENT_CAPACITY: usize = 64; const LOG_CAPACITY: usize = 500; const DISPLAY_LOG_CHARACTERS: usize = 4096; +const DEMO_SHELL_HISTORY: usize = 8; +const DEMO_SHELL_NOTICE: &str = "This shell is simulated. Commands are never executed.\nNo network, disk or power operations are performed.\nType exit or press Esc to return to the installer."; const ASCII_BORDER: BorderSet = BorderSet { top_left: "+", top_right: "+", @@ -387,6 +389,56 @@ enum Action { Quit, } +#[derive(Default)] +struct DemoShell { + command: String, + responses: VecDeque, +} + +impl DemoShell { + fn key(&mut self, key: KeyEvent) -> bool { + if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + return false; + } + if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') { + self.command.clear(); + return false; + } + match key.code { + KeyCode::Esc => return true, + KeyCode::Backspace => { + self.command.pop(); + } + KeyCode::Char(c) if !c.is_control() => self.command.push(c), + KeyCode::Enter => { + if self.command.trim() == "exit" { + return true; + } + if self.responses.len() == DEMO_SHELL_HISTORY { + self.responses.pop_front(); + } + self.responses + .push_back("DEMO: Command not executed".into()); + self.command.clear(); + } + _ => {} + } + false + } + + fn text(&self) -> String { + format!( + "{DEMO_SHELL_NOTICE}\n\n{}\n\ndemo$ {}", + self.responses + .iter() + .cloned() + .collect::>() + .join("\n"), + display_text(&self.command) + ) + } +} + pub(super) struct Output { pub log: Option, pub mirrors: Vec, @@ -515,6 +567,7 @@ pub(super) async fn run( None }; let mut shell: Option>> = None; + let mut demo_shell: Option = None; let mut printed_screen = None; loop { for _ in 0..EVENT_CAPACITY { @@ -522,7 +575,11 @@ pub(super) async fn run( output.record(&event)?; if let Some(message) = model.event(event) { output.announce(&message)?; - if display.is_none() && shell.is_none() { + if display.is_none() + && shell.is_none() + && demo_shell.is_none() + && (interactive || !model.failed) + { println!("{}", display_text(&message)); } } @@ -582,30 +639,46 @@ pub(super) async fn run( } } else { if let Some(display) = &mut display { - display - .terminal - .draw(|frame| render(frame, &model, scenario.is_some()))?; - } else if printed_screen != Some(model.screen) { - println!( - "\n{}\n{}", - title(model.screen), - display_text(&model.details) - ); - for (index, choice) in model.choices().iter().enumerate() { - println!("{}: {choice}", index + 1); - } - if matches!(model.screen, Screen::StreamUrl | Screen::HostConfigUrl) { - println!("Enter an http:// or https:// URL, then press Enter"); + display.terminal.draw(|frame| match &demo_shell { + Some(shell) => render_demo_shell(frame, shell), + None => render(frame, &model, scenario.is_some()), + })?; + } else if printed_screen != Some((model.screen, demo_shell.is_some())) { + if let Some(shell) = &demo_shell { + println!("\nSIMULATED SHELL\n{}", shell.text()); + } else { + println!( + "\n{}\n{}", + title(model.screen), + display_text(&model.details) + ); + for (index, choice) in model.choices().iter().enumerate() { + println!("{}: {choice}", index + 1); + } + if matches!(model.screen, Screen::StreamUrl | Screen::HostConfigUrl) { + println!("Enter an http:// or https:// URL, then press Enter"); + } + println!("Number + Enter: select; S + Enter: shell; Esc: menu; V: details"); } - println!("Number + Enter: select; S + Enter: shell; Esc: menu; V: details"); - printed_screen = Some(model.screen); + printed_screen = Some((model.screen, demo_shell.is_some())); } if event::poll(Duration::ZERO)? { let event = event::read()?; if let TerminalEvent::Key(key) = event { - if let Some(action) = model.key(key, scenario.is_some()) { + if let Some(shell) = &mut demo_shell { + if shell.key(key) { + demo_shell = None; + printed_screen = None; + } else if key.code == KeyCode::Enter { + printed_screen = None; + } + } else if let Some(action) = model.key(key, scenario.is_some()) { match action { Action::Quit => return Ok(()), + Action::Shell if scenario.is_some() => { + demo_shell = Some(DemoShell::default()); + printed_screen = None; + } Action::Shell => { if let Some(display) = &mut display { display.suspend()?; @@ -666,8 +739,12 @@ pub(super) async fn run( if signal.is_none() { return Err(anyhow!("Installer interrupt handler closed")); } - model.screen = if worker.is_some() { Screen::Menu } else { Screen::Recovery }; - model.selected = 0; + if let Some(shell) = &mut demo_shell { + shell.command.clear(); + } else { + model.screen = if worker.is_some() { Screen::Menu } else { Screen::Recovery }; + model.selected = 0; + } } _ = termination.recv() => return Err(anyhow!("Installer stopped; servicing may continue in Trident")), } @@ -703,6 +780,24 @@ fn block(title: &str) -> Block<'_> { .title(title) } +fn render_demo_shell(frame: &mut Frame, shell: &DemoShell) { + let areas = Layout::vertical([Constraint::Length(2), Constraint::Min(4)]).split(frame.area()); + frame.render_widget( + Paragraph::new(" TRIDENT INSTALLER / DEMO / SIMULATED SHELL").style( + Style::default() + .fg(Color::Cyan) + .add_modifier(Modifier::BOLD), + ), + areas[0], + ); + frame.render_widget( + Paragraph::new(shell.text()) + .block(block("No commands are executed")) + .wrap(Wrap { trim: false }), + areas[1], + ); +} + fn render(frame: &mut Frame, model: &Model, demo: bool) { let areas = Layout::vertical([ Constraint::Length(2), @@ -720,7 +815,11 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { frame.render_widget( Paragraph::new(header).style( Style::default() - .fg(Color::Cyan) + .fg(if model.screen == Screen::Error { + Color::LightRed + } else { + Color::Cyan + }) .add_modifier(Modifier::BOLD), ), areas[0], @@ -744,7 +843,9 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { Screen::Force => Paragraph::new("Force reinstall will erase the disk selected by Trident.\n\nExisting partitions and files will be lost.\nCancel is selected by default.") .block(block("Destructive action")).wrap(Wrap { trim: false }), _ => Paragraph::new(display_text(&model.details)) - .block(block(if model.screen == Screen::Error { "Full error details / PgUp, PgDn to scroll" } else { "Result" })) + .block(if model.screen == Screen::Error { + block("Error details / PgUp, PgDn to scroll").border_style(Style::default().fg(Color::LightRed)) + } else { block("Result") }) .wrap(Wrap { trim: false }).scroll((model.scroll, 0)), }; frame.render_widget(content, areas[2]); @@ -803,6 +904,40 @@ mod tests { KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) } + #[test] + fn demo_shell_never_executes_commands_and_exit_returns() { + let root = TempDir::new().unwrap(); + let marker = root.path().join("must-not-exist"); + let mut shell = DemoShell::default(); + for c in format!("touch {}", marker.display()).chars() { + assert!(!shell.key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE))); + } + assert!(!shell.key(enter())); + assert!(!marker.exists()); + assert!(shell.text().contains("DEMO: Command not executed")); + for c in "exit".chars() { + assert!(!shell.key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE))); + } + assert!(shell.key(enter())); + } + + #[test] + fn demo_shell_is_visibly_simulated() { + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal + .draw(|frame| render_demo_shell(frame, &DemoShell::default())) + .unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains("SIMULATED SHELL"), "{text}"); + assert!(text.contains("Commands are never executed"), "{text}"); + } + #[tokio::test] async fn recovery_reloads_configuration_without_using_defaults_on_error() { let root = TempDir::new().unwrap(); From 240b03ab56c75712417554eb8afdce6a58fb7d39 Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 2 Oct 2026 12:13:51 -0700 Subject: [PATCH 04/62] feature: Add cyan Trident wordmark and installer branding Use the supplied ASCII wordmark and Trident Linux Installer display name. Keep controls visible at 80x24 and fall back to a compact header for smaller terminals. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 6 +- crates/installer/src/main.rs | 3 +- crates/installer/src/ui.rs | 164 ++++++++++++++++---- packaging/rpm/trident.spec | 2 +- packaging/systemd/trident-installer.service | 2 +- 5 files changed, 144 insertions(+), 33 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index a3447a5a49..e5cfc20f1c 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -1,4 +1,4 @@ -# Trident installer +# Trident Linux Installer `installer` builds the `trident-installer` binary and optional RPM. It is an autorun wrapper around Trident's gRPC API, not a partitioning engine. @@ -70,6 +70,10 @@ placeholders or automatically choose a disk for an HC. ## Console and recovery +The UI uses the cyan Trident ASCII wordmark and the name Trident Linux +Installer. At 80x24 and larger the complete wordmark is shown; smaller terminals +use a compact text header to preserve usable controls and error details. + On the ISO, `--system-console` prefers the graphical virtual console for local keyboard/monitor and BMC KVM, independent of `console=` order. An active serial console is the fallback. The chosen getty is stopped; other active consoles diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index ae9b8f944f..b9a56d757e 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -23,12 +23,13 @@ mod ui; const LOG_PATH: &str = "/var/log/trident-installer.log"; const PRIVATE_FILE_MODE: u32 = 0o600; const WORKER_THREADS: usize = 2; +const APPLICATION_NAME: &str = "Trident Linux Installer"; #[derive(Debug, Parser)] #[command( name = "trident-installer", version, - about = "Autorun OS installer for Trident installer media" + about = APPLICATION_NAME )] struct Args { #[arg(long, default_value = DEFAULT_CONFIG)] diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 7beb3edc92..bb84cf40bd 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -15,7 +15,7 @@ use crossterm::{ use log::error; use ratatui::{ backend::CrosstermBackend, - layout::{Constraint, Layout}, + layout::{Alignment, Constraint, Layout, Rect}, style::{Color, Modifier, Style}, symbols::border::Set as BorderSet, text::{Line, Span}, @@ -38,6 +38,7 @@ use crate::{ config::Config, demo::{self, Scenario}, source::{self, Request}, + APPLICATION_NAME, }; const FRAME_INTERVAL: Duration = Duration::from_millis(100); @@ -46,6 +47,19 @@ const EVENT_CAPACITY: usize = 64; const LOG_CAPACITY: usize = 500; const DISPLAY_LOG_CHARACTERS: usize = 4096; const DEMO_SHELL_HISTORY: usize = 8; +const WORDMARK_HEADER_HEIGHT: u16 = 10; +const COMPACT_HEADER_HEIGHT: u16 = 2; +const MIN_WORDMARK_TERMINAL_HEIGHT: u16 = 24; +const TRIDENT_WORDMARK: [&str; 8] = [ + r#"88888888888 d8b 888 888"#, + r#" 888 Y8P 888 888"#, + r#" 888 888 888"#, + r#" 888 888d888 888 .d88888 .d88b. 88888b. 888888"#, + r#" 888 888P" 888 d88" 888 d8P Y8b 888 "88b 888"#, + r#" 888 888 888 888 888 88888888 888 888 888"#, + r#" 888 888 888 Y88b 888 Y8b. 888 888 Y88b."#, + r#" 888 888 888 "Y88888 "Y8888 888 888 "Y888"#, +]; const DEMO_SHELL_NOTICE: &str = "This shell is simulated. Commands are never executed.\nNo network, disk or power operations are performed.\nType exit or press Esc to return to the installer."; const ASCII_BORDER: BorderSet = BorderSet { top_left: "+", @@ -780,16 +794,54 @@ fn block(title: &str) -> Block<'_> { .title(title) } +fn wordmark_width() -> usize { + TRIDENT_WORDMARK + .iter() + .fold(0, |width, row| width.max(row.len())) +} + +fn header_height(viewport: Rect) -> u16 { + if usize::from(viewport.width) >= wordmark_width() + && viewport.height >= MIN_WORDMARK_TERMINAL_HEIGHT + { + WORDMARK_HEADER_HEIGHT + } else { + COMPACT_HEADER_HEIGHT + } +} + +fn render_header(frame: &mut Frame, area: Rect, state: &str, demo: bool, state_color: Color) { + let brand = Style::default() + .fg(Color::Cyan) + .add_modifier(Modifier::BOLD); + let mut lines = Vec::new(); + if area.height >= WORDMARK_HEADER_HEIGHT { + let width = wordmark_width(); + lines.extend( + TRIDENT_WORDMARK + .iter() + .map(|row| Line::from(Span::styled(format!("{row:(); + assert_eq!(&row[padding..padding + expected.len()], *expected); + for column in padding..padding + expected.len() { + assert_eq!(buffer.content[row_index * 80 + column].fg, Color::Cyan); + } + } + let text = buffer + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains(APPLICATION_NAME), "{text}"); + assert!(text.contains("ALL ACTIONS SIMULATED"), "{text}"); + } + + #[test] + fn small_terminals_use_a_readable_compact_header() { + assert_eq!( + header_height(Rect::new(0, 0, 40, 24)), + COMPACT_HEADER_HEIGHT + ); + assert_eq!( + header_height(Rect::new(0, 0, 80, 16)), + COMPACT_HEADER_HEIGHT + ); + let mut model = Model::new(); + model.screen = Screen::Success; + let mut terminal = Terminal::new(TestBackend::new(40, 16)).unwrap(); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains(APPLICATION_NAME), "{text}"); + assert!(text.contains("Reboot"), "{text}"); + assert!(text.contains("Shell"), "{text}"); + } + #[tokio::test] async fn recovery_reloads_configuration_without_using_defaults_on_error() { let root = TempDir::new().unwrap(); diff --git a/packaging/rpm/trident.spec b/packaging/rpm/trident.spec index 46acb8cbdc..df3d4e4113 100644 --- a/packaging/rpm/trident.spec +++ b/packaging/rpm/trident.spec @@ -252,7 +252,7 @@ The Trident ACL components required to orchestrate servicing of ACL images. # ------------------------------------------------------------------------------ %package installer -Summary: Trident installer console for installer media +Summary: Trident Linux Installer console for installer media Requires: %{name} = %{version}-%{release} Requires: bash Conflicts: %{name}-install-service diff --git a/packaging/systemd/trident-installer.service b/packaging/systemd/trident-installer.service index 6a18bbc964..8d9a1fd3c4 100644 --- a/packaging/systemd/trident-installer.service +++ b/packaging/systemd/trident-installer.service @@ -1,5 +1,5 @@ [Unit] -Description=Trident Installer +Description=Trident Linux Installer Wants=tridentd.socket systemd-udev-settle.service After=tridentd.socket systemd-udev-settle.service network.target Conflicts=trident-install.service From 070e33d042c2c0692f32e4fcd96a8d57f549ba83 Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 2 Oct 2026 13:12:01 -0700 Subject: [PATCH 05/62] feature: Add live log verbosity and severity colours Keep all diagnostic records while selecting display verbosity live. Add coloured RPC logs and blank lines around the cyan wordmark without hiding console actions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 6 +- crates/installer/src/ui.rs | 245 +++++++++++++++++++++++++++++++++---- 2 files changed, 225 insertions(+), 26 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index e5cfc20f1c..adaf9d99d1 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -89,8 +89,10 @@ edits take effect. Invalid or missing configuration never supplies implicit defaults for an installation. Display filtering never removes daemon log records. All received responses are -also appended to the private `/var/log/trident-installer.log`. `V` toggles -detailed logs; PgUp/PgDn scroll. A missing final Completed response means unknown +also appended to the private `/var/log/trident-installer.log`. Logs are coloured +by severity. `V` opens a live verbosity picker (Off, Error, Warn, Info, Debug, +Trace); this only filters the display, not stored diagnostics. PgUp/PgDn scroll. +A missing final Completed response means unknown outcome, not success. Another write requires a successful Trident status query establishing that the daemon is idle. diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index bb84cf40bd..68238e1a4f 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -12,14 +12,14 @@ use crossterm::{ execute, terminal::{self, EnterAlternateScreen, LeaveAlternateScreen}, }; -use log::error; +use log::{error, Level, LevelFilter}; use ratatui::{ backend::CrosstermBackend, layout::{Alignment, Constraint, Layout, Rect}, style::{Color, Modifier, Style}, symbols::border::Set as BorderSet, text::{Line, Span}, - widgets::{Block, Borders, List, ListItem, Paragraph, Wrap}, + widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Wrap}, Frame, Terminal, }; use tokio::{ @@ -47,7 +47,15 @@ const EVENT_CAPACITY: usize = 64; const LOG_CAPACITY: usize = 500; const DISPLAY_LOG_CHARACTERS: usize = 4096; const DEMO_SHELL_HISTORY: usize = 8; -const WORDMARK_HEADER_HEIGHT: u16 = 10; +const WORDMARK_HEADER_HEIGHT: u16 = 12; +const VERBOSITY_LEVELS: [LevelFilter; 6] = [ + LevelFilter::Off, + LevelFilter::Error, + LevelFilter::Warn, + LevelFilter::Info, + LevelFilter::Debug, + LevelFilter::Trace, +]; const COMPACT_HEADER_HEIGHT: u16 = 2; const MIN_WORDMARK_TERMINAL_HEIGHT: u16 = 24; const TRIDENT_WORDMARK: [&str; 8] = [ @@ -92,7 +100,9 @@ struct Model { activity: String, details: String, logs: VecDeque<(LogLevel, String)>, - verbose: bool, + verbosity: LevelFilter, + verbosity_open: bool, + verbosity_selected: usize, selected: usize, scroll: u16, url: String, @@ -112,7 +122,9 @@ impl Model { activity: "Preparing installer".into(), details: String::new(), logs: VecDeque::new(), - verbose: false, + verbosity: LevelFilter::Info, + verbosity_open: false, + verbosity_selected: 3, selected: 0, scroll: 0, url: String::new(), @@ -182,14 +194,7 @@ impl Model { self.logs.pop_front(); } self.logs.push_back((level, message.clone())); - if matches!( - level, - LogLevel::Error | LogLevel::Warn | LogLevel::Info | LogLevel::Unspecified - ) { - Some(format!("{level}: {message}")) - } else { - None - } + Some(format!("{level}: {message}")) } Some(ResponseBody::Completed(completed)) => match client::completion(completed) { Ok(Completion::Success { @@ -251,6 +256,34 @@ impl Model { if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { return None; } + if self.verbosity_open { + match key.code { + KeyCode::Esc => self.verbosity_open = false, + KeyCode::Up | KeyCode::BackTab => { + self.verbosity_selected = (self.verbosity_selected + VERBOSITY_LEVELS.len() + - 1) + % VERBOSITY_LEVELS.len(); + } + KeyCode::Down | KeyCode::Tab => { + self.verbosity_selected = + (self.verbosity_selected + 1) % VERBOSITY_LEVELS.len(); + } + KeyCode::Char(digit) if digit.is_ascii_digit() => { + if let Some(index) = digit.to_digit(10).and_then(|value| value.checked_sub(1)) { + if (index as usize) < VERBOSITY_LEVELS.len() { + self.verbosity_selected = index as usize; + } + } + } + KeyCode::Enter => { + self.verbosity = VERBOSITY_LEVELS[self.verbosity_selected]; + self.verbosity_open = false; + self.scroll = 0; + } + _ => {} + } + return None; + } if matches!(self.screen, Screen::StreamUrl | Screen::HostConfigUrl) { return match key.code { KeyCode::Esc => { @@ -302,8 +335,12 @@ impl Model { } KeyCode::Char('q') if demo => return Some(Action::Quit), KeyCode::Char('s') => return Some(Action::Shell), - KeyCode::Char('v') => { - self.verbose = !self.verbose; + KeyCode::Char('v' | 'V') => { + self.verbosity_selected = VERBOSITY_LEVELS + .iter() + .position(|level| *level == self.verbosity) + .expect("invariant: all log levels are present in the verbosity picker"); + self.verbosity_open = true; return None; } KeyCode::PageUp => { @@ -587,12 +624,24 @@ pub(super) async fn run( for _ in 0..EVENT_CAPACITY { let Ok(event) = rx.try_recv() else { break }; output.record(&event)?; + let level = match &event { + Event::Response(response) => match &response.response { + Some(ResponseBody::Log(log)) => Some(log.level()), + _ => None, + }, + _ => None, + }; + let visible = level.is_none_or(|level| log_visible(level, model.verbosity)); + let mirrored = level.is_none_or(|level| log_visible(level, LevelFilter::Info)); if let Some(message) = model.event(event) { - output.announce(&message)?; + if mirrored { + output.announce(&message)?; + } if display.is_none() && shell.is_none() && demo_shell.is_none() && (interactive || !model.failed) + && visible { println!("{}", display_text(&message)); } @@ -657,9 +706,17 @@ pub(super) async fn run( Some(shell) => render_demo_shell(frame, shell), None => render(frame, &model, scenario.is_some()), })?; - } else if printed_screen != Some((model.screen, demo_shell.is_some())) { + } else if printed_screen + != Some((model.screen, demo_shell.is_some(), model.verbosity_open)) + { if let Some(shell) = &demo_shell { println!("\nSIMULATED SHELL\n{}", shell.text()); + } else if model.verbosity_open { + println!("\nLOG VERBOSITY (display only)"); + for (index, level) in VERBOSITY_LEVELS.iter().enumerate() { + println!("{}: {level}", index + 1); + } + println!("Number + Enter: apply; Esc: cancel"); } else { println!( "\n{}\n{}", @@ -672,9 +729,9 @@ pub(super) async fn run( if matches!(model.screen, Screen::StreamUrl | Screen::HostConfigUrl) { println!("Enter an http:// or https:// URL, then press Enter"); } - println!("Number + Enter: select; S + Enter: shell; Esc: menu; V: details"); + println!("Number + Enter: select; S + Enter: shell; Esc: menu; V: verbosity"); } - printed_screen = Some((model.screen, demo_shell.is_some())); + printed_screen = Some((model.screen, demo_shell.is_some(), model.verbosity_open)); } if event::poll(Duration::ZERO)? { let event = event::read()?; @@ -772,6 +829,27 @@ fn display_text(text: &str) -> String { .replace('\t', " ") } +fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { + let level = match level { + LogLevel::Error => Level::Error, + LogLevel::Warn | LogLevel::Unspecified => Level::Warn, + LogLevel::Info => Level::Info, + LogLevel::Debug => Level::Debug, + LogLevel::Trace => Level::Trace, + }; + level.to_level_filter() <= verbosity +} + +fn log_color(level: LogLevel) -> Color { + match level { + LogLevel::Error => Color::LightRed, + LogLevel::Warn | LogLevel::Unspecified => Color::Yellow, + LogLevel::Info => Color::Green, + LogLevel::Debug => Color::Cyan, + LogLevel::Trace => Color::DarkGray, + } +} + fn title(screen: Screen) -> &'static str { match screen { Screen::Progress => "INSTALLING", @@ -816,12 +894,14 @@ fn render_header(frame: &mut Frame, area: Rect, state: &str, demo: bool, state_c .add_modifier(Modifier::BOLD); let mut lines = Vec::new(); if area.height >= WORDMARK_HEADER_HEIGHT { + lines.push(Line::default()); let width = wordmark_width(); lines.extend( TRIDENT_WORDMARK .iter() .map(|row| Line::from(Span::styled(format!("{row: 0, + Screen::AlreadyPresent => 1, + _ => 2, + }), + Constraint::Min(3), Constraint::Length(action_height), Constraint::Length(2), ]) @@ -885,11 +969,14 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { let content = match model.screen { Screen::Progress | Screen::Menu => { let mut lines = vec![Line::from(Span::styled(model.activity.clone(), Style::default().fg(Color::Cyan))), Line::default()]; - let logs = model.logs.iter().filter(|(level, _)| model.verbose || matches!(level, LogLevel::Error | LogLevel::Warn | LogLevel::Info | LogLevel::Unspecified)).collect::>(); + let logs = model.logs.iter().filter(|(level, _)| log_visible(*level, model.verbosity)).collect::>(); let visible = usize::from(areas[2].height.saturating_sub(4)); let end = logs.len().saturating_sub(usize::from(model.scroll).min(logs.len().saturating_sub(1))); let start = end.saturating_sub(visible); - lines.extend(logs[start..end].iter().map(|(level, message)| Line::from(format!("{level} {message}")))); + lines.extend(logs[start..end].iter().map(|(level, message)| Line::from(vec![ + Span::styled(format!("{:<5} ", level.to_string()), Style::default().fg(log_color(*level)).add_modifier(Modifier::BOLD)), + Span::styled(message.clone(), Style::default().fg(log_color(*level))), + ]))); Paragraph::new(lines).block(block("Activity / logs")).wrap(Wrap { trim: false }) } Screen::StreamUrl | Screen::HostConfigUrl => Paragraph::new(format!("Enter an http:// or https:// URL:\n\n{}\n\nEnter: start Esc: back", display_text(&model.url))) @@ -934,7 +1021,8 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { "Enter: start Esc: return to recovery".to_owned() } else { format!( - "Up/Down + Enter: select S: shell V: details Esc: menu{}", + "Tab/Arrows: choose Enter: select S: shell V: {} Esc: menu{}", + model.verbosity, if demo { " Q: quit" } else { "" } ) }; @@ -942,6 +1030,50 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { Paragraph::new(format!("{controls}\n{countdown}")).style(Style::default().fg(Color::Cyan)), areas[4], ); + if model.verbosity_open { + render_verbosity_picker(frame, model); + } +} + +fn render_verbosity_picker(frame: &mut Frame, model: &Model) { + let viewport = frame.area(); + let width = viewport.width.min(44); + let height = viewport.height.min(10); + let area = Rect::new( + viewport.x + (viewport.width - width) / 2, + viewport.y + (viewport.height - height) / 2, + width, + height, + ); + frame.render_widget(Clear, area); + let rows = VERBOSITY_LEVELS + .iter() + .enumerate() + .map(|(index, level)| { + Line::from(Span::styled( + format!( + "{} {level}", + if index == model.verbosity_selected { + ">" + } else { + " " + } + ), + if index == model.verbosity_selected { + Style::default() + .fg(Color::Cyan) + .add_modifier(Modifier::BOLD) + } else { + Style::default() + }, + )) + }) + .chain([Line::default(), Line::from("Enter: apply Esc: cancel")]) + .collect::>(); + frame.render_widget( + Paragraph::new(rows).block(block("Log verbosity / display only")), + area, + ); } #[cfg(test)] @@ -999,7 +1131,9 @@ mod tests { .unwrap(); let buffer = terminal.backend().buffer(); let padding = (80 - wordmark_width()) / 2; + assert!(buffer.content[..80].iter().all(|cell| cell.symbol() == " ")); for (row_index, expected) in TRIDENT_WORDMARK.iter().enumerate() { + let row_index = row_index + 1; let row = buffer.content[row_index * 80..(row_index + 1) * 80] .iter() .map(|cell| cell.symbol()) @@ -1008,6 +1142,9 @@ mod tests { for column in padding..padding + expected.len() { assert_eq!(buffer.content[row_index * 80 + column].fg, Color::Cyan); } + assert!(buffer.content[9 * 80..10 * 80] + .iter() + .all(|cell| cell.symbol() == " ")); } let text = buffer .content @@ -1018,6 +1155,66 @@ mod tests { assert!(text.contains("ALL ACTIONS SIMULATED"), "{text}"); } + #[test] + fn live_verbosity_picker_filters_without_discarding_logs() { + let mut model = Model::new(); + model + .logs + .push_back((LogLevel::Info, "ordinary event".into())); + model + .logs + .push_back((LogLevel::Trace, "diagnostic detail".into())); + assert!(!log_visible(LogLevel::Trace, model.verbosity)); + model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), true); + assert!(model.verbosity_open); + model.key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), true); + model.key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), true); + model.key(enter(), true); + assert_eq!(model.verbosity, LevelFilter::Trace); + assert!(!model.verbosity_open); + assert!(log_visible(LogLevel::Trace, model.verbosity)); + assert_eq!(model.logs.len(), 2); + model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), true); + model.key(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE), true); + model.key(enter(), true); + assert_eq!(model.verbosity, LevelFilter::Off); + assert!(!log_visible(LogLevel::Error, model.verbosity)); + assert_eq!(model.logs.len(), 2); + } + + #[test] + fn rpc_logs_are_coloured_by_severity() { + let mut model = Model::new(); + model.verbosity = LevelFilter::Trace; + let records = [ + (LogLevel::Error, "error-line", Color::LightRed), + (LogLevel::Warn, "warn-line", Color::Yellow), + (LogLevel::Info, "info-line", Color::Green), + (LogLevel::Debug, "debug-line", Color::Cyan), + (LogLevel::Trace, "trace-line", Color::DarkGray), + ]; + for (level, text, _) in records { + model.logs.push_back((level, text.into())); + } + let mut terminal = Terminal::new(TestBackend::new(80, 32)).unwrap(); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + let buffer = terminal.backend().buffer(); + for (_, message, expected) in records { + let row = buffer + .content + .chunks(80) + .find(|row| { + row.iter() + .map(|cell| cell.symbol()) + .collect::() + .contains(message) + }) + .unwrap(); + let text = row.iter().map(|cell| cell.symbol()).collect::(); + assert_eq!(row[text.find(message).unwrap()].fg, expected); + } + } + #[test] fn small_terminals_use_a_readable_compact_header() { assert_eq!( From 9b6b8fe7431bb8bf3b0a97645c54ef48a1b70646 Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 2 Oct 2026 14:13:00 -0700 Subject: [PATCH 06/62] test: Add Tailor installer ISO and disposable GTK VM launcher Build matching installer/base RPMs in Azure Linux, reuse a pre-existing non-ACL COSI, and package an autorun ISO. Provide a manual QEMU launcher using fresh storage and disk-first UEFI boot order. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- test/.gitignore | 7 + test/README.md | 50 +++++++ test/build.py | 217 ++++++++++++++++++++++++++++ test/installer/files/installer.toml | 8 + test/installer/image.yaml | 78 ++++++++++ test/run.py | 166 +++++++++++++++++++++ test/tailor.yaml | 8 + test/test_scripts.py | 40 +++++ 8 files changed, 574 insertions(+) create mode 100644 test/.gitignore create mode 100644 test/README.md create mode 100755 test/build.py create mode 100644 test/installer/files/installer.toml create mode 100644 test/installer/image.yaml create mode 100755 test/run.py create mode 100644 test/tailor.yaml create mode 100644 test/test_scripts.py diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000000..81ed2ece65 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,7 @@ +/artifacts/ +/.work/ +/.vm/ +/.tailor/ +/installer/.rendered/ +/installer/cosi/ +/installer/rpms/ diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000000..4be824f411 --- /dev/null +++ b/test/README.md @@ -0,0 +1,50 @@ +# Real installer ISO test + +Build an Azure Linux installer ISO containing the optional `trident-installer` +RPM from this branch and an existing **non-ACL** COSI. The COSI is reused, not +rebuilt. + +```console +python3 test/build.py --cosi /path/to/regular.cosi +python3 test/run.py --display gtk +``` + +The payload is copied into the ISO's `cosi/` folder. No HC is supplied, so +autorun chooses StreamDisk. `reboot = false` leaves the success screen visible; +use its Reboot button to boot the installed OS. + +The generated ISO is `test/artifacts/installer_amd64_iso.iso`. This local build +uses a pre-existing regular Azure Linux 3.0.20260909 COSI (175 MB compressed, +4 GiB original disk). The launcher creates an 8 GiB disposable disk. + +The launcher follows the ACL test runner's UEFI setup, but is Python. It creates +a new sparse qcow2 disk and private firmware variables for every run. No host +disk or shared host directory is exposed to the guest. Per-device `bootindex` +makes the hard disk first: a blank disk falls through to the ISO, and the +installed OS takes over after reboot. + +```console +python3 test/run.py --display gtk --keep +python3 test/run.py --dry-run +``` + +`--keep` retains the disk, firmware variables and serial log for inspection. +Otherwise that run's private `.vm/` directory is removed after QEMU exits. +GTK uses the graphical installer console; serial status is captured separately. + +Build prerequisites: Python 3.11+, Docker, Tailor and the cached +`azl3/trident-builder:latest` image. Trident binaries are built inside Azure +Linux rather than using workstation binaries with potentially incompatible +glibc. The normal repository RPM spec packages them. Tailor runs Image +Customizer 1.6 on an Azure Linux 3 minimal base. + +Runtime prerequisites: QEMU, `qemu-img`, and matching OVMF code/variables. +KVM is used when available; otherwise QEMU emulation is used. + +```console +python3 test/build.py --skip-rpms +python3 test/build.py --cosi /path/to/regular.cosi --prepare-only +``` + +Payloads, RPMs, ISO artifacts and VM disks are ignored by Git. This tests QEMU's +IDE CD-ROM path, not USB/BMC virtual-media detection. diff --git a/test/build.py b/test/build.py new file mode 100755 index 0000000000..4c42b82648 --- /dev/null +++ b/test/build.py @@ -0,0 +1,217 @@ +#!/usr/bin/env python3 +import argparse +import datetime +import json +from pathlib import Path +import shlex +import shutil +import subprocess +import tarfile +import tempfile +import tomllib + +HERE = Path(__file__).resolve().parent +REPO = HERE.parent +IMAGE = HERE / "installer" +WORK = HERE / ".work" +BUILDER = "azl3/trident-builder:latest" +GIB = 1024**3 + + +def run(command, **kwargs): + print("+", shlex.join(map(str, command)), flush=True) + subprocess.run(list(map(str, command)), check=True, **kwargs) + + +def metadata(path): + with tarfile.open(path, "r:") as archive: + member = archive.getmember("metadata.json") + if not member.isfile() or member.size > 16 * 1024**2: + raise ValueError("COSI metadata is not a bounded regular file") + info = json.load(archive.extractfile(member)) + release = info.get("osRelease", "") + if isinstance(release, dict): + release = json.dumps(release) + if "azurelinux" not in release.lower(): + raise ValueError("This fixture requires a regular Azure Linux COSI") + if any( + name in release.lower() + for name in ( + "azurelinux-container-host", + "azure container linux", + '"variantid": "acl"', + ) + ): + raise ValueError("ACL is deliberately excluded from this fixture") + if info.get("version") != "1.2" or not (info.get("disk") or {}).get("size"): + raise ValueError("The fixture needs a COSI v1.2 image with disk metadata") + if info.get("osArch") != "x86_64": + raise ValueError("The fixture requires an x86_64 payload") + return info + + +def stage_cosi(path): + path = path.resolve(strict=True) + info = metadata(path) + destination = IMAGE / "cosi/payload.cosi" + destination.parent.mkdir(parents=True, exist_ok=True) + if path != destination: + shutil.copy2(path, destination) + WORK.mkdir(exist_ok=True) + (WORK / "payload.json").write_text( + json.dumps( + { + "source": str(path), + "diskBytes": info["disk"]["size"], + "recommendedDiskGiB": max( + 8, (info["disk"]["size"] + GIB - 1) // GIB + 1 + ), + "osRelease": info["osRelease"], + }, + indent=2, + ) + + "\n" + ) + print(f"Payload: {path} ({info['disk']['size'] // GIB} GiB original disk)") + + +def build_rpms(): + run(["make", ".cargo/config", "OVERRIDE_RUST_FEED=true"], cwd=REPO) + version = tomllib.loads((REPO / "crates/trident/Cargo.toml").read_text())[ + "package" + ]["version"] + commit = subprocess.check_output( + ["git", "rev-parse", "--short", "HEAD"], cwd=REPO, text=True + ).strip() + release = f"dev.{datetime.date.today():%Y%m%d}99.v{commit}.azl3" + product_version = f"{version}-{release}" + run( + [ + "docker", + "run", + "--rm", + "-e", + f"TRIDENT_VERSION={product_version}", + "-v", + f"{REPO}:/work", + "-w", + "/work", + BUILDER, + "cargo", + "build", + "--locked", + "--release", + "--target-dir", + "target/azl3", + "--features", + "trident/dangerous-options,trident/grpc-preview", + "-p", + "trident", + "-p", + "trident-acl-agent", + "-p", + "installer", + ] + ) + tag = f"trident-installer-test-rpms:{commit}" + with tempfile.TemporaryDirectory(prefix="rpm-context-", dir=WORK) as temporary: + context = Path(temporary) + shutil.copytree(REPO / "packaging", context / "packaging") + for name in ["LICENSE", "NOTICE"]: + shutil.copy2(REPO / name, context / name) + (context / "bin").mkdir() + for name in ["trident", "trident-acl-agent", "trident-installer"]: + shutil.copy2(REPO / "target/azl3/release" / name, context / "bin" / name) + run( + [ + "docker", + "build", + "-t", + tag, + "--build-arg", + f"TRIDENT_VERSION={product_version}", + "--build-arg", + f"RPM_VER={version}", + "--build-arg", + f"RPM_REL={release}", + "-f", + str(context / "packaging/docker/Dockerfile.azl"), + str(context), + ] + ) + container = subprocess.check_output( + ["docker", "create", tag], text=True + ).strip() + try: + run( + [ + "docker", + "cp", + f"{container}:/work/trident-rpms.tar.gz", + str(context / "rpms.tar.gz"), + ] + ) + finally: + run(["docker", "rm", "-v", container]) + extracted = context / "extracted" + extracted.mkdir() + with tarfile.open(context / "rpms.tar.gz") as archive: + archive.extractall(extracted, filter="data") + destination = IMAGE / "rpms" + destination.mkdir(exist_ok=True) + for old in destination.glob("*.rpm"): + old.unlink() + for rpm in extracted.rglob("*.rpm"): + shutil.copy2(rpm, destination / rpm.name) + installer = list(destination.glob("trident-installer-*.rpm")) + if len(installer) != 1: + raise ValueError("RPM build did not produce exactly one installer package") + print(f"Installer RPM: {installer[0]}") + + +def main(): + parser = argparse.ArgumentParser( + description="Build a Tailor ISO with the current Trident Linux Installer" + ) + parser.add_argument("--cosi", type=Path, help="Existing non-ACL Azure Linux COSI") + parser.add_argument( + "--skip-rpms", + action="store_true", + help="Reuse RPMs already staged by this script", + ) + parser.add_argument( + "--prepare-only", + action="store_true", + help="Stage/validate inputs without building the ISO", + ) + args = parser.parse_args() + for tool in ["docker", "tailor", "make"]: + if not shutil.which(tool): + parser.error(f"{tool} is required") + payload = args.cosi or IMAGE / "cosi/payload.cosi" + if not payload.is_file(): + parser.error("Supply --cosi /path/to/an/existing/non-ACL.cosi") + stage_cosi(payload) + if args.prepare_only: + return + if not args.skip_rpms: + build_rpms() + if not list((IMAGE / "rpms").glob("trident-installer-*.rpm")): + parser.error("No installer RPM staged; run without --skip-rpms") + run(["tailor", "--manifest", str(HERE / "tailor.yaml"), "validate"], cwd=HERE) + run( + [ + "tailor", + "--manifest", + str(HERE / "tailor.yaml"), + "build", + "installer", + "--log-dir", + str(WORK / "logs"), + ], + cwd=HERE, + ) + + +if __name__ == "__main__": + main() diff --git a/test/installer/files/installer.toml b/test/installer/files/installer.toml new file mode 100644 index 0000000000..762a1bedcb --- /dev/null +++ b/test/installer/files/installer.toml @@ -0,0 +1,8 @@ +mode = "autorun" + +[media] +cdromLabel = "CDROM" +cosiDirectory = "cosi/" + +[autorun] +reboot = false diff --git a/test/installer/image.yaml b/test/installer/image.yaml new file mode 100644 index 0000000000..5125aa3bd6 --- /dev/null +++ b/test/installer/image.yaml @@ -0,0 +1,78 @@ +name: installer + +outputs: + - format: iso + +base: + azureLinux: + version: "3.0" + variant: minimal-os + +rpmSources: + - rpms + +extraDependencies: + - files + - cosi + - rpms + +config: + previewFeatures: + - input-image-oci + os: + hostname: trident-installer-test + bootloader: + resetType: hard-reset + selinux: + mode: disabled + kernelCommandLine: + extraCommandLine: + - console=tty0 + - console=ttyS0,115200n8 + packages: + install: + - trident-installer + - device-mapper + - squashfs-tools + - veritysetup + - curl + - less + - iproute + services: + enable: + - tridentd.socket + - trident-installer.service + additionalFiles: + - source: files/installer.toml + destination: /etc/trident/installer.toml + storage: + bootType: efi + disks: + - partitionTableType: gpt + maxSize: 4G + partitions: + - id: esp + type: esp + size: 8M + - id: rootfs + size: grow + filesystems: + - deviceId: esp + type: fat32 + mountPoint: + path: /boot/efi + options: umask=0077 + - deviceId: rootfs + type: ext4 + mountPoint: + path: / + iso: + initramfsType: full-os + additionalFiles: + - source: cosi/payload.cosi + destination: /cosi/payload.cosi + kernelCommandLine: + extraCommandLine: + - console=tty0 + - console=ttyS0,115200n8 + - loglevel=4 diff --git a/test/run.py b/test/run.py new file mode 100755 index 0000000000..3d5e98ff85 --- /dev/null +++ b/test/run.py @@ -0,0 +1,166 @@ +#!/usr/bin/env python3 +import argparse +import contextlib +import json +import os +from pathlib import Path +import shlex +import shutil +import subprocess +import tempfile + +HERE = Path(__file__).resolve().parent +CODE_CANDIDATES = [ + Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), + Path("/usr/share/OVMF/OVMF_CODE.fd"), + Path("/usr/share/edk2/ovmf/OVMF_CODE.fd"), + Path("/usr/share/qemu/edk2-x86_64-code.fd"), +] + + +def command(iso, scratch, code, memory, cpus, display): + acceleration = ( + ["-enable-kvm", "-cpu", "host"] if os.access("/dev/kvm", os.W_OK) else [] + ) + return [ + "qemu-system-x86_64", + *acceleration, + "-m", + str(memory), + "-smp", + str(cpus), + "-machine", + "q35", + "-drive", + f"if=pflash,format=raw,unit=0,readonly=on,file={code}", + "-drive", + f"if=pflash,format=raw,unit=1,file={scratch / 'vars.fd'}", + "-drive", + f"if=none,id=hd0,file={scratch / 'disk.qcow2'},format=qcow2", + "-device", + "virtio-blk-pci,drive=hd0,bootindex=1", + "-drive", + f"if=none,id=cd0,file={iso},media=cdrom,readonly=on", + "-device", + "ide-cd,bus=ide.0,drive=cd0,bootindex=2", + "-nic", + "user,model=virtio-net-pci", + "-display", + display, + "-serial", + f"file:{scratch / 'serial.log'}", + ] + + +def main(): + parser = argparse.ArgumentParser( + description="Boot the installer ISO in a disposable UEFI VM" + ) + parser.add_argument("--iso", type=Path) + parser.add_argument("--display", default="gtk", choices=["gtk", "sdl", "none"]) + parser.add_argument("--memory", type=int, default=6144, help="Guest RAM in MiB") + parser.add_argument("--cpus", type=int, default=2) + parser.add_argument( + "--disk-size", + type=int, + help="Fresh qcow2 size in GiB; derived from payload by default", + ) + parser.add_argument( + "--keep", + action="store_true", + help="Keep the disk, firmware variables and serial log", + ) + parser.add_argument( + "--dry-run", + action="store_true", + help="Print launch command without creating or booting a VM", + ) + args = parser.parse_args() + for tool in ["qemu-system-x86_64", "qemu-img"]: + if not shutil.which(tool): + parser.error(f"{tool} is required") + images = sorted((HERE / "artifacts").glob("installer*.iso")) + iso = args.iso or (images[0] if len(images) == 1 else None) + if iso is None or not iso.is_file(): + parser.error("Build the ISO first, or provide --iso PATH") + iso = iso.resolve() + if "," in str(iso): + parser.error("QEMU drive paths containing commas are not supported") + code = next((path for path in CODE_CANDIDATES if path.is_file()), None) + if code is None: + parser.error("OVMF firmware is required") + variables = code.with_name(code.name.replace("CODE", "VARS")) + if not variables.is_file(): + parser.error(f"Matching OVMF variables not found: {variables}") + metadata_path = HERE / ".work/payload.json" + recommended = ( + json.loads(metadata_path.read_text())["recommendedDiskGiB"] + if metadata_path.exists() + else 8 + ) + disk_size = recommended if args.disk_size is None else args.disk_size + if disk_size < recommended or args.memory < 1024 or args.cpus < 1: + parser.error(f"Use at least {recommended} GiB disk, 1024 MiB RAM, and one CPU") + if args.dry_run: + print( + shlex.join( + command( + iso, + HERE / ".vm/NEW-DISPOSABLE-VM", + code, + args.memory, + args.cpus, + args.display, + ) + ) + ) + return + state = HERE / ".vm" + state.mkdir(exist_ok=True) + scratch = Path(tempfile.mkdtemp(prefix="installer-", dir=state)) + try: + shutil.copy2(variables, scratch / "vars.fd") + os.chmod(scratch / "vars.fd", 0o600) + subprocess.run( + [ + "qemu-img", + "create", + "-f", + "qcow2", + str(scratch / "disk.qcow2"), + f"{disk_size}G", + ], + check=True, + ) + launch = command(iso, scratch, code, args.memory, args.cpus, args.display) + print( + f"ISO: {iso}\nDisposable disk: {scratch / 'disk.qcow2'}\nSerial log: {scratch / 'serial.log'}", + flush=True, + ) + print( + "Disk-first UEFI boot: the blank disk falls through to the ISO; after Reboot the installed OS wins.", + flush=True, + ) + process = subprocess.Popen(launch) + try: + status = process.wait() + finally: + if process.poll() is None: + process.terminate() + try: + process.wait(timeout=5) + except subprocess.TimeoutExpired: + process.kill() + process.wait() + if status: + raise subprocess.CalledProcessError(status, launch) + finally: + if args.keep: + print(f"Kept VM files: {scratch}") + else: + shutil.rmtree(scratch) + + +if __name__ == "__main__": + with contextlib.suppress(KeyboardInterrupt): + main() diff --git a/test/tailor.yaml b/test/tailor.yaml new file mode 100644 index 0000000000..36a874321c --- /dev/null +++ b/test/tailor.yaml @@ -0,0 +1,8 @@ +schemaVersion: 1 + +toolchains: + default: ic + entries: + - name: ic + container: mcr.microsoft.com/azurelinux/imagecustomizer + tag: "1.6" diff --git a/test/test_scripts.py b/test/test_scripts.py new file mode 100644 index 0000000000..db9d06344d --- /dev/null +++ b/test/test_scripts.py @@ -0,0 +1,40 @@ +import importlib.util +from pathlib import Path +import unittest + +HERE = Path(__file__).resolve().parent +SPEC = importlib.util.spec_from_file_location("installer_vm", HERE / "run.py") +VM = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(VM) + + +class VmCommandTests(unittest.TestCase): + def test_vm_exposes_only_new_disposable_storage(self): + scratch = Path("/tmp/installer-test-owned") + launch = VM.command( + Path("/tmp/installer-test.iso"), + scratch, + Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), + 6144, + 2, + "gtk", + ) + drives = [ + launch[index + 1] for index, value in enumerate(launch) if value == "-drive" + ] + self.assertEqual(len(drives), 4) + self.assertIn(f"file={scratch}/disk.qcow2", drives[2]) + self.assertIn("readonly=on", drives[0]) + self.assertIn("readonly=on", drives[3]) + self.assertNotIn("-virtfs", launch) + self.assertNotIn("-fsdev", launch) + self.assertIn("virtio-blk-pci,drive=hd0,bootindex=1", launch) + self.assertIn("ide-cd,bus=ide.0,drive=cd0,bootindex=2", launch) + self.assertIn("gtk", launch) + self.assertEqual( + launch[launch.index("-serial") + 1], f"file:{scratch}/serial.log" + ) + + +if __name__ == "__main__": + unittest.main() From b498ec053757d00726e27eea29d96bfa97a8a301 Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 2 Oct 2026 18:59:41 -0700 Subject: [PATCH 07/62] test: Add localhost VNC display for SSH VM testing Run headlessly with a loopback-only VNC listener and document the SSH tunnel. Keep GTK support and disposable storage unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- test/README.md | 21 +++++++++++++++++++++ test/run.py | 35 ++++++++++++++++++++++++++++++----- test/test_scripts.py | 15 +++++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/test/README.md b/test/README.md index 4be824f411..9c290c72c0 100644 --- a/test/README.md +++ b/test/README.md @@ -32,6 +32,27 @@ python3 test/run.py --dry-run Otherwise that run's private `.vm/` directory is removed after QEMU exits. GTK uses the graphical installer console; serial status is captured separately. +## Over SSH, including from a Wayland desktop + +On the VM host: + +```console +./test/run.py --vnc --keep +``` + +On your local desktop, open a second terminal: + +```console +ssh -N -L 5901:127.0.0.1:5901 USER@HOST +``` + +Connect a VNC client such as GNOME Connections or Remmina to +`vnc://127.0.0.1:5901`. QEMU binds VNC to loopback only; the SSH tunnel carries +the connection. No X11 forwarding or desktop environment changes are needed. +Use `--vnc 2` and tunnel TCP 5902 if display 1 is already occupied. + +## Build prerequisites + Build prerequisites: Python 3.11+, Docker, Tailor and the cached `azl3/trident-builder:latest` image. Trident binaries are built inside Azure Linux rather than using workstation binaries with potentially incompatible diff --git a/test/run.py b/test/run.py index 3d5e98ff85..78d799a4cc 100755 --- a/test/run.py +++ b/test/run.py @@ -18,11 +18,11 @@ ] -def command(iso, scratch, code, memory, cpus, display): +def command(iso, scratch, code, memory, cpus, display, vnc=None): acceleration = ( ["-enable-kvm", "-cpu", "host"] if os.access("/dev/kvm", os.W_OK) else [] ) - return [ + launch = [ "qemu-system-x86_64", *acceleration, "-m", @@ -46,10 +46,13 @@ def command(iso, scratch, code, memory, cpus, display): "-nic", "user,model=virtio-net-pci", "-display", - display, + "none" if vnc is not None else display, "-serial", f"file:{scratch / 'serial.log'}", ] + if vnc is not None: + launch.extend(["-vnc", f"127.0.0.1:{vnc}"]) + return launch def main(): @@ -57,7 +60,16 @@ def main(): description="Boot the installer ISO in a disposable UEFI VM" ) parser.add_argument("--iso", type=Path) - parser.add_argument("--display", default="gtk", choices=["gtk", "sdl", "none"]) + displays = parser.add_mutually_exclusive_group() + displays.add_argument("--display", default="gtk", choices=["gtk", "sdl", "none"]) + displays.add_argument( + "--vnc", + type=int, + nargs="?", + const=1, + metavar="DISPLAY", + help="Headless VNC on localhost; display 1 is TCP 5901 (default)", + ) parser.add_argument("--memory", type=int, default=6144, help="Guest RAM in MiB") parser.add_argument("--cpus", type=int, default=2) parser.add_argument( @@ -76,6 +88,8 @@ def main(): help="Print launch command without creating or booting a VM", ) args = parser.parse_args() + if args.vnc is not None and not 0 <= args.vnc <= 99: + parser.error("--vnc display must be between 0 and 99") for tool in ["qemu-system-x86_64", "qemu-img"]: if not shutil.which(tool): parser.error(f"{tool} is required") @@ -111,6 +125,7 @@ def main(): args.memory, args.cpus, args.display, + args.vnc, ) ) ) @@ -132,7 +147,9 @@ def main(): ], check=True, ) - launch = command(iso, scratch, code, args.memory, args.cpus, args.display) + launch = command( + iso, scratch, code, args.memory, args.cpus, args.display, args.vnc + ) print( f"ISO: {iso}\nDisposable disk: {scratch / 'disk.qcow2'}\nSerial log: {scratch / 'serial.log'}", flush=True, @@ -141,6 +158,14 @@ def main(): "Disk-first UEFI boot: the blank disk falls through to the ISO; after Reboot the installed OS wins.", flush=True, ) + if args.vnc is not None: + port = 5900 + args.vnc + print( + f"VNC: localhost:{port} only. On your local machine, tunnel with:\n" + f" ssh -N -L {port}:127.0.0.1:{port} USER@HOST\n" + f"Then open vnc://127.0.0.1:{port} in a VNC client.", + flush=True, + ) process = subprocess.Popen(launch) try: status = process.wait() diff --git a/test/test_scripts.py b/test/test_scripts.py index db9d06344d..3114beb97f 100644 --- a/test/test_scripts.py +++ b/test/test_scripts.py @@ -9,6 +9,20 @@ class VmCommandTests(unittest.TestCase): + def test_vnc_is_headless_and_bound_to_loopback(self): + launch = VM.command( + Path("/tmp/installer-test.iso"), + Path("/tmp/installer-test-owned"), + Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), + 6144, + 2, + "gtk", + vnc=1, + ) + self.assertEqual(launch[launch.index("-display") + 1], "none") + self.assertEqual(launch[launch.index("-vnc") + 1], "127.0.0.1:1") + self.assertNotIn("gtk", launch) + def test_vm_exposes_only_new_disposable_storage(self): scratch = Path("/tmp/installer-test-owned") launch = VM.command( @@ -31,6 +45,7 @@ def test_vm_exposes_only_new_disposable_storage(self): self.assertIn("virtio-blk-pci,drive=hd0,bootindex=1", launch) self.assertIn("ide-cd,bus=ide.0,drive=cd0,bootindex=2", launch) self.assertIn("gtk", launch) + self.assertNotIn("-vnc", launch) self.assertEqual( launch[launch.index("-serial") + 1], f"file:{scratch}/serial.log" ) From 48d533946fc2f4bff67086423a7f53ca0c5e9253 Mon Sep 17 00:00:00 2001 From: Paco Date: Sat, 3 Oct 2026 12:13:54 -0700 Subject: [PATCH 08/62] fix: Keep installer console and show source preparation Prevent getty and logind from reclaiming tty1, explicitly foreground the installer, and replace the broad blkid media probe with lsblk. Show each preparation stage on screen and serial. Start VNC guests paused until a viewer connects. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 3 + crates/installer/src/client.rs | 17 +++- crates/installer/src/source.rs | 34 ++++++-- crates/installer/src/ui.rs | 15 ++++ crates/osutils/src/blkid.rs | 4 +- crates/osutils/src/dependencies.rs | 19 ++--- crates/osutils/src/lsblk.rs | 71 +++++++++++++++- crates/osutils/src/terminal.rs | 19 ++++- test/README.md | 13 ++- test/build.py | 8 ++ test/installer/files/installer-logind.conf | 3 + test/installer/image.yaml | 4 + test/run.py | 97 ++++++++++++++++++++-- test/test_scripts.py | 52 ++++++++++++ 14 files changed, 326 insertions(+), 33 deletions(-) create mode 100644 test/installer/files/installer-logind.conf diff --git a/crates/installer/README.md b/crates/installer/README.md index adaf9d99d1..718cd58a39 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -78,6 +78,9 @@ On the ISO, `--system-console` prefers the graphical virtual console for local keyboard/monitor and BMC KVM, independent of `console=` order. An active serial console is the fallback. The chosen getty is stopped; other active consoles receive plain status once and identify the interactive console. +The ISO must also disable `getty@tty1.service` and suppress logind's automatic +virtual gettys (`NAutoVTs=0`, `ReserveVT=0`): stopping a getty before +`getty.target` starts does not prevent it from claiming tty1 later. No ordinary source picker or confirmation precedes autorun. Missing sources and failures show scrollable error details. Continue opens recovery: Shell, diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index 4afa588f66..cb57a291b7 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -33,6 +33,7 @@ const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); #[derive(Debug)] pub(super) enum Event { + Preparing(String), Prepared { description: String, reboot: bool, @@ -147,7 +148,15 @@ pub(super) async fn execute( }, ) .await?; - let prepared = task::spawn_blocking(move || source::prepare(&settings, &request)).await?; + let progress = tx.clone(); + let prepared = task::spawn_blocking(move || { + source::prepare_with_progress(&settings, &request, |stage| { + progress + .blocking_send(Event::Preparing(stage.to_owned())) + .map_err(|_| anyhow!("Installer event receiver closed")) + }) + }) + .await?; let (settings, plan) = match prepared { Ok(prepared) => prepared, Err(error) => { @@ -174,6 +183,11 @@ pub(super) async fn execute( ) .await?; if let Plan::Stream { image } = &plan { + send( + &tx, + Event::Preparing("Checking image metadata and available disks".into()), + ) + .await?; let image = image.clone(); match task::spawn_blocking(move || source::stream_preflight(&image, force)).await? { Ok(true) => return send(&tx, Event::AlreadyPresent( @@ -183,6 +197,7 @@ pub(super) async fn execute( Err(error) => return send(&tx, Event::Error { details: format!("{error:#}"), uncertain: false }).await, } } + send(&tx, Event::Preparing("Connecting to Trident daemon".into())).await?; let channel = match connect(TRIDENT_DEFAULT_SOCKET_URI).await { Ok(channel) => channel, Err(error) => { diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 97a3a8327d..4de595f966 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -13,7 +13,6 @@ use tar::Archive; use url::Url; use osutils::{ - blkid, filesystems::MountFileSystemType, findmnt::FindMnt, lsblk::{self, BlockDevice, BlockDeviceType}, @@ -57,25 +56,42 @@ impl Plan { } } -pub(super) fn prepare(settings: &Config, request: &Request) -> Result<(Config, Plan), Error> { - let root = media_root(settings)?; - prepare_from_media(settings, request, root.as_deref()) +pub(super) fn prepare_with_progress( + settings: &Config, + request: &Request, + mut progress: impl FnMut(&str) -> Result<(), Error>, +) -> Result<(Config, Plan), Error> { + let root = media_root(settings, &mut progress)?; + prepare_from_media_with_progress(settings, request, root.as_deref(), &mut progress) } +#[cfg(test)] fn prepare_from_media( settings: &Config, request: &Request, root: Option<&Path>, ) -> Result<(Config, Plan), Error> { + prepare_from_media_with_progress(settings, request, root, &mut |_| Ok(())) +} + +fn prepare_from_media_with_progress( + settings: &Config, + request: &Request, + root: Option<&Path>, + progress: &mut impl FnMut(&str) -> Result<(), Error>, +) -> Result<(Config, Plan), Error> { + progress("Reading installer configuration")?; let settings = match root { Some(root) => settings.overlay(root)?, None => settings.clone(), }; settings.require_autorun()?; + progress("Scanning media for COSI images")?; let images = match root { Some(root) => config::discover_images(root, &settings.media.cosi_directory)?, None => Vec::new(), }; + progress("Checking Host Configuration")?; let source = match request { Request::HostConfiguration(source) => { remote_url(source)?; @@ -126,8 +142,12 @@ fn prepare_from_media( Ok((settings, plan)) } -fn media_root(settings: &Config) -> Result, Error> { +fn media_root( + settings: &Config, + progress: &mut impl FnMut(&str) -> Result<(), Error>, +) -> Result, Error> { if let Some(path) = &settings.media.mount_path { + progress("Checking the configured media mount")?; ensure!( mountpoint::check_is_mountpoint(path)?, "media.mountPath is not a mount point: {}", @@ -140,7 +160,8 @@ fn media_root(settings: &Config) -> Result, Error> { .cdrom_label .as_deref() .unwrap_or(config::DEFAULT_MEDIA_LABEL); - let devices = blkid::devices_by_label(label)?; + progress("Looking for installer media by filesystem label")?; + let devices = lsblk::devices_by_label(label)?; let Some(device) = config::choose_media(&devices)? else { return Ok(None); }; @@ -161,6 +182,7 @@ fn media_root(settings: &Config) -> Result, Error> { "Existing installer mount does not match the requested read-only media" ); } else { + progress("Mounting installer media read-only")?; mount::mount( device, target, diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 68238e1a4f..dd18b4df22 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -149,6 +149,10 @@ impl Model { fn event(&mut self, event: Event) -> Option { match event { + Event::Preparing(activity) => { + self.activity = activity; + Some(self.activity.clone()) + } Event::Prepared { description, reboot, @@ -1182,6 +1186,17 @@ mod tests { assert_eq!(model.logs.len(), 2); } + #[test] + fn preparation_steps_replace_the_idle_status() { + let mut model = Model::new(); + let step = "Looking for installer media by filesystem label"; + assert_eq!( + model.event(Event::Preparing(step.into())), + Some(step.into()) + ); + assert_eq!(model.activity, step); + } + #[test] fn rpc_logs_are_coloured_by_severity() { let mut model = Model::new(); diff --git a/crates/osutils/src/blkid.rs b/crates/osutils/src/blkid.rs index 38a988cd6c..07153efc1b 100644 --- a/crates/osutils/src/blkid.rs +++ b/crates/osutils/src/blkid.rs @@ -1,6 +1,6 @@ -use std::path::{Path, PathBuf}; +use std::path::Path; -use anyhow::{ensure, Context, Error}; +use anyhow::{Context, Error}; use uuid::Uuid; use crate::dependencies::Dependency; diff --git a/crates/osutils/src/dependencies.rs b/crates/osutils/src/dependencies.rs index f96d87760d..55f9e0ebed 100644 --- a/crates/osutils/src/dependencies.rs +++ b/crates/osutils/src/dependencies.rs @@ -10,7 +10,7 @@ use std::{ use log::trace; use nix::{ - sys::signal::{self, SigSet, SigmaskHow, Signal}, + sys::signal::{self, Signal}, unistd::{self, Pid}, }; use strum_macros::IntoStaticStr; @@ -19,6 +19,8 @@ use trident_api::error::{ ExecutionEnvironmentMisconfigurationError, ServicingError, TridentError, TridentResultExt, }; +use crate::terminal; + #[derive(Debug, thiserror::Error)] pub enum DependencyError { #[error("Failed to find dependency '{dependency}': {source}")] @@ -249,7 +251,7 @@ impl Command { .process_group(0); let mut child = command.spawn()?; let child_group = Pid::from_raw(i32::try_from(child.id()).map_err(io::Error::other)?); - let foreground = Self::set_foreground_group(terminal, child_group); + let foreground = terminal::set_foreground_group(terminal, child_group); if let Err(error) = foreground { child.kill()?; child.wait()?; @@ -257,7 +259,7 @@ impl Command { } let continued = signal::killpg(child_group, Signal::SIGCONT); let status = child.wait(); - let restored = Self::set_foreground_group(terminal, parent_group); + let restored = terminal::set_foreground_group(terminal, parent_group); restored?; continued?; Ok(Output { @@ -282,17 +284,6 @@ impl Command { self } - fn set_foreground_group(terminal: &File, group: Pid) -> io::Result<()> { - let mut blocked = SigSet::empty(); - blocked.add(Signal::SIGTTOU); - let mut previous = SigSet::empty(); - signal::pthread_sigmask(SigmaskHow::SIG_BLOCK, Some(&blocked), Some(&mut previous))?; - let result = unistd::tcsetpgrp(terminal, group); - signal::pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&previous), None)?; - result?; - Ok(()) - } - pub fn with_arg>(mut self, arg: S) -> Self { self.arg(arg); self diff --git a/crates/osutils/src/lsblk.rs b/crates/osutils/src/lsblk.rs index 8b0beb23ef..ce6703c585 100644 --- a/crates/osutils/src/lsblk.rs +++ b/crates/osutils/src/lsblk.rs @@ -3,7 +3,7 @@ use std::{ path::{Path, PathBuf}, }; -use anyhow::{bail, Context, Error}; +use anyhow::{bail, ensure, Context, Error}; use serde::{Deserialize, Serialize}; use sysdefs::osuuid::OsUuid; @@ -24,6 +24,9 @@ pub struct BlockDevice { /// Device name pub name: String, + /// Filesystem label + pub label: Option, + /// Filesystem type pub fstype: Option, @@ -173,6 +176,26 @@ pub fn list() -> Result, Error> { Ok(parsed) } +/// Finds all devices with a filesystem label, including devices nested below disks. +pub fn devices_by_label(label: impl AsRef) -> Result, Error> { + let label = label.as_ref(); + ensure!(!label.is_empty(), "Filesystem label must not be empty"); + let mut devices = Vec::new(); + collect_devices_by_label(&list()?, label, &mut devices); + devices.sort(); + devices.dedup(); + Ok(devices) +} + +fn collect_devices_by_label(devices: &[BlockDevice], label: &str, matches: &mut Vec) { + for device in devices { + if device.label.as_deref() == Some(label) { + matches.push(device.device_path()); + } + collect_devices_by_label(&device.children, label, matches); + } +} + /// Finds and returns all block devices (and their children) that match a /// given predicate. /// @@ -307,6 +330,46 @@ where mod tests { use super::*; + #[test] + fn label_scan_includes_cdrom_and_detects_duplicate_filesystem_labels() { + let devices = vec![ + BlockDevice { + name: "sr0".into(), + label: Some("CDROM".into()), + blkdev_type: BlockDeviceType::Rom, + ..Default::default() + }, + BlockDevice { + name: "sda".into(), + children: vec![BlockDevice { + name: "sda1".into(), + label: Some("CDROM".into()), + ..Default::default() + }], + ..Default::default() + }, + ]; + let mut matches = Vec::new(); + collect_devices_by_label(&devices, "CDROM", &mut matches); + matches.sort(); + assert_eq!( + matches, + [PathBuf::from("/dev/sda1"), PathBuf::from("/dev/sr0")] + ); + assert!(devices_by_label("").is_err()); + } + + #[test] + fn parses_optical_drive_label_from_lsblk_json() { + let devices = parse_lsblk_output( + r#"{"blockdevices":[{"name":"sr0","size":355497984,"type":"rom","label":"CDROM"}]}"#, + ) + .unwrap(); + let mut matches = Vec::new(); + collect_devices_by_label(&devices, "CDROM", &mut matches); + assert_eq!(matches, [PathBuf::from("/dev/sr0")]); + } + /// Output obtained from running `lsblk --json --bytes --output-all --paths /dev/sda` /// on the functional test VM AzL 3.0, lsblk from util-linux 2.40.2 const SAMPLE_LSBLK_OUTPUT: &str = indoc::indoc! { @@ -868,6 +931,7 @@ mod tests { fn test_parse_lsblk_output() { let expected_block_device_list = vec![BlockDevice { name: "/dev/sda".into(), + label: None, fstype: None, fssize: None, fsuuid: None, @@ -881,6 +945,7 @@ mod tests { children: vec![ BlockDevice { name: "/dev/sda1".into(), + label: None, fstype: Some("vfat".into()), fssize: Some(ByteCount(52293632)), fsuuid: Some(OsUuid::Relaxed("6AA2-09D2".to_string())), @@ -898,6 +963,7 @@ mod tests { }, BlockDevice { name: "/dev/sda2".into(), + label: None, fstype: Some("ext4".into()), fssize: Some(ByteCount(4143841280)), fsuuid: Some("f3c65403-dafc-4dbf-873a-c17d6fb98a2f".into()), @@ -915,6 +981,7 @@ mod tests { }, BlockDevice { name: "/dev/sda3".into(), + label: None, fstype: None, fssize: None, fsuuid: None, @@ -932,6 +999,7 @@ mod tests { }, BlockDevice { name: "/dev/sda4".into(), + label: None, fstype: Some("swap".into()), fssize: None, fsuuid: Some("91180e8c-096a-43d8-88b2-c24a5b5eaee6".into()), @@ -949,6 +1017,7 @@ mod tests { }, BlockDevice { name: "/dev/sda5".into(), + label: None, fstype: Some("ext4".into()), fssize: Some(ByteCount(92500992)), fsuuid: Some("2253b2d0-62db-4ae9-8343-4023a361a9c3".into()), diff --git a/crates/osutils/src/terminal.rs b/crates/osutils/src/terminal.rs index 79f6df7271..80d7f42bf5 100644 --- a/crates/osutils/src/terminal.rs +++ b/crates/osutils/src/terminal.rs @@ -6,7 +6,11 @@ use std::{ }; use anyhow::{ensure, Context, Error}; -use nix::{libc, unistd}; +use nix::{ + libc, + sys::signal::{self, SigSet, SigmaskHow, Signal}, + unistd::{self, Pid}, +}; use crate::dependencies::Dependency; @@ -71,6 +75,8 @@ pub fn attach(path: impl AsRef) -> Result { if unsafe { libc::ioctl(terminal.as_raw_fd(), libc::TIOCSCTTY, 1) } < 0 { return Err(io::Error::last_os_error()).context("Failed to claim controlling terminal"); } + set_foreground_group(&terminal, unistd::getpgrp()) + .context("Failed to make installer the terminal's foreground process group")?; for target in [libc::STDIN_FILENO, libc::STDOUT_FILENO] { // dup2 redirects the inherited stdio descriptors without transferring ownership. if unsafe { libc::dup2(terminal.as_raw_fd(), target) } < 0 { @@ -80,6 +86,17 @@ pub fn attach(path: impl AsRef) -> Result { Ok(terminal) } +pub(crate) fn set_foreground_group(terminal: &File, group: Pid) -> io::Result<()> { + let mut blocked = SigSet::empty(); + blocked.add(Signal::SIGTTOU); + let mut previous = SigSet::empty(); + signal::pthread_sigmask(SigmaskHow::SIG_BLOCK, Some(&blocked), Some(&mut previous))?; + let result = unistd::tcsetpgrp(terminal, group); + signal::pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&previous), None)?; + result?; + Ok(()) +} + pub fn shell(terminal: &File) -> Result<(), Error> { Dependency::Bash .cmd() diff --git a/test/README.md b/test/README.md index 9c290c72c0..159734b015 100644 --- a/test/README.md +++ b/test/README.md @@ -12,6 +12,10 @@ python3 test/run.py --display gtk The payload is copied into the ISO's `cosi/` folder. No HC is supplied, so autorun chooses StreamDisk. `reboot = false` leaves the success screen visible; use its Reboot button to boot the installed OS. +The ISO disables `getty@tty1.service` and logind's automatic virtual gettys so +they cannot reclaim the installer's graphical keyboard after startup. The +installer mirrors source-discovery stages to the serial log; an idle-looking +progress screen can be diagnosed there without sending keys to the guest. The generated ISO is `test/artifacts/installer_amd64_iso.iso`. This local build uses a pre-existing regular Azure Linux 3.0.20260909 COSI (175 MB compressed, @@ -31,6 +35,10 @@ python3 test/run.py --dry-run `--keep` retains the disk, firmware variables and serial log for inspection. Otherwise that run's private `.vm/` directory is removed after QEMU exits. GTK uses the graphical installer console; serial status is captured separately. +Do not rebuild over an ISO still attached to a running VM. Use +`python3 test/build.py --output-dir test/.work/next-iso` and then pass +`--iso test/.work/next-iso/installer_amd64_iso.iso` to `run.py` to test a +replacement without changing the running guest's CD-ROM. ## Over SSH, including from a Wayland desktop @@ -48,7 +56,10 @@ ssh -N -L 5901:127.0.0.1:5901 USER@HOST Connect a VNC client such as GNOME Connections or Remmina to `vnc://127.0.0.1:5901`. QEMU binds VNC to loopback only; the SSH tunnel carries -the connection. No X11 forwarding or desktop environment changes are needed. +the connection. The guest stays paused until the viewer connects, so there is +no setup race. The screen is initially black while the guest is paused; boot +begins automatically when the viewer connects. No X11 forwarding or desktop +environment changes are needed. Use `--vnc 2` and tunnel TCP 5902 if display 1 is already occupied. ## Build prerequisites diff --git a/test/build.py b/test/build.py index 4c42b82648..9605a30113 100755 --- a/test/build.py +++ b/test/build.py @@ -184,6 +184,12 @@ def main(): action="store_true", help="Stage/validate inputs without building the ISO", ) + parser.add_argument( + "--output-dir", + type=Path, + default=HERE / "artifacts", + help="Build output directory (use a different path while an older ISO is attached to a VM)", + ) args = parser.parse_args() for tool in ["docker", "tailor", "make"]: if not shutil.which(tool): @@ -206,6 +212,8 @@ def main(): str(HERE / "tailor.yaml"), "build", "installer", + "--output-dir", + str(args.output_dir.resolve()), "--log-dir", str(WORK / "logs"), ], diff --git a/test/installer/files/installer-logind.conf b/test/installer/files/installer-logind.conf new file mode 100644 index 0000000000..6e1a22c65d --- /dev/null +++ b/test/installer/files/installer-logind.conf @@ -0,0 +1,3 @@ +[Login] +NAutoVTs=0 +ReserveVT=0 diff --git a/test/installer/image.yaml b/test/installer/image.yaml index 5125aa3bd6..77a892b947 100644 --- a/test/installer/image.yaml +++ b/test/installer/image.yaml @@ -42,9 +42,13 @@ config: enable: - tridentd.socket - trident-installer.service + disable: + - getty@tty1.service additionalFiles: - source: files/installer.toml destination: /etc/trident/installer.toml + - source: files/installer-logind.conf + destination: /etc/systemd/logind.conf.d/installer.conf storage: bootType: efi disks: diff --git a/test/run.py b/test/run.py index 78d799a4cc..f26b0e9522 100755 --- a/test/run.py +++ b/test/run.py @@ -4,12 +4,16 @@ import json import os from pathlib import Path +import select import shlex import shutil +import socket import subprocess import tempfile +import time HERE = Path(__file__).resolve().parent +QMP_STARTUP_TIMEOUT_SECONDS = 30 CODE_CANDIDATES = [ Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), Path("/usr/share/OVMF/OVMF_CODE.fd"), @@ -51,10 +55,82 @@ def command(iso, scratch, code, memory, cpus, display, vnc=None): f"file:{scratch / 'serial.log'}", ] if vnc is not None: - launch.extend(["-vnc", f"127.0.0.1:{vnc}"]) + launch.extend( + [ + "-S", + "-qmp", + f"unix:{scratch / 'monitor.sock'},server=on,wait=off", + "-vnc", + f"127.0.0.1:{vnc}", + ] + ) return launch +class Qmp: + def __init__(self, connection, process): + self.connection = connection + self.process = process + self.pending = b"" + self.viewer_connected = False + + def receive(self): + while b"\n" not in self.pending: + if self.process.poll() is not None: + raise RuntimeError("QEMU exited while waiting for a VNC viewer") + if not select.select([self.connection], [], [], 1)[0]: + continue + data = self.connection.recv(4096) + if not data: + raise RuntimeError("QEMU monitor disconnected") + self.pending += data + line, self.pending = self.pending.split(b"\n", 1) + message = json.loads(line) + if message.get("event") == "VNC_INITIALIZED": + self.viewer_connected = True + elif message.get("event") == "VNC_DISCONNECTED": + self.viewer_connected = False + return message + + def execute(self, command): + request = {"execute": command, "id": command} + self.connection.sendall(json.dumps(request).encode() + b"\n") + while True: + response = self.receive() + if response.get("id") == command: + if "error" in response: + raise RuntimeError(f"QEMU {command}: {response['error']}") + return response["return"] + + +def wait_for_vnc_viewer(process, monitor_path, on_ready): + deadline = time.monotonic() + QMP_STARTUP_TIMEOUT_SECONDS + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as connection: + while True: + if process.poll() is not None: + raise RuntimeError("QEMU exited before the VNC monitor was ready") + try: + connection.connect(str(monitor_path)) + break + except (FileNotFoundError, ConnectionRefusedError): + if time.monotonic() >= deadline: + raise RuntimeError("QEMU monitor did not become ready") + time.sleep(0.1) + qmp = Qmp(connection, process) + if "QMP" not in qmp.receive(): + raise RuntimeError("Invalid greeting from QEMU monitor") + qmp.execute("qmp_capabilities") + status = qmp.execute("query-vnc") + if not status.get("enabled"): + raise RuntimeError("QEMU VNC display is not enabled") + on_ready() + if not status.get("clients"): + while not qmp.viewer_connected: + qmp.receive() + print("VNC viewer connected. Starting the guest now.", flush=True) + qmp.execute("cont") + + def main(): parser = argparse.ArgumentParser( description="Boot the installer ISO in a disposable UEFI VM" @@ -160,14 +236,21 @@ def main(): ) if args.vnc is not None: port = 5900 + args.vnc - print( - f"VNC: localhost:{port} only. On your local machine, tunnel with:\n" - f" ssh -N -L {port}:127.0.0.1:{port} USER@HOST\n" - f"Then open vnc://127.0.0.1:{port} in a VNC client.", - flush=True, - ) process = subprocess.Popen(launch) try: + if args.vnc is not None: + + def show_vnc_instructions(): + print( + f"Guest paused; VNC on localhost:{port} only. Tunnel from your local machine:\n" + f" ssh -N -L {port}:127.0.0.1:{port} USER@HOST\n" + f"Connect to vnc://127.0.0.1:{port}; boot begins after the viewer connects.", + flush=True, + ) + + wait_for_vnc_viewer( + process, scratch / "monitor.sock", show_vnc_instructions + ) status = process.wait() finally: if process.poll() is None: diff --git a/test/test_scripts.py b/test/test_scripts.py index 3114beb97f..24fc7360e9 100644 --- a/test/test_scripts.py +++ b/test/test_scripts.py @@ -1,5 +1,9 @@ import importlib.util +import json from pathlib import Path +import socket +import tempfile +import threading import unittest HERE = Path(__file__).resolve().parent @@ -21,8 +25,56 @@ def test_vnc_is_headless_and_bound_to_loopback(self): ) self.assertEqual(launch[launch.index("-display") + 1], "none") self.assertEqual(launch[launch.index("-vnc") + 1], "127.0.0.1:1") + self.assertIn("-S", launch) + self.assertEqual( + launch[launch.index("-qmp") + 1], + "unix:/tmp/installer-test-owned/monitor.sock,server=on,wait=off", + ) self.assertNotIn("gtk", launch) + def test_vnc_connection_resumes_paused_guest_after_viewer_handshake(self): + class RunningProcess: + def poll(self): + return None + + with tempfile.TemporaryDirectory() as directory: + monitor = Path(directory) / "monitor.sock" + commands = [] + ready = [] + + def monitor_server(): + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listener: + listener.bind(str(monitor)) + listener.listen(1) + connection, _ = listener.accept() + with connection, connection.makefile("r") as reader: + connection.sendall( + b'{"QMP":{"version":{},"capabilities":[]}}\r\n' + ) + for expected in ["qmp_capabilities", "query-vnc", "cont"]: + request = json.loads(reader.readline()) + commands.append(request["execute"]) + if expected == "query-vnc": + connection.sendall( + b'{"return":{"enabled":true,"clients":[]},"id":"query-vnc"}\r\n' + ) + connection.sendall(b'{"event":"VNC_INITIALIZED"}\r\n') + else: + response = {"return": {}, "id": expected} + connection.sendall( + json.dumps(response).encode() + b"\r\n" + ) + + server = threading.Thread(target=monitor_server, daemon=True) + server.start() + VM.wait_for_vnc_viewer( + RunningProcess(), monitor, lambda: ready.append(True) + ) + server.join(timeout=3) + self.assertFalse(server.is_alive()) + self.assertEqual(commands, ["qmp_capabilities", "query-vnc", "cont"]) + self.assertEqual(ready, [True]) + def test_vm_exposes_only_new_disposable_storage(self): scratch = Path("/tmp/installer-test-owned") launch = VM.command( From c3c3e8c68d092c9540417400216bec32edc9bc64 Mon Sep 17 00:00:00 2001 From: Paco Date: Sat, 3 Oct 2026 12:34:22 -0700 Subject: [PATCH 09/62] fix: Keep installer running when a secondary console fails Mirror output is best-effort and warns before dropping an unwritable terminal instead of killing the interactive installer. Reproduce a tty write failure in a unit test and document the chosen test COSI's missing EFI loader. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 4 ++- crates/installer/src/main.rs | 11 ++++--- crates/installer/src/ui.rs | 56 +++++++++++++++++++++++++++++------- test/README.md | 10 +++++-- test/run.py | 2 +- 5 files changed, 62 insertions(+), 21 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 718cd58a39..552d96b905 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -77,7 +77,9 @@ use a compact text header to preserve usable controls and error details. On the ISO, `--system-console` prefers the graphical virtual console for local keyboard/monitor and BMC KVM, independent of `console=` order. An active serial console is the fallback. The chosen getty is stopped; other active consoles -receive plain status once and identify the interactive console. +receive plain status once and identify the interactive console. Failure to +open or write to a secondary console is logged and stops mirroring to that +console; it does not stop the installation. The ISO must also disable `getty@tty1.service` and suppress logind's automatic virtual gettys (`NAutoVTs=0`, `ReserveVT=0`): stopping a getty before `getty.target` starts does not prevent it from claiming tty1 later. diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index b9a56d757e..7898eab61e 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -7,7 +7,7 @@ use std::{ use anyhow::{Context, Error}; use clap::Parser; -use log::LevelFilter; +use log::{warn, LevelFilter}; use tokio::runtime::Builder; use osutils::{systemd, terminal}; @@ -95,11 +95,10 @@ async fn run(args: Args) -> Result<(), Error> { let _terminal = terminal::attach(primary)?; output.control = primary.display().to_string(); for path in consoles.iter().filter(|path| path.as_path() != primary) { - output - .mirrors - .push(OpenOptions::new().write(true).open(path).with_context(|| { - format!("Failed to open active console '{}'", path.display()) - })?); + match OpenOptions::new().write(true).open(path) { + Ok(terminal) => output.mirrors.push((path.clone(), terminal)), + Err(error) => warn!("Could not mirror status to '{}': {error}", path.display()), + } } } } diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index dd18b4df22..1fbbec889b 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -12,7 +12,7 @@ use crossterm::{ execute, terminal::{self, EnterAlternateScreen, LeaveAlternateScreen}, }; -use log::{error, Level, LevelFilter}; +use log::{error, warn, Level, LevelFilter}; use ratatui::{ backend::CrosstermBackend, layout::{Alignment, Constraint, Layout, Rect}, @@ -496,7 +496,7 @@ impl DemoShell { pub(super) struct Output { pub log: Option, - pub mirrors: Vec, + pub mirrors: Vec<(PathBuf, File)>, pub control: String, } @@ -509,17 +509,22 @@ impl Output { Ok(()) } - fn announce(&mut self, message: &str) -> Result<(), Error> { - for mirror in &mut self.mirrors { - writeln!( + fn announce(&mut self, message: &str) { + self.mirrors.retain_mut(|(path, mirror)| { + if let Err(error) = writeln!( mirror, "\r\n[installer; control {}] {}", self.control, display_text(message) - ) - .context("Failed to mirror installer output to an active console")?; - } - Ok(()) + ) { + warn!( + "Stopped mirroring installer status to '{}': {error}", + path.display() + ); + return false; + } + true + }); } } @@ -639,7 +644,7 @@ pub(super) async fn run( let mirrored = level.is_none_or(|level| log_visible(level, LevelFilter::Info)); if let Some(message) = model.event(event) { if mirrored { - output.announce(&message)?; + output.announce(&message); } if display.is_none() && shell.is_none() @@ -1089,6 +1094,37 @@ mod tests { use ratatui::backend::TestBackend; use tempfile::TempDir; + #[test] + fn unavailable_serial_mirror_does_not_stop_the_installer() { + let root = TempDir::new().unwrap(); + let transcript = root.path().join("serial.log"); + let mut output = Output { + log: None, + mirrors: vec![ + ( + PathBuf::from("/dev/full"), + OpenOptions::new().write(true).open("/dev/full").unwrap(), + ), + ( + transcript.clone(), + OpenOptions::new() + .create(true) + .truncate(true) + .write(true) + .open(&transcript) + .unwrap(), + ), + ], + control: "/dev/tty1".into(), + }; + output.announce("Preparing installer"); + assert_eq!(output.mirrors.len(), 1); + output.announce("Still running"); + assert!(fs::read_to_string(transcript) + .unwrap() + .contains("Still running")); + } + fn enter() -> KeyEvent { KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) } diff --git a/test/README.md b/test/README.md index 159734b015..d5638d5b6c 100644 --- a/test/README.md +++ b/test/README.md @@ -11,7 +11,7 @@ python3 test/run.py --display gtk The payload is copied into the ISO's `cosi/` folder. No HC is supplied, so autorun chooses StreamDisk. `reboot = false` leaves the success screen visible; -use its Reboot button to boot the installed OS. +use its Reboot button to check what the installed disk boots. The ISO disables `getty@tty1.service` and logind's automatic virtual gettys so they cannot reclaim the installer's graphical keyboard after startup. The installer mirrors source-discovery stages to the serial log; an idle-looking @@ -24,8 +24,12 @@ uses a pre-existing regular Azure Linux 3.0.20260909 COSI (175 MB compressed, The launcher follows the ACL test runner's UEFI setup, but is Python. It creates a new sparse qcow2 disk and private firmware variables for every run. No host disk or shared host directory is exposed to the guest. Per-device `bootindex` -makes the hard disk first: a blank disk falls through to the ISO, and the -installed OS takes over after reboot. +makes the hard disk first: a blank or non-bootable disk falls through to the ISO. +For this cached `regular.cosi`, the installer successfully streams the image, +but a subsequent UEFI boot reports a missing `\\EFI\\BOOT\\grubx64.efi` even +after ejecting the ISO. This fixture verifies installation, **not** a bootable +target OS; choose a known-bootable COSI to test both. If the ISO remains attached, +the repeat-install guard prevents a second stream. ```console python3 test/run.py --display gtk --keep diff --git a/test/run.py b/test/run.py index f26b0e9522..35e80ee48e 100755 --- a/test/run.py +++ b/test/run.py @@ -231,7 +231,7 @@ def main(): flush=True, ) print( - "Disk-first UEFI boot: the blank disk falls through to the ISO; after Reboot the installed OS wins.", + "Disk-first UEFI boot: a blank or non-bootable disk falls through to the ISO.", flush=True, ) if args.vnc is not None: From 6752bb6d01a3e4b0f04115b93b250109ad87a77f Mon Sep 17 00:00:00 2001 From: Paco Date: Sat, 3 Oct 2026 14:03:28 -0700 Subject: [PATCH 10/62] feature: Add dual-source installer logs and serial logs mode Format INST and TRIDENT records with elapsed time and severity colors, set DEBUG display default, and keep serial logging unfiltered. Reject the reserved serial UI mode and support headless autorun while suppressing serial getty on the test ISO. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 32 +- crates/installer/src/client.rs | 4 + crates/installer/src/config.rs | 28 ++ crates/installer/src/main.rs | 47 +- crates/installer/src/ui.rs | 453 +++++++++++++----- packaging/installer.toml | 1 + test/README.md | 4 + .../files/installer-serial-getty.conf | 2 + test/installer/files/installer.toml | 1 + test/installer/image.yaml | 9 +- 10 files changed, 437 insertions(+), 144 deletions(-) create mode 100644 test/installer/files/installer-serial-getty.conf diff --git a/crates/installer/README.md b/crates/installer/README.md index 552d96b905..fd33c68910 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -26,6 +26,7 @@ Read `/etc/trident/installer.toml` or the path supplied with `--config`. ```toml mode = "autorun" +serialMode = "logs" [media] cdromLabel = "TRIDENT_INSTALL" @@ -41,6 +42,8 @@ reboot = true Unknown fields and conflicting media selectors are errors. `interactive` is reserved for later work, not an alias for autorun. COSI directory paths must stay beneath the media root. +`serialMode` defaults to `logs`. `ui` is reserved and currently rejected with +an error rather than showing an unfinished serial TUI. Media is mounted read-only at `/run/trident/installer-media`. Duplicate labels are rejected. Optional `installer/installer.toml` on the mounted media can @@ -74,12 +77,17 @@ The UI uses the cyan Trident ASCII wordmark and the name Trident Linux Installer. At 80x24 and larger the complete wordmark is shown; smaller terminals use a compact text header to preserve usable controls and error details. -On the ISO, `--system-console` prefers the graphical virtual console for local -keyboard/monitor and BMC KVM, independent of `console=` order. An active serial -console is the fallback. The chosen getty is stopped; other active consoles -receive plain status once and identify the interactive console. Failure to -open or write to a secondary console is logged and stops mirroring to that -console; it does not stop the installation. +On the ISO, `--system-console` uses the graphical virtual console for local +keyboard/monitor and BMC KVM, independent of `console=` order. With +`serialMode = "logs"`, active serial consoles receive only formatted installer +and gRPC logs, never a TUI, even when no graphical console exists. Autorun +continues without an interactive console; missing inputs, repeat-install +protection and other cases needing operator input emit an error and stop. +Failures to open or write a serial console are logged and mirroring to that +console stops; they do not stop installation. Disable serial getty on a +logs-only installer ISO so its login prompt cannot mix with application logs. +Firmware, kernel and systemd output during boot is separate from the +installer's stream and may precede it; scrapers should match tagged log lines. The ISO must also disable `getty@tty1.service` and suppress logind's automatic virtual gettys (`NAutoVTs=0`, `ReserveVT=0`): stopping a getty before `getty.target` starts does not prevent it from claiming tty1 later. @@ -94,9 +102,15 @@ edits take effect. Invalid or missing configuration never supplies implicit defaults for an installation. Display filtering never removes daemon log records. All received responses are -also appended to the private `/var/log/trident-installer.log`. Logs are coloured -by severity. `V` opens a live verbosity picker (Off, Error, Warn, Info, Debug, -Trace); this only filters the display, not stored diagnostics. PgUp/PgDn scroll. +also appended to the private `/var/log/trident-installer.log`. Installer events +and daemon records use `MM:SS [INST:LEVEL] message` and +`MM:SS [TRIDENT:LEVEL] message`, respectively. The serial stream includes all +levels, even when the TUI is filtered, and escapes embedded newlines as `\n` +to keep one record per line. The TUI defaults to Debug: errors red, warnings +orange, info bright blue, debug purple, trace gray; installer source labels +are magenta and Trident labels green. `V` opens a live verbosity picker (Off, +Error, Warn, Info, Debug, Trace) beside the operation; this only filters the +display, not serial or stored diagnostics. PgUp/PgDn scroll. A missing final Completed response means unknown outcome, not success. Another write requires a successful Trident status query establishing that the daemon is idle. diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index cb57a291b7..a5db450517 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -33,6 +33,10 @@ const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); #[derive(Debug)] pub(super) enum Event { + InstallerLog { + level: log::Level, + message: String, + }, Preparing(String), Prepared { description: String, diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index 4cf49c2c87..d08232e0a2 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -19,6 +19,8 @@ pub(super) const DEFAULT_MEDIA_LABEL: &str = "TRIDENT_INSTALL"; pub(super) struct Config { pub mode: Mode, #[serde(default)] + pub serial_mode: SerialMode, + #[serde(default)] pub media: Media, #[serde(default)] pub autorun: Autorun, @@ -32,6 +34,14 @@ pub(super) enum Mode { Interactive, } +#[derive(Serialize, Deserialize, Debug, Default, Clone, Copy, PartialEq, Eq)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub(super) enum SerialMode { + #[default] + Logs, + Ui, +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub(super) struct Media { @@ -98,6 +108,10 @@ impl Config { } fn validate(&self) -> Result<(), Error> { + ensure!( + self.serial_mode == SerialMode::Logs, + "serialMode = \"ui\" is not implemented yet; use \"logs\"" + ); ensure!( self.media.cdrom_label.is_none() || self.media.mount_path.is_none(), "media.cdromLabel and media.mountPath are mutually exclusive" @@ -146,6 +160,10 @@ impl Config { }; let overlay: TomlValue = toml::from_str(&text) .with_context(|| format!("Invalid media configuration '{}'", path.display()))?; + ensure!( + overlay.get("serialMode").is_none(), + "Media configuration cannot change bootstrap serialMode" + ); if let Some(media) = overlay.get("media") { ensure!( media.get("cdromLabel").is_none() && media.get("mountPath").is_none(), @@ -234,6 +252,7 @@ mod tests { Config::parse("").unwrap_err(); Config::parse("mode = 'autorun'\nunknown = true").unwrap_err(); let config = Config::parse("mode = 'autorun'").unwrap(); + assert_eq!(config.serial_mode, SerialMode::Logs); assert!(config.autorun.reboot); assert_eq!(config.media.cosi_directory, Path::new("cosi")); Config::parse("mode = 'interactive'") @@ -242,6 +261,13 @@ mod tests { .unwrap_err(); } + #[test] + fn serial_ui_mode_is_reserved_not_silently_enabled() { + Config::parse("mode = 'autorun'\nserialMode = 'logs'").unwrap(); + let error = Config::parse("mode = 'autorun'\nserialMode = 'ui'").unwrap_err(); + assert!(error.to_string().contains("not implemented"), "{error:#}"); + } + #[test] fn packaged_configuration_matches_the_runtime_schema() { let config = Config::parse(include_str!("../../../packaging/installer.toml")).unwrap(); @@ -303,5 +329,7 @@ mod tests { assert_eq!(config.media.cosi_directory, Path::new("cosi")); fs::write(&path, "[media]\ncdromLabel = 'OTHER'").unwrap(); config.overlay(root.path()).unwrap_err(); + fs::write(&path, "serialMode = 'ui'").unwrap(); + config.overlay(root.path()).unwrap_err(); } } diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index 7898eab61e..6a39151039 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -1,18 +1,21 @@ use std::{ fs::{self, OpenOptions, Permissions}, + io::Write, os::unix::fs::{OpenOptionsExt, PermissionsExt}, path::PathBuf, process::ExitCode, + sync::OnceLock, }; use anyhow::{Context, Error}; use clap::Parser; use log::{warn, LevelFilter}; use tokio::runtime::Builder; +use tokio::sync::mpsc::UnboundedSender; use osutils::{systemd, terminal}; -use crate::{config::DEFAULT_CONFIG, demo::Scenario, ui::Output}; +use crate::{client::Event, config::DEFAULT_CONFIG, demo::Scenario, ui::Output}; mod client; mod config; @@ -24,6 +27,7 @@ const LOG_PATH: &str = "/var/log/trident-installer.log"; const PRIVATE_FILE_MODE: u32 = 0o600; const WORKER_THREADS: usize = 2; const APPLICATION_NAME: &str = "Trident Linux Installer"; +static INSTALLER_LOG_TX: OnceLock> = OnceLock::new(); #[derive(Debug, Parser)] #[command( @@ -45,6 +49,26 @@ struct Args { fn main() -> ExitCode { env_logger::Builder::from_default_env() .filter_level(LevelFilter::Info) + .filter_module("installer", LevelFilter::Trace) + .filter_module("osutils", LevelFilter::Trace) + .format(|buf, record| { + let message = record.args().to_string(); + if let Some(tx) = INSTALLER_LOG_TX.get() { + if tx + .send(Event::InstallerLog { + level: record.level(), + message: message.clone(), + }) + .is_err() + { + writeln!( + buf, + "Installer log receiver unavailable; entry retained in journal" + )?; + } + } + writeln!(buf, "[{} {}] {}", record.level(), record.target(), message) + }) .init(); let args = Args::parse(); let result = Builder::new_multi_thread() @@ -66,7 +90,6 @@ async fn run(args: Args) -> Result<(), Error> { let mut output = Output { log: None, mirrors: Vec::new(), - control: "current terminal".into(), }; if args.demo.is_none() { output.log = Some( @@ -80,21 +103,11 @@ async fn run(args: Args) -> Result<(), Error> { fs::set_permissions(LOG_PATH, Permissions::from_mode(PRIVATE_FILE_MODE))?; if args.system_console { let consoles = terminal::active_consoles()?; - let primary = terminal::preferred_console(&consoles)?; - let name = primary - .file_name() - .context("Console has no device name")? - .to_string_lossy(); - let unit = - if name.starts_with("tty") && name.chars().skip(3).all(|c| c.is_ascii_digit()) { - format!("getty@{name}.service") - } else { - format!("serial-getty@{name}.service") - }; - systemd::stop_unit(&unit)?; - let _terminal = terminal::attach(primary)?; - output.control = primary.display().to_string(); - for path in consoles.iter().filter(|path| path.as_path() != primary) { + if let Some(graphical) = consoles.iter().find(|path| path.ends_with("tty1")) { + systemd::stop_unit("getty@tty1.service")?; + let _terminal = terminal::attach(graphical)?; + } + for path in consoles.iter().filter(|path| !path.ends_with("tty1")) { match OpenOptions::new().write(true).open(path) { Ok(terminal) => output.mirrors.push((path.clone(), terminal)), Err(error) => warn!("Could not mirror status to '{}': {error}", path.display()), diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 1fbbec889b..e275907822 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -38,7 +38,7 @@ use crate::{ config::Config, demo::{self, Scenario}, source::{self, Request}, - APPLICATION_NAME, + APPLICATION_NAME, INSTALLER_LOG_TX, }; const FRAME_INTERVAL: Duration = Duration::from_millis(100); @@ -93,13 +93,75 @@ enum Screen { Menu, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum LogSource { + Inst, + Trident, +} + +impl LogSource { + fn label(self) -> &'static str { + match self { + Self::Inst => "INST", + Self::Trident => "TRIDENT", + } + } + + fn color(self) -> Color { + match self { + Self::Inst => Color::LightMagenta, + Self::Trident => Color::LightGreen, + } + } +} + +#[derive(Debug, Clone)] +struct LogEntry { + elapsed: Duration, + source: LogSource, + level: LogLevel, + message: String, +} + +impl LogEntry { + fn prefix(&self) -> String { + let seconds = self.elapsed.as_secs(); + format!("{:02}:{:02} [", seconds / 60, seconds % 60) + } + + fn plain(&self) -> String { + let message = display_text(&self.message).replace('\n', "\\n"); + format!( + "{}{}:{}] {message}", + self.prefix(), + self.source.label(), + self.level + ) + } + + fn styled(&self) -> Line<'_> { + let level = Style::default().fg(log_color(self.level)); + Line::from(vec![ + Span::raw(self.prefix()), + Span::styled( + self.source.label(), + Style::default().fg(self.source.color()), + ), + Span::raw(":"), + Span::styled(self.level.to_string(), level.add_modifier(Modifier::BOLD)), + Span::raw("] "), + Span::styled(self.message.as_str(), level), + ]) + } +} + #[derive(Debug)] struct Model { screen: Screen, source: String, activity: String, details: String, - logs: VecDeque<(LogLevel, String)>, + logs: VecDeque, verbosity: LevelFilter, verbosity_open: bool, verbosity_selected: usize, @@ -122,9 +184,9 @@ impl Model { activity: "Preparing installer".into(), details: String::new(), logs: VecDeque::new(), - verbosity: LevelFilter::Info, + verbosity: LevelFilter::Debug, verbosity_open: false, - verbosity_selected: 3, + verbosity_selected: 4, selected: 0, scroll: 0, url: String::new(), @@ -147,58 +209,81 @@ impl Model { self.screen = Screen::Error; } - fn event(&mut self, event: Event) -> Option { + fn log(&mut self, source: LogSource, level: LogLevel, message: String) -> LogEntry { + let entry = LogEntry { + elapsed: self.started.elapsed(), + source, + level, + message: display_text(&message), + }; + let mut characters = entry.message.chars(); + let mut displayed = characters + .by_ref() + .take(DISPLAY_LOG_CHARACTERS) + .collect::() + .replace('\n', "\\n"); + if characters.next().is_some() { + displayed.push_str(" [Display shortened; full record preserved in diagnostic log]"); + } + if self.logs.len() == LOG_CAPACITY { + self.logs.pop_front(); + } + self.logs.push_back(LogEntry { + message: displayed, + ..entry.clone() + }); + entry + } + + fn event(&mut self, event: Event) -> LogEntry { match event { + Event::InstallerLog { level, message } => self.log( + LogSource::Inst, + match level { + Level::Error => LogLevel::Error, + Level::Warn => LogLevel::Warn, + Level::Info => LogLevel::Info, + Level::Debug => LogLevel::Debug, + Level::Trace => LogLevel::Trace, + }, + message, + ), Event::Preparing(activity) => { - self.activity = activity; - Some(self.activity.clone()) + self.activity = activity.clone(); + self.log(LogSource::Inst, LogLevel::Info, activity) } Event::Prepared { description, reboot, stream_image, } => { - self.source = description; + self.source = description.clone(); self.reboot = reboot; self.stream_image = stream_image; - Some(self.source.clone()) + self.log(LogSource::Inst, LogLevel::Info, description) } Event::AlreadyPresent(details) => { - self.details = details; + self.details = details.clone(); self.reboot_at = None; self.screen = Screen::AlreadyPresent; self.selected = 0; - Some(self.details.clone()) + self.log(LogSource::Inst, LogLevel::Warn, details) } Event::Error { details, uncertain } => { - self.fail(details, uncertain); - Some(self.details.clone()) + self.fail(details.clone(), uncertain); + self.log(LogSource::Inst, LogLevel::Error, details) } Event::Response(response) => match response.response { Some(ResponseBody::Started(_)) => { self.activity = "Servicing started".into(); - Some(self.activity.clone()) + self.log(LogSource::Inst, LogLevel::Info, self.activity.clone()) } Some(ResponseBody::Log(log)) => { let level = log.level(); - let mut characters = log.message.chars(); - let mut message = display_text( - &characters - .by_ref() - .take(DISPLAY_LOG_CHARACTERS) - .collect::(), - ); - if characters.next().is_some() { - message.push_str("\n[Display shortened; the full record is preserved in the diagnostic log]"); - } if level == LogLevel::Info { - self.activity = message.clone(); - } - if self.logs.len() == LOG_CAPACITY { - self.logs.pop_front(); + self.activity = display_text(&log.message); } - self.logs.push_back((level, message.clone())); - Some(format!("{level}: {message}")) + self.log(LogSource::Trident, level, log.message) } Some(ResponseBody::Completed(completed)) => match client::completion(completed) { Ok(Completion::Success { @@ -216,24 +301,23 @@ impl Model { }; self.reboot_at = (self.reboot && reboot_required && performed) .then(|| Instant::now() + REBOOT_DELAY); - Some(self.details.clone()) + self.log(LogSource::Inst, LogLevel::Info, self.details.clone()) } Ok(Completion::Failure(details)) => { self.uncertain = false; - self.fail(details, false); - Some(self.details.clone()) + self.fail(details.clone(), false); + self.log(LogSource::Inst, LogLevel::Error, details) } Err(error) => { - self.fail(format!("{error:#}"), true); - Some(self.details.clone()) + let details = format!("{error:#}"); + self.fail(details.clone(), true); + self.log(LogSource::Inst, LogLevel::Error, details) } }, None => { - self.fail( - "Response has no body; installation outcome is unknown".into(), - true, - ); - Some(self.details.clone()) + let details = "Response has no body; installation outcome is unknown"; + self.fail(details.into(), true); + self.log(LogSource::Inst, LogLevel::Error, details.into()) } }, } @@ -497,7 +581,6 @@ impl DemoShell { pub(super) struct Output { pub log: Option, pub mirrors: Vec<(PathBuf, File)>, - pub control: String, } impl Output { @@ -509,14 +592,9 @@ impl Output { Ok(()) } - fn announce(&mut self, message: &str) { + fn announce(&mut self, entry: &LogEntry) { self.mirrors.retain_mut(|(path, mirror)| { - if let Err(error) = writeln!( - mirror, - "\r\n[installer; control {}] {}", - self.control, - display_text(message) - ) { + if let Err(error) = writeln!(mirror, "{}", entry.plain()) { warn!( "Stopped mirroring installer status to '{}': {error}", path.display() @@ -609,6 +687,10 @@ pub(super) async fn run( mut output: Output, ) -> Result<(), Error> { let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let (log_tx, mut log_rx) = mpsc::unbounded_channel(); + INSTALLER_LOG_TX + .set(log_tx) + .map_err(|_| anyhow!("Installer log receiver already initialized"))?; let mut interrupts = unix::signal(SignalKind::interrupt())?; let mut termination = unix::signal(SignalKind::terminate())?; let mut model = Model::new(); @@ -631,29 +713,31 @@ pub(super) async fn run( let mut printed_screen = None; loop { for _ in 0..EVENT_CAPACITY { - let Ok(event) = rx.try_recv() else { break }; - output.record(&event)?; - let level = match &event { - Event::Response(response) => match &response.response { - Some(ResponseBody::Log(log)) => Some(log.level()), - _ => None, + let event = match rx.try_recv() { + Ok(event) => event, + Err(_) => match log_rx.try_recv() { + Ok(event) => event, + Err(_) => break, }, - _ => None, }; - let visible = level.is_none_or(|level| log_visible(level, model.verbosity)); - let mirrored = level.is_none_or(|level| log_visible(level, LevelFilter::Info)); - if let Some(message) = model.event(event) { - if mirrored { - output.announce(&message); - } - if display.is_none() - && shell.is_none() - && demo_shell.is_none() - && (interactive || !model.failed) - && visible - { - println!("{}", display_text(&message)); - } + output.record(&event)?; + let already_present = matches!(event, Event::AlreadyPresent(_)); + let entry = model.event(event); + output.announce(&entry); + if display.is_none() + && shell.is_none() + && demo_shell.is_none() + && log_visible(entry.level, model.verbosity) + { + println!("{}", entry.plain()); + } + if !interactive && already_present { + model.failed = true; + let explanation = + "Already installed; remove the media or confirm a force reinstall on a graphical console"; + model.details.push_str(&format!("\n{explanation}")); + let error = model.log(LogSource::Inst, LogLevel::Error, explanation.into()); + output.announce(&error); } } if worker.as_ref().is_some_and(JoinHandle::is_finished) { @@ -693,6 +777,12 @@ pub(super) async fn run( { model.reboot_at = None; if scenario.is_none() { + let reboot = model.log( + LogSource::Inst, + LogLevel::Info, + "Rebooting after successful installation".into(), + ); + output.announce(&reboot); match systemd::reboot() { Ok(()) => return Ok(()), Err(error) => model.fail(format!("Failed to reboot: {error:#}"), false), @@ -700,7 +790,7 @@ pub(super) async fn run( } } if !interactive { - if worker.is_none() && rx.is_empty() { + if worker.is_none() && rx.is_empty() && log_rx.is_empty() && model.reboot_at.is_none() { if model.failed { return Err(anyhow!( "{}\nRecovery requires an interactive terminal.", @@ -796,18 +886,32 @@ pub(super) async fn run( model.screen = Screen::Success; model.selected = 0; } - Action::Reboot => match systemd::reboot() { - Ok(()) => return Ok(()), - Err(error) => { - model.fail(format!("Failed to reboot: {error:#}"), false) + Action::Reboot => { + let action = model.log( + LogSource::Inst, + LogLevel::Info, + "Reboot requested".into(), + ); + output.announce(&action); + if let Err(error) = systemd::reboot() { + model.fail(format!("Failed to reboot: {error:#}"), false); + } else { + return Ok(()); } - }, - Action::Poweroff => match systemd::poweroff() { - Ok(()) => return Ok(()), - Err(error) => { - model.fail(format!("Failed to shut down: {error:#}"), false) + } + Action::Poweroff => { + let action = model.log( + LogSource::Inst, + LogLevel::Info, + "Shutdown requested".into(), + ); + output.announce(&action); + if let Err(error) = systemd::poweroff() { + model.fail(format!("Failed to shut down: {error:#}"), false); + } else { + return Ok(()); } - }, + } } } } @@ -852,10 +956,10 @@ fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { fn log_color(level: LogLevel) -> Color { match level { LogLevel::Error => Color::LightRed, - LogLevel::Warn | LogLevel::Unspecified => Color::Yellow, - LogLevel::Info => Color::Green, - LogLevel::Debug => Color::Cyan, - LogLevel::Trace => Color::DarkGray, + LogLevel::Warn | LogLevel::Unspecified => Color::Rgb(255, 165, 0), + LogLevel::Info => Color::LightBlue, + LogLevel::Debug => Color::Rgb(170, 100, 255), + LogLevel::Trace => Color::Gray, } } @@ -971,21 +1075,33 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { Color::Cyan }, ); - frame.render_widget( - Paragraph::new(display_text(&model.source)).wrap(Wrap { trim: false }), - areas[1], - ); + if areas[1].width > 22 { + let operation = + Layout::horizontal([Constraint::Min(0), Constraint::Length(21)]).split(areas[1]); + frame.render_widget( + Paragraph::new(display_text(&model.source)).wrap(Wrap { trim: false }), + operation[0], + ); + frame.render_widget( + Paragraph::new(format!("Logs: {} [V]", model.verbosity)) + .alignment(Alignment::Right) + .style(Style::default().fg(Color::Cyan)), + operation[1], + ); + } else { + frame.render_widget( + Paragraph::new(display_text(&model.source)).wrap(Wrap { trim: false }), + areas[1], + ); + } let content = match model.screen { Screen::Progress | Screen::Menu => { let mut lines = vec![Line::from(Span::styled(model.activity.clone(), Style::default().fg(Color::Cyan))), Line::default()]; - let logs = model.logs.iter().filter(|(level, _)| log_visible(*level, model.verbosity)).collect::>(); + let logs = model.logs.iter().filter(|entry| log_visible(entry.level, model.verbosity)).collect::>(); let visible = usize::from(areas[2].height.saturating_sub(4)); let end = logs.len().saturating_sub(usize::from(model.scroll).min(logs.len().saturating_sub(1))); let start = end.saturating_sub(visible); - lines.extend(logs[start..end].iter().map(|(level, message)| Line::from(vec![ - Span::styled(format!("{:<5} ", level.to_string()), Style::default().fg(log_color(*level)).add_modifier(Modifier::BOLD)), - Span::styled(message.clone(), Style::default().fg(log_color(*level))), - ]))); + lines.extend(logs[start..end].iter().map(|entry| entry.styled())); Paragraph::new(lines).block(block("Activity / logs")).wrap(Wrap { trim: false }) } Screen::StreamUrl | Screen::HostConfigUrl => Paragraph::new(format!("Enter an http:// or https:// URL:\n\n{}\n\nEnter: start Esc: back", display_text(&model.url))) @@ -1030,8 +1146,7 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { "Enter: start Esc: return to recovery".to_owned() } else { format!( - "Tab/Arrows: choose Enter: select S: shell V: {} Esc: menu{}", - model.verbosity, + "Tab/Arrows: choose Enter: select S: shell V: logs Esc: menu{}", if demo { " Q: quit" } else { "" } ) }; @@ -1093,6 +1208,7 @@ mod tests { use ratatui::backend::TestBackend; use tempfile::TempDir; + use trident_proto::v1::{Completed, RebootStatus, ServicingResponse, StatusCode}; #[test] fn unavailable_serial_mirror_does_not_stop_the_installer() { @@ -1115,14 +1231,23 @@ mod tests { .unwrap(), ), ], - control: "/dev/tty1".into(), }; - output.announce("Preparing installer"); + let entry = LogEntry { + elapsed: Duration::from_secs(125), + source: LogSource::Inst, + level: LogLevel::Info, + message: "Preparing installer".into(), + }; + output.announce(&entry); assert_eq!(output.mirrors.len(), 1); - output.announce("Still running"); - assert!(fs::read_to_string(transcript) - .unwrap() - .contains("Still running")); + output.announce(&LogEntry { + message: "Still running".into(), + ..entry + }); + assert_eq!( + fs::read_to_string(transcript).unwrap(), + "02:05 [INST:INFO] Preparing installer\n02:05 [INST:INFO] Still running\n" + ); } fn enter() -> KeyEvent { @@ -1198,17 +1323,17 @@ mod tests { #[test] fn live_verbosity_picker_filters_without_discarding_logs() { let mut model = Model::new(); - model - .logs - .push_back((LogLevel::Info, "ordinary event".into())); - model - .logs - .push_back((LogLevel::Trace, "diagnostic detail".into())); + model.log(LogSource::Inst, LogLevel::Info, "ordinary event".into()); + model.log( + LogSource::Trident, + LogLevel::Trace, + "diagnostic detail".into(), + ); + assert_eq!(model.verbosity, LevelFilter::Debug); assert!(!log_visible(LogLevel::Trace, model.verbosity)); model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), true); assert!(model.verbosity_open); model.key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), true); - model.key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), true); model.key(enter(), true); assert_eq!(model.verbosity, LevelFilter::Trace); assert!(!model.verbosity_open); @@ -1226,10 +1351,9 @@ mod tests { fn preparation_steps_replace_the_idle_status() { let mut model = Model::new(); let step = "Looking for installer media by filesystem label"; - assert_eq!( - model.event(Event::Preparing(step.into())), - Some(step.into()) - ); + let entry = model.event(Event::Preparing(step.into())); + assert_eq!(entry.source, LogSource::Inst); + assert_eq!(entry.message, step); assert_eq!(model.activity, step); } @@ -1239,13 +1363,13 @@ mod tests { model.verbosity = LevelFilter::Trace; let records = [ (LogLevel::Error, "error-line", Color::LightRed), - (LogLevel::Warn, "warn-line", Color::Yellow), - (LogLevel::Info, "info-line", Color::Green), - (LogLevel::Debug, "debug-line", Color::Cyan), - (LogLevel::Trace, "trace-line", Color::DarkGray), + (LogLevel::Warn, "warn-line", Color::Rgb(255, 165, 0)), + (LogLevel::Info, "info-line", Color::LightBlue), + (LogLevel::Debug, "debug-line", Color::Rgb(170, 100, 255)), + (LogLevel::Trace, "trace-line", Color::Gray), ]; for (level, text, _) in records { - model.logs.push_back((level, text.into())); + model.log(LogSource::Trident, level, text.into()); } let mut terminal = Terminal::new(TestBackend::new(80, 32)).unwrap(); terminal.draw(|frame| render(frame, &model, true)).unwrap(); @@ -1263,7 +1387,102 @@ mod tests { .unwrap(); let text = row.iter().map(|cell| cell.symbol()).collect::(); assert_eq!(row[text.find(message).unwrap()].fg, expected); + let source = text.find("TRIDENT").unwrap(); + assert_eq!(row[source].fg, Color::LightGreen); + } + } + + #[test] + fn operation_and_verbosity_share_one_line() { + let mut model = Model::new(); + model.source = "StreamDisk from file:///media/cosi/payload.cosi".into(); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + let row = terminal.backend().buffer().content[12 * 80..13 * 80] + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(row.contains("StreamDisk from file://"), "{row}"); + assert!(row.contains("Logs: DEBUG [V]"), "{row}"); + } + + #[test] + fn all_log_levels_reach_serial_regardless_of_display_filter() { + let mut model = Model::new(); + let root = TempDir::new().unwrap(); + let serial = root.path().join("serial.log"); + let mut output = Output { + log: None, + mirrors: vec![(serial.clone(), File::create(&serial).unwrap())], + }; + for level in [ + LogLevel::Error, + LogLevel::Warn, + LogLevel::Info, + LogLevel::Debug, + LogLevel::Trace, + ] { + let entry = model.log(LogSource::Trident, level, "first\nsecond".into()); + output.announce(&entry); } + let text = fs::read_to_string(serial).unwrap(); + let lines = text.lines().collect::>(); + assert_eq!(lines.len(), 5); + assert!(lines.iter().all(|line| line.starts_with("00:00 [TRIDENT:"))); + assert!(lines.iter().all(|line| line.ends_with("first\\nsecond"))); + assert!(lines[4].contains("[TRIDENT:TRACE]")); + assert!(!log_visible(LogLevel::Trace, model.verbosity)); + assert_eq!(model.logs.len(), 5); + } + + #[test] + fn installer_logger_records_use_inst_source_and_colour() { + let mut model = Model::new(); + let entry = model.event(Event::InstallerLog { + level: Level::Debug, + message: "Loaded installer settings".into(), + }); + assert_eq!(entry.source, LogSource::Inst); + assert_eq!( + entry.plain(), + "00:00 [INST:DEBUG] Loaded installer settings" + ); + assert_eq!(entry.styled().spans[1].style.fg, Some(Color::LightMagenta)); + assert_eq!( + entry.styled().spans[3].style.fg, + Some(Color::Rgb(170, 100, 255)) + ); + assert_eq!(model.logs.len(), 1); + } + + #[test] + fn headless_autorun_keeps_automatic_reboot_enabled_by_default() { + let config = Config::parse("mode = 'autorun'\nserialMode = 'logs'").unwrap(); + let mut model = Model::new(); + model.reboot = config.autorun.reboot; + model.event(Event::Response(ServicingResponse { + response: Some(ResponseBody::Completed(Completed { + status: StatusCode::Success.into(), + reboot_status: RebootStatus::RebootRequired.into(), + ..Default::default() + })), + ..Default::default() + })); + assert!(model.reboot_at.is_some()); + } + + #[test] + fn errors_for_missing_inputs_are_tagged_for_serial_without_a_tui() { + let mut model = Model::new(); + let entry = model.event(Event::Error { + details: "No image or Host Configuration".into(), + uncertain: false, + }); + assert!(model.failed); + assert_eq!( + entry.plain(), + "00:00 [INST:ERROR] No image or Host Configuration" + ); } #[test] diff --git a/packaging/installer.toml b/packaging/installer.toml index 4f1234fe94..ab16c5d6e0 100644 --- a/packaging/installer.toml +++ b/packaging/installer.toml @@ -1,4 +1,5 @@ mode = "autorun" +serialMode = "logs" [media] cdromLabel = "TRIDENT_INSTALL" diff --git a/test/README.md b/test/README.md index d5638d5b6c..8367dcbf83 100644 --- a/test/README.md +++ b/test/README.md @@ -12,6 +12,10 @@ python3 test/run.py --display gtk The payload is copied into the ISO's `cosi/` folder. No HC is supplied, so autorun chooses StreamDisk. `reboot = false` leaves the success screen visible; use its Reboot button to check what the installed disk boots. +`serialMode = "logs"` routes every INST and TRIDENT log record to the serial +console as `MM:SS [SOURCE:LEVEL] message`, independent of TUI verbosity. +This ISO suppresses the serial getty prompt and normal kernel/systemd status +output; UEFI firmware output may still appear before the installer starts. The ISO disables `getty@tty1.service` and logind's automatic virtual gettys so they cannot reclaim the installer's graphical keyboard after startup. The installer mirrors source-discovery stages to the serial log; an idle-looking diff --git a/test/installer/files/installer-serial-getty.conf b/test/installer/files/installer-serial-getty.conf new file mode 100644 index 0000000000..0721aef5e2 --- /dev/null +++ b/test/installer/files/installer-serial-getty.conf @@ -0,0 +1,2 @@ +[Unit] +ConditionPathExists=!/etc/trident/installer.toml diff --git a/test/installer/files/installer.toml b/test/installer/files/installer.toml index 762a1bedcb..d845a2ca79 100644 --- a/test/installer/files/installer.toml +++ b/test/installer/files/installer.toml @@ -1,4 +1,5 @@ mode = "autorun" +serialMode = "logs" [media] cdromLabel = "CDROM" diff --git a/test/installer/image.yaml b/test/installer/image.yaml index 77a892b947..da0fb16469 100644 --- a/test/installer/image.yaml +++ b/test/installer/image.yaml @@ -29,6 +29,9 @@ config: extraCommandLine: - console=tty0 - console=ttyS0,115200n8 + - quiet + - loglevel=0 + - systemd.show_status=false packages: install: - trident-installer @@ -49,6 +52,8 @@ config: destination: /etc/trident/installer.toml - source: files/installer-logind.conf destination: /etc/systemd/logind.conf.d/installer.conf + - source: files/installer-serial-getty.conf + destination: /etc/systemd/system/serial-getty@ttyS0.service.d/installer.conf storage: bootType: efi disks: @@ -79,4 +84,6 @@ config: extraCommandLine: - console=tty0 - console=ttyS0,115200n8 - - loglevel=4 + - quiet + - loglevel=0 + - systemd.show_status=false From 074e20c4f29a0483ea6c1ef8101571c0ecc6f685 Mon Sep 17 00:00:00 2001 From: Paco Date: Sat, 3 Oct 2026 14:12:40 -0700 Subject: [PATCH 11/62] docs: Clarify tagged serial logs and headless autorun Document firmware boot noise and serial-only install, reboot, and operator-required error behavior verified in disposable VMs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 3 ++- test/README.md | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index fd33c68910..73a2f87bd3 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -48,7 +48,7 @@ an error rather than showing an unfinished serial TUI. Media is mounted read-only at `/run/trident/installer-media`. Duplicate labels are rejected. Optional `installer/installer.toml` on the mounted media can override mode, image directory, and autorun options, but not bootstrap media -discovery. Relative HC paths are media-relative; absolute paths and `file://` +discovery or `serialMode`. Relative HC paths are media-relative; absolute paths and `file://` refer to the live filesystem. HTTP(S) HC sources are supported. With an HC, use Install. Otherwise stream the first regular `.cosi` file in @@ -111,6 +111,7 @@ orange, info bright blue, debug purple, trace gray; installer source labels are magenta and Trident labels green. `V` opens a live verbosity picker (Off, Error, Warn, Info, Debug, Trace) beside the operation; this only filters the display, not serial or stored diagnostics. PgUp/PgDn scroll. +TRACE output can expose low-level inputs; restrict access to BMC serial captures. A missing final Completed response means unknown outcome, not success. Another write requires a successful Trident status query establishing that the daemon is idle. diff --git a/test/README.md b/test/README.md index 8367dcbf83..e7954ed9aa 100644 --- a/test/README.md +++ b/test/README.md @@ -16,6 +16,9 @@ use its Reboot button to check what the installed disk boots. console as `MM:SS [SOURCE:LEVEL] message`, independent of TUI verbosity. This ISO suppresses the serial getty prompt and normal kernel/systemd status output; UEFI firmware output may still appear before the installer starts. +With only a serial console active, autorun runs without a TUI and honors +`autorun.reboot`; missing inputs or an already-present image produce tagged +errors instead of waiting for a keyboard response. The ISO disables `getty@tty1.service` and logind's automatic virtual gettys so they cannot reclaim the installer's graphical keyboard after startup. The installer mirrors source-discovery stages to the serial log; an idle-looking From 49f164c86949ac52c75ec23f310355a5c80f8271 Mon Sep 17 00:00:00 2001 From: Paco Date: Sat, 3 Oct 2026 20:37:47 -0700 Subject: [PATCH 12/62] feature: Follow installer serial logs with configurable verbosity Default serial output to DEBUG independently of the TUI while retaining full diagnostics. Stream tagged installer/Trident serial records into the VM launch terminal without firmware escape sequences. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 10 ++++-- crates/installer/src/client.rs | 5 ++- crates/installer/src/config.rs | 38 +++++++++++++++++++++ crates/installer/src/demo.rs | 6 +++- crates/installer/src/ui.rs | 53 +++++++++++++++++++---------- packaging/installer.toml | 1 + test/README.md | 12 +++++-- test/installer/files/installer.toml | 1 + test/run.py | 35 ++++++++++++++++++- test/test_scripts.py | 39 +++++++++++++++++++++ 10 files changed, 173 insertions(+), 27 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 73a2f87bd3..7f3b5fa6d7 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -27,6 +27,7 @@ Read `/etc/trident/installer.toml` or the path supplied with `--config`. ```toml mode = "autorun" serialMode = "logs" +serialVerbosity = "debug" [media] cdromLabel = "TRIDENT_INSTALL" @@ -44,6 +45,8 @@ reserved for later work, not an alias for autorun. COSI directory paths must stay beneath the media root. `serialMode` defaults to `logs`. `ui` is reserved and currently rejected with an error rather than showing an unfinished serial TUI. +`serialVerbosity` defaults to `debug` and accepts `off`, `error`, `warn`, +`info`, `debug`, and `trace`. Media is mounted read-only at `/run/trident/installer-media`. Duplicate labels are rejected. Optional `installer/installer.toml` on the mounted media can @@ -104,13 +107,14 @@ defaults for an installation. Display filtering never removes daemon log records. All received responses are also appended to the private `/var/log/trident-installer.log`. Installer events and daemon records use `MM:SS [INST:LEVEL] message` and -`MM:SS [TRIDENT:LEVEL] message`, respectively. The serial stream includes all -levels, even when the TUI is filtered, and escapes embedded newlines as `\n` +`MM:SS [TRIDENT:LEVEL] message`, respectively. `serialVerbosity` controls the +serial stream independently of TUI verbosity, and embedded newlines become `\n` to keep one record per line. The TUI defaults to Debug: errors red, warnings orange, info bright blue, debug purple, trace gray; installer source labels are magenta and Trident labels green. `V` opens a live verbosity picker (Off, Error, Warn, Info, Debug, Trace) beside the operation; this only filters the -display, not serial or stored diagnostics. PgUp/PgDn scroll. +display, not serial or stored diagnostics. The private diagnostic file retains +all levels even when serialVerbosity filters them. PgUp/PgDn scroll. TRACE output can expose low-level inputs; restrict access to BMC serial captures. A missing final Completed response means unknown outcome, not success. Another write requires a successful Trident status query diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index a5db450517..f35f75c48b 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -24,7 +24,7 @@ use trident_proto::{ }; use crate::{ - config::{Config, DEFAULT_HOST_CONFIGURATION}, + config::{Config, SerialVerbosity, DEFAULT_HOST_CONFIGURATION}, source::{self, Plan, Request}, }; @@ -41,6 +41,7 @@ pub(super) enum Event { Prepared { description: String, reboot: bool, + serial_verbosity: SerialVerbosity, stream_image: Option, }, Response(ServicingResponse), @@ -148,6 +149,7 @@ pub(super) async fn execute( Event::Prepared { description, reboot: settings.autorun.reboot, + serial_verbosity: settings.serial_verbosity, stream_image: None, }, ) @@ -179,6 +181,7 @@ pub(super) async fn execute( Event::Prepared { description: plan.description(), reboot: settings.autorun.reboot, + serial_verbosity: settings.serial_verbosity, stream_image: match &plan { Plan::Stream { image } => Some(image.clone()), Plan::Install { .. } => None, diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index d08232e0a2..789711021b 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -5,6 +5,7 @@ use std::{ }; use anyhow::{bail, ensure, Context, Error}; +use log::LevelFilter; use serde::{Deserialize, Serialize}; use toml::Value as TomlValue; @@ -21,6 +22,8 @@ pub(super) struct Config { #[serde(default)] pub serial_mode: SerialMode, #[serde(default)] + pub serial_verbosity: SerialVerbosity, + #[serde(default)] pub media: Media, #[serde(default)] pub autorun: Autorun, @@ -42,6 +45,31 @@ pub(super) enum SerialMode { Ui, } +#[derive(Serialize, Deserialize, Debug, Default, Clone, Copy, PartialEq, Eq)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub(super) enum SerialVerbosity { + Off, + Error, + Warn, + Info, + #[default] + Debug, + Trace, +} + +impl SerialVerbosity { + pub(super) fn filter(self) -> LevelFilter { + match self { + Self::Off => LevelFilter::Off, + Self::Error => LevelFilter::Error, + Self::Warn => LevelFilter::Warn, + Self::Info => LevelFilter::Info, + Self::Debug => LevelFilter::Debug, + Self::Trace => LevelFilter::Trace, + } + } +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub(super) struct Media { @@ -253,6 +281,7 @@ mod tests { Config::parse("mode = 'autorun'\nunknown = true").unwrap_err(); let config = Config::parse("mode = 'autorun'").unwrap(); assert_eq!(config.serial_mode, SerialMode::Logs); + assert_eq!(config.serial_verbosity, SerialVerbosity::Debug); assert!(config.autorun.reboot); assert_eq!(config.media.cosi_directory, Path::new("cosi")); Config::parse("mode = 'interactive'") @@ -268,6 +297,15 @@ mod tests { assert!(error.to_string().contains("not implemented"), "{error:#}"); } + #[test] + fn serial_verbosity_defaults_to_debug_and_rejects_invalid_levels() { + let config = Config::parse("mode = 'autorun'\nserialVerbosity = 'trace'").unwrap(); + assert_eq!(config.serial_verbosity.filter(), LevelFilter::Trace); + let config = Config::parse("mode = 'autorun'\nserialVerbosity = 'off'").unwrap(); + assert_eq!(config.serial_verbosity.filter(), LevelFilter::Off); + Config::parse("mode = 'autorun'\nserialVerbosity = 'verbose'").unwrap_err(); + } + #[test] fn packaged_configuration_matches_the_runtime_schema() { let config = Config::parse(include_str!("../../../packaging/installer.toml")).unwrap(); diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs index b7b7cc32c0..c8939db854 100644 --- a/crates/installer/src/demo.rs +++ b/crates/installer/src/demo.rs @@ -9,7 +9,10 @@ use trident_proto::v1::{ ServicingKind, ServicingResponse, Started, StatusCode, TridentError, TridentErrorKind, }; -use crate::client::{self, Event}; +use crate::{ + client::{self, Event}, + config::SerialVerbosity, +}; const DEMO_STEP: Duration = Duration::from_millis(500); @@ -28,6 +31,7 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err Event::Prepared { description: "DEMO: StreamDisk from ISO image".into(), reboot: false, + serial_verbosity: SerialVerbosity::Debug, stream_image: None, }, ) diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index e275907822..d6e8760d78 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -35,7 +35,7 @@ use trident_proto::v1::{servicing_response::Response as ResponseBody, LogLevel}; use crate::{ client::{self, Completion, Event}, - config::Config, + config::{Config, SerialVerbosity}, demo::{self, Scenario}, source::{self, Request}, APPLICATION_NAME, INSTALLER_LOG_TX, @@ -163,6 +163,7 @@ struct Model { details: String, logs: VecDeque, verbosity: LevelFilter, + serial_verbosity: LevelFilter, verbosity_open: bool, verbosity_selected: usize, selected: usize, @@ -185,6 +186,7 @@ impl Model { details: String::new(), logs: VecDeque::new(), verbosity: LevelFilter::Debug, + serial_verbosity: SerialVerbosity::Debug.filter(), verbosity_open: false, verbosity_selected: 4, selected: 0, @@ -255,10 +257,12 @@ impl Model { Event::Prepared { description, reboot, + serial_verbosity, stream_image, } => { self.source = description.clone(); self.reboot = reboot; + self.serial_verbosity = serial_verbosity.filter(); self.stream_image = stream_image; self.log(LogSource::Inst, LogLevel::Info, description) } @@ -592,7 +596,10 @@ impl Output { Ok(()) } - fn announce(&mut self, entry: &LogEntry) { + fn announce(&mut self, entry: &LogEntry, verbosity: LevelFilter) { + if !log_visible(entry.level, verbosity) { + return; + } self.mirrors.retain_mut(|(path, mirror)| { if let Err(error) = writeln!(mirror, "{}", entry.plain()) { warn!( @@ -723,7 +730,7 @@ pub(super) async fn run( output.record(&event)?; let already_present = matches!(event, Event::AlreadyPresent(_)); let entry = model.event(event); - output.announce(&entry); + output.announce(&entry, model.serial_verbosity); if display.is_none() && shell.is_none() && demo_shell.is_none() @@ -737,7 +744,7 @@ pub(super) async fn run( "Already installed; remove the media or confirm a force reinstall on a graphical console"; model.details.push_str(&format!("\n{explanation}")); let error = model.log(LogSource::Inst, LogLevel::Error, explanation.into()); - output.announce(&error); + output.announce(&error, model.serial_verbosity); } } if worker.as_ref().is_some_and(JoinHandle::is_finished) { @@ -782,7 +789,7 @@ pub(super) async fn run( LogLevel::Info, "Rebooting after successful installation".into(), ); - output.announce(&reboot); + output.announce(&reboot, model.serial_verbosity); match systemd::reboot() { Ok(()) => return Ok(()), Err(error) => model.fail(format!("Failed to reboot: {error:#}"), false), @@ -892,7 +899,7 @@ pub(super) async fn run( LogLevel::Info, "Reboot requested".into(), ); - output.announce(&action); + output.announce(&action, model.serial_verbosity); if let Err(error) = systemd::reboot() { model.fail(format!("Failed to reboot: {error:#}"), false); } else { @@ -905,7 +912,7 @@ pub(super) async fn run( LogLevel::Info, "Shutdown requested".into(), ); - output.announce(&action); + output.announce(&action, model.serial_verbosity); if let Err(error) = systemd::poweroff() { model.fail(format!("Failed to shut down: {error:#}"), false); } else { @@ -1238,12 +1245,15 @@ mod tests { level: LogLevel::Info, message: "Preparing installer".into(), }; - output.announce(&entry); + output.announce(&entry, LevelFilter::Debug); assert_eq!(output.mirrors.len(), 1); - output.announce(&LogEntry { - message: "Still running".into(), - ..entry - }); + output.announce( + &LogEntry { + message: "Still running".into(), + ..entry + }, + LevelFilter::Debug, + ); assert_eq!( fs::read_to_string(transcript).unwrap(), "02:05 [INST:INFO] Preparing installer\n02:05 [INST:INFO] Still running\n" @@ -1407,7 +1417,7 @@ mod tests { } #[test] - fn all_log_levels_reach_serial_regardless_of_display_filter() { + fn serial_verbosity_filters_independently_of_display() { let mut model = Model::new(); let root = TempDir::new().unwrap(); let serial = root.path().join("serial.log"); @@ -1423,16 +1433,23 @@ mod tests { LogLevel::Trace, ] { let entry = model.log(LogSource::Trident, level, "first\nsecond".into()); - output.announce(&entry); + output.announce(&entry, SerialVerbosity::Debug.filter()); } - let text = fs::read_to_string(serial).unwrap(); + let text = fs::read_to_string(&serial).unwrap(); let lines = text.lines().collect::>(); - assert_eq!(lines.len(), 5); + assert_eq!(lines.len(), 4); assert!(lines.iter().all(|line| line.starts_with("00:00 [TRIDENT:"))); assert!(lines.iter().all(|line| line.ends_with("first\\nsecond"))); - assert!(lines[4].contains("[TRIDENT:TRACE]")); + let trace = model.log(LogSource::Trident, LogLevel::Trace, "trace detail".into()); + output.announce(&trace, SerialVerbosity::Trace.filter()); + let text = fs::read_to_string(serial).unwrap(); + assert!(text + .lines() + .last() + .unwrap() + .ends_with("[TRIDENT:TRACE] trace detail")); assert!(!log_visible(LogLevel::Trace, model.verbosity)); - assert_eq!(model.logs.len(), 5); + assert_eq!(model.logs.len(), 6); } #[test] diff --git a/packaging/installer.toml b/packaging/installer.toml index ab16c5d6e0..4a736d3ab4 100644 --- a/packaging/installer.toml +++ b/packaging/installer.toml @@ -1,5 +1,6 @@ mode = "autorun" serialMode = "logs" +serialVerbosity = "debug" [media] cdromLabel = "TRIDENT_INSTALL" diff --git a/test/README.md b/test/README.md index e7954ed9aa..1fad3028d6 100644 --- a/test/README.md +++ b/test/README.md @@ -12,8 +12,9 @@ python3 test/run.py --display gtk The payload is copied into the ISO's `cosi/` folder. No HC is supplied, so autorun chooses StreamDisk. `reboot = false` leaves the success screen visible; use its Reboot button to check what the installed disk boots. -`serialMode = "logs"` routes every INST and TRIDENT log record to the serial -console as `MM:SS [SOURCE:LEVEL] message`, independent of TUI verbosity. +`serialMode = "logs"` routes INST and TRIDENT log records to the serial +console as `MM:SS [SOURCE:LEVEL] message`, up to `serialVerbosity` (default +`debug`), independently of TUI verbosity. This ISO suppresses the serial getty prompt and normal kernel/systemd status output; UEFI firmware output may still appear before the installer starts. With only a serial console active, autorun runs without a TUI and honors @@ -45,7 +46,12 @@ python3 test/run.py --dry-run `--keep` retains the disk, firmware variables and serial log for inspection. Otherwise that run's private `.vm/` directory is removed after QEMU exits. -GTK uses the graphical installer console; serial status is captured separately. +The launcher follows tagged INST and TRIDENT serial records in the same +terminal, including TRACE when `serialVerbosity = "trace"`, without forwarding +firmware/GRUB escape sequences that could redraw your terminal. The complete +raw serial capture stays +in the indicated `serial.log` file while the VM runs; use `--keep` to retain it +after exit. GTK uses a separate graphical window for the interactive console. Do not rebuild over an ISO still attached to a running VM. Use `python3 test/build.py --output-dir test/.work/next-iso` and then pass `--iso test/.work/next-iso/installer_amd64_iso.iso` to `run.py` to test a diff --git a/test/installer/files/installer.toml b/test/installer/files/installer.toml index d845a2ca79..7d71bf62fb 100644 --- a/test/installer/files/installer.toml +++ b/test/installer/files/installer.toml @@ -1,5 +1,6 @@ mode = "autorun" serialMode = "logs" +serialVerbosity = "debug" [media] cdromLabel = "CDROM" diff --git a/test/run.py b/test/run.py index 35e80ee48e..4f49fcba92 100755 --- a/test/run.py +++ b/test/run.py @@ -4,16 +4,20 @@ import json import os from pathlib import Path +import re import select import shlex import shutil import socket import subprocess +import sys import tempfile import time HERE = Path(__file__).resolve().parent QMP_STARTUP_TIMEOUT_SECONDS = 30 +SERIAL_POLL_SECONDS = 0.1 +INSTALLER_RECORD = re.compile(r"^\d{2,}:[0-5]\d \[(?:INST|TRIDENT):[A-Z]+\] ") CODE_CANDIDATES = [ Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), Path("/usr/share/OVMF/OVMF_CODE.fd"), @@ -131,6 +135,31 @@ def wait_for_vnc_viewer(process, monitor_path, on_ready): qmp.execute("cont") +def follow_serial(process, path, output=sys.stdout, interval=SERIAL_POLL_SECONDS): + stream = None + pending = "" + try: + while True: + if stream is None and path.exists(): + stream = path.open(encoding="utf-8", errors="replace") + if stream is not None: + pending += stream.read() + while "\n" in pending: + line, pending = pending.split("\n", 1) + line = line.rstrip("\r") + if INSTALLER_RECORD.match(line): + print(line, file=output, flush=True) + status = process.poll() + if status is not None: + if pending and INSTALLER_RECORD.match(pending): + print(pending.rstrip("\r"), file=output, flush=True) + return status + time.sleep(interval) + finally: + if stream is not None: + stream.close() + + def main(): parser = argparse.ArgumentParser( description="Boot the installer ISO in a disposable UEFI VM" @@ -251,7 +280,11 @@ def show_vnc_instructions(): wait_for_vnc_viewer( process, scratch / "monitor.sock", show_vnc_instructions ) - status = process.wait() + print( + "Following installer serial records here (full capture: serial.log).", + flush=True, + ) + status = follow_serial(process, scratch / "serial.log") finally: if process.poll() is None: process.terminate() diff --git a/test/test_scripts.py b/test/test_scripts.py index 24fc7360e9..daacfa50f3 100644 --- a/test/test_scripts.py +++ b/test/test_scripts.py @@ -1,9 +1,11 @@ import importlib.util +import io import json from pathlib import Path import socket import tempfile import threading +import time import unittest HERE = Path(__file__).resolve().parent @@ -13,6 +15,43 @@ class VmCommandTests(unittest.TestCase): + def test_follows_new_serial_records_without_replaying_firmware_escape_codes(self): + class RunningProcess: + def __init__(self): + self.finished = threading.Event() + + def poll(self): + return 0 if self.finished.is_set() else None + + process = RunningProcess() + with tempfile.TemporaryDirectory() as directory: + serial = Path(directory) / "serial.log" + output = io.StringIO() + + def guest(): + time.sleep(0.03) + with serial.open("w") as log: + log.write("\x1b[2JWelcome to firmware\r\n") + log.write("00:00 [INST:INFO] Installer starting\r\n") + log.flush() + time.sleep(0.03) + log.write("00:01 [TRIDENT:TRACE] first\\nsecond\r\n") + log.flush() + process.finished.set() + + writer = threading.Thread(target=guest) + writer.start() + self.assertEqual( + VM.follow_serial(process, serial, output=output, interval=0.005), 0 + ) + writer.join(timeout=3) + self.assertFalse(writer.is_alive()) + self.assertEqual( + output.getvalue(), + "00:00 [INST:INFO] Installer starting\n" + "00:01 [TRIDENT:TRACE] first\\nsecond\n", + ) + def test_vnc_is_headless_and_bound_to_loopback(self): launch = VM.command( Path("/tmp/installer-test.iso"), From 641c9d61d37940477e0def1218dc9115ebed3194 Mon Sep 17 00:00:00 2001 From: Paco Date: Sun, 4 Oct 2026 00:18:14 -0700 Subject: [PATCH 13/62] feature: Keep logs visible on installer result screens Repeat the serial prefix on each line of a multiline message. Highlight success and failure in the log panel title, frame and final record, while preserving scrollable logs and full error details. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 9 +- crates/installer/src/ui.rs | 338 ++++++++++++++++++++++++++++++------- test/README.md | 3 +- test/test_scripts.py | 6 +- 4 files changed, 288 insertions(+), 68 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 7f3b5fa6d7..f83d36ce84 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -96,7 +96,10 @@ virtual gettys (`NAutoVTs=0`, `ReserveVT=0`): stopping a getty before `getty.target` starts does not prevent it from claiming tty1 later. No ordinary source picker or confirmation precedes autorun. Missing sources -and failures show scrollable error details. Continue opens recovery: Shell, +and failures keep recent logs visible in a red result frame; `D` opens the +scrollable full error details and Esc returns to the logs. Successful installs +keep their logs in a green result frame. Each result is also the last highlighted +log entry. Continue opens recovery: Shell, Stream COSI URL, remote HC URL, or Shutdown. Shell `exit` returns to the originating screen. Installation continues while a shell is open; automatic reboot waits. The success screen offers Reboot and Shell. @@ -108,8 +111,8 @@ Display filtering never removes daemon log records. All received responses are also appended to the private `/var/log/trident-installer.log`. Installer events and daemon records use `MM:SS [INST:LEVEL] message` and `MM:SS [TRIDENT:LEVEL] message`, respectively. `serialVerbosity` controls the -serial stream independently of TUI verbosity, and embedded newlines become `\n` -to keep one record per line. The TUI defaults to Debug: errors red, warnings +serial stream independently of TUI verbosity; each line of a multiline message +gets its own `MM:SS [SOURCE:LEVEL]` prefix. The TUI defaults to Debug: errors red, warnings orange, info bright blue, debug purple, trace gray; installer source labels are magenta and Trident labels green. `V` opens a live verbosity picker (Off, Error, Warn, Info, Debug, Trace) beside the operation; this only filters the diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index d6e8760d78..67c980bc39 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -121,6 +121,7 @@ struct LogEntry { source: LogSource, level: LogLevel, message: String, + result_color: Option, } impl LogEntry { @@ -130,26 +131,29 @@ impl LogEntry { } fn plain(&self) -> String { - let message = display_text(&self.message).replace('\n', "\\n"); - format!( - "{}{}:{}] {message}", - self.prefix(), - self.source.label(), - self.level - ) + let prefix = format!("{}{}:{}] ", self.prefix(), self.source.label(), self.level); + display_text(&self.message) + .split('\n') + .map(|line| format!("{prefix}{line}")) + .collect::>() + .join("\n") } fn styled(&self) -> Line<'_> { - let level = Style::default().fg(log_color(self.level)); + let level = Style::default().fg(self.result_color.unwrap_or(log_color(self.level))); + let surrounding = self + .result_color + .map(|color| Style::default().fg(color)) + .unwrap_or_default(); Line::from(vec![ - Span::raw(self.prefix()), + Span::styled(self.prefix(), surrounding), Span::styled( self.source.label(), Style::default().fg(self.source.color()), ), - Span::raw(":"), + Span::styled(":", surrounding), Span::styled(self.level.to_string(), level.add_modifier(Modifier::BOLD)), - Span::raw("] "), + Span::styled("] ", surrounding), Span::styled(self.message.as_str(), level), ]) } @@ -175,6 +179,7 @@ struct Model { stream_image: Option, started: Instant, failed: bool, + error_details_open: bool, } impl Model { @@ -198,17 +203,24 @@ impl Model { stream_image: None, started: Instant::now(), failed: false, + error_details_open: false, } } - fn fail(&mut self, details: String, uncertain: bool) { - self.details = details; + fn fail(&mut self, details: String, uncertain: bool) -> LogEntry { + self.details = details.clone(); self.uncertain |= uncertain; self.reboot_at = None; self.failed = true; self.scroll = 0; self.selected = 0; self.screen = Screen::Error; + self.error_details_open = false; + self.result_log( + LogLevel::Error, + format!("FAILURE: {details}"), + Color::LightRed, + ) } fn log(&mut self, source: LogSource, level: LogLevel, message: String) -> LogEntry { @@ -217,13 +229,14 @@ impl Model { source, level, message: display_text(&message), + result_color: None, }; let mut characters = entry.message.chars(); let mut displayed = characters .by_ref() .take(DISPLAY_LOG_CHARACTERS) .collect::() - .replace('\n', "\\n"); + .replace('\n', " "); if characters.next().is_some() { displayed.push_str(" [Display shortened; full record preserved in diagnostic log]"); } @@ -237,6 +250,15 @@ impl Model { entry } + fn result_log(&mut self, level: LogLevel, message: String, color: Color) -> LogEntry { + let mut entry = self.log(LogSource::Inst, level, message); + entry.result_color = Some(color); + if let Some(last) = self.logs.back_mut() { + last.result_color = Some(color); + } + entry + } + fn event(&mut self, event: Event) -> LogEntry { match event { Event::InstallerLog { level, message } => self.log( @@ -271,12 +293,13 @@ impl Model { self.reboot_at = None; self.screen = Screen::AlreadyPresent; self.selected = 0; - self.log(LogSource::Inst, LogLevel::Warn, details) - } - Event::Error { details, uncertain } => { - self.fail(details.clone(), uncertain); - self.log(LogSource::Inst, LogLevel::Error, details) + self.result_log( + LogLevel::Warn, + format!("ALREADY PRESENT: {details}"), + Color::Yellow, + ) } + Event::Error { details, uncertain } => self.fail(details, uncertain), Event::Response(response) => match response.response { Some(ResponseBody::Started(_)) => { self.activity = "Servicing started".into(); @@ -305,23 +328,24 @@ impl Model { }; self.reboot_at = (self.reboot && reboot_required && performed) .then(|| Instant::now() + REBOOT_DELAY); - self.log(LogSource::Inst, LogLevel::Info, self.details.clone()) + self.result_log( + LogLevel::Info, + format!("SUCCESS: {}", self.details), + Color::LightGreen, + ) } Ok(Completion::Failure(details)) => { self.uncertain = false; - self.fail(details.clone(), false); - self.log(LogSource::Inst, LogLevel::Error, details) + self.fail(details, false) } Err(error) => { let details = format!("{error:#}"); - self.fail(details.clone(), true); - self.log(LogSource::Inst, LogLevel::Error, details) + self.fail(details, true) } }, None => { let details = "Response has no body; installation outcome is unknown"; - self.fail(details.into(), true); - self.log(LogSource::Inst, LogLevel::Error, details.into()) + self.fail(details.into(), true) } }, } @@ -344,6 +368,13 @@ impl Model { } } + fn showing_logs(&self) -> bool { + matches!( + self.screen, + Screen::Progress | Screen::Menu | Screen::Success | Screen::AlreadyPresent + ) || self.screen == Screen::Error && !self.error_details_open + } + fn key(&mut self, key: KeyEvent, demo: bool) -> Option { if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { return None; @@ -407,6 +438,11 @@ impl Model { _ => None, }; } + if self.screen == Screen::Error && matches!(key.code, KeyCode::Char('d' | 'D')) { + self.error_details_open = !self.error_details_open; + self.scroll = 0; + return None; + } if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') { self.screen = if self.screen == Screen::Progress { Screen::Menu @@ -436,7 +472,7 @@ impl Model { return None; } KeyCode::PageUp => { - self.scroll = if matches!(self.screen, Screen::Progress | Screen::Menu) { + self.scroll = if self.showing_logs() { self.scroll.saturating_add(5) } else { self.scroll.saturating_sub(5) @@ -444,7 +480,7 @@ impl Model { return None; } KeyCode::PageDown => { - self.scroll = if matches!(self.screen, Screen::Progress | Screen::Menu) { + self.scroll = if self.showing_logs() { self.scroll.saturating_sub(5) } else { self.scroll.saturating_add(5) @@ -452,6 +488,11 @@ impl Model { return None; } KeyCode::Esc => { + if self.screen == Screen::Error && self.error_details_open { + self.error_details_open = false; + self.scroll = 0; + return None; + } self.screen = match self.screen { Screen::Progress => Screen::Menu, Screen::Menu => Screen::Progress, @@ -749,21 +790,31 @@ pub(super) async fn run( } if worker.as_ref().is_some_and(JoinHandle::is_finished) { if let Some(finished) = worker.take() { - match finished.await { - Ok(Ok(())) => {} + let failure = match finished.await { + Ok(Ok(())) => None, Ok(Err(error)) => { - model.fail(format!("Installer worker failed: {error:#}"), true) + Some(model.fail(format!("Installer worker failed: {error:#}"), true)) + } + Err(error) => { + Some(model.fail(format!("Installer worker terminated: {error}"), true)) } - Err(error) => model.fail(format!("Installer worker terminated: {error}"), true), + }; + if let Some(failure) = failure { + output.announce(&failure, model.serial_verbosity); } } } if shell.as_ref().is_some_and(JoinHandle::is_finished) { if let Some(finished) = shell.take() { - match finished.await { - Ok(Ok(())) => {} - Ok(Err(error)) => model.fail(format!("Shell failed: {error:#}"), false), - Err(error) => model.fail(format!("Shell task terminated: {error}"), false), + let failure = match finished.await { + Ok(Ok(())) => None, + Ok(Err(error)) => Some(model.fail(format!("Shell failed: {error:#}"), false)), + Err(error) => { + Some(model.fail(format!("Shell task terminated: {error}"), false)) + } + }; + if let Some(failure) = failure { + output.announce(&failure, model.serial_verbosity); } } if let Some(display) = &mut display { @@ -792,7 +843,10 @@ pub(super) async fn run( output.announce(&reboot, model.serial_verbosity); match systemd::reboot() { Ok(()) => return Ok(()), - Err(error) => model.fail(format!("Failed to reboot: {error:#}"), false), + Err(error) => { + let failure = model.fail(format!("Failed to reboot: {error:#}"), false); + output.announce(&failure, model.serial_verbosity); + } } } } @@ -868,7 +922,8 @@ pub(super) async fn run( } Action::Start(request, force) => { if worker.is_some() { - model.fail("An installer operation is still active. Wait for it to finish.".into(), true); + let failure = model.fail("An installer operation is still active. Wait for it to finish.".into(), true); + output.announce(&failure, model.serial_verbosity); } else { model.screen = Screen::Progress; model.activity = "Preparing installation".into(); @@ -901,7 +956,9 @@ pub(super) async fn run( ); output.announce(&action, model.serial_verbosity); if let Err(error) = systemd::reboot() { - model.fail(format!("Failed to reboot: {error:#}"), false); + let failure = + model.fail(format!("Failed to reboot: {error:#}"), false); + output.announce(&failure, model.serial_verbosity); } else { return Ok(()); } @@ -914,7 +971,9 @@ pub(super) async fn run( ); output.announce(&action, model.serial_verbosity); if let Err(error) = systemd::poweroff() { - model.fail(format!("Failed to shut down: {error:#}"), false); + let failure = model + .fail(format!("Failed to shut down: {error:#}"), false); + output.announce(&failure, model.serial_verbosity); } else { return Ok(()); } @@ -1050,6 +1109,56 @@ fn render_demo_shell(frame: &mut Frame, shell: &DemoShell) { ); } +fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { + let (title, color) = match model.screen { + Screen::Success => ("SUCCESS - Installation complete", Color::LightGreen), + Screen::Error => ("FAILURE - Installation stopped", Color::LightRed), + Screen::AlreadyPresent => ("ALREADY PRESENT - No write", Color::Yellow), + _ => ("Activity / logs", Color::DarkGray), + }; + let mut lines = Vec::new(); + let header_lines = if matches!(model.screen, Screen::Progress | Screen::Menu) { + 2 + } else { + 0 + }; + if header_lines > 0 { + lines.push(Line::from(Span::styled( + model.activity.clone(), + Style::default().fg(Color::Cyan), + ))); + lines.push(Line::default()); + } + let mut logs = model + .logs + .iter() + .filter(|entry| entry.result_color.is_some() || log_visible(entry.level, model.verbosity)) + .collect::>(); + if matches!( + model.screen, + Screen::Success | Screen::Error | Screen::AlreadyPresent + ) { + if let Some(index) = logs.iter().rposition(|entry| entry.result_color.is_some()) { + let result = logs.remove(index); + logs.push(result); + } + } + let visible = usize::from(area.height.saturating_sub(2 + header_lines)); + let end = logs + .len() + .saturating_sub(usize::from(model.scroll).min(logs.len().saturating_sub(1))); + let start = end.saturating_sub(visible); + lines.extend(logs[start..end].iter().map(|entry| entry.styled())); + let block = Block::default() + .borders(Borders::ALL) + .border_set(ASCII_BORDER) + .border_style(Style::default().fg(color)) + .title(Span::styled(title, Style::default().fg(color))); + Paragraph::new(lines) + .block(block) + .wrap(Wrap { trim: false }) +} + fn render(frame: &mut Frame, model: &Model, demo: bool) { let choices = model.choices(); let action_height = if choices.is_empty() { @@ -1076,10 +1185,11 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { areas[0], title(model.screen), demo, - if model.screen == Screen::Error { - Color::LightRed - } else { - Color::Cyan + match model.screen { + Screen::Error => Color::LightRed, + Screen::Success => Color::LightGreen, + Screen::AlreadyPresent => Color::Yellow, + _ => Color::Cyan, }, ); if areas[1].width > 22 { @@ -1102,24 +1212,17 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { ); } let content = match model.screen { - Screen::Progress | Screen::Menu => { - let mut lines = vec![Line::from(Span::styled(model.activity.clone(), Style::default().fg(Color::Cyan))), Line::default()]; - let logs = model.logs.iter().filter(|entry| log_visible(entry.level, model.verbosity)).collect::>(); - let visible = usize::from(areas[2].height.saturating_sub(4)); - let end = logs.len().saturating_sub(usize::from(model.scroll).min(logs.len().saturating_sub(1))); - let start = end.saturating_sub(visible); - lines.extend(logs[start..end].iter().map(|entry| entry.styled())); - Paragraph::new(lines).block(block("Activity / logs")).wrap(Wrap { trim: false }) - } + _ if model.showing_logs() => log_panel(model, areas[2]), Screen::StreamUrl | Screen::HostConfigUrl => Paragraph::new(format!("Enter an http:// or https:// URL:\n\n{}\n\nEnter: start Esc: back", display_text(&model.url))) .block(block("Remote source")).wrap(Wrap { trim: false }), Screen::Force => Paragraph::new("Force reinstall will erase the disk selected by Trident.\n\nExisting partitions and files will be lost.\nCancel is selected by default.") .block(block("Destructive action")).wrap(Wrap { trim: false }), - _ => Paragraph::new(display_text(&model.details)) - .block(if model.screen == Screen::Error { - block("Error details / PgUp, PgDn to scroll").border_style(Style::default().fg(Color::LightRed)) - } else { block("Result") }) + Screen::Error => Paragraph::new(display_text(&model.details)) + .block(block("Full error details / Esc to return") + .border_style(Style::default().fg(Color::LightRed))) .wrap(Wrap { trim: false }).scroll((model.scroll, 0)), + _ => Paragraph::new(display_text(&model.details)) + .block(block("Result")).wrap(Wrap { trim: false }), }; frame.render_widget(content, areas[2]); let choices = choices @@ -1153,8 +1256,13 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { "Enter: start Esc: return to recovery".to_owned() } else { format!( - "Tab/Arrows: choose Enter: select S: shell V: logs Esc: menu{}", - if demo { " Q: quit" } else { "" } + "Arrows: choose Enter: select S: shell V: logs{} Esc: back{}", + if model.screen == Screen::Error { + " D: details" + } else { + "" + }, + if demo { " Q: quit" } else { "" } ) }; frame.render_widget( @@ -1244,6 +1352,7 @@ mod tests { source: LogSource::Inst, level: LogLevel::Info, message: "Preparing installer".into(), + result_color: None, }; output.announce(&entry, LevelFilter::Debug); assert_eq!(output.mirrors.len(), 1); @@ -1437,9 +1546,11 @@ mod tests { } let text = fs::read_to_string(&serial).unwrap(); let lines = text.lines().collect::>(); - assert_eq!(lines.len(), 4); + assert_eq!(lines.len(), 8); assert!(lines.iter().all(|line| line.starts_with("00:00 [TRIDENT:"))); - assert!(lines.iter().all(|line| line.ends_with("first\\nsecond"))); + assert_eq!(lines[0], "00:00 [TRIDENT:ERROR] first"); + assert_eq!(lines[1], "00:00 [TRIDENT:ERROR] second"); + assert!(!text.contains("\\n")); let trace = model.log(LogSource::Trident, LogLevel::Trace, "trace detail".into()); output.announce(&trace, SerialVerbosity::Trace.filter()); let text = fs::read_to_string(serial).unwrap(); @@ -1498,8 +1609,111 @@ mod tests { assert!(model.failed); assert_eq!( entry.plain(), - "00:00 [INST:ERROR] No image or Host Configuration" + "00:00 [INST:ERROR] FAILURE: No image or Host Configuration" + ); + } + + #[test] + fn success_keeps_prior_logs_and_highlights_the_final_record() { + let mut model = Model::new(); + model.log( + LogSource::Trident, + LogLevel::Debug, + "Earlier disk action".into(), ); + let result = model.event(Event::Response(ServicingResponse { + response: Some(ResponseBody::Completed(Completed { + status: StatusCode::Success.into(), + reboot_status: RebootStatus::RebootNotRequired.into(), + ..Default::default() + })), + ..Default::default() + })); + assert_eq!(result.result_color, Some(Color::LightGreen)); + assert_eq!( + result.plain(), + "00:00 [INST:INFO] SUCCESS: Installation completed successfully.\n\ + 00:00 [INST:INFO] Remove the installation media before booting the installed OS." + ); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + let rows = terminal + .backend() + .buffer() + .content + .chunks(80) + .collect::>(); + let rendered = rows + .iter() + .map(|row| row.iter().map(|cell| cell.symbol()).collect::()) + .collect::>(); + let title = rendered + .iter() + .position(|row| row.contains("SUCCESS - Installation complete")) + .unwrap(); + assert_eq!( + rows[title][rendered[title].find("SUCCESS").unwrap()].fg, + Color::LightGreen + ); + let result_row = rendered + .iter() + .position(|row| row.contains("SUCCESS: Installation")) + .unwrap(); + assert_eq!( + rows[result_row][rendered[result_row].find("SUCCESS:").unwrap()].fg, + Color::LightGreen + ); + assert!(rendered + .iter() + .any(|row| row.contains("Earlier disk action"))); + model.verbosity = LevelFilter::Off; + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains("SUCCESS: Installation")); + assert!(!text.contains("Earlier disk action")); + } + + #[test] + fn failure_shows_logs_by_default_and_keeps_full_details_accessible() { + let mut model = Model::new(); + model.log(LogSource::Trident, LogLevel::Warn, "Earlier warning".into()); + let result = model.event(Event::Error { + details: "Network timeout\nUnderlying transport failure".into(), + uncertain: false, + }); + assert_eq!(result.result_color, Some(Color::LightRed)); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains("FAILURE - Installation stopped")); + assert!(text.contains("Earlier warning")); + assert!(text.contains("FAILURE: Network timeout")); + model.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE), true); + assert!(model.error_details_open); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains("Underlying transport failure")); + model.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), true); + assert_eq!(model.screen, Screen::Error); + assert!(!model.error_details_open); } #[test] diff --git a/test/README.md b/test/README.md index 1fad3028d6..453fb424b9 100644 --- a/test/README.md +++ b/test/README.md @@ -14,7 +14,8 @@ autorun chooses StreamDisk. `reboot = false` leaves the success screen visible; use its Reboot button to check what the installed disk boots. `serialMode = "logs"` routes INST and TRIDENT log records to the serial console as `MM:SS [SOURCE:LEVEL] message`, up to `serialVerbosity` (default -`debug`), independently of TUI verbosity. +`debug`), independently of TUI verbosity. Multiline messages repeat the prefix +on each line instead of printing a literal `\n`. This ISO suppresses the serial getty prompt and normal kernel/systemd status output; UEFI firmware output may still appear before the installer starts. With only a serial console active, autorun runs without a TUI and honors diff --git a/test/test_scripts.py b/test/test_scripts.py index daacfa50f3..0ef8fb6527 100644 --- a/test/test_scripts.py +++ b/test/test_scripts.py @@ -35,7 +35,8 @@ def guest(): log.write("00:00 [INST:INFO] Installer starting\r\n") log.flush() time.sleep(0.03) - log.write("00:01 [TRIDENT:TRACE] first\\nsecond\r\n") + log.write("00:01 [TRIDENT:TRACE] first\r\n") + log.write("00:01 [TRIDENT:TRACE] second\r\n") log.flush() process.finished.set() @@ -49,7 +50,8 @@ def guest(): self.assertEqual( output.getvalue(), "00:00 [INST:INFO] Installer starting\n" - "00:01 [TRIDENT:TRACE] first\\nsecond\n", + "00:01 [TRIDENT:TRACE] first\n" + "00:01 [TRIDENT:TRACE] second\n", ) def test_vnc_is_headless_and_bound_to_loopback(self): From f89e4321ea02330e081907ab0b2e3fc54eb4532f Mon Sep 17 00:00:00 2001 From: Paco Date: Sun, 4 Oct 2026 00:22:42 -0700 Subject: [PATCH 14/62] test: Assert failure result colours Verify the failure panel title and final log record render red while retaining prior logs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/ui.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 67c980bc39..c89c800ed2 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -1700,6 +1700,22 @@ mod tests { assert!(text.contains("FAILURE - Installation stopped")); assert!(text.contains("Earlier warning")); assert!(text.contains("FAILURE: Network timeout")); + for marker in ["FAILURE - Installation stopped", "FAILURE: Network timeout"] { + let row = terminal + .backend() + .buffer() + .content + .chunks(80) + .find(|row| { + row.iter() + .map(|cell| cell.symbol()) + .collect::() + .contains(marker) + }) + .unwrap(); + let content = row.iter().map(|cell| cell.symbol()).collect::(); + assert_eq!(row[content.find(marker).unwrap()].fg, Color::LightRed); + } model.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE), true); assert!(model.error_details_open); terminal.draw(|frame| render(frame, &model, true)).unwrap(); From 4b865ae669783296a129c3b8140d92a57d8e9100 Mon Sep 17 00:00:00 2001 From: Paco Date: Sun, 4 Oct 2026 10:52:49 -0700 Subject: [PATCH 15/62] feature: Add opt-in autorun stream force Keep repeat-install protection by default, remove the extra guard explanation, and allow explicit autorun.force to bypass only matching-COSI detection. Mounted and read-only disk checks remain enforced. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 9 +++++++-- crates/installer/src/client.rs | 16 +++++++++++++++- crates/installer/src/config.rs | 16 +++++++++++++++- packaging/installer.toml | 1 + test/installer/files/installer.toml | 1 + 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index f83d36ce84..d18ec167a7 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -38,6 +38,7 @@ cosiDirectory = "cosi/" # If omitted, use /etc/trident/config.yaml when that file exists. # hostConfiguration = "installer/host-configuration.yaml" reboot = true +force = false ``` Unknown fields and conflicting media selectors are errors. `interactive` is @@ -125,8 +126,12 @@ establishing that the daemon is idle. Streaming checks COSI filesystem UUIDs before writing. A complete match opens the already-present screen with a separately confirmed Force reinstall action. -It is an identity heuristic, not proof of image contents or OS health. Mounted -or read-only potential stream targets are never force-overwritten. +`autorun.force` defaults to false. Setting it to true skips the matching-UUID +check on automatic COSI streams, without weakening the mounted/read-only target +checks. It does not change HC installs or manually supplied recovery URLs. +**With force enabled, an ISO left in the boot path can reimage the machine on +every boot.** Remove or deprioritize the media after installation. +Mounted or read-only potential stream targets are never force-overwritten. **HC installation intentionally has no repeat-install/Force guard:** the HC is the operator's authority to overwrite its selected disks. Remove ISO media or correct boot order to avoid repeated HC-driven installations. diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index f35f75c48b..8a16829c9d 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -31,6 +31,10 @@ use crate::{ const CONNECTION_TIMEOUT: Duration = Duration::from_secs(30); const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); +fn stream_force_requested(automatic: bool, configured: bool, confirmed: bool) -> bool { + confirmed || (automatic && configured) +} + #[derive(Debug)] pub(super) enum Event { InstallerLog { @@ -131,6 +135,7 @@ pub(super) async fn execute( require_idle: bool, tx: Sender, ) -> Result<(), Error> { + let automatic = matches!(request, Request::Autorun); let description = match &request { Request::Autorun => format!( "Autorun: HC '{}' or first ISO COSI in '{}'", @@ -196,9 +201,10 @@ pub(super) async fn execute( ) .await?; let image = image.clone(); + let force = stream_force_requested(automatic, settings.autorun.force, force); match task::spawn_blocking(move || source::stream_preflight(&image, force)).await? { Ok(true) => return send(&tx, Event::AlreadyPresent( - "Every COSI filesystem UUID was found on an attached disk.\nNo installation was started.\nThis is an identity guard, not a health check.".into() + "Every COSI filesystem UUID was found on an attached disk.\nNo installation was started.".into() )).await, Ok(false) => {} Err(error) => return send(&tx, Event::Error { details: format!("{error:#}"), uncertain: false }).await, @@ -360,6 +366,14 @@ mod tests { type ResponseStream = Iter>>; + #[test] + fn only_automatic_streams_use_configured_force() { + assert!(!stream_force_requested(true, false, false)); + assert!(stream_force_requested(true, true, false)); + assert!(!stream_force_requested(false, true, false)); + assert!(stream_force_requested(false, false, true)); + } + fn successful() -> Completed { Completed { status: StatusCode::Success.into(), diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index 789711021b..c78b624eac 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -102,6 +102,8 @@ pub(super) struct Autorun { pub host_configuration: Option, #[serde(default = "default_reboot")] pub reboot: bool, + #[serde(default)] + pub force: bool, } impl Default for Autorun { @@ -109,6 +111,7 @@ impl Default for Autorun { Self { host_configuration: None, reboot: default_reboot(), + force: false, } } } @@ -283,6 +286,7 @@ mod tests { assert_eq!(config.serial_mode, SerialMode::Logs); assert_eq!(config.serial_verbosity, SerialVerbosity::Debug); assert!(config.autorun.reboot); + assert!(!config.autorun.force); assert_eq!(config.media.cosi_directory, Path::new("cosi")); Config::parse("mode = 'interactive'") .unwrap() @@ -290,6 +294,15 @@ mod tests { .unwrap_err(); } + #[test] + fn autorun_force_is_opt_in() { + let config = Config::parse("mode = 'autorun'\n[autorun]\nforce = true").unwrap(); + assert!(config.autorun.force); + let config = Config::parse("mode = 'autorun'\n[autorun]\nforce = false").unwrap(); + assert!(!config.autorun.force); + Config::parse("mode = 'autorun'\n[autorun]\nforce = 'yes'").unwrap_err(); + } + #[test] fn serial_ui_mode_is_reserved_not_silently_enabled() { Config::parse("mode = 'autorun'\nserialMode = 'logs'").unwrap(); @@ -358,12 +371,13 @@ mod tests { let root = TempDir::new().unwrap(); fs::create_dir(root.path().join("installer")).unwrap(); let path = root.path().join(MEDIA_CONFIG); - fs::write(&path, "[autorun]\nreboot = false").unwrap(); + fs::write(&path, "[autorun]\nreboot = false\nforce = true").unwrap(); let config = Config::parse("mode = 'autorun'") .unwrap() .overlay(root.path()) .unwrap(); assert!(!config.autorun.reboot); + assert!(config.autorun.force); assert_eq!(config.media.cosi_directory, Path::new("cosi")); fs::write(&path, "[media]\ncdromLabel = 'OTHER'").unwrap(); config.overlay(root.path()).unwrap_err(); diff --git a/packaging/installer.toml b/packaging/installer.toml index 4a736d3ab4..3d92d95d07 100644 --- a/packaging/installer.toml +++ b/packaging/installer.toml @@ -8,3 +8,4 @@ cosiDirectory = "cosi/" [autorun] reboot = true +force = false diff --git a/test/installer/files/installer.toml b/test/installer/files/installer.toml index 7d71bf62fb..d25f0b3e15 100644 --- a/test/installer/files/installer.toml +++ b/test/installer/files/installer.toml @@ -8,3 +8,4 @@ cosiDirectory = "cosi/" [autorun] reboot = false +force = false From 41f706bd6cb14b8dea7305555971ea14bb132aaa Mon Sep 17 00:00:00 2001 From: Paco Date: Sun, 4 Oct 2026 11:13:43 -0700 Subject: [PATCH 16/62] feature: Apply clear neutrals installer palette Use the selected Option A1 palette for the wordmark, source labels, severity hierarchy, result frames and terminal background while retaining text-based status indicators. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 6 +- crates/installer/src/ui.rs | 122 +++++++++++++++++++++++-------------- 2 files changed, 79 insertions(+), 49 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index d18ec167a7..faace24c51 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -114,8 +114,10 @@ and daemon records use `MM:SS [INST:LEVEL] message` and `MM:SS [TRIDENT:LEVEL] message`, respectively. `serialVerbosity` controls the serial stream independently of TUI verbosity; each line of a multiline message gets its own `MM:SS [SOURCE:LEVEL]` prefix. The TUI defaults to Debug: errors red, warnings -orange, info bright blue, debug purple, trace gray; installer source labels -are magenta and Trident labels green. `V` opens a live verbosity picker (Off, +amber, INFO pale neutral, DEBUG steel gray and TRACE slate gray; INST labels +are muted slate and TRIDENT labels seafoam. The wordmark remains cyan and +success is green. Terminals with limited palettes approximate these colours. +`V` opens a live verbosity picker (Off, Error, Warn, Info, Debug, Trace) beside the operation; this only filters the display, not serial or stored diagnostics. The private diagnostic file retains all levels even when serialVerbosity filters them. PgUp/PgDn scroll. diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index c89c800ed2..52f70d1bef 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -47,6 +47,17 @@ const EVENT_CAPACITY: usize = 64; const LOG_CAPACITY: usize = 500; const DISPLAY_LOG_CHARACTERS: usize = 4096; const DEMO_SHELL_HISTORY: usize = 8; +const COLOR_BACKGROUND: Color = Color::Rgb(11, 17, 24); +const COLOR_FRAME: Color = Color::Rgb(53, 69, 83); +const COLOR_BRAND: Color = Color::Rgb(75, 216, 220); +const COLOR_INST: Color = Color::Rgb(171, 186, 196); +const COLOR_TRIDENT: Color = Color::Rgb(114, 185, 165); +const COLOR_ERROR: Color = Color::Rgb(255, 107, 115); +const COLOR_WARN: Color = Color::Rgb(245, 182, 93); +const COLOR_INFO: Color = Color::Rgb(229, 234, 236); +const COLOR_DEBUG: Color = Color::Rgb(167, 180, 189); +const COLOR_TRACE: Color = Color::Rgb(99, 115, 129); +const COLOR_SUCCESS: Color = Color::Rgb(93, 200, 148); const WORDMARK_HEADER_HEIGHT: u16 = 12; const VERBOSITY_LEVELS: [LevelFilter; 6] = [ LevelFilter::Off, @@ -109,8 +120,8 @@ impl LogSource { fn color(self) -> Color { match self { - Self::Inst => Color::LightMagenta, - Self::Trident => Color::LightGreen, + Self::Inst => COLOR_INST, + Self::Trident => COLOR_TRIDENT, } } } @@ -216,11 +227,7 @@ impl Model { self.selected = 0; self.screen = Screen::Error; self.error_details_open = false; - self.result_log( - LogLevel::Error, - format!("FAILURE: {details}"), - Color::LightRed, - ) + self.result_log(LogLevel::Error, format!("FAILURE: {details}"), COLOR_ERROR) } fn log(&mut self, source: LogSource, level: LogLevel, message: String) -> LogEntry { @@ -296,7 +303,7 @@ impl Model { self.result_log( LogLevel::Warn, format!("ALREADY PRESENT: {details}"), - Color::Yellow, + COLOR_WARN, ) } Event::Error { details, uncertain } => self.fail(details, uncertain), @@ -331,7 +338,7 @@ impl Model { self.result_log( LogLevel::Info, format!("SUCCESS: {}", self.details), - Color::LightGreen, + COLOR_SUCCESS, ) } Ok(Completion::Failure(details)) => { @@ -1021,11 +1028,11 @@ fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { fn log_color(level: LogLevel) -> Color { match level { - LogLevel::Error => Color::LightRed, - LogLevel::Warn | LogLevel::Unspecified => Color::Rgb(255, 165, 0), - LogLevel::Info => Color::LightBlue, - LogLevel::Debug => Color::Rgb(170, 100, 255), - LogLevel::Trace => Color::Gray, + LogLevel::Error => COLOR_ERROR, + LogLevel::Warn | LogLevel::Unspecified => COLOR_WARN, + LogLevel::Info => COLOR_INFO, + LogLevel::Debug => COLOR_DEBUG, + LogLevel::Trace => COLOR_TRACE, } } @@ -1047,7 +1054,8 @@ fn block(title: &str) -> Block<'_> { Block::default() .borders(Borders::ALL) .border_set(ASCII_BORDER) - .border_style(Style::default().fg(Color::DarkGray)) + .style(Style::default().bg(COLOR_BACKGROUND)) + .border_style(Style::default().fg(COLOR_FRAME)) .title(title) } @@ -1069,7 +1077,7 @@ fn header_height(viewport: Rect) -> u16 { fn render_header(frame: &mut Frame, area: Rect, state: &str, demo: bool, state_color: Color) { let brand = Style::default() - .fg(Color::Cyan) + .fg(COLOR_BRAND) .add_modifier(Modifier::BOLD); let mut lines = Vec::new(); if area.height >= WORDMARK_HEADER_HEIGHT { @@ -1087,7 +1095,7 @@ fn render_header(frame: &mut Frame, area: Rect, state: &str, demo: bool, state_c if demo { status.push(Span::styled( " / DEMO: ALL ACTIONS SIMULATED", - Style::default().fg(Color::Cyan), + Style::default().fg(COLOR_BRAND), )); } lines.push(Line::from(status)); @@ -1095,12 +1103,16 @@ fn render_header(frame: &mut Frame, area: Rect, state: &str, demo: bool, state_c } fn render_demo_shell(frame: &mut Frame, shell: &DemoShell) { + frame.render_widget( + Block::default().style(Style::default().bg(COLOR_BACKGROUND)), + frame.area(), + ); let areas = Layout::vertical([ Constraint::Length(header_height(frame.area())), Constraint::Min(4), ]) .split(frame.area()); - render_header(frame, areas[0], "SIMULATED SHELL", true, Color::Cyan); + render_header(frame, areas[0], "SIMULATED SHELL", true, COLOR_BRAND); frame.render_widget( Paragraph::new(shell.text()) .block(block("No commands are executed")) @@ -1111,10 +1123,10 @@ fn render_demo_shell(frame: &mut Frame, shell: &DemoShell) { fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { let (title, color) = match model.screen { - Screen::Success => ("SUCCESS - Installation complete", Color::LightGreen), - Screen::Error => ("FAILURE - Installation stopped", Color::LightRed), - Screen::AlreadyPresent => ("ALREADY PRESENT - No write", Color::Yellow), - _ => ("Activity / logs", Color::DarkGray), + Screen::Success => ("SUCCESS - Installation complete", COLOR_SUCCESS), + Screen::Error => ("FAILURE - Installation stopped", COLOR_ERROR), + Screen::AlreadyPresent => ("ALREADY PRESENT - No write", COLOR_WARN), + _ => ("Activity / logs", COLOR_FRAME), }; let mut lines = Vec::new(); let header_lines = if matches!(model.screen, Screen::Progress | Screen::Menu) { @@ -1125,7 +1137,7 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { if header_lines > 0 { lines.push(Line::from(Span::styled( model.activity.clone(), - Style::default().fg(Color::Cyan), + Style::default().fg(COLOR_BRAND), ))); lines.push(Line::default()); } @@ -1152,6 +1164,7 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { let block = Block::default() .borders(Borders::ALL) .border_set(ASCII_BORDER) + .style(Style::default().bg(COLOR_BACKGROUND)) .border_style(Style::default().fg(color)) .title(Span::styled(title, Style::default().fg(color))); Paragraph::new(lines) @@ -1160,6 +1173,10 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { } fn render(frame: &mut Frame, model: &Model, demo: bool) { + frame.render_widget( + Block::default().style(Style::default().bg(COLOR_BACKGROUND)), + frame.area(), + ); let choices = model.choices(); let action_height = if choices.is_empty() { 0 @@ -1186,10 +1203,10 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { title(model.screen), demo, match model.screen { - Screen::Error => Color::LightRed, - Screen::Success => Color::LightGreen, - Screen::AlreadyPresent => Color::Yellow, - _ => Color::Cyan, + Screen::Error => COLOR_ERROR, + Screen::Success => COLOR_SUCCESS, + Screen::AlreadyPresent => COLOR_WARN, + _ => COLOR_BRAND, }, ); if areas[1].width > 22 { @@ -1202,7 +1219,7 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { frame.render_widget( Paragraph::new(format!("Logs: {} [V]", model.verbosity)) .alignment(Alignment::Right) - .style(Style::default().fg(Color::Cyan)), + .style(Style::default().fg(COLOR_BRAND)), operation[1], ); } else { @@ -1219,7 +1236,7 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { .block(block("Destructive action")).wrap(Wrap { trim: false }), Screen::Error => Paragraph::new(display_text(&model.details)) .block(block("Full error details / Esc to return") - .border_style(Style::default().fg(Color::LightRed))) + .border_style(Style::default().fg(COLOR_ERROR))) .wrap(Wrap { trim: false }).scroll((model.scroll, 0)), _ => Paragraph::new(display_text(&model.details)) .block(block("Result")).wrap(Wrap { trim: false }), @@ -1235,7 +1252,7 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { )) .style(if index == model.selected { Style::default() - .fg(Color::Cyan) + .fg(COLOR_BRAND) .add_modifier(Modifier::BOLD) } else { Style::default() @@ -1266,7 +1283,7 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { ) }; frame.render_widget( - Paragraph::new(format!("{controls}\n{countdown}")).style(Style::default().fg(Color::Cyan)), + Paragraph::new(format!("{controls}\n{countdown}")).style(Style::default().fg(COLOR_BRAND)), areas[4], ); if model.verbosity_open { @@ -1300,7 +1317,7 @@ fn render_verbosity_picker(frame: &mut Frame, model: &Model) { ), if index == model.verbosity_selected { Style::default() - .fg(Color::Cyan) + .fg(COLOR_BRAND) .add_modifier(Modifier::BOLD) } else { Style::default() @@ -1408,12 +1425,14 @@ mod tests { } #[test] - fn wordmark_is_cyan_and_preserved_at_80_by_24() { + fn wordmark_and_background_use_clear_neutrals_palette_at_80_by_24() { let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); terminal .draw(|frame| render(frame, &Model::new(), true)) .unwrap(); let buffer = terminal.backend().buffer(); + assert_eq!(buffer.content[0].bg, Color::Rgb(11, 17, 24)); + assert_eq!(buffer.content[14 * 80].fg, Color::Rgb(53, 69, 83)); let padding = (80 - wordmark_width()) / 2; assert!(buffer.content[..80].iter().all(|cell| cell.symbol() == " ")); for (row_index, expected) in TRIDENT_WORDMARK.iter().enumerate() { @@ -1424,7 +1443,10 @@ mod tests { .collect::(); assert_eq!(&row[padding..padding + expected.len()], *expected); for column in padding..padding + expected.len() { - assert_eq!(buffer.content[row_index * 80 + column].fg, Color::Cyan); + assert_eq!( + buffer.content[row_index * 80 + column].fg, + Color::Rgb(75, 216, 220) + ); } assert!(buffer.content[9 * 80..10 * 80] .iter() @@ -1481,11 +1503,11 @@ mod tests { let mut model = Model::new(); model.verbosity = LevelFilter::Trace; let records = [ - (LogLevel::Error, "error-line", Color::LightRed), - (LogLevel::Warn, "warn-line", Color::Rgb(255, 165, 0)), - (LogLevel::Info, "info-line", Color::LightBlue), - (LogLevel::Debug, "debug-line", Color::Rgb(170, 100, 255)), - (LogLevel::Trace, "trace-line", Color::Gray), + (LogLevel::Error, "error-line", Color::Rgb(255, 107, 115)), + (LogLevel::Warn, "warn-line", Color::Rgb(245, 182, 93)), + (LogLevel::Info, "info-line", Color::Rgb(229, 234, 236)), + (LogLevel::Debug, "debug-line", Color::Rgb(167, 180, 189)), + (LogLevel::Trace, "trace-line", Color::Rgb(99, 115, 129)), ]; for (level, text, _) in records { model.log(LogSource::Trident, level, text.into()); @@ -1507,7 +1529,7 @@ mod tests { let text = row.iter().map(|cell| cell.symbol()).collect::(); assert_eq!(row[text.find(message).unwrap()].fg, expected); let source = text.find("TRIDENT").unwrap(); - assert_eq!(row[source].fg, Color::LightGreen); + assert_eq!(row[source].fg, Color::Rgb(114, 185, 165)); } } @@ -1575,10 +1597,13 @@ mod tests { entry.plain(), "00:00 [INST:DEBUG] Loaded installer settings" ); - assert_eq!(entry.styled().spans[1].style.fg, Some(Color::LightMagenta)); + assert_eq!( + entry.styled().spans[1].style.fg, + Some(Color::Rgb(171, 186, 196)) + ); assert_eq!( entry.styled().spans[3].style.fg, - Some(Color::Rgb(170, 100, 255)) + Some(Color::Rgb(167, 180, 189)) ); assert_eq!(model.logs.len(), 1); } @@ -1629,7 +1654,7 @@ mod tests { })), ..Default::default() })); - assert_eq!(result.result_color, Some(Color::LightGreen)); + assert_eq!(result.result_color, Some(Color::Rgb(93, 200, 148))); assert_eq!( result.plain(), "00:00 [INST:INFO] SUCCESS: Installation completed successfully.\n\ @@ -1653,7 +1678,7 @@ mod tests { .unwrap(); assert_eq!( rows[title][rendered[title].find("SUCCESS").unwrap()].fg, - Color::LightGreen + Color::Rgb(93, 200, 148) ); let result_row = rendered .iter() @@ -1661,7 +1686,7 @@ mod tests { .unwrap(); assert_eq!( rows[result_row][rendered[result_row].find("SUCCESS:").unwrap()].fg, - Color::LightGreen + Color::Rgb(93, 200, 148) ); assert!(rendered .iter() @@ -1687,7 +1712,7 @@ mod tests { details: "Network timeout\nUnderlying transport failure".into(), uncertain: false, }); - assert_eq!(result.result_color, Some(Color::LightRed)); + assert_eq!(result.result_color, Some(Color::Rgb(255, 107, 115))); let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); terminal.draw(|frame| render(frame, &model, true)).unwrap(); let text = terminal @@ -1714,7 +1739,10 @@ mod tests { }) .unwrap(); let content = row.iter().map(|cell| cell.symbol()).collect::(); - assert_eq!(row[content.find(marker).unwrap()].fg, Color::LightRed); + assert_eq!( + row[content.find(marker).unwrap()].fg, + Color::Rgb(255, 107, 115) + ); } model.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE), true); assert!(model.error_details_open); From eb9c25d7469be8d35ceff5bc85ba046d5fbf74bc Mon Sep 17 00:00:00 2001 From: Paco Date: Sun, 4 Oct 2026 15:54:02 -0700 Subject: [PATCH 17/62] feature: Render A1 colours with programmable Linux VT palette Map installer roles to ANSI slots on tty1, program their A1 RGB values, and restore console defaults on shell suspension and exit. Preserve direct RGB rendering on other terminals. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 5 +- crates/installer/src/main.rs | 4 +- crates/installer/src/ui.rs | 214 ++++++++++++++++++++++++++------- crates/osutils/src/terminal.rs | 31 ++++- 4 files changed, 209 insertions(+), 45 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index faace24c51..201f0a655c 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -116,7 +116,10 @@ serial stream independently of TUI verbosity; each line of a multiline message gets its own `MM:SS [SOURCE:LEVEL]` prefix. The TUI defaults to Debug: errors red, warnings amber, INFO pale neutral, DEBUG steel gray and TRACE slate gray; INST labels are muted slate and TRIDENT labels seafoam. The wordmark remains cyan and -success is green. Terminals with limited palettes approximate these colours. +success is green. On the graphical Linux virtual terminal, the installer +programs its 16 ANSI palette slots to A1 RGB values, then restores the +original palette when opening a shell or leaving the UI. Workstation demo +terminals use the RGB values directly. `V` opens a live verbosity picker (Off, Error, Warn, Info, Debug, Trace) beside the operation; this only filters the display, not serial or stored diagnostics. The private diagnostic file retains diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index 6a39151039..40ebd64951 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -87,6 +87,7 @@ fn main() -> ExitCode { } async fn run(args: Args) -> Result<(), Error> { + let mut linux_vt = false; let mut output = Output { log: None, mirrors: Vec::new(), @@ -106,6 +107,7 @@ async fn run(args: Args) -> Result<(), Error> { if let Some(graphical) = consoles.iter().find(|path| path.ends_with("tty1")) { systemd::stop_unit("getty@tty1.service")?; let _terminal = terminal::attach(graphical)?; + linux_vt = true; } for path in consoles.iter().filter(|path| !path.ends_with("tty1")) { match OpenOptions::new().write(true).open(path) { @@ -116,5 +118,5 @@ async fn run(args: Args) -> Result<(), Error> { } } let config_path = args.demo.is_none().then_some(args.config); - ui::run(config_path, args.demo, args.plain, output).await + ui::run(config_path, args.demo, args.plain, linux_vt, output).await } diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 52f70d1bef..33e9b62987 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -58,6 +58,44 @@ const COLOR_INFO: Color = Color::Rgb(229, 234, 236); const COLOR_DEBUG: Color = Color::Rgb(167, 180, 189); const COLOR_TRACE: Color = Color::Rgb(99, 115, 129); const COLOR_SUCCESS: Color = Color::Rgb(93, 200, 148); +// Linux VT maps RGB SGR values into 16 colours, so give each UI role its own ANSI slot. +const VT_PALETTE: &[(u8, Color, Color)] = &[ + (0, COLOR_BACKGROUND, Color::Black), + (1, COLOR_ERROR, Color::Red), + (2, COLOR_TRIDENT, Color::Green), + (3, COLOR_WARN, Color::Yellow), + (4, COLOR_FRAME, Color::Blue), + (5, COLOR_INST, Color::Magenta), + (6, COLOR_BRAND, Color::Cyan), + (7, COLOR_DEBUG, Color::Gray), + (8, COLOR_TRACE, Color::DarkGray), + (10, COLOR_SUCCESS, Color::LightGreen), + (15, COLOR_INFO, Color::White), +]; + +fn theme_color(color: Color, linux_vt: bool) -> Color { + if !linux_vt { + return color; + } + VT_PALETTE + .iter() + .find(|(_, rgb, _)| *rgb == color) + .map(|(_, _, ansi)| *ansi) + .unwrap_or(color) +} + +fn vt_rgb_palette() -> Result, io::Error> { + VT_PALETTE + .iter() + .map(|(slot, color, _)| match color { + Color::Rgb(red, green, blue) => Ok((*slot, *red, *green, *blue)), + other => Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("Linux VT palette requires RGB values, got {other:?}"), + )), + }) + .collect() +} const WORDMARK_HEADER_HEIGHT: u16 = 12; const VERBOSITY_LEVELS: [LevelFilter; 6] = [ LevelFilter::Off, @@ -118,11 +156,14 @@ impl LogSource { } } - fn color(self) -> Color { - match self { - Self::Inst => COLOR_INST, - Self::Trident => COLOR_TRIDENT, - } + fn color(self, linux_vt: bool) -> Color { + theme_color( + match self { + Self::Inst => COLOR_INST, + Self::Trident => COLOR_TRIDENT, + }, + linux_vt, + ) } } @@ -150,17 +191,20 @@ impl LogEntry { .join("\n") } - fn styled(&self) -> Line<'_> { - let level = Style::default().fg(self.result_color.unwrap_or(log_color(self.level))); + fn styled(&self, linux_vt: bool) -> Line<'_> { + let level = Style::default().fg(theme_color( + self.result_color.unwrap_or(log_color(self.level)), + linux_vt, + )); let surrounding = self .result_color - .map(|color| Style::default().fg(color)) + .map(|color| Style::default().fg(theme_color(color, linux_vt))) .unwrap_or_default(); Line::from(vec![ Span::styled(self.prefix(), surrounding), Span::styled( self.source.label(), - Style::default().fg(self.source.color()), + Style::default().fg(self.source.color(linux_vt)), ), Span::styled(":", surrounding), Span::styled(self.level.to_string(), level.add_modifier(Modifier::BOLD)), @@ -191,6 +235,7 @@ struct Model { started: Instant, failed: bool, error_details_open: bool, + linux_vt: bool, } impl Model { @@ -215,6 +260,7 @@ impl Model { started: Instant::now(), failed: false, error_details_open: false, + linux_vt: false, } } @@ -664,13 +710,15 @@ impl Output { struct Display { terminal: Terminal>, active: bool, + linux_vt: bool, } impl Display { - fn new() -> Result { + fn new(linux_vt: bool) -> Result { let mut display = Self { terminal: Terminal::new(CrosstermBackend::new(io::stdout()))?, active: false, + linux_vt, }; display.resume()?; Ok(display) @@ -679,16 +727,29 @@ impl Display { fn resume(&mut self) -> Result<(), Error> { terminal::enable_raw_mode()?; self.active = true; + if self.linux_vt { + os_terminal::set_vt_palette(&mut io::stdout(), &vt_rgb_palette()?)?; + } execute!(self.terminal.backend_mut(), EnterAlternateScreen)?; self.terminal.clear()?; Ok(()) } fn suspend(&mut self) -> Result<(), Error> { - terminal::disable_raw_mode()?; - execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; - self.terminal.show_cursor()?; + let restore = (|| -> Result<(), Error> { + terminal::disable_raw_mode()?; + execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; + self.terminal.show_cursor()?; + Ok(()) + })(); + let palette = if self.linux_vt { + os_terminal::reset_vt_palette(&mut io::stdout()) + } else { + Ok(()) + }; self.active = false; + restore?; + palette?; Ok(()) } } @@ -739,6 +800,7 @@ pub(super) async fn run( config_path: Option, scenario: Option, plain: bool, + linux_vt: bool, mut output: Output, ) -> Result<(), Error> { let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); @@ -749,6 +811,7 @@ pub(super) async fn run( let mut interrupts = unix::signal(SignalKind::interrupt())?; let mut termination = unix::signal(SignalKind::terminate())?; let mut model = Model::new(); + model.linux_vt = linux_vt; let mut worker = Some(spawn_operation( config_path.clone(), Request::Autorun, @@ -759,7 +822,7 @@ pub(super) async fn run( )); let interactive = io::stdin().is_terminal() && io::stdout().is_terminal(); let mut display = if interactive && !plain { - Some(Display::new()?) + Some(Display::new(linux_vt)?) } else { None }; @@ -1050,12 +1113,12 @@ fn title(screen: Screen) -> &'static str { } } -fn block(title: &str) -> Block<'_> { +fn block(title: &str, linux_vt: bool) -> Block<'_> { Block::default() .borders(Borders::ALL) .border_set(ASCII_BORDER) - .style(Style::default().bg(COLOR_BACKGROUND)) - .border_style(Style::default().fg(COLOR_FRAME)) + .style(Style::default().bg(theme_color(COLOR_BACKGROUND, linux_vt))) + .border_style(Style::default().fg(theme_color(COLOR_FRAME, linux_vt))) .title(title) } @@ -1075,9 +1138,16 @@ fn header_height(viewport: Rect) -> u16 { } } -fn render_header(frame: &mut Frame, area: Rect, state: &str, demo: bool, state_color: Color) { +fn render_header( + frame: &mut Frame, + area: Rect, + state: &str, + demo: bool, + state_color: Color, + linux_vt: bool, +) { let brand = Style::default() - .fg(COLOR_BRAND) + .fg(theme_color(COLOR_BRAND, linux_vt)) .add_modifier(Modifier::BOLD); let mut lines = Vec::new(); if area.height >= WORDMARK_HEADER_HEIGHT { @@ -1091,11 +1161,14 @@ fn render_header(frame: &mut Frame, area: Rect, state: &str, demo: bool, state_c lines.push(Line::default()); } lines.push(Line::from(Span::styled(APPLICATION_NAME, brand))); - let mut status = vec![Span::styled(state, Style::default().fg(state_color))]; + let mut status = vec![Span::styled( + state, + Style::default().fg(theme_color(state_color, linux_vt)), + )]; if demo { status.push(Span::styled( " / DEMO: ALL ACTIONS SIMULATED", - Style::default().fg(COLOR_BRAND), + Style::default().fg(theme_color(COLOR_BRAND, linux_vt)), )); } lines.push(Line::from(status)); @@ -1112,10 +1185,10 @@ fn render_demo_shell(frame: &mut Frame, shell: &DemoShell) { Constraint::Min(4), ]) .split(frame.area()); - render_header(frame, areas[0], "SIMULATED SHELL", true, COLOR_BRAND); + render_header(frame, areas[0], "SIMULATED SHELL", true, COLOR_BRAND, false); frame.render_widget( Paragraph::new(shell.text()) - .block(block("No commands are executed")) + .block(block("No commands are executed", false)) .wrap(Wrap { trim: false }), areas[1], ); @@ -1137,7 +1210,7 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { if header_lines > 0 { lines.push(Line::from(Span::styled( model.activity.clone(), - Style::default().fg(COLOR_BRAND), + Style::default().fg(theme_color(COLOR_BRAND, model.linux_vt)), ))); lines.push(Line::default()); } @@ -1160,13 +1233,20 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { .len() .saturating_sub(usize::from(model.scroll).min(logs.len().saturating_sub(1))); let start = end.saturating_sub(visible); - lines.extend(logs[start..end].iter().map(|entry| entry.styled())); + lines.extend( + logs[start..end] + .iter() + .map(|entry| entry.styled(model.linux_vt)), + ); let block = Block::default() .borders(Borders::ALL) .border_set(ASCII_BORDER) - .style(Style::default().bg(COLOR_BACKGROUND)) - .border_style(Style::default().fg(color)) - .title(Span::styled(title, Style::default().fg(color))); + .style(Style::default().bg(theme_color(COLOR_BACKGROUND, model.linux_vt))) + .border_style(Style::default().fg(theme_color(color, model.linux_vt))) + .title(Span::styled( + title, + Style::default().fg(theme_color(color, model.linux_vt)), + )); Paragraph::new(lines) .block(block) .wrap(Wrap { trim: false }) @@ -1174,7 +1254,7 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { fn render(frame: &mut Frame, model: &Model, demo: bool) { frame.render_widget( - Block::default().style(Style::default().bg(COLOR_BACKGROUND)), + Block::default().style(Style::default().bg(theme_color(COLOR_BACKGROUND, model.linux_vt))), frame.area(), ); let choices = model.choices(); @@ -1208,6 +1288,7 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { Screen::AlreadyPresent => COLOR_WARN, _ => COLOR_BRAND, }, + model.linux_vt, ); if areas[1].width > 22 { let operation = @@ -1219,7 +1300,7 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { frame.render_widget( Paragraph::new(format!("Logs: {} [V]", model.verbosity)) .alignment(Alignment::Right) - .style(Style::default().fg(COLOR_BRAND)), + .style(Style::default().fg(theme_color(COLOR_BRAND, model.linux_vt))), operation[1], ); } else { @@ -1231,15 +1312,15 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { let content = match model.screen { _ if model.showing_logs() => log_panel(model, areas[2]), Screen::StreamUrl | Screen::HostConfigUrl => Paragraph::new(format!("Enter an http:// or https:// URL:\n\n{}\n\nEnter: start Esc: back", display_text(&model.url))) - .block(block("Remote source")).wrap(Wrap { trim: false }), + .block(block("Remote source", model.linux_vt)).wrap(Wrap { trim: false }), Screen::Force => Paragraph::new("Force reinstall will erase the disk selected by Trident.\n\nExisting partitions and files will be lost.\nCancel is selected by default.") - .block(block("Destructive action")).wrap(Wrap { trim: false }), + .block(block("Destructive action", model.linux_vt)).wrap(Wrap { trim: false }), Screen::Error => Paragraph::new(display_text(&model.details)) - .block(block("Full error details / Esc to return") - .border_style(Style::default().fg(COLOR_ERROR))) + .block(block("Full error details / Esc to return", model.linux_vt) + .border_style(Style::default().fg(theme_color(COLOR_ERROR, model.linux_vt)))) .wrap(Wrap { trim: false }).scroll((model.scroll, 0)), _ => Paragraph::new(display_text(&model.details)) - .block(block("Result")).wrap(Wrap { trim: false }), + .block(block("Result", model.linux_vt)).wrap(Wrap { trim: false }), }; frame.render_widget(content, areas[2]); let choices = choices @@ -1252,14 +1333,17 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { )) .style(if index == model.selected { Style::default() - .fg(COLOR_BRAND) + .fg(theme_color(COLOR_BRAND, model.linux_vt)) .add_modifier(Modifier::BOLD) } else { Style::default() }) }) .collect::>(); - frame.render_widget(List::new(choices).block(block("Actions")), areas[3]); + frame.render_widget( + List::new(choices).block(block("Actions", model.linux_vt)), + areas[3], + ); let countdown = model .reboot_at .map(|deadline| { @@ -1283,7 +1367,8 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { ) }; frame.render_widget( - Paragraph::new(format!("{controls}\n{countdown}")).style(Style::default().fg(COLOR_BRAND)), + Paragraph::new(format!("{controls}\n{countdown}")) + .style(Style::default().fg(theme_color(COLOR_BRAND, model.linux_vt))), areas[4], ); if model.verbosity_open { @@ -1317,7 +1402,7 @@ fn render_verbosity_picker(frame: &mut Frame, model: &Model) { ), if index == model.verbosity_selected { Style::default() - .fg(COLOR_BRAND) + .fg(theme_color(COLOR_BRAND, model.linux_vt)) .add_modifier(Modifier::BOLD) } else { Style::default() @@ -1327,7 +1412,7 @@ fn render_verbosity_picker(frame: &mut Frame, model: &Model) { .chain([Line::default(), Line::from("Enter: apply Esc: cancel")]) .collect::>(); frame.render_widget( - Paragraph::new(rows).block(block("Log verbosity / display only")), + Paragraph::new(rows).block(block("Log verbosity / display only", model.linux_vt)), area, ); } @@ -1533,6 +1618,51 @@ mod tests { } } + #[test] + fn linux_vt_palette_uses_distinct_slots_for_a1_log_levels() { + let entries = vt_rgb_palette().unwrap(); + assert!(entries.contains(&(15, 229, 234, 236))); + assert!(entries.contains(&(7, 167, 180, 189))); + assert!(entries.contains(&(8, 99, 115, 129))); + assert_eq!(theme_color(COLOR_INFO, true), Color::White); + assert_eq!(theme_color(COLOR_DEBUG, true), Color::Gray); + assert_eq!(theme_color(COLOR_TRACE, true), Color::DarkGray); + assert_eq!(theme_color(COLOR_SUCCESS, true), Color::LightGreen); + assert_eq!(theme_color(COLOR_DEBUG, false), Color::Rgb(167, 180, 189)); + + let mut model = Model::new(); + model.linux_vt = true; + model.verbosity = LevelFilter::Trace; + for (level, message) in [ + (LogLevel::Info, "info-vt"), + (LogLevel::Debug, "debug-vt"), + (LogLevel::Trace, "trace-vt"), + ] { + model.log(LogSource::Trident, level, message.into()); + } + let mut terminal = Terminal::new(TestBackend::new(80, 32)).unwrap(); + terminal.draw(|frame| render(frame, &model, false)).unwrap(); + let buffer = terminal.backend().buffer(); + for (message, expected) in [ + ("info-vt", Color::White), + ("debug-vt", Color::Gray), + ("trace-vt", Color::DarkGray), + ] { + let row = buffer + .content + .chunks(80) + .find(|row| { + row.iter() + .map(|cell| cell.symbol()) + .collect::() + .contains(message) + }) + .unwrap(); + let text = row.iter().map(|cell| cell.symbol()).collect::(); + assert_eq!(row[text.find(message).unwrap()].fg, expected); + } + } + #[test] fn operation_and_verbosity_share_one_line() { let mut model = Model::new(); @@ -1598,11 +1728,11 @@ mod tests { "00:00 [INST:DEBUG] Loaded installer settings" ); assert_eq!( - entry.styled().spans[1].style.fg, + entry.styled(false).spans[1].style.fg, Some(Color::Rgb(171, 186, 196)) ); assert_eq!( - entry.styled().spans[3].style.fg, + entry.styled(false).spans[3].style.fg, Some(Color::Rgb(167, 180, 189)) ); assert_eq!(model.logs.len(), 1); diff --git a/crates/osutils/src/terminal.rs b/crates/osutils/src/terminal.rs index 80d7f42bf5..16a5d4d390 100644 --- a/crates/osutils/src/terminal.rs +++ b/crates/osutils/src/terminal.rs @@ -1,6 +1,6 @@ use std::{ fs::{self, File, OpenOptions}, - io::{self, IsTerminal}, + io::{self, IsTerminal, Write}, os::{fd::AsRawFd, unix::fs::FileTypeExt}, path::{Path, PathBuf}, }; @@ -16,6 +16,26 @@ use crate::dependencies::Dependency; const ACTIVE_CONSOLES_PATH: &str = "/sys/class/tty/console/active"; const GRAPHICAL_CONSOLE: &str = "tty1"; +const VT_PALETTE_SIZE: u8 = 16; + +/// Sets RGB values for the Linux virtual console's 16 ANSI colour slots. +pub fn set_vt_palette(output: &mut impl Write, colours: &[(u8, u8, u8, u8)]) -> io::Result<()> { + for &(slot, red, green, blue) in colours { + if slot >= VT_PALETTE_SIZE { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("Invalid Linux VT palette slot {slot}"), + )); + } + write!(output, "\x1b]P{slot:x}{red:02x}{green:02x}{blue:02x}")?; + } + output.flush() +} + +pub fn reset_vt_palette(output: &mut impl Write) -> io::Result<()> { + output.write_all(b"\x1b]R")?; + output.flush() +} pub fn active_consoles() -> Result, Error> { let active = fs::read_to_string(ACTIVE_CONSOLES_PATH) @@ -123,4 +143,13 @@ mod tests { console_paths("../tty1").unwrap_err(); console_paths("").unwrap_err(); } + + #[test] + fn linux_vt_palette_sequences_set_rgb_slots_and_reset() { + let mut output = Vec::new(); + set_vt_palette(&mut output, &[(0, 11, 17, 24), (15, 229, 234, 236)]).unwrap(); + reset_vt_palette(&mut output).unwrap(); + assert_eq!(output, b"\x1b]P00b1118\x1b]Pfe5eaec\x1b]R"); + set_vt_palette(&mut output, &[(16, 0, 0, 0)]).unwrap_err(); + } } From aa41f07a0a2820b1017965875f28a57cdd7e76bc Mon Sep 17 00:00:00 2001 From: Paco Date: Sun, 4 Oct 2026 15:58:37 -0700 Subject: [PATCH 18/62] fix: Clear stale TUI before Linux VT shell Unlike ordinary terminal emulators, the Linux virtual console does not restore a previous screen on alternate-screen exit. Clear it only when switching to a real shell on tty1. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/ui.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 33e9b62987..8d872baeda 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -8,9 +8,12 @@ use std::{ use anyhow::{anyhow, Context, Error}; use crossterm::{ + cursor::MoveTo, event::{self, Event as TerminalEvent, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}, execute, - terminal::{self, EnterAlternateScreen, LeaveAlternateScreen}, + terminal::{ + self, Clear as ClearTerminal, ClearType, EnterAlternateScreen, LeaveAlternateScreen, + }, }; use log::{error, warn, Level, LevelFilter}; use ratatui::{ @@ -739,6 +742,14 @@ impl Display { let restore = (|| -> Result<(), Error> { terminal::disable_raw_mode()?; execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; + if self.linux_vt { + // Linux VT leaves the old TUI visible when exiting its alternate screen. + execute!( + self.terminal.backend_mut(), + ClearTerminal(ClearType::All), + MoveTo(0, 0) + )?; + } self.terminal.show_cursor()?; Ok(()) })(); From 73f9b4ba40fe596f29f04328d4df5fb37890c9e9 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 16:09:42 -0800 Subject: [PATCH 19/62] feature: Discover installer media by filesystem label Support labels on GPT USB partitions as well as optical filesystems, keep the old cdromLabel as a config alias, and use a FAT-compatible default. Refuse streaming whenever the mounted media could be Trident's target disk. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 14 +++++-- crates/installer/src/config.rs | 41 ++++++++++++++------ crates/installer/src/source.rs | 69 +++++++++++++++++++++++++--------- crates/osutils/src/lsblk.rs | 16 ++++++++ packaging/installer.toml | 2 +- 5 files changed, 108 insertions(+), 34 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 201f0a655c..b838dc7974 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -30,9 +30,9 @@ serialMode = "logs" serialVerbosity = "debug" [media] -cdromLabel = "TRIDENT_INSTALL" +filesystemLabel = "TRD_INSTALL" cosiDirectory = "cosi/" -# Instead of cdromLabel, supply mountPath for already-mounted media. +# Instead of filesystemLabel, supply mountPath for already-mounted media. [autorun] # If omitted, use /etc/trident/config.yaml when that file exists. @@ -49,8 +49,14 @@ an error rather than showing an unfinished serial TUI. `serialVerbosity` defaults to `debug` and accepts `off`, `error`, `warn`, `info`, `debug`, and `trace`. -Media is mounted read-only at `/run/trident/installer-media`. Duplicate labels -are rejected. Optional `installer/installer.toml` on the mounted media can +`media.filesystemLabel` is a filesystem label, not a CD-ROM device name. The +FAT-compatible default (`TRD_INSTALL`, 11 characters) can identify an ISO9660 +filesystem or a FAT filesystem on a GPT USB partition. Set it to the actual +label on the installation medium (Image Customizer ISO output uses `CDROM`). +The older `cdromLabel` key is accepted as an alias; setting both is an error. +Media is mounted read-only with filesystem auto-detection at +`/run/trident/installer-media`. Duplicate labels are rejected. Optional +`installer/installer.toml` on the mounted media can override mode, image directory, and autorun options, but not bootstrap media discovery or `serialMode`. Relative HC paths are media-relative; absolute paths and `file://` refer to the live filesystem. HTTP(S) HC sources are supported. diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index c78b624eac..e67f0dd029 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -13,7 +13,7 @@ pub(super) const DEFAULT_CONFIG: &str = "/etc/trident/installer.toml"; pub(super) const DEFAULT_HOST_CONFIGURATION: &str = "/etc/trident/config.yaml"; pub(super) const MEDIA_CONFIG: &str = "installer/installer.toml"; pub(super) const MEDIA_MOUNT: &str = "/run/trident/installer-media"; -pub(super) const DEFAULT_MEDIA_LABEL: &str = "TRIDENT_INSTALL"; +pub(super) const DEFAULT_MEDIA_LABEL: &str = "TRD_INSTALL"; #[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase", deny_unknown_fields)] @@ -73,8 +73,8 @@ impl SerialVerbosity { #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub(super) struct Media { - #[serde(default, skip_serializing_if = "Option::is_none")] - pub cdrom_label: Option, + #[serde(default, alias = "cdromLabel", skip_serializing_if = "Option::is_none")] + pub filesystem_label: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub mount_path: Option, #[serde(default = "default_cosi_directory")] @@ -84,7 +84,7 @@ pub(super) struct Media { impl Default for Media { fn default() -> Self { Self { - cdrom_label: None, + filesystem_label: None, mount_path: None, cosi_directory: default_cosi_directory(), } @@ -144,13 +144,13 @@ impl Config { "serialMode = \"ui\" is not implemented yet; use \"logs\"" ); ensure!( - self.media.cdrom_label.is_none() || self.media.mount_path.is_none(), - "media.cdromLabel and media.mountPath are mutually exclusive" + self.media.filesystem_label.is_none() || self.media.mount_path.is_none(), + "media.filesystemLabel and media.mountPath are mutually exclusive" ); - if let Some(label) = &self.media.cdrom_label { + if let Some(label) = &self.media.filesystem_label { ensure!( !label.trim().is_empty(), - "media.cdromLabel must not be empty" + "media.filesystemLabel must not be empty" ); } if let Some(path) = &self.media.mount_path { @@ -197,8 +197,10 @@ impl Config { ); if let Some(media) = overlay.get("media") { ensure!( - media.get("cdromLabel").is_none() && media.get("mountPath").is_none(), - "Media configuration cannot change bootstrap cdromLabel or mountPath" + media.get("filesystemLabel").is_none() + && media.get("cdromLabel").is_none() + && media.get("mountPath").is_none(), + "Media configuration cannot change bootstrap filesystemLabel or mountPath" ); } let mut base = TomlValue::try_from(self)?; @@ -288,6 +290,7 @@ mod tests { assert!(config.autorun.reboot); assert!(!config.autorun.force); assert_eq!(config.media.cosi_directory, Path::new("cosi")); + assert_eq!(DEFAULT_MEDIA_LABEL.len(), 11); Config::parse("mode = 'interactive'") .unwrap() .require_autorun() @@ -324,7 +327,7 @@ mod tests { let config = Config::parse(include_str!("../../../packaging/installer.toml")).unwrap(); config.require_autorun().unwrap(); assert_eq!( - config.media.cdrom_label.as_deref(), + config.media.filesystem_label.as_deref(), Some(DEFAULT_MEDIA_LABEL) ); assert!(config.autorun.reboot); @@ -335,7 +338,7 @@ mod tests { Config::parse(indoc! {" mode = 'autorun' [media] - cdromLabel = 'TEST' + filesystemLabel = 'TEST' mountPath = '/media' "}) .unwrap_err(); @@ -379,9 +382,23 @@ mod tests { assert!(!config.autorun.reboot); assert!(config.autorun.force); assert_eq!(config.media.cosi_directory, Path::new("cosi")); + fs::write(&path, "[media]\nfilesystemLabel = 'OTHER'").unwrap(); + config.overlay(root.path()).unwrap_err(); fs::write(&path, "[media]\ncdromLabel = 'OTHER'").unwrap(); config.overlay(root.path()).unwrap_err(); fs::write(&path, "serialMode = 'ui'").unwrap(); config.overlay(root.path()).unwrap_err(); } + + #[test] + fn legacy_cdrom_label_alias_still_mounts_the_same_filesystem() { + let legacy = Config::parse("mode = 'autorun'\n[media]\ncdromLabel = 'CDROM'").unwrap(); + let generic = + Config::parse("mode = 'autorun'\n[media]\nfilesystemLabel = 'CDROM'").unwrap(); + assert_eq!(legacy, generic); + Config::parse( + "mode = 'autorun'\n[media]\ncdromLabel = 'CDROM'\nfilesystemLabel = 'TRD_INSTALL'", + ) + .unwrap_err(); + } } diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 4de595f966..0dcc36509c 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -157,7 +157,7 @@ fn media_root( } let label = settings .media - .cdrom_label + .filesystem_label .as_deref() .unwrap_or(config::DEFAULT_MEDIA_LABEL); progress("Looking for installer media by filesystem label")?; @@ -358,6 +358,25 @@ pub(super) fn stream_preflight(image: &Url, force: bool) -> Result .as_ref() .context("COSI disk metadata is missing")? .size; + ensure_stream_target_safe(&devices, required)?; + if force { + return Ok(false); + } + let expected = metadata + .images + .iter() + .filter_map(|image| image.fs_uuid.as_deref()) + .filter(|uuid| !uuid.is_empty()) + .map(str::to_ascii_lowercase) + .collect::>(); + ensure!( + !expected.is_empty(), + "COSI has no filesystem UUIDs for repeat-install protection" + ); + Ok(contains_image(&devices, &expected)) +} + +fn ensure_stream_target_safe(devices: &[BlockDevice], required: u64) -> Result<(), Error> { let candidates = devices .iter() .filter(|device| device.blkdev_type == BlockDeviceType::Disk) @@ -377,25 +396,11 @@ pub(super) fn stream_preflight(image: &Url, force: bool) -> Result for device in candidates.iter().filter(|device| device.size == minimum) { ensure!( !device.readonly && device.get_all_mountpoints_recursive().is_empty(), - "StreamDisk could select a read-only or mounted disk '{}'; refusing to overwrite live media", + "StreamDisk could select mounted or read-only disk '{}', possibly the installer USB; refusing to overwrite installation media. Use media smaller than the COSI disk size or a Host Configuration with an explicit target disk", device.device_path().display() ); } - if force { - return Ok(false); - } - let expected = metadata - .images - .iter() - .filter_map(|image| image.fs_uuid.as_deref()) - .filter(|uuid| !uuid.is_empty()) - .map(str::to_ascii_lowercase) - .collect::>(); - ensure!( - !expected.is_empty(), - "COSI has no filesystem UUIDs for repeat-install protection" - ); - Ok(contains_image(&devices, &expected)) + Ok(()) } fn contains_image(devices: &[BlockDevice], expected: &HashSet) -> bool { @@ -543,4 +548,34 @@ mod tests { &HashSet::from(["uuid-1".into(), "uuid-2".into()]) )); } + + #[test] + fn smaller_gpt_usb_media_is_not_a_stream_target() { + let media = BlockDevice { + name: "sdb".into(), + size: 2 * 1024 * 1024 * 1024, + blkdev_type: BlockDeviceType::Disk, + children: vec![BlockDevice { + name: "sdb1".into(), + label: Some("TRD_INSTALL".into()), + mountpoints: vec![config::MEDIA_MOUNT.into()], + ..Default::default() + }], + ..Default::default() + }; + let target = BlockDevice { + name: "vda".into(), + size: 8 * 1024 * 1024 * 1024, + blkdev_type: BlockDeviceType::Disk, + ..Default::default() + }; + let required = 4 * 1024 * 1024 * 1024; + ensure_stream_target_safe(&[media.clone(), target.clone()], required).unwrap(); + let larger_media = BlockDevice { + size: 6 * 1024 * 1024 * 1024, + ..media + }; + let error = ensure_stream_target_safe(&[larger_media, target], required).unwrap_err(); + assert!(error.to_string().contains("installer USB"), "{error:#}"); + } } diff --git a/crates/osutils/src/lsblk.rs b/crates/osutils/src/lsblk.rs index ce6703c585..71b5019a65 100644 --- a/crates/osutils/src/lsblk.rs +++ b/crates/osutils/src/lsblk.rs @@ -370,6 +370,22 @@ mod tests { assert_eq!(matches, [PathBuf::from("/dev/sr0")]); } + #[test] + fn parses_gpt_usb_partition_label_from_lsblk_json() { + let devices = parse_lsblk_output( + r#"{"blockdevices":[{"name":"sdb","size":1073741824,"type":"disk","pttype":"gpt","children":[{"name":"sdb1","size":1069547520,"type":"part","fstype":"vfat","label":"TRD_INSTALL"}]}]}"#, + ) + .unwrap(); + let mut matches = Vec::new(); + collect_devices_by_label(&devices, "TRD_INSTALL", &mut matches); + assert_eq!(matches, [PathBuf::from("/dev/sdb1")]); + assert_eq!( + devices[0].partition_table_type, + Some(PartitionTableType::Gpt) + ); + assert!(devices[0].label.is_none()); + } + /// Output obtained from running `lsblk --json --bytes --output-all --paths /dev/sda` /// on the functional test VM AzL 3.0, lsblk from util-linux 2.40.2 const SAMPLE_LSBLK_OUTPUT: &str = indoc::indoc! { diff --git a/packaging/installer.toml b/packaging/installer.toml index 3d92d95d07..fe88aa4ab4 100644 --- a/packaging/installer.toml +++ b/packaging/installer.toml @@ -3,7 +3,7 @@ serialMode = "logs" serialVerbosity = "debug" [media] -cdromLabel = "TRIDENT_INSTALL" +filesystemLabel = "TRD_INSTALL" cosiDirectory = "cosi/" [autorun] From b35257a2a3d45e4ff33275d04c2caef4b6f510b8 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 16:26:03 -0800 Subject: [PATCH 20/62] test: Exercise GPT USB installer media safely Wait for USB/BMC block-device enumeration, add a reproducible GPT/FAT32 COSI fixture, and attach it read-only to a disposable QEMU VM. Preserve the existing ISO-only test configuration. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 8 +++- crates/installer/src/source.rs | 21 ++++++++- test/README.md | 27 ++++++++++- test/make_usb.py | 84 ++++++++++++++++++++++++++++++++++ test/run.py | 33 +++++++++++-- test/test_scripts.py | 19 ++++++++ 6 files changed, 185 insertions(+), 7 deletions(-) create mode 100644 test/make_usb.py diff --git a/crates/installer/README.md b/crates/installer/README.md index b838dc7974..677d7e3a5b 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -55,7 +55,9 @@ filesystem or a FAT filesystem on a GPT USB partition. Set it to the actual label on the installation medium (Image Customizer ISO output uses `CDROM`). The older `cdromLabel` key is accepted as an alias; setting both is an error. Media is mounted read-only with filesystem auto-detection at -`/run/trident/installer-media`. Duplicate labels are rejected. Optional +`/run/trident/installer-media`. Media discovery waits up to 60 seconds for +USB/BMC block-device enumeration before reporting a missing source. Duplicate +labels are rejected. Optional `installer/installer.toml` on the mounted media can override mode, image directory, and autorun options, but not bootstrap media discovery or `serialMode`. Relative HC paths are media-relative; absolute paths and `file://` @@ -143,6 +145,10 @@ checks. It does not change HC installs or manually supplied recovery URLs. **With force enabled, an ISO left in the boot path can reimage the machine on every boot.** Remove or deprioritize the media after installation. Mounted or read-only potential stream targets are never force-overwritten. +If the installer USB itself is the smallest disk fitting the COSI, streaming +stops rather than overwriting it; use a USB smaller than the COSI's original +disk size or an HC selecting an explicit target. The current StreamDisk RPC +cannot exclude a specific disk. **HC installation intentionally has no repeat-install/Force guard:** the HC is the operator's authority to overwrite its selected disks. Remove ISO media or correct boot order to avoid repeated HC-driven installations. diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 0dcc36509c..3da84dfbba 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -3,7 +3,8 @@ use std::{ fs::{self, File}, io::{ErrorKind, Read}, path::{Path, PathBuf}, - time::Duration, + thread, + time::{Duration, Instant}, }; use anyhow::{anyhow, bail, ensure, Context, Error}; @@ -24,6 +25,8 @@ use crate::config::{self, Config}; pub(super) const IMAGE_MARKER: &str = "installer://image"; const SOURCE_TIMEOUT: Duration = Duration::from_secs(30); +const MEDIA_DISCOVERY_TIMEOUT: Duration = Duration::from_secs(60); +const MEDIA_POLL_INTERVAL: Duration = Duration::from_secs(2); const MAX_HOST_CONFIGURATION_BYTES: u64 = 2 * 1024 * 1024; const MAX_METADATA_BYTES: u64 = 16 * 1024 * 1024; const METADATA_PATH: &str = "metadata.json"; @@ -161,7 +164,21 @@ fn media_root( .as_deref() .unwrap_or(config::DEFAULT_MEDIA_LABEL); progress("Looking for installer media by filesystem label")?; - let devices = lsblk::devices_by_label(label)?; + let deadline = Instant::now() + MEDIA_DISCOVERY_TIMEOUT; + let mut attempts = 0; + let devices = loop { + let devices = lsblk::devices_by_label(label)?; + if !devices.is_empty() || Instant::now() >= deadline { + break devices; + } + if attempts % 5 == 0 { + progress(&format!( + "Waiting for installation media labelled '{label}'" + ))?; + } + attempts += 1; + thread::sleep(MEDIA_POLL_INTERVAL); + }; let Some(device) = config::choose_media(&devices)? else { return Ok(None); }; diff --git a/test/README.md b/test/README.md index 453fb424b9..9f1bf8efe9 100644 --- a/test/README.md +++ b/test/README.md @@ -58,6 +58,30 @@ Do not rebuild over an ISO still attached to a running VM. Use `--iso test/.work/next-iso/installer_amd64_iso.iso` to `run.py` to test a replacement without changing the running guest's CD-ROM. +## GPT USB installation media + +The installer finds a filesystem by label, so it can read COSI files from an +optical ISO9660 filesystem or a GPT USB partition (FAT32 in this fixture). +`filesystemLabel = "TRD_INSTALL"` fits FAT32's 11-character label limit. +Image Customizer ISO output is labelled `CDROM`, so use the test ISO with +`filesystemLabel` changed to `TRD_INSTALL` when exercising the separate USB +medium; keep the two media labels distinct to avoid ambiguous discovery. + +```console +python3 test/make_usb.py --cosi test/installer/cosi/payload.cosi +python3 test/run.py --iso test/.work/gpt-usb-iso/installer_amd64_iso.iso \ + --usb-media test/.work/installer-usb.raw --vnc --keep +``` + +`make_usb.py` creates only a local, sparse 768 MiB GPT/FAT32 image under +`test/.work`. It uses Docker's cached builder for `sfdisk` and mtools to +populate `cosi/payload.cosi`; it does not repartition a host device. +`run.py` attaches it as a read-only USB mass-storage device. The USB is smaller +than the COSI's 4 GiB original disk, so Trident cannot select it as an install +target. A larger USB that could be the smallest eligible disk is rejected by +the installer preflight rather than being overwritten; the StreamDisk RPC +does not yet accept a target disk override. + ## Over SSH, including from a Wayland desktop On the VM host: @@ -85,7 +109,8 @@ Use `--vnc 2` and tunnel TCP 5902 if display 1 is already occupied. Build prerequisites: Python 3.11+, Docker, Tailor and the cached `azl3/trident-builder:latest` image. Trident binaries are built inside Azure Linux rather than using workstation binaries with potentially incompatible -glibc. The normal repository RPM spec packages them. Tailor runs Image +glibc. The USB image fixture also needs mtools (`mformat`, `mmd`, `mcopy`). +The normal repository RPM spec packages them. Tailor runs Image Customizer 1.6 on an Azure Linux 3 minimal base. Runtime prerequisites: QEMU, `qemu-img`, and matching OVMF code/variables. diff --git a/test/make_usb.py b/test/make_usb.py new file mode 100644 index 0000000000..db8b126f38 --- /dev/null +++ b/test/make_usb.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +import argparse +import json +import os +from pathlib import Path +import shutil +import subprocess +import tempfile + +from build import BUILDER, GIB, HERE, IMAGE, metadata, run + +LABEL = "TRD_INSTALL" +DEFAULT_SIZE_MIB = 768 +PARTITION_RESERVE_MIB = 68 +SECTOR_SIZE = 512 + + +def build_usb(cosi, output, size_mib): + source = cosi.resolve(strict=True) + info = metadata(source) + if size_mib < 512: + raise ValueError("A FAT32 USB test image needs at least 512 MiB") + if size_mib * 1024**2 >= info["disk"]["size"]: + raise ValueError("USB test media must be smaller than the COSI disk size") + if output.exists() or output.is_symlink(): + raise FileExistsError(f"Refusing to overwrite existing USB image: {output}") + + output.parent.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(prefix="usb-media-", dir=output.parent) as scratch: + image = Path(scratch) / "usb.raw" + with image.open("wb") as file: + file.truncate(size_mib * 1024**2) + volume = f"{scratch}:/media" + base = [ + "docker", + "run", + "--rm", + "-i", + "--user", + f"{os.getuid()}:{os.getgid()}", + "-v", + volume, + BUILDER, + ] + subprocess.run( + base + ["sfdisk", "/media/usb.raw"], + input=f"label: gpt\n,{size_mib - PARTITION_RESERVE_MIB}M,U\n", + check=True, + text=True, + ) + result = subprocess.run( + base + ["sfdisk", "--json", "/media/usb.raw"], + check=True, + capture_output=True, + text=True, + ) + table = json.loads(result.stdout)["partitiontable"] + if table["label"] != "gpt" or len(table["partitions"]) != 1: + raise ValueError("Expected one GPT partition on USB test media") + part = table["partitions"][0] + device = f"{image}@@{part['start'] * SECTOR_SIZE}" + run(["mformat", "-i", device, "-T", part["size"], "-F", "-v", LABEL, "::"]) + run(["mmd", "-i", device, "::/cosi"]) + run(["mcopy", "-i", device, source, "::/cosi/payload.cosi"]) + shutil.move(image, output) + print(f"GPT/FAT32 installer USB: {output} ({size_mib} MiB, label {LABEL})") + + +def main(): + parser = argparse.ArgumentParser( + description="Create a GPT/FAT32 USB image for installer VM testing" + ) + parser.add_argument("--cosi", type=Path, default=IMAGE / "cosi/payload.cosi") + parser.add_argument("--output", type=Path, default=HERE / ".work/installer-usb.raw") + parser.add_argument("--size-mib", type=int, default=DEFAULT_SIZE_MIB) + args = parser.parse_args() + for tool in ["docker", "mformat", "mmd", "mcopy"]: + if shutil.which(tool) is None: + parser.error(f"{tool} is required") + build_usb(args.cosi, args.output.resolve(), args.size_mib) + + +if __name__ == "__main__": + main() diff --git a/test/run.py b/test/run.py index 4f49fcba92..cdec973390 100755 --- a/test/run.py +++ b/test/run.py @@ -26,7 +26,7 @@ ] -def command(iso, scratch, code, memory, cpus, display, vnc=None): +def command(iso, scratch, code, memory, cpus, display, vnc=None, usb_media=None): acceleration = ( ["-enable-kvm", "-cpu", "host"] if os.access("/dev/kvm", os.W_OK) else [] ) @@ -68,6 +68,17 @@ def command(iso, scratch, code, memory, cpus, display, vnc=None): f"127.0.0.1:{vnc}", ] ) + if usb_media is not None: + launch.extend( + [ + "-device", + "qemu-xhci,id=usb", + "-drive", + f"if=none,id=installer-usb,file={usb_media},format=raw,readonly=on", + "-device", + "usb-storage,bus=usb.0,drive=installer-usb,bootindex=3", + ] + ) return launch @@ -165,6 +176,11 @@ def main(): description="Boot the installer ISO in a disposable UEFI VM" ) parser.add_argument("--iso", type=Path) + parser.add_argument( + "--usb-media", + type=Path, + help="Attach a GPT/FAT USB image read-only; never pass a host block device", + ) displays = parser.add_mutually_exclusive_group() displays.add_argument("--display", default="gtk", choices=["gtk", "sdl", "none"]) displays.add_argument( @@ -203,7 +219,10 @@ def main(): if iso is None or not iso.is_file(): parser.error("Build the ISO first, or provide --iso PATH") iso = iso.resolve() - if "," in str(iso): + usb_media = args.usb_media.resolve() if args.usb_media is not None else None + if usb_media is not None and not usb_media.is_file(): + parser.error("--usb-media must be a regular image file, not a physical device") + if any("," in str(path) for path in (iso, usb_media) if path is not None): parser.error("QEMU drive paths containing commas are not supported") code = next((path for path in CODE_CANDIDATES if path.is_file()), None) if code is None: @@ -231,6 +250,7 @@ def main(): args.cpus, args.display, args.vnc, + usb_media, ) ) ) @@ -253,7 +273,14 @@ def main(): check=True, ) launch = command( - iso, scratch, code, args.memory, args.cpus, args.display, args.vnc + iso, + scratch, + code, + args.memory, + args.cpus, + args.display, + args.vnc, + usb_media, ) print( f"ISO: {iso}\nDisposable disk: {scratch / 'disk.qcow2'}\nSerial log: {scratch / 'serial.log'}", diff --git a/test/test_scripts.py b/test/test_scripts.py index 0ef8fb6527..2ab0e5ee0e 100644 --- a/test/test_scripts.py +++ b/test/test_scripts.py @@ -15,6 +15,25 @@ class VmCommandTests(unittest.TestCase): + def test_usb_media_is_a_read_only_gpt_image_with_lower_boot_priority(self): + image = Path("/tmp/disposable-installer-usb.raw") + launch = VM.command( + Path("/tmp/installer-test.iso"), + Path("/tmp/installer-test-owned"), + Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), + 6144, + 2, + "gtk", + usb_media=image, + ) + drives = [ + launch[index + 1] for index, value in enumerate(launch) if value == "-drive" + ] + self.assertIn(f"file={image},format=raw,readonly=on", drives[-1]) + self.assertIn("qemu-xhci,id=usb", launch) + self.assertIn("usb-storage,bus=usb.0,drive=installer-usb,bootindex=3", launch) + self.assertNotIn("-virtfs", launch) + def test_follows_new_serial_records_without_replaying_firmware_escape_codes(self): class RunningProcess: def __init__(self): From 9c5f97d09e69b0e8329450f927599582f641ddf5 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 16:32:42 -0800 Subject: [PATCH 21/62] fix: Wait for GPT USB media enumeration before autorun An ISO boot can start the installer before USB storage is enumerated. Poll the filesystem label for up to 60 seconds during autorun, but do not delay manually supplied remote recovery sources. Document the tested FAT32 GPT fixture and keep the optical ISO test label CDROM. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 4 +++- crates/installer/src/source.rs | 5 +++-- test/README.md | 13 +++++++++---- test/installer/files/installer.toml | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 677d7e3a5b..2e380a846a 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -56,7 +56,9 @@ label on the installation medium (Image Customizer ISO output uses `CDROM`). The older `cdromLabel` key is accepted as an alias; setting both is an error. Media is mounted read-only with filesystem auto-detection at `/run/trident/installer-media`. Media discovery waits up to 60 seconds for -USB/BMC block-device enumeration before reporting a missing source. Duplicate +USB/BMC block-device enumeration during autorun before reporting a missing +source; manually supplied recovery URLs do not wait. Other filesystem types +require corresponding kernel mount support on the live OS. Duplicate labels are rejected. Optional `installer/installer.toml` on the mounted media can override mode, image directory, and autorun options, but not bootstrap media diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 3da84dfbba..1abb18f5e0 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -64,7 +64,7 @@ pub(super) fn prepare_with_progress( request: &Request, mut progress: impl FnMut(&str) -> Result<(), Error>, ) -> Result<(Config, Plan), Error> { - let root = media_root(settings, &mut progress)?; + let root = media_root(settings, matches!(request, Request::Autorun), &mut progress)?; prepare_from_media_with_progress(settings, request, root.as_deref(), &mut progress) } @@ -147,6 +147,7 @@ fn prepare_from_media_with_progress( fn media_root( settings: &Config, + wait_for_media: bool, progress: &mut impl FnMut(&str) -> Result<(), Error>, ) -> Result, Error> { if let Some(path) = &settings.media.mount_path { @@ -168,7 +169,7 @@ fn media_root( let mut attempts = 0; let devices = loop { let devices = lsblk::devices_by_label(label)?; - if !devices.is_empty() || Instant::now() >= deadline { + if !devices.is_empty() || !wait_for_media || Instant::now() >= deadline { break devices; } if attempts % 5 == 0 { diff --git a/test/README.md b/test/README.md index 9f1bf8efe9..3154fe30f6 100644 --- a/test/README.md +++ b/test/README.md @@ -63,13 +63,16 @@ replacement without changing the running guest's CD-ROM. The installer finds a filesystem by label, so it can read COSI files from an optical ISO9660 filesystem or a GPT USB partition (FAT32 in this fixture). `filesystemLabel = "TRD_INSTALL"` fits FAT32's 11-character label limit. -Image Customizer ISO output is labelled `CDROM`, so use the test ISO with -`filesystemLabel` changed to `TRD_INSTALL` when exercising the separate USB -medium; keep the two media labels distinct to avoid ambiguous discovery. +Image Customizer ISO output is labelled `CDROM`. To build the USB test ISO, +temporarily set `filesystemLabel = "TRD_INSTALL"` in +`test/installer/files/installer.toml`, run +`python3 test/build.py --skip-rpms --output-dir test/.work/gpt-usb-ready`, +and restore `filesystemLabel = "CDROM"` afterward. Keep the two media labels +distinct to avoid ambiguous discovery. ```console python3 test/make_usb.py --cosi test/installer/cosi/payload.cosi -python3 test/run.py --iso test/.work/gpt-usb-iso/installer_amd64_iso.iso \ +python3 test/run.py --iso test/.work/gpt-usb-ready/installer_amd64_iso.iso \ --usb-media test/.work/installer-usb.raw --vnc --keep ``` @@ -81,6 +84,8 @@ than the COSI's 4 GiB original disk, so Trident cannot select it as an install target. A larger USB that could be the smallest eligible disk is rejected by the installer preflight rather than being overwritten; the StreamDisk RPC does not yet accept a target disk override. +This fixture exercises a Rufus-style GPT/FAT32 layout, not Rufus itself or +NTFS; non-FAT media require filesystem support in the live OS. ## Over SSH, including from a Wayland desktop diff --git a/test/installer/files/installer.toml b/test/installer/files/installer.toml index d25f0b3e15..49d2a72b9b 100644 --- a/test/installer/files/installer.toml +++ b/test/installer/files/installer.toml @@ -3,7 +3,7 @@ serialMode = "logs" serialVerbosity = "debug" [media] -cdromLabel = "CDROM" +filesystemLabel = "CDROM" cosiDirectory = "cosi/" [autorun] From 4944703d9cbc0a2b0f2f26034a2215b45621201d Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 21:26:27 -0800 Subject: [PATCH 22/62] docs: Document installer and media helper functions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/client.rs | 21 ++++++++++ crates/installer/src/config.rs | 24 +++++++++++ crates/installer/src/demo.rs | 2 + crates/installer/src/main.rs | 2 + crates/installer/src/source.rs | 33 +++++++++++++++ crates/installer/src/ui.rs | 64 ++++++++++++++++++++++++++++++ crates/osutils/src/dependencies.rs | 4 ++ crates/osutils/src/lsblk.rs | 4 ++ crates/osutils/src/systemd.rs | 3 ++ crates/osutils/src/terminal.rs | 11 +++++ test/build.py | 5 +++ test/make_usb.py | 2 + test/run.py | 8 ++++ test/test_scripts.py | 12 ++++++ 14 files changed, 195 insertions(+) diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index 8a16829c9d..7332cf1dd0 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -31,6 +31,7 @@ use crate::{ const CONNECTION_TIMEOUT: Duration = Duration::from_secs(30); const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); +/// Applies configured force only to autorun streams, while respecting explicit confirmation. fn stream_force_requested(automatic: bool, configured: bool, confirmed: bool) -> bool { confirmed || (automatic && configured) } @@ -65,6 +66,7 @@ pub(super) enum Completion { Failure(String), } +/// Interprets Trident's final response, rejecting invalid or daemon-managed reboot states. pub(super) fn completion(completed: Completed) -> Result { match StatusCode::try_from(completed.status).context("Invalid final status code")? { StatusCode::Success => { @@ -101,6 +103,7 @@ pub(super) fn completion(completed: Completed) -> Result { } } +/// Renders a structured Trident error as readable multi-line diagnostics. fn format_error(error: &RemoteError) -> String { let category = match ErrorCategory::try_from(error.kind) { Ok(ErrorCategory::Unspecified) => "Unspecified", @@ -128,6 +131,10 @@ fn format_error(error: &RemoteError) -> String { details } +/// Prepares a source, checks stream safety, and forwards Trident's gRPC responses to the UI. +/// +/// After an uncertain previous operation, `require_idle` prevents a new write until the daemon +/// confirms it is idle. Ambiguous RPC failures are reported as uncertain instead of retrying. pub(super) async fn execute( settings: Config, request: Request, @@ -262,6 +269,7 @@ pub(super) async fn execute( receive(response, &tx).await } +/// Connects to the Trident daemon with a bounded connection timeout. async fn connect(address: &str) -> Result { Endpoint::new(address.to_owned())? .connect_timeout(CONNECTION_TIMEOUT) @@ -270,6 +278,7 @@ async fn connect(address: &str) -> Result { .with_context(|| format!("Failed to connect to Trident at '{address}'")) } +/// Starts the selected gRPC operation, leaving reboot control to the installer. async fn start(channel: Channel, plan: Plan) -> Result, Status> { let reboot = Some(RebootManagement { handling: RebootHandling::CallerHandlesReboot.into(), @@ -300,6 +309,7 @@ async fn start(channel: Channel, plan: Plan) -> Result, tx: &Sender, @@ -334,6 +344,7 @@ async fn receive( } } +/// Sends an installer event, returning an error if the UI receiver has closed. pub(super) async fn send(tx: &Sender, event: Event) -> Result<(), Error> { tx.send(event) .await @@ -367,6 +378,7 @@ mod tests { type ResponseStream = Iter>>; #[test] + /// Ensures a configured force flag does not affect manually chosen streams. fn only_automatic_streams_use_configured_force() { assert!(!stream_force_requested(true, false, false)); assert!(stream_force_requested(true, true, false)); @@ -374,6 +386,7 @@ mod tests { assert!(stream_force_requested(false, false, true)); } + /// Builds a valid completed response for gRPC client tests. fn successful() -> Completed { Completed { status: StatusCode::Success.into(), @@ -384,6 +397,7 @@ mod tests { } #[test] + /// Ensures final responses reject invalid statuses and unexpected reboot behavior. fn final_response_is_strict() { assert_eq!( completion(successful()).unwrap(), @@ -417,6 +431,7 @@ mod tests { } #[test] + /// Ensures structured errors retain useful diagnostics without debug formatting. fn errors_are_readable_without_debug_dumps() { let error = RemoteError { kind: ErrorCategory::ServicingError.into(), @@ -447,6 +462,7 @@ mod tests { impl StreamingService for Fake { type StreamDiskStream = ResponseStream; + /// Returns a sample stream, optionally omitting completion to simulate a disconnect. async fn stream_disk( &self, request: TonicRequest, @@ -493,6 +509,7 @@ mod tests { type InstallStageStream = ResponseStream; type InstallFinalizeStream = ResponseStream; + /// Captures an install request and returns a completed response. async fn install( &self, request: TonicRequest, @@ -506,6 +523,7 @@ mod tests { )]))) } + /// Rejects stage-only requests, which the fake does not support. async fn install_stage( &self, _: TonicRequest, @@ -513,6 +531,7 @@ mod tests { Err(Status::unimplemented("test only supports full install")) } + /// Rejects finalize-only requests, which the fake does not support. async fn install_finalize( &self, _: TonicRequest, @@ -522,6 +541,7 @@ mod tests { } #[tokio::test] + /// Ensures the full Host Configuration reaches gRPC with caller-managed reboot. async fn install_sends_full_configuration_and_caller_managed_reboot() { let root = TempDir::new().unwrap(); let path = root.path().join("trident.sock"); @@ -569,6 +589,7 @@ mod tests { } #[tokio::test] + /// Ensures a Unix gRPC stream retains trace logs and detects missing completion. async fn real_unix_transport_preserves_trace_and_detects_missing_completion() { for complete in [true, false] { let root = TempDir::new().unwrap(); diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index e67f0dd029..3660dc0063 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -58,6 +58,7 @@ pub(super) enum SerialVerbosity { } impl SerialVerbosity { + /// Converts the configured serial level into the log filter used when mirroring records. pub(super) fn filter(self) -> LevelFilter { match self { Self::Off => LevelFilter::Off, @@ -82,6 +83,7 @@ pub(super) struct Media { } impl Default for Media { + /// Leaves media discovery on the default label and searches the `cosi/` directory. fn default() -> Self { Self { filesystem_label: None, @@ -91,6 +93,7 @@ impl Default for Media { } } +/// Returns the relative COSI directory used when the media config omits it. fn default_cosi_directory() -> PathBuf { "cosi".into() } @@ -107,6 +110,7 @@ pub(super) struct Autorun { } impl Default for Autorun { + /// Enables reboot after a successful install but never forces a repeat stream. fn default() -> Self { Self { host_configuration: None, @@ -116,11 +120,13 @@ impl Default for Autorun { } } +/// Enables reboot when the autorun configuration does not specify a preference. fn default_reboot() -> bool { true } impl Config { + /// Reads and validates the bootstrap installer configuration from disk. pub(super) fn read(path: impl AsRef) -> Result { let path = path.as_ref(); let text = fs::read_to_string(path).with_context(|| { @@ -132,12 +138,14 @@ impl Config { Self::parse(&text) } + /// Parses installer TOML, rejecting unknown fields and unsupported settings. pub(super) fn parse(text: &str) -> Result { let config: Self = toml::from_str(text).context("Invalid installer TOML configuration")?; config.validate()?; Ok(config) } + /// Checks media paths, source fields, and the reserved serial UI mode. fn validate(&self) -> Result<(), Error> { ensure!( self.serial_mode == SerialMode::Logs, @@ -172,6 +180,7 @@ impl Config { Ok(()) } + /// Rejects interactive mode until interactive source selection is implemented. pub(super) fn require_autorun(&self) -> Result<(), Error> { ensure!( self.mode == Mode::Autorun, @@ -180,6 +189,7 @@ impl Config { Ok(()) } + /// Applies an optional media-local config without allowing bootstrap media or serial changes. pub(super) fn overlay(&self, root: impl AsRef) -> Result { let path = root.as_ref().join(MEDIA_CONFIG); let text = match fs::read_to_string(&path) { @@ -211,6 +221,7 @@ impl Config { } } +/// Recursively overlays TOML tables; non-table values replace their base values. fn merge(base: &mut TomlValue, overlay: TomlValue) { match (base, overlay) { (TomlValue::Table(base), TomlValue::Table(overlay)) => { @@ -227,6 +238,9 @@ fn merge(base: &mut TomlValue, overlay: TomlValue) { } } +/// Lists regular `.cosi` files directly beneath the media directory in name order. +/// +/// Rejects directories that resolve outside the media root; an absent directory yields no images. pub(super) fn discover_images( root: impl AsRef, directory: impl AsRef, @@ -263,6 +277,7 @@ pub(super) fn discover_images( Ok(images) } +/// Selects the sole media device, or fails rather than guessing between duplicate labels. pub(super) fn choose_media(devices: &[PathBuf]) -> Result, Error> { match devices { [] => Ok(None), @@ -281,6 +296,7 @@ mod tests { use tempfile::TempDir; #[test] + /// Ensures mode is required and omitted settings keep their safe defaults. fn explicit_mode_and_strict_fields() { Config::parse("").unwrap_err(); Config::parse("mode = 'autorun'\nunknown = true").unwrap_err(); @@ -298,6 +314,7 @@ mod tests { } #[test] + /// Ensures force is false by default and accepts only a boolean override. fn autorun_force_is_opt_in() { let config = Config::parse("mode = 'autorun'\n[autorun]\nforce = true").unwrap(); assert!(config.autorun.force); @@ -307,6 +324,7 @@ mod tests { } #[test] + /// Ensures the reserved serial UI mode fails validation instead of being enabled. fn serial_ui_mode_is_reserved_not_silently_enabled() { Config::parse("mode = 'autorun'\nserialMode = 'logs'").unwrap(); let error = Config::parse("mode = 'autorun'\nserialMode = 'ui'").unwrap_err(); @@ -314,6 +332,7 @@ mod tests { } #[test] + /// Ensures serial verbosity defaults to debug and invalid levels are rejected. fn serial_verbosity_defaults_to_debug_and_rejects_invalid_levels() { let config = Config::parse("mode = 'autorun'\nserialVerbosity = 'trace'").unwrap(); assert_eq!(config.serial_verbosity.filter(), LevelFilter::Trace); @@ -323,6 +342,7 @@ mod tests { } #[test] + /// Ensures the shipped TOML stays compatible with the runtime schema. fn packaged_configuration_matches_the_runtime_schema() { let config = Config::parse(include_str!("../../../packaging/installer.toml")).unwrap(); config.require_autorun().unwrap(); @@ -334,6 +354,7 @@ mod tests { } #[test] + /// Ensures media selectors are exclusive and the image directory stays relative. fn media_selector_and_path_validation() { Config::parse(indoc! {" mode = 'autorun' @@ -353,6 +374,7 @@ mod tests { } #[test] + /// Ensures image discovery is sorted, nonrecursive, and excludes symlinks. fn catalog_is_sorted_nonrecursive_and_ignores_symlinks() { let root = TempDir::new().unwrap(); let directory = root.path().join("cosi"); @@ -370,6 +392,7 @@ mod tests { } #[test] + /// Ensures media overlays cannot change bootstrap settings or discard defaults. fn media_overlay_preserves_defaults_and_bootstrap() { let root = TempDir::new().unwrap(); fs::create_dir(root.path().join("installer")).unwrap(); @@ -391,6 +414,7 @@ mod tests { } #[test] + /// Ensures the former CD-ROM key maps to the filesystem-label setting. fn legacy_cdrom_label_alias_still_mounts_the_same_filesystem() { let legacy = Config::parse("mode = 'autorun'\n[media]\ncdromLabel = 'CDROM'").unwrap(); let generic = diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs index c8939db854..48de66803f 100644 --- a/crates/installer/src/demo.rs +++ b/crates/installer/src/demo.rs @@ -25,6 +25,7 @@ pub(super) enum Scenario { Disconnect, } +/// Emits a simulated servicing sequence without connecting to Trident or touching disks. pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Error> { client::send( &tx, @@ -99,6 +100,7 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err .await } +/// Wraps a demo gRPC response in the same event shape used by a real install. async fn response(tx: &Sender, response: ResponseBody) -> Result<(), Error> { client::send( tx, diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index 40ebd64951..3d1a3cfb79 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -46,6 +46,7 @@ struct Args { system_console: bool, } +/// Starts logging and the installer runtime, returning a failing exit code on startup errors. fn main() -> ExitCode { env_logger::Builder::from_default_env() .filter_level(LevelFilter::Info) @@ -86,6 +87,7 @@ fn main() -> ExitCode { } } +/// Sets up private diagnostics and optional console ownership before starting the UI. async fn run(args: Args) -> Result<(), Error> { let mut linux_vt = false; let mut output = Output { diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 1abb18f5e0..6283b00c5f 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -51,6 +51,7 @@ pub(super) enum Plan { } impl Plan { + /// Describes the selected source for the progress window. pub(super) fn description(&self) -> String { match self { Self::Install { source, .. } => format!("Install from {source}"), @@ -59,6 +60,9 @@ impl Plan { } } +/// Resolves installation media and returns an install or stream plan with progress callbacks. +/// +/// Only autorun waits for media enumeration; explicit recovery sources proceed immediately. pub(super) fn prepare_with_progress( settings: &Config, request: &Request, @@ -69,6 +73,7 @@ pub(super) fn prepare_with_progress( } #[cfg(test)] +/// Prepares a plan from an already-mounted media root for tests. fn prepare_from_media( settings: &Config, request: &Request, @@ -77,6 +82,7 @@ fn prepare_from_media( prepare_from_media_with_progress(settings, request, root, &mut |_| Ok(())) } +/// Merges media settings and selects a Host Configuration before falling back to the first COSI. fn prepare_from_media_with_progress( settings: &Config, request: &Request, @@ -145,6 +151,10 @@ fn prepare_from_media_with_progress( Ok((settings, plan)) } +/// Finds labelled media and mounts it read-only, or validates a configured mount path. +/// +/// When `wait_for_media` is set, waits up to the discovery timeout for late USB partitions. +/// A pre-existing mount must belong to the selected device and be read-only. fn media_root( settings: &Config, wait_for_media: bool, @@ -216,6 +226,7 @@ fn media_root( Ok(Some(target.into())) } +/// Accepts only credential-free HTTP(S) URLs for manually entered recovery sources. pub(super) fn remote_url(source: &str) -> Result { let url = Url::parse(source).context("Enter an absolute http:// or https:// URL")?; ensure!( @@ -229,6 +240,9 @@ pub(super) fn remote_url(source: &str) -> Result { Ok(url) } +/// Resolves local, remote, or media-relative Host Configuration sources. +/// +/// Media-relative paths must resolve beneath the media root. fn source_url(source: &str, root: Option<&Path>) -> Result { if source.starts_with('/') { return file_url(Path::new(source)); @@ -253,11 +267,13 @@ fn source_url(source: &str, root: Option<&Path>) -> Result { file_url(&path) } +/// Converts a local path to a `file://` URL, preserving URL escaping. fn file_url(path: &Path) -> Result { Url::from_file_path(path) .map_err(|()| anyhow!("Cannot convert '{}' to a file URL", path.display())) } +/// Opens a local file or a time-limited HTTP(S) response for source inspection. fn reader(url: &Url) -> Result, Error> { match url.scheme() { "file" => { @@ -280,6 +296,7 @@ fn reader(url: &Url) -> Result, Error> { } } +/// Reads a Host Configuration as UTF-8 while enforcing the configured byte limit. fn read_text(url: &Url) -> Result { let mut text = String::new(); reader(url)? @@ -292,6 +309,7 @@ fn read_text(url: &Url) -> Result { Ok(text) } +/// Parses and validates a Host Configuration, resolving `installer://image` to the chosen COSI. pub(super) fn resolve_host_configuration( text: &str, selected: Option<&Path>, @@ -331,6 +349,7 @@ struct ImageMetadata { fs_uuid: Option, } +/// Extracts and checks COSI disk metadata within a bounded archive read. fn metadata(reader: impl Read) -> Result { let mut archive = Archive::new(reader.take(MAX_METADATA_BYTES)); for entry in archive.entries().context("Invalid COSI archive")? { @@ -368,6 +387,9 @@ fn metadata(reader: impl Read) -> Result { bail!("COSI metadata.json was not found within the metadata read limit") } +/// Checks disk eligibility and reports whether a complete matching image is already present. +/// +/// `force` bypasses only the repeat-install guard, not the installer-media safety check. pub(super) fn stream_preflight(image: &Url, force: bool) -> Result { let metadata = metadata(reader(image)?)?; let devices = lsblk::list().context("Failed to inspect attached disks")?; @@ -394,6 +416,7 @@ pub(super) fn stream_preflight(image: &Url, force: bool) -> Result Ok(contains_image(&devices, &expected)) } +/// Refuses streaming if Trident could choose a mounted or read-only smallest fitting disk. fn ensure_stream_target_safe(devices: &[BlockDevice], required: u64) -> Result<(), Error> { let candidates = devices .iter() @@ -421,7 +444,9 @@ fn ensure_stream_target_safe(devices: &[BlockDevice], required: u64) -> Result<( Ok(()) } +/// Checks whether every filesystem UUID in the COSI is present on attached devices. fn contains_image(devices: &[BlockDevice], expected: &HashSet) -> bool { + /// Collects filesystem UUIDs recursively, normalizing case for matching. fn collect(devices: &[BlockDevice], present: &mut HashSet) { for device in devices { if let Some(uuid) = &device.fsuuid { @@ -446,6 +471,7 @@ mod tests { use tempfile::TempDir; #[test] + /// Ensures recovery sources cannot silently fall back to local files or other schemes. fn remote_sources_are_explicit_and_limited() { remote_url("https://example.com/image.cosi").unwrap(); for invalid in [ @@ -459,6 +485,7 @@ mod tests { } #[test] + /// Ensures the image marker needs a COSI and leaves other image URLs untouched. fn marker_requires_an_image_and_does_not_touch_other_urls() { let yaml = valid_host_configuration(); resolve_host_configuration(yaml, None).unwrap_err(); @@ -480,6 +507,7 @@ mod tests { ); } + /// Provides a valid Host Configuration containing the installer image marker. fn valid_host_configuration() -> &'static str { indoc! {" storage: @@ -508,6 +536,7 @@ mod tests { } #[test] + /// Ensures Host Configuration takes priority and invalid input never falls back to COSI. fn host_configuration_wins_and_invalid_hc_never_falls_back() { let root = TempDir::new().unwrap(); fs::create_dir(root.path().join("cosi")).unwrap(); @@ -531,6 +560,7 @@ mod tests { } #[test] + /// Ensures an oversized Host Configuration is rejected before parsing. fn configuration_read_limit_is_enforced() { let root = TempDir::new().unwrap(); let path = root.path().join("oversized.yaml"); @@ -544,6 +574,7 @@ mod tests { } #[test] + /// Ensures unreadable and missing COSI metadata cannot pass preflight. fn truncated_and_missing_metadata_are_errors() { metadata(Cursor::new(b"not a tar file")).unwrap_err(); let mut builder = TarBuilder::new(Vec::new()); @@ -552,6 +583,7 @@ mod tests { } #[test] + /// Ensures repeat-install detection requires every expected filesystem UUID. fn repeat_guard_requires_the_complete_nonempty_uuid_set() { let devices = vec![BlockDevice { children: vec![BlockDevice { @@ -568,6 +600,7 @@ mod tests { } #[test] + /// Ensures a small installer USB is skipped and an eligible mounted USB is refused. fn smaller_gpt_usb_media_is_not_a_stream_target() { let media = BlockDevice { name: "sdb".into(), diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 8d872baeda..21d66deb6d 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -76,6 +76,8 @@ const VT_PALETTE: &[(u8, Color, Color)] = &[ (15, COLOR_INFO, Color::White), ]; +/// Maps known theme RGB colors to distinct ANSI roles on Linux VTs. +/// Other colors and non-VT terminals retain the original value. fn theme_color(color: Color, linux_vt: bool) -> Color { if !linux_vt { return color; @@ -87,6 +89,7 @@ fn theme_color(color: Color, linux_vt: bool) -> Color { .unwrap_or(color) } +/// Builds the Linux VT palette entries, rejecting any theme slot without an RGB value. fn vt_rgb_palette() -> Result, io::Error> { VT_PALETTE .iter() @@ -152,6 +155,7 @@ enum LogSource { } impl LogSource { + /// Returns the source tag used in both displayed and mirrored diagnostics. fn label(self) -> &'static str { match self { Self::Inst => "INST", @@ -159,6 +163,7 @@ impl LogSource { } } + /// Uses the source's theme color or its Linux VT equivalent. fn color(self, linux_vt: bool) -> Color { theme_color( match self { @@ -180,11 +185,13 @@ struct LogEntry { } impl LogEntry { + /// Formats elapsed time as a minutes-and-seconds prefix without wrapping at an hour. fn prefix(&self) -> String { let seconds = self.elapsed.as_secs(); format!("{:02}:{:02} [", seconds / 60, seconds % 60) } + /// Prefixes each line of a diagnostic for plain output and serial mirrors. fn plain(&self) -> String { let prefix = format!("{}{}:{}] ", self.prefix(), self.source.label(), self.level); display_text(&self.message) @@ -194,6 +201,7 @@ impl LogEntry { .join("\n") } + /// Styles the source and severity separately, highlighting result records as a whole. fn styled(&self, linux_vt: bool) -> Line<'_> { let level = Style::default().fg(theme_color( self.result_color.unwrap_or(log_color(self.level)), @@ -242,6 +250,7 @@ struct Model { } impl Model { + /// Starts on the progress screen with default display and serial log levels. fn new() -> Self { Self { screen: Screen::Progress, @@ -267,6 +276,7 @@ impl Model { } } + /// Records a failure, preserving any earlier uncertainty and cancelling automatic reboot. fn fail(&mut self, details: String, uncertain: bool) -> LogEntry { self.details = details.clone(); self.uncertain |= uncertain; @@ -279,6 +289,7 @@ impl Model { self.result_log(LogLevel::Error, format!("FAILURE: {details}"), COLOR_ERROR) } + /// Retains a bounded, single-line display copy while returning the full diagnostic. fn log(&mut self, source: LogSource, level: LogLevel, message: String) -> LogEntry { let entry = LogEntry { elapsed: self.started.elapsed(), @@ -306,6 +317,7 @@ impl Model { entry } + /// Marks an installer result record with a color even when logs are filtered. fn result_log(&mut self, level: LogLevel, message: String, color: Color) -> LogEntry { let mut entry = self.log(LogSource::Inst, level, message); entry.result_color = Some(color); @@ -315,6 +327,7 @@ impl Model { entry } + /// Applies a worker event to the current screen and returns its diagnostic record. fn event(&mut self, event: Event) -> LogEntry { match event { Event::InstallerLog { level, message } => self.log( @@ -407,6 +420,7 @@ impl Model { } } + /// Lists actions in keyboard-selection order for the current screen. fn choices(&self) -> Vec<&'static str> { match self.screen { Screen::Recovery => vec![ @@ -424,6 +438,7 @@ impl Model { } } + /// Shows the log panel on result screens unless full error details are open. fn showing_logs(&self) -> bool { matches!( self.screen, @@ -431,6 +446,8 @@ impl Model { ) || self.screen == Screen::Error && !self.error_details_open } + /// Updates navigation for a key press and returns an action only when one is requested. + /// URL entry and the verbosity picker consume keys before screen-level shortcuts. fn key(&mut self, key: KeyEvent, demo: bool) -> Option { if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { return None; @@ -636,6 +653,7 @@ struct DemoShell { } impl DemoShell { + /// Edits simulated input without executing it; returns true on exit or Escape. fn key(&mut self, key: KeyEvent) -> bool { if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { return false; @@ -666,6 +684,7 @@ impl DemoShell { false } + /// Includes the simulation warning, recent responses, and sanitized prompt input. fn text(&self) -> String { format!( "{DEMO_SHELL_NOTICE}\n\n{}\n\ndemo$ {}", @@ -685,6 +704,7 @@ pub(super) struct Output { } impl Output { + /// Persists the complete event in the diagnostic file, if one is configured. fn record(&mut self, event: &Event) -> Result<(), Error> { if let Some(log) = &mut self.log { writeln!(log, "{event:#?}").context("Failed to preserve installer diagnostics")?; @@ -693,6 +713,7 @@ impl Output { Ok(()) } + /// Sends visible records to serial mirrors, dropping mirrors that fail to write. fn announce(&mut self, entry: &LogEntry, verbosity: LevelFilter) { if !log_visible(entry.level, verbosity) { return; @@ -717,6 +738,7 @@ struct Display { } impl Display { + /// Creates a terminal display and enters its alternate screen. fn new(linux_vt: bool) -> Result { let mut display = Self { terminal: Terminal::new(CrosstermBackend::new(io::stdout()))?, @@ -727,6 +749,7 @@ impl Display { Ok(display) } + /// Restores raw-mode drawing and the VT palette after a shell exits. fn resume(&mut self) -> Result<(), Error> { terminal::enable_raw_mode()?; self.active = true; @@ -738,6 +761,8 @@ impl Display { Ok(()) } + /// Leaves raw mode and the alternate screen so an interactive shell can use the terminal. + /// On Linux VTs, also clears the old UI and restores the original palette. fn suspend(&mut self) -> Result<(), Error> { let restore = (|| -> Result<(), Error> { terminal::disable_raw_mode()?; @@ -766,6 +791,7 @@ impl Display { } impl Drop for Display { + /// Attempts terminal restoration if the display was not explicitly suspended. fn drop(&mut self) { if self.active { if let Err(error) = self.suspend() { @@ -775,6 +801,8 @@ impl Drop for Display { } } +/// Starts a demo run or reloads live configuration for each installation attempt. +/// Configuration read errors are reported as events rather than worker failures. fn spawn_operation( config_path: Option, request: Request, @@ -807,6 +835,9 @@ fn spawn_operation( }) } +/// Runs the installer event loop, starting autorun and offering recovery on a terminal. +/// Noninteractive runs return after work completes and fail if recovery needs user input; +/// `plain` selects text output instead of the full-screen display. pub(super) async fn run( config_path: Option, scenario: Option, @@ -1082,6 +1113,7 @@ pub(super) async fn run( } } +/// Removes terminal control characters while retaining newlines and expanding tabs. fn display_text(text: &str) -> String { text.chars() .filter(|c| !c.is_control() || *c == '\n' || *c == '\t') @@ -1089,6 +1121,7 @@ fn display_text(text: &str) -> String { .replace('\t', " ") } +/// Compares an RPC severity against the chosen filter, treating unspecified as a warning. fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { let level = match level { LogLevel::Error => Level::Error, @@ -1100,6 +1133,7 @@ fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { level.to_level_filter() <= verbosity } +/// Selects the severity color, treating unspecified severity as a warning. fn log_color(level: LogLevel) -> Color { match level { LogLevel::Error => COLOR_ERROR, @@ -1110,6 +1144,7 @@ fn log_color(level: LogLevel) -> Color { } } +/// Provides the short status heading for each installer screen. fn title(screen: Screen) -> &'static str { match screen { Screen::Progress => "INSTALLING", @@ -1124,6 +1159,7 @@ fn title(screen: Screen) -> &'static str { } } +/// Gives panels a consistent ASCII border and VT-aware background. fn block(title: &str, linux_vt: bool) -> Block<'_> { Block::default() .borders(Borders::ALL) @@ -1133,12 +1169,14 @@ fn block(title: &str, linux_vt: bool) -> Block<'_> { .title(title) } +/// Finds the widest wordmark row for layout and centering. fn wordmark_width() -> usize { TRIDENT_WORDMARK .iter() .fold(0, |width, row| width.max(row.len())) } +/// Reserves wordmark space only when both the terminal width and height fit it. fn header_height(viewport: Rect) -> u16 { if usize::from(viewport.width) >= wordmark_width() && viewport.height >= MIN_WORDMARK_TERMINAL_HEIGHT @@ -1149,6 +1187,7 @@ fn header_height(viewport: Rect) -> u16 { } } +/// Draws the wordmark when space permits, plus status and the demo warning. fn render_header( frame: &mut Frame, area: Rect, @@ -1186,6 +1225,7 @@ fn render_header( frame.render_widget(Paragraph::new(lines).alignment(Alignment::Center), area); } +/// Draws a visibly simulated shell without presenting it as a real terminal. fn render_demo_shell(frame: &mut Frame, shell: &DemoShell) { frame.render_widget( Block::default().style(Style::default().bg(COLOR_BACKGROUND)), @@ -1205,6 +1245,7 @@ fn render_demo_shell(frame: &mut Frame, shell: &DemoShell) { ); } +/// Filters and scrolls retained logs, moving the latest result to the end on result screens. fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { let (title, color) = match model.screen { Screen::Success => ("SUCCESS - Installation complete", COLOR_SUCCESS), @@ -1263,6 +1304,7 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { .wrap(Wrap { trim: false }) } +/// Draws the current screen, actions, log controls, and any pending reboot countdown. fn render(frame: &mut Frame, model: &Model, demo: bool) { frame.render_widget( Block::default().style(Style::default().bg(theme_color(COLOR_BACKGROUND, model.linux_vt))), @@ -1387,6 +1429,7 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { } } +/// Overlays the display-only log filter without modifying the stored diagnostics. fn render_verbosity_picker(frame: &mut Frame, model: &Model) { let viewport = frame.area(); let width = viewport.width.min(44); @@ -1438,6 +1481,7 @@ mod tests { use tempfile::TempDir; use trident_proto::v1::{Completed, RebootStatus, ServicingResponse, StatusCode}; + /// A failed serial destination is removed without blocking healthy mirrors. #[test] fn unavailable_serial_mirror_does_not_stop_the_installer() { let root = TempDir::new().unwrap(); @@ -1482,10 +1526,12 @@ mod tests { ); } + /// Creates an unmodified Enter press for navigation tests. fn enter() -> KeyEvent { KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) } + /// Demo commands never reach the host, and `exit` closes the simulation. #[test] fn demo_shell_never_executes_commands_and_exit_returns() { let root = TempDir::new().unwrap(); @@ -1503,6 +1549,7 @@ mod tests { assert!(shell.key(enter())); } + /// The demo shell advertises that it cannot execute commands. #[test] fn demo_shell_is_visibly_simulated() { let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); @@ -1520,6 +1567,7 @@ mod tests { assert!(text.contains("Commands are never executed"), "{text}"); } + /// The full-size header and background keep their intended colors and spacing. #[test] fn wordmark_and_background_use_clear_neutrals_palette_at_80_by_24() { let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); @@ -1557,6 +1605,7 @@ mod tests { assert!(text.contains("ALL ACTIONS SIMULATED"), "{text}"); } + /// Changing display verbosity hides entries without deleting retained logs. #[test] fn live_verbosity_picker_filters_without_discarding_logs() { let mut model = Model::new(); @@ -1584,6 +1633,7 @@ mod tests { assert_eq!(model.logs.len(), 2); } + /// Preparation events replace the initial idle activity with the current step. #[test] fn preparation_steps_replace_the_idle_status() { let mut model = Model::new(); @@ -1594,6 +1644,7 @@ mod tests { assert_eq!(model.activity, step); } + /// RPC log severities and the Trident source receive independent colors. #[test] fn rpc_logs_are_coloured_by_severity() { let mut model = Model::new(); @@ -1629,6 +1680,7 @@ mod tests { } } + /// VT palette slots distinguish log severities without changing RGB-mode colors. #[test] fn linux_vt_palette_uses_distinct_slots_for_a1_log_levels() { let entries = vt_rgb_palette().unwrap(); @@ -1674,6 +1726,7 @@ mod tests { } } + /// The source and verbosity indicator both fit on the standard-width status line. #[test] fn operation_and_verbosity_share_one_line() { let mut model = Model::new(); @@ -1688,6 +1741,7 @@ mod tests { assert!(row.contains("Logs: DEBUG [V]"), "{row}"); } + /// Serial filtering preserves line prefixes and does not change display filtering. #[test] fn serial_verbosity_filters_independently_of_display() { let mut model = Model::new(); @@ -1726,6 +1780,7 @@ mod tests { assert_eq!(model.logs.len(), 6); } + /// Installer logger events retain their own source tag and severity styling. #[test] fn installer_logger_records_use_inst_source_and_colour() { let mut model = Model::new(); @@ -1749,6 +1804,7 @@ mod tests { assert_eq!(model.logs.len(), 1); } + /// The autorun default schedules a reboot when servicing requires one. #[test] fn headless_autorun_keeps_automatic_reboot_enabled_by_default() { let config = Config::parse("mode = 'autorun'\nserialMode = 'logs'").unwrap(); @@ -1765,6 +1821,7 @@ mod tests { assert!(model.reboot_at.is_some()); } + /// Missing inputs produce a tagged failure record suitable for serial output. #[test] fn errors_for_missing_inputs_are_tagged_for_serial_without_a_tui() { let mut model = Model::new(); @@ -1779,6 +1836,7 @@ mod tests { ); } + /// A success result remains highlighted even when earlier logs are filtered out. #[test] fn success_keeps_prior_logs_and_highlights_the_final_record() { let mut model = Model::new(); @@ -1845,6 +1903,7 @@ mod tests { assert!(!text.contains("Earlier disk action")); } + /// Failures show prior logs first while still allowing full diagnostic details. #[test] fn failure_shows_logs_by_default_and_keeps_full_details_accessible() { let mut model = Model::new(); @@ -1901,6 +1960,7 @@ mod tests { assert!(!model.error_details_open); } + /// Compact layouts retain the application heading and available actions. #[test] fn small_terminals_use_a_readable_compact_header() { assert_eq!( @@ -1927,6 +1987,7 @@ mod tests { assert!(text.contains("Shell"), "{text}"); } + /// Recovery reports invalid reloaded configuration instead of using defaults. #[tokio::test] async fn recovery_reloads_configuration_without_using_defaults_on_error() { let root = TempDir::new().unwrap(); @@ -1946,6 +2007,7 @@ mod tests { } } + /// Continuing from an error opens recovery; shell actions leave their screen unchanged. #[test] fn error_continue_and_shell_keep_the_originating_screen() { let mut model = Model::new(); @@ -1963,6 +2025,7 @@ mod tests { assert!(matches!(model.key(enter(), false), Some(Action::Reboot))); } + /// Force reinstall opens with Cancel selected and needs a second confirmation. #[test] fn force_is_never_the_default_and_requires_confirmation() { let mut model = Model::new(); @@ -1975,6 +2038,7 @@ mod tests { assert_eq!(model.screen, Screen::AlreadyPresent); } + /// Standard-size result and recovery screens expose their expected actions. #[test] fn screens_render_at_80_by_24_with_visible_actions() { for screen in [ diff --git a/crates/osutils/src/dependencies.rs b/crates/osutils/src/dependencies.rs index 55f9e0ebed..766948b9d6 100644 --- a/crates/osutils/src/dependencies.rs +++ b/crates/osutils/src/dependencies.rs @@ -231,6 +231,10 @@ pub struct Command { } impl Command { + /// Runs a command in the terminal's foreground process group with attached stdio. + /// + /// Restores the parent's foreground group after the child exits and rejects piped stdin. + /// Nonzero exits are returned as [`DependencyError`] values. pub fn run_interactive(&self, terminal: &File) -> Result<(), Box> { let binary = self.dependency.path()?; let execute = || -> Result { diff --git a/crates/osutils/src/lsblk.rs b/crates/osutils/src/lsblk.rs index 71b5019a65..31edba2532 100644 --- a/crates/osutils/src/lsblk.rs +++ b/crates/osutils/src/lsblk.rs @@ -187,6 +187,7 @@ pub fn devices_by_label(label: impl AsRef) -> Result, Error> { Ok(devices) } +/// Adds paths of devices with an exact filesystem-label match, including descendants. fn collect_devices_by_label(devices: &[BlockDevice], label: &str, matches: &mut Vec) { for device in devices { if device.label.as_deref() == Some(label) { @@ -331,6 +332,7 @@ mod tests { use super::*; #[test] + /// Ensures optical and partition labels both match, and rejects an empty label. fn label_scan_includes_cdrom_and_detects_duplicate_filesystem_labels() { let devices = vec![ BlockDevice { @@ -360,6 +362,7 @@ mod tests { } #[test] + /// Ensures optical-drive labels survive parsing the `lsblk` JSON tree. fn parses_optical_drive_label_from_lsblk_json() { let devices = parse_lsblk_output( r#"{"blockdevices":[{"name":"sr0","size":355497984,"type":"rom","label":"CDROM"}]}"#, @@ -371,6 +374,7 @@ mod tests { } #[test] + /// Ensures a GPT USB partition's label is found beneath its parent disk. fn parses_gpt_usb_partition_label_from_lsblk_json() { let devices = parse_lsblk_output( r#"{"blockdevices":[{"name":"sdb","size":1073741824,"type":"disk","pttype":"gpt","children":[{"name":"sdb1","size":1069547520,"type":"part","fstype":"vfat","label":"TRD_INSTALL"}]}]}"#, diff --git a/crates/osutils/src/systemd.rs b/crates/osutils/src/systemd.rs index 1602d26331..7be4b7f8e9 100644 --- a/crates/osutils/src/systemd.rs +++ b/crates/osutils/src/systemd.rs @@ -42,6 +42,7 @@ where .with_context(|| format!("Failed to restart unit: {}", unit.as_ref())) } +/// Stops the named systemd unit, reporting failures with the unit name. pub fn stop_unit(unit: impl AsRef) -> Result<(), Error> { let unit = unit.as_ref(); Dependency::Systemctl @@ -51,11 +52,13 @@ pub fn stop_unit(unit: impl AsRef) -> Result<(), Error> { .with_context(|| format!("Failed to stop unit '{unit}'")) } +/// Requests a reboot through systemd and reports command failures. pub fn reboot() -> Result<(), Error> { Dependency::Systemctl.cmd().arg("reboot").run_and_check()?; Ok(()) } +/// Requests a power-off through systemd and reports command failures. pub fn poweroff() -> Result<(), Error> { Dependency::Systemctl .cmd() diff --git a/crates/osutils/src/terminal.rs b/crates/osutils/src/terminal.rs index 16a5d4d390..0b9a79a076 100644 --- a/crates/osutils/src/terminal.rs +++ b/crates/osutils/src/terminal.rs @@ -32,17 +32,20 @@ pub fn set_vt_palette(output: &mut impl Write, colours: &[(u8, u8, u8, u8)]) -> output.flush() } +/// Restores the Linux virtual console's built-in ANSI colour palette. pub fn reset_vt_palette(output: &mut impl Write) -> io::Result<()> { output.write_all(b"\x1b]R")?; output.flush() } +/// Reads the active kernel consoles, resolving `tty0` to the graphical `tty1`. pub fn active_consoles() -> Result, Error> { let active = fs::read_to_string(ACTIVE_CONSOLES_PATH) .context("Failed to read active kernel consoles")?; console_paths(&active) } +/// Validates console names before producing unique `/dev` paths. fn console_paths(active: &str) -> Result, Error> { let mut consoles = Vec::new(); for name in active.split_whitespace() { @@ -64,6 +67,7 @@ fn console_paths(active: &str) -> Result, Error> { Ok(consoles) } +/// Prefers `tty1` for the TUI and otherwise uses the first active console. pub fn preferred_console(consoles: &[PathBuf]) -> Result<&Path, Error> { consoles .iter() @@ -76,6 +80,9 @@ pub fn preferred_console(consoles: &[PathBuf]) -> Result<&Path, Error> { .context("No console available") } +/// Claims a character terminal as the controlling TTY and redirects stdin/stdout to it. +/// +/// The caller must stop competing getty units before attaching. pub fn attach(path: impl AsRef) -> Result { let path = path.as_ref(); let terminal = OpenOptions::new() @@ -106,6 +113,7 @@ pub fn attach(path: impl AsRef) -> Result { Ok(terminal) } +/// Transfers foreground terminal ownership while blocking `SIGTTOU` during the handoff. pub(crate) fn set_foreground_group(terminal: &File, group: Pid) -> io::Result<()> { let mut blocked = SigSet::empty(); blocked.add(Signal::SIGTTOU); @@ -117,6 +125,7 @@ pub(crate) fn set_foreground_group(terminal: &File, group: Pid) -> io::Result<() Ok(()) } +/// Runs an interactive Bash on the supplied terminal and returns when it exits. pub fn shell(terminal: &File) -> Result<(), Error> { Dependency::Bash .cmd() @@ -130,6 +139,7 @@ mod tests { use super::*; #[test] + /// Ensures graphical console aliases are deduplicated and invalid names fail. fn graphical_console_wins_and_aliases_are_deduplicated() { let consoles = console_paths("ttyS0 tty0 tty1").unwrap(); assert_eq!( @@ -145,6 +155,7 @@ mod tests { } #[test] + /// Ensures VT palette slots are encoded correctly and out-of-range slots fail. fn linux_vt_palette_sequences_set_rgb_slots_and_reset() { let mut output = Vec::new(); set_vt_palette(&mut output, &[(0, 11, 17, 24), (15, 229, 234, 236)]).unwrap(); diff --git a/test/build.py b/test/build.py index 9605a30113..b08edc6157 100755 --- a/test/build.py +++ b/test/build.py @@ -19,11 +19,13 @@ def run(command, **kwargs): + """Run a build command and fail if it exits unsuccessfully.""" print("+", shlex.join(map(str, command)), flush=True) subprocess.run(list(map(str, command)), check=True, **kwargs) def metadata(path): + """Validate that the fixture is a supported non-ACL Azure Linux COSI.""" with tarfile.open(path, "r:") as archive: member = archive.getmember("metadata.json") if not member.isfile() or member.size > 16 * 1024**2: @@ -51,6 +53,7 @@ def metadata(path): def stage_cosi(path): + """Copy the validated COSI into the ISO recipe and record its disk size.""" path = path.resolve(strict=True) info = metadata(path) destination = IMAGE / "cosi/payload.cosi" @@ -76,6 +79,7 @@ def stage_cosi(path): def build_rpms(): + """Build the Trident RPMs in a container and stage the installer sub-package.""" run(["make", ".cargo/config", "OVERRIDE_RUST_FEED=true"], cwd=REPO) version = tomllib.loads((REPO / "crates/trident/Cargo.toml").read_text())[ "package" @@ -170,6 +174,7 @@ def build_rpms(): def main(): + """Parse fixture options and build or validate the installer ISO.""" parser = argparse.ArgumentParser( description="Build a Tailor ISO with the current Trident Linux Installer" ) diff --git a/test/make_usb.py b/test/make_usb.py index db8b126f38..988c8e2ffc 100644 --- a/test/make_usb.py +++ b/test/make_usb.py @@ -16,6 +16,7 @@ def build_usb(cosi, output, size_mib): + """Create a new GPT/FAT32 image smaller than the COSI's target disk.""" source = cosi.resolve(strict=True) info = metadata(source) if size_mib < 512: @@ -67,6 +68,7 @@ def build_usb(cosi, output, size_mib): def main(): + """Validate image-builder arguments and create a disposable USB fixture.""" parser = argparse.ArgumentParser( description="Create a GPT/FAT32 USB image for installer VM testing" ) diff --git a/test/run.py b/test/run.py index cdec973390..b40c614133 100755 --- a/test/run.py +++ b/test/run.py @@ -27,6 +27,7 @@ def command(iso, scratch, code, memory, cpus, display, vnc=None, usb_media=None): + """Construct QEMU arguments with disposable storage and read-only install media.""" acceleration = ( ["-enable-kvm", "-cpu", "host"] if os.access("/dev/kvm", os.W_OK) else [] ) @@ -84,12 +85,14 @@ def command(iso, scratch, code, memory, cpus, display, vnc=None, usb_media=None) class Qmp: def __init__(self, connection, process): + """Track the QMP connection and viewer state for a paused guest.""" self.connection = connection self.process = process self.pending = b"" self.viewer_connected = False def receive(self): + """Read a QMP response or event, updating the VNC connection state.""" while b"\n" not in self.pending: if self.process.poll() is not None: raise RuntimeError("QEMU exited while waiting for a VNC viewer") @@ -108,6 +111,7 @@ def receive(self): return message def execute(self, command): + """Send a QMP command and wait for its matching response.""" request = {"execute": command, "id": command} self.connection.sendall(json.dumps(request).encode() + b"\n") while True: @@ -119,6 +123,7 @@ def execute(self, command): def wait_for_vnc_viewer(process, monitor_path, on_ready): + """Resume the paused guest only after the VNC monitor reports a viewer.""" deadline = time.monotonic() + QMP_STARTUP_TIMEOUT_SECONDS with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as connection: while True: @@ -147,6 +152,7 @@ def wait_for_vnc_viewer(process, monitor_path, on_ready): def follow_serial(process, path, output=sys.stdout, interval=SERIAL_POLL_SECONDS): + """Print new installer records from serial, excluding firmware and escape codes.""" stream = None pending = "" try: @@ -172,6 +178,7 @@ def follow_serial(process, path, output=sys.stdout, interval=SERIAL_POLL_SECONDS def main(): + """Launch a disposable VM, optionally wait for VNC, and follow serial logs.""" parser = argparse.ArgumentParser( description="Boot the installer ISO in a disposable UEFI VM" ) @@ -297,6 +304,7 @@ def main(): if args.vnc is not None: def show_vnc_instructions(): + """Print loopback VNC connection instructions before guest boot.""" print( f"Guest paused; VNC on localhost:{port} only. Tunnel from your local machine:\n" f" ssh -N -L {port}:127.0.0.1:{port} USER@HOST\n" diff --git a/test/test_scripts.py b/test/test_scripts.py index 2ab0e5ee0e..ad87cb534e 100644 --- a/test/test_scripts.py +++ b/test/test_scripts.py @@ -16,6 +16,7 @@ class VmCommandTests(unittest.TestCase): def test_usb_media_is_a_read_only_gpt_image_with_lower_boot_priority(self): + """The USB fixture is attached read-only behind the disposable disk.""" image = Path("/tmp/disposable-installer-usb.raw") launch = VM.command( Path("/tmp/installer-test.iso"), @@ -35,11 +36,15 @@ def test_usb_media_is_a_read_only_gpt_image_with_lower_boot_priority(self): self.assertNotIn("-virtfs", launch) def test_follows_new_serial_records_without_replaying_firmware_escape_codes(self): + """Only complete and newly appended installer records reach the console.""" + class RunningProcess: def __init__(self): + """Create a flag for simulated guest completion.""" self.finished = threading.Event() def poll(self): + """Return an exit status only after the guest finishes.""" return 0 if self.finished.is_set() else None process = RunningProcess() @@ -48,6 +53,7 @@ def poll(self): output = io.StringIO() def guest(): + """Write firmware noise followed by incremental installer records.""" time.sleep(0.03) with serial.open("w") as log: log.write("\x1b[2JWelcome to firmware\r\n") @@ -74,6 +80,7 @@ def guest(): ) def test_vnc_is_headless_and_bound_to_loopback(self): + """VNC mode pauses boot and binds only the loopback interface.""" launch = VM.command( Path("/tmp/installer-test.iso"), Path("/tmp/installer-test-owned"), @@ -93,8 +100,11 @@ def test_vnc_is_headless_and_bound_to_loopback(self): self.assertNotIn("gtk", launch) def test_vnc_connection_resumes_paused_guest_after_viewer_handshake(self): + """A viewer handshake must precede the QMP continue command.""" + class RunningProcess: def poll(self): + """Simulate a still-running guest.""" return None with tempfile.TemporaryDirectory() as directory: @@ -103,6 +113,7 @@ def poll(self): ready = [] def monitor_server(): + """Serve the minimal QMP handshake and a viewer connection event.""" with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listener: listener.bind(str(monitor)) listener.listen(1) @@ -136,6 +147,7 @@ def monitor_server(): self.assertEqual(ready, [True]) def test_vm_exposes_only_new_disposable_storage(self): + """Only the new qcow2 disk may be a writable guest storage device.""" scratch = Path("/tmp/installer-test-owned") launch = VM.command( Path("/tmp/installer-test.iso"), From 05125c00f9faa940b7ed1488133fa06f7e7bf126 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 21:35:48 -0800 Subject: [PATCH 23/62] chore: Move installer defaults under packaging/etc Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Makefile | 4 ++-- crates/installer/src/config.rs | 2 +- packaging/{ => etc}/installer.toml | 0 packaging/rpm/trident.spec | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename packaging/{ => etc}/installer.toml (100%) diff --git a/Makefile b/Makefile index 6f9a3150a8..c6e5c0da2f 100644 --- a/Makefile +++ b/Makefile @@ -229,7 +229,7 @@ target/$(DISTRO)/release/trident target/$(DISTRO)/release/trident-acl-agent targ -p installer # This will do a proper build on azl3, exactly as the pipelines would, with the custom registry and all. -bin/trident-rpms-%.tar.gz: packaging/docker/Dockerfile.full packaging/installer.toml packaging/systemd/*.service packaging/rpm/trident.spec packaging/selinux-policy-trident/* LICENSE NOTICE version-vars azl-version-vars +bin/trident-rpms-%.tar.gz: packaging/docker/Dockerfile.full packaging/etc/installer.toml packaging/systemd/*.service packaging/rpm/trident.spec packaging/selinux-policy-trident/* LICENSE NOTICE version-vars azl-version-vars @case "$@" in \ *$(DISTRO).tar.gz) ;; \ *) echo "Invalid target '$@' for DISTRO '$(DISTRO)'. Expected target ending in '$(DISTRO).tar.gz'."; exit 1 ;; \ @@ -263,7 +263,7 @@ bin/trident-rpms-%.tar.gz: packaging/docker/Dockerfile.full packaging/installer. # backward compatibility; other distros use the in-container distro build. TRIDENT_RPM_BIN_DIR := $(if $(filter azl3,$(DISTRO)),target/release,target/$(DISTRO)/release) -bin/trident-rpms.tar.gz: azl-version-vars packaging/docker/Dockerfile.azl packaging/installer.toml packaging/systemd/*.service packaging/rpm/trident.spec $(TRIDENT_RPM_BIN_DIR)/trident $(TRIDENT_RPM_BIN_DIR)/trident-acl-agent $(TRIDENT_RPM_BIN_DIR)/trident-installer packaging/selinux-policy-trident/* LICENSE NOTICE +bin/trident-rpms.tar.gz: azl-version-vars packaging/docker/Dockerfile.azl packaging/etc/installer.toml packaging/systemd/*.service packaging/rpm/trident.spec $(TRIDENT_RPM_BIN_DIR)/trident $(TRIDENT_RPM_BIN_DIR)/trident-acl-agent $(TRIDENT_RPM_BIN_DIR)/trident-installer packaging/selinux-policy-trident/* LICENSE NOTICE @mkdir -p bin/ @if [ ! -f bin/trident ] || ! cmp -s $(TRIDENT_RPM_BIN_DIR)/trident bin/trident; then \ cp $(TRIDENT_RPM_BIN_DIR)/trident bin/trident; \ diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index 3660dc0063..3893b13a33 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -344,7 +344,7 @@ mod tests { #[test] /// Ensures the shipped TOML stays compatible with the runtime schema. fn packaged_configuration_matches_the_runtime_schema() { - let config = Config::parse(include_str!("../../../packaging/installer.toml")).unwrap(); + let config = Config::parse(include_str!("../../../packaging/etc/installer.toml")).unwrap(); config.require_autorun().unwrap(); assert_eq!( config.media.filesystem_label.as_deref(), diff --git a/packaging/installer.toml b/packaging/etc/installer.toml similarity index 100% rename from packaging/installer.toml rename to packaging/etc/installer.toml diff --git a/packaging/rpm/trident.spec b/packaging/rpm/trident.spec index df3d4e4113..96243ffe2c 100644 --- a/packaging/rpm/trident.spec +++ b/packaging/rpm/trident.spec @@ -337,7 +337,7 @@ install -D -m 755 target/release/%{name}-acl-agent %{buildroot}/%{_bindir}/%{nam install -D -m 755 target/release/%{name}-installer %{buildroot}/%{_bindir}/%{name}-installer install -D -m 644 packaging/systemd/%{name}-acl-agent.service %{buildroot}%{_unitdir}/%{name}-acl-agent.service install -D -m 644 packaging/systemd/%{name}-installer.service %{buildroot}%{_unitdir}/%{name}-installer.service -install -D -m 644 packaging/installer.toml %{buildroot}/etc/%{name}/installer.toml +install -D -m 644 packaging/etc/installer.toml %{buildroot}/etc/%{name}/installer.toml # Copy Trident SELinux policy module to /usr/share/selinux/packages install -D -m 0644 %{name}.pp.bz2 %{buildroot}%{_datadir}/selinux/packages/%{selinuxtype}/%{name}.pp.bz2 From 6e099a6263af7bdafe0246ae9eac242394f2d765 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 21:50:07 -0800 Subject: [PATCH 24/62] test: Add Tailor TUI installer ISO image Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/images/.gitignore | 1 + tests/images/tailor.yaml | 8 ++ tests/images/tui-installer/.gitignore | 2 + tests/images/tui-installer/README.md | 26 ++++++ .../tui-installer/files/installer-logind.conf | 3 + .../files/installer-serial-getty.conf | 2 + .../images/tui-installer/files/installer.toml | 11 +++ tests/images/tui-installer/image.yaml | 89 +++++++++++++++++++ 8 files changed, 142 insertions(+) create mode 100644 tests/images/.gitignore create mode 100644 tests/images/tailor.yaml create mode 100644 tests/images/tui-installer/.gitignore create mode 100644 tests/images/tui-installer/README.md create mode 100644 tests/images/tui-installer/files/installer-logind.conf create mode 100644 tests/images/tui-installer/files/installer-serial-getty.conf create mode 100644 tests/images/tui-installer/files/installer.toml create mode 100644 tests/images/tui-installer/image.yaml diff --git a/tests/images/.gitignore b/tests/images/.gitignore new file mode 100644 index 0000000000..40a4bdbe07 --- /dev/null +++ b/tests/images/.gitignore @@ -0,0 +1 @@ +/.tailor/ diff --git a/tests/images/tailor.yaml b/tests/images/tailor.yaml new file mode 100644 index 0000000000..36a874321c --- /dev/null +++ b/tests/images/tailor.yaml @@ -0,0 +1,8 @@ +schemaVersion: 1 + +toolchains: + default: ic + entries: + - name: ic + container: mcr.microsoft.com/azurelinux/imagecustomizer + tag: "1.6" diff --git a/tests/images/tui-installer/.gitignore b/tests/images/tui-installer/.gitignore new file mode 100644 index 0000000000..85bebb1a42 --- /dev/null +++ b/tests/images/tui-installer/.gitignore @@ -0,0 +1,2 @@ +/cosi/ +/rpms/ diff --git a/tests/images/tui-installer/README.md b/tests/images/tui-installer/README.md new file mode 100644 index 0000000000..f1fd0abf2b --- /dev/null +++ b/tests/images/tui-installer/README.md @@ -0,0 +1,26 @@ +# TUI installer ISO + +Tailor builds an Azure Linux 3 ISO with the `trident-installer` RPM and Trident's +gRPC daemon. The installer boots in autorun mode, mounts the `CDROM`-labelled +ISO read-only, and streams the staged COSI. It does **not** enter demo mode: +boot only with a disposable target disk. The success screen remains visible +until an operator chooses Reboot. + +Stage matching Trident RPMs (including `trident-installer`) in `rpms/` and an +Azure Linux COSI in `cosi/payload.cosi`. These inputs are local and ignored by +Git. Use matching RPMs from `bin/RPMS/` after building the Trident RPMs, +and a COSI such as `artifacts/trident-testimage.cosi` from the existing image +builder. From the repository root: + +```console +tailor --manifest tests/images/tailor.yaml validate +tailor --manifest tests/images/tailor.yaml build tui-installer \ + --output-dir tests/images/artifacts +``` + +The generated ISO is `tests/images/artifacts/tui-installer_amd64_iso.iso`. +Build this image with Tailor, not the legacy `testimages.py` CLI. +The checked-in configuration uses `filesystemLabel = "CDROM"` to match Image +Customizer ISO output; change it if the image is copied to differently labelled +media. `trident-installer` is optional in the RPM build, but its package must +be present in `rpms/` for this image to build. diff --git a/tests/images/tui-installer/files/installer-logind.conf b/tests/images/tui-installer/files/installer-logind.conf new file mode 100644 index 0000000000..6e1a22c65d --- /dev/null +++ b/tests/images/tui-installer/files/installer-logind.conf @@ -0,0 +1,3 @@ +[Login] +NAutoVTs=0 +ReserveVT=0 diff --git a/tests/images/tui-installer/files/installer-serial-getty.conf b/tests/images/tui-installer/files/installer-serial-getty.conf new file mode 100644 index 0000000000..0721aef5e2 --- /dev/null +++ b/tests/images/tui-installer/files/installer-serial-getty.conf @@ -0,0 +1,2 @@ +[Unit] +ConditionPathExists=!/etc/trident/installer.toml diff --git a/tests/images/tui-installer/files/installer.toml b/tests/images/tui-installer/files/installer.toml new file mode 100644 index 0000000000..49d2a72b9b --- /dev/null +++ b/tests/images/tui-installer/files/installer.toml @@ -0,0 +1,11 @@ +mode = "autorun" +serialMode = "logs" +serialVerbosity = "debug" + +[media] +filesystemLabel = "CDROM" +cosiDirectory = "cosi/" + +[autorun] +reboot = false +force = false diff --git a/tests/images/tui-installer/image.yaml b/tests/images/tui-installer/image.yaml new file mode 100644 index 0000000000..a2093bce6e --- /dev/null +++ b/tests/images/tui-installer/image.yaml @@ -0,0 +1,89 @@ +name: tui-installer + +outputs: + - format: iso + +base: + azureLinux: + version: "3.0" + variant: minimal-os + +rpmSources: + - rpms + +extraDependencies: + - files + - cosi + - rpms + +config: + previewFeatures: + - input-image-oci + os: + hostname: trident-installer + bootloader: + resetType: hard-reset + selinux: + mode: disabled + kernelCommandLine: + extraCommandLine: + - console=tty0 + - console=ttyS0,115200n8 + - quiet + - loglevel=0 + - systemd.show_status=false + packages: + install: + - trident-installer + - device-mapper + - squashfs-tools + - veritysetup + - curl + - less + - iproute + services: + enable: + - tridentd.socket + - trident-installer.service + disable: + - getty@tty1.service + additionalFiles: + - source: files/installer.toml + destination: /etc/trident/installer.toml + - source: files/installer-logind.conf + destination: /etc/systemd/logind.conf.d/installer.conf + - source: files/installer-serial-getty.conf + destination: /etc/systemd/system/serial-getty@ttyS0.service.d/installer.conf + storage: + bootType: efi + disks: + - partitionTableType: gpt + maxSize: 4G + partitions: + - id: esp + type: esp + size: 8M + - id: rootfs + size: grow + filesystems: + - deviceId: esp + type: fat32 + mountPoint: + path: /boot/efi + options: umask=0077 + - deviceId: rootfs + type: ext4 + mountPoint: + path: / + iso: + initramfsType: full-os + additionalFiles: + - source: cosi/payload.cosi + destination: /cosi/payload.cosi + kernelCommandLine: + extraCommandLine: + - console=tty0 + - console=ttyS0,115200n8 + - quiet + - loglevel=0 + - systemd.show_status=false From 4fd8e2a04ab476a97a2ad20aa58629ce509a4acf Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 21:50:27 -0800 Subject: [PATCH 25/62] docs: Show how to boot Tailor installer ISO in VNC Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/images/tui-installer/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/images/tui-installer/README.md b/tests/images/tui-installer/README.md index f1fd0abf2b..30f4086fd5 100644 --- a/tests/images/tui-installer/README.md +++ b/tests/images/tui-installer/README.md @@ -20,6 +20,18 @@ tailor --manifest tests/images/tailor.yaml build tui-installer \ The generated ISO is `tests/images/artifacts/tui-installer_amd64_iso.iso`. Build this image with Tailor, not the legacy `testimages.py` CLI. +To view the installer in a disposable VM: + +```console +python3 test/run.py --iso tests/images/artifacts/tui-installer_amd64_iso.iso --vnc +``` + +Connect a VNC viewer to `vnc://127.0.0.1:5901`; the VM waits for a viewer +before booting. If the VM host is remote, tunnel that port as described in +[`test/README.md`](../../../test/README.md#over-ssh-including-from-a-wayland-desktop). +Use `--disk-size` when the COSI needs more than the launcher's default 8 GiB +disposable target disk. + The checked-in configuration uses `filesystemLabel = "CDROM"` to match Image Customizer ISO output; change it if the image is copied to differently labelled media. `trident-installer` is optional in the RPM build, but its package must From b6a6d24afccd8a283ff54a17599e3eb76f14d96c Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 22:35:37 -0800 Subject: [PATCH 26/62] docs: Boot TUI installer with virtdeploy and netlaunch Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/images/tui-installer/README.md | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/tests/images/tui-installer/README.md b/tests/images/tui-installer/README.md index 30f4086fd5..04655730da 100644 --- a/tests/images/tui-installer/README.md +++ b/tests/images/tui-installer/README.md @@ -20,17 +20,28 @@ tailor --manifest tests/images/tailor.yaml build tui-installer \ The generated ISO is `tests/images/artifacts/tui-installer_amd64_iso.iso`. Build this image with Tailor, not the legacy `testimages.py` CLI. -To view the installer in a disposable VM: +Use the existing virt-deploy and netlaunch tools to boot it in a VM with a new +disposable disk (at least as large as the COSI's target disk): ```console -python3 test/run.py --iso tests/images/artifacts/tui-installer_amd64_iso.iso --vnc +mkdir -p test/.work +bin/virtdeploy create-one --namespace tui-installer-demo \ + --network 192.168.244.0/24 --disks 8 --mem 6 \ + --no-secure-boot --no-tpm \ + --netlaunch test/.work/tui-installer-netlaunch.yaml +bin/netlaunch --port 46119 \ + --config test/.work/tui-installer-netlaunch.yaml \ + --iso tests/images/artifacts/tui-installer_amd64_iso.iso ``` -Connect a VNC viewer to `vnc://127.0.0.1:5901`; the VM waits for a viewer -before booting. If the VM host is remote, tunnel that port as described in -[`test/README.md`](../../../test/README.md#over-ssh-including-from-a-wayland-desktop). -Use `--disk-size` when the COSI needs more than the launcher's default 8 GiB -disposable target disk. +The ISO-only `--port` mode needs netlaunch with the fix in #909; older versions +exit on the firmware's first HTTP probe before the ISO can boot. Leave +netlaunch serving until the installer has loaded. In another terminal, run +`virsh --connect qemu:///system domdisplay tui-installer-demo-vm-0` to find +the SPICE address. Connect with a SPICE viewer; for a remote VM host, tunnel +that address's localhost port over SSH. When finished, stop netlaunch and run +`bin/virtdeploy clean --namespace tui-installer-demo` to remove only this VM, +its disk, and its libvirt network. The checked-in configuration uses `filesystemLabel = "CDROM"` to match Image Customizer ISO output; change it if the image is copied to differently labelled From 4dac5fbea341d82b8a976318cc61c6eb18b83f2f Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 23:12:30 -0800 Subject: [PATCH 27/62] fix: Load root login profile in installer shell Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 6 ++++-- crates/osutils/src/terminal.rs | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 2e380a846a..1b2e7f7eba 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -112,8 +112,10 @@ scrollable full error details and Esc returns to the logs. Successful installs keep their logs in a green result frame. Each result is also the last highlighted log entry. Continue opens recovery: Shell, Stream COSI URL, remote HC URL, or Shutdown. Shell `exit` returns to the -originating screen. Installation continues while a shell is open; automatic -reboot waits. The success screen offers Reboot and Shell. +originating screen. The recovery shell is a root login Bash, so it reads +`/etc/profile` and the root profile (if present); on the installer ISO, +`/etc/profile` gives root a red prompt. Installation continues while a shell +is open; automatic reboot waits. The success screen offers Reboot and Shell. Before each recovery attempt the baked configuration is reloaded, so shell edits take effect. Invalid or missing configuration never supplies implicit defaults for an installation. diff --git a/crates/osutils/src/terminal.rs b/crates/osutils/src/terminal.rs index 0b9a79a076..2658b8c25c 100644 --- a/crates/osutils/src/terminal.rs +++ b/crates/osutils/src/terminal.rs @@ -125,11 +125,11 @@ pub(crate) fn set_foreground_group(terminal: &File, group: Pid) -> io::Result<() Ok(()) } -/// Runs an interactive Bash on the supplied terminal and returns when it exits. +/// Runs an interactive login Bash on the supplied terminal, loading the root profile. pub fn shell(terminal: &File) -> Result<(), Error> { Dependency::Bash .cmd() - .args(["--noprofile", "--norc", "-i"]) + .args(["--login", "-i"]) .run_interactive(terminal)?; Ok(()) } From 1f0247ad139e4eef5771f50b39baf3bf753133d0 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 4 Nov 2026 23:41:17 -0800 Subject: [PATCH 28/62] docs: Add installer TOML configuration reference Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 2 + docs/Reference/Installer-Configuration.md | 95 +++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 docs/Reference/Installer-Configuration.md diff --git a/crates/installer/README.md b/crates/installer/README.md index 1b2e7f7eba..e02effd6a8 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -22,6 +22,8 @@ exits when complete. ## Configuration Read `/etc/trident/installer.toml` or the path supplied with `--config`. +See the [Installer Configuration reference](../../docs/Reference/Installer-Configuration.md) +for all fields, defaults, and media-overlay rules. `mode` is mandatory. All other fields are optional: ```toml diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md new file mode 100644 index 0000000000..d68c2ea1d2 --- /dev/null +++ b/docs/Reference/Installer-Configuration.md @@ -0,0 +1,95 @@ +--- +sidebar_position: 7 +title: Installer Configuration +--- + +# Installer Configuration (TOML) + +`trident-installer` reads `/etc/trident/installer.toml` by default. Pass +`--config PATH` to use another file. This TOML file controls the **installer**; +the [Host Configuration](./Host-Configuration/Host-Configuration.md) is a +separate YAML document describing what Trident installs. A live installation +requires a readable TOML file. `--demo` does not read it. + +The packaged default is: + +```toml +mode = "autorun" +serialMode = "logs" +serialVerbosity = "debug" + +[media] +filesystemLabel = "TRD_INSTALL" +cosiDirectory = "cosi/" + +[autorun] +reboot = true +force = false +``` + +Only `mode` is required. Other fields may be omitted; unknown fields and +invalid values are errors. + +## Fields + +| Key | Default | Description | +| --- | --- | --- | +| `mode` | **Required** | Only `"autorun"` works. `"interactive"` is reserved and rejected. | +| `serialMode` | `"logs"` | Sends plain installer and Trident log records to active serial consoles, without a serial TUI. `"ui"` is reserved and rejected. | +| `serialVerbosity` | `"debug"` | Serial log filter: `"off"`, `"error"`, `"warn"`, `"info"`, `"debug"`, or `"trace"`. Independent of the on-screen log filter, which starts at Debug. | +| `media.filesystemLabel` | `"TRD_INSTALL"` | Exact filesystem label to find on optical media or a USB partition. `TRD_INSTALL` fits FAT's 11-character label limit; Image Customizer ISOs use `CDROM` instead. | +| `media.cdromLabel` | — | Legacy alias for `filesystemLabel`. Do not specify both names. | +| `media.mountPath` | Unset | Absolute path to an **already mounted** filesystem instead of label discovery. Cannot be combined with `filesystemLabel`; this path is not mounted or made read-only by the installer. | +| `media.cosiDirectory` | `"cosi"` | Nonempty directory relative to the media root. Absolute paths and `..` are rejected. Only regular `.cosi` files directly inside it are considered, in filename order. | +| `autorun.hostConfiguration` | Unset | Host Configuration source. When omitted, `/etc/trident/config.yaml` is used **if it exists**; otherwise autorun looks for a COSI. | +| `autorun.reboot` | `true` | Reboot after successful servicing **only if** Trident reports that work was performed and a reboot is required. | +| `autorun.force` | `false` | On automatic COSI streaming, bypass the matching-filesystem-UUID repeat-install guard. Does not affect Host Configuration installs, manual recovery sources, or the mounted/read-only disk safety check. | + +`filesystemLabel` discovery waits up to 60 seconds during autorun for late +USB/BMC device enumeration. It fails if multiple devices have the label. The +selected filesystem is mounted read-only at `/run/trident/installer-media`; +the live OS must support its filesystem. A manually selected remote recovery +source does not wait for media. + +### Host Configuration sources + +`autorun.hostConfiguration` accepts: + +| Form | Example | Resolution | +| --- | --- | --- | +| Media-relative path | `"installer/host.yaml"` | Beneath the mounted media root; cannot escape it. | +| Absolute path | `"/etc/trident/config.yaml"` | A file in the live installer OS. | +| File URL | `"file:///etc/trident/config.yaml"` | A file in the live installer OS. | +| HTTP(S) URL | `"https://example.com/host.yaml"` | Fetched over the network. | + +Host Configuration input must be valid YAML and is limited to 2 MiB. A +configured or default Host Configuration takes priority over COSI streaming; +an invalid or unreadable one is an error, **not** permission to fall back to +a COSI. A Host Configuration image URL of `installer://image` resolves to the +first discovered COSI on the media; it fails if no COSI is available. Authored +disk paths and image checksums remain unchanged. + +Without a Host Configuration, the first regular COSI in `cosiDirectory` is +streamed. If neither source is available, the graphical console offers +recovery; a headless run reports an error instead. Even with +`autorun.force = true`, streaming refuses a mounted or read-only disk that +Trident could select as its smallest fitting target, such as a large +installer USB. + +## Optional configuration on the media + +After finding or mounting media, the installer merges +`installer/installer.toml` from its root over the bootstrap configuration. +For example: + +```toml +[autorun] +reboot = false +``` + +The media file can change `serialVerbosity`, `media.cosiDirectory`, `mode`, +and `autorun` options. It cannot change `serialMode`, +`media.filesystemLabel` (including `cdromLabel`), or `media.mountPath`: +those settings are needed to find the media in the first place. Other +fields still undergo the same validation; setting `mode = "interactive"` +remains unsupported. From 1693e3aeaa7264ce261ad8867a62231247a914c4 Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 5 Nov 2026 15:29:42 -0800 Subject: [PATCH 29/62] docs: Explain installer Install and StreamDisk selection Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 4 +- docs/Reference/Installer-Configuration.md | 63 ++++++++++++++++++++--- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index e02effd6a8..83173e1933 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -24,7 +24,9 @@ exits when complete. Read `/etc/trident/installer.toml` or the path supplied with `--config`. See the [Installer Configuration reference](../../docs/Reference/Installer-Configuration.md) for all fields, defaults, and media-overlay rules. -`mode` is mandatory. All other fields are optional: +`mode = "autorun"` is required and controls startup, not the servicing +operation: a Host Configuration selects Install; without one, the first media +COSI selects StreamDisk. All other fields are optional: ```toml mode = "autorun" diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index d68c2ea1d2..22a35da0cf 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -30,11 +30,52 @@ force = false Only `mode` is required. Other fields may be omitted; unknown fields and invalid values are errors. +## Selecting Install or StreamDisk + +`mode = "autorun"` means **start without an interactive source picker**. It +does not choose the Trident operation; `mode = "install"` and `mode = "stream"` +are not valid TOML settings. After looking for media and applying any +[media-local overrides](#optional-configuration-on-the-media), autorun makes +this choice: + +| Priority | Condition | Operation | +| --- | --- | --- | +| 1 | `autorun.hostConfiguration` is set | **Install** using that Host Configuration, even if a COSI is present. | +| 2 | Otherwise, `/etc/trident/config.yaml` exists in the live OS | **Install** using that file, even if a COSI is present. | +| 3 | Otherwise, media contains a `.cosi` file | **StreamDisk** using the first regular COSI in `media.cosiDirectory`, sorted by filename. | +| 4 | Neither source is available | Show recovery actions on the graphical console, or report an error without one. | + +For example, to **Install** from a Host Configuration placed on an +Image Customizer ISO: + +```toml +# /etc/trident/installer.toml in the live installer OS +mode = "autorun" + +[media] +filesystemLabel = "CDROM" + +[autorun] +hostConfiguration = "installer/host.yaml" +reboot = false +``` + +Put `installer/host.yaml` on the ISO filesystem (not only in the live OS). +If its `image.url` is `installer://image`, also put a COSI in `cosi/`; the +marker resolves to the first discovered COSI, but the operation remains +**Install**, with disk layout taken from the Host Configuration. An explicit +image URL in the Host Configuration does not require a media COSI. See the +[Host Configuration reference](./Host-Configuration/Host-Configuration.md) +for its YAML schema. To **StreamDisk** instead, leave +`autorun.hostConfiguration` unset *and* ensure +`/etc/trident/config.yaml` is absent from the live OS. A specified but +invalid Host Configuration fails rather than falling back to streaming. + ## Fields | Key | Default | Description | | --- | --- | --- | -| `mode` | **Required** | Only `"autorun"` works. `"interactive"` is reserved and rejected. | +| `mode` | **Required** | Only `"autorun"` works; this is a startup mode, not the Install/StreamDisk selector. `"interactive"` is reserved and rejected. | | `serialMode` | `"logs"` | Sends plain installer and Trident log records to active serial consoles, without a serial TUI. `"ui"` is reserved and rejected. | | `serialVerbosity` | `"debug"` | Serial log filter: `"off"`, `"error"`, `"warn"`, `"info"`, `"debug"`, or `"trace"`. Independent of the on-screen log filter, which starts at Debug. | | `media.filesystemLabel` | `"TRD_INSTALL"` | Exact filesystem label to find on optical media or a USB partition. `TRD_INSTALL` fits FAT's 11-character label limit; Image Customizer ISOs use `CDROM` instead. | @@ -62,12 +103,11 @@ source does not wait for media. | File URL | `"file:///etc/trident/config.yaml"` | A file in the live installer OS. | | HTTP(S) URL | `"https://example.com/host.yaml"` | Fetched over the network. | -Host Configuration input must be valid YAML and is limited to 2 MiB. A -configured or default Host Configuration takes priority over COSI streaming; -an invalid or unreadable one is an error, **not** permission to fall back to -a COSI. A Host Configuration image URL of `installer://image` resolves to the -first discovered COSI on the media; it fails if no COSI is available. Authored -disk paths and image checksums remain unchanged. +Host Configuration input must be valid YAML and is limited to 2 MiB. A Host +Configuration image URL of `installer://image` fails if no COSI is available. +Authored disk paths and image checksums remain unchanged. Even with an +explicit Host Configuration source, autorun checks for media first (up to +the 60-second label-discovery wait) so it can resolve `installer://image`. Without a Host Configuration, the first regular COSI in `cosiDirectory` is streamed. If neither source is available, the graphical console offers @@ -80,13 +120,20 @@ installer USB. After finding or mounting media, the installer merges `installer/installer.toml` from its root over the bootstrap configuration. -For example: +For example, this media-local file selects **Install** without changing the +bootstrap file baked into the live OS: ```toml +# /installer/installer.toml on the media [autorun] +hostConfiguration = "installer/host.yaml" reboot = false ``` +The bootstrap `media.filesystemLabel` must already match the media for this +overlay to be found. Place the referenced `installer/host.yaml` on the same +media. + The media file can change `serialVerbosity`, `media.cosiDirectory`, `mode`, and `autorun` options. It cannot change `serialMode`, `media.filesystemLabel` (including `cdromLabel`), or `media.mountPath`: From b250c98cb824c35606cc508bb19ae09310e7ce1c Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 5 Nov 2026 15:38:18 -0800 Subject: [PATCH 30/62] docs: Clarify installer mode is required Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/Reference/Installer-Configuration.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index 22a35da0cf..0cb809fcb6 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -11,7 +11,7 @@ the [Host Configuration](./Host-Configuration/Host-Configuration.md) is a separate YAML document describing what Trident installs. A live installation requires a readable TOML file. `--demo` does not read it. -The packaged default is: +The packaged `/etc/trident/installer.toml` explicitly sets: ```toml mode = "autorun" @@ -27,8 +27,9 @@ reboot = true force = false ``` -Only `mode` is required. Other fields may be omitted; unknown fields and -invalid values are errors. +There is **no implicit default for `mode`** in a TOML file: omitting it is an +error, even though the packaged file sets it to `"autorun"`. All other fields +may be omitted; unknown fields and invalid values are errors. ## Selecting Install or StreamDisk @@ -73,9 +74,12 @@ invalid Host Configuration fails rather than falling back to streaming. ## Fields +The Default column gives the effective value when a key is omitted, not +necessarily the value written in the packaged file. + | Key | Default | Description | | --- | --- | --- | -| `mode` | **Required** | Only `"autorun"` works; this is a startup mode, not the Install/StreamDisk selector. `"interactive"` is reserved and rejected. | +| `mode` | **None; required** | Set `"autorun"` (as the packaged file does). This is a startup mode, not the Install/StreamDisk selector. `"interactive"` is reserved and rejected. | | `serialMode` | `"logs"` | Sends plain installer and Trident log records to active serial consoles, without a serial TUI. `"ui"` is reserved and rejected. | | `serialVerbosity` | `"debug"` | Serial log filter: `"off"`, `"error"`, `"warn"`, `"info"`, `"debug"`, or `"trace"`. Independent of the on-screen log filter, which starts at Debug. | | `media.filesystemLabel` | `"TRD_INSTALL"` | Exact filesystem label to find on optical media or a USB partition. `TRD_INSTALL` fits FAT's 11-character label limit; Image Customizer ISOs use `CDROM` instead. | From f964cefa26a8d354caabc6c628dade145870e9bd Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 5 Nov 2026 15:50:46 -0800 Subject: [PATCH 31/62] feat: Start installer RPM only on marked live media Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Makefile | 4 +- crates/installer/README.md | 49 ++++++++++--------- crates/installer/src/config.rs | 7 ++- crates/installer/src/source.rs | 41 ++++++++++++---- docs/Reference/Installer-Configuration.md | 47 +++++++++++++++--- packaging/etc/installer.toml | 1 - packaging/rpm/trident.spec | 15 +++++- .../trident-installer.conf | 2 + .../trident-installer.conf | 2 + packaging/systemd/trident-installer.service | 1 + test/README.md | 14 +++--- test/installer/files/installer-logind.conf | 3 -- .../files/installer-serial-getty.conf | 2 - test/installer/files/installer.toml | 1 - test/installer/image.yaml | 17 +------ tests/images/tui-installer/README.md | 18 ++++--- .../tui-installer/files/installer-logind.conf | 3 -- .../files/installer-serial-getty.conf | 2 - .../images/tui-installer/files/installer.toml | 11 ----- tests/images/tui-installer/image.yaml | 21 +------- 20 files changed, 141 insertions(+), 120 deletions(-) create mode 100644 packaging/systemd/getty@tty1.service.d/trident-installer.conf create mode 100644 packaging/systemd/serial-getty@.service.d/trident-installer.conf delete mode 100644 test/installer/files/installer-logind.conf delete mode 100644 test/installer/files/installer-serial-getty.conf delete mode 100644 tests/images/tui-installer/files/installer-logind.conf delete mode 100644 tests/images/tui-installer/files/installer-serial-getty.conf delete mode 100644 tests/images/tui-installer/files/installer.toml diff --git a/Makefile b/Makefile index c6e5c0da2f..b3654b829d 100644 --- a/Makefile +++ b/Makefile @@ -229,7 +229,7 @@ target/$(DISTRO)/release/trident target/$(DISTRO)/release/trident-acl-agent targ -p installer # This will do a proper build on azl3, exactly as the pipelines would, with the custom registry and all. -bin/trident-rpms-%.tar.gz: packaging/docker/Dockerfile.full packaging/etc/installer.toml packaging/systemd/*.service packaging/rpm/trident.spec packaging/selinux-policy-trident/* LICENSE NOTICE version-vars azl-version-vars +bin/trident-rpms-%.tar.gz: packaging/docker/Dockerfile.full packaging/etc/installer.toml packaging/systemd/*.service packaging/systemd/*/*.conf packaging/rpm/trident.spec packaging/selinux-policy-trident/* LICENSE NOTICE version-vars azl-version-vars @case "$@" in \ *$(DISTRO).tar.gz) ;; \ *) echo "Invalid target '$@' for DISTRO '$(DISTRO)'. Expected target ending in '$(DISTRO).tar.gz'."; exit 1 ;; \ @@ -263,7 +263,7 @@ bin/trident-rpms-%.tar.gz: packaging/docker/Dockerfile.full packaging/etc/instal # backward compatibility; other distros use the in-container distro build. TRIDENT_RPM_BIN_DIR := $(if $(filter azl3,$(DISTRO)),target/release,target/$(DISTRO)/release) -bin/trident-rpms.tar.gz: azl-version-vars packaging/docker/Dockerfile.azl packaging/etc/installer.toml packaging/systemd/*.service packaging/rpm/trident.spec $(TRIDENT_RPM_BIN_DIR)/trident $(TRIDENT_RPM_BIN_DIR)/trident-acl-agent $(TRIDENT_RPM_BIN_DIR)/trident-installer packaging/selinux-policy-trident/* LICENSE NOTICE +bin/trident-rpms.tar.gz: azl-version-vars packaging/docker/Dockerfile.azl packaging/etc/installer.toml packaging/systemd/*.service packaging/systemd/*/*.conf packaging/rpm/trident.spec $(TRIDENT_RPM_BIN_DIR)/trident $(TRIDENT_RPM_BIN_DIR)/trident-acl-agent $(TRIDENT_RPM_BIN_DIR)/trident-installer packaging/selinux-policy-trident/* LICENSE NOTICE @mkdir -p bin/ @if [ ! -f bin/trident ] || ! cmp -s $(TRIDENT_RPM_BIN_DIR)/trident bin/trident; then \ cp $(TRIDENT_RPM_BIN_DIR)/trident bin/trident; \ diff --git a/crates/installer/README.md b/crates/installer/README.md index 83173e1933..6e3b02f96b 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -34,9 +34,9 @@ serialMode = "logs" serialVerbosity = "debug" [media] -filesystemLabel = "TRD_INSTALL" cosiDirectory = "cosi/" -# Instead of filesystemLabel, supply mountPath for already-mounted media. +# Optional: filesystemLabel = "CUSTOM_LABEL" +# Or use mountPath for already-mounted media. [autorun] # If omitted, use /etc/trident/config.yaml when that file exists. @@ -53,10 +53,11 @@ an error rather than showing an unfinished serial TUI. `serialVerbosity` defaults to `debug` and accepts `off`, `error`, `warn`, `info`, `debug`, and `trace`. -`media.filesystemLabel` is a filesystem label, not a CD-ROM device name. The -FAT-compatible default (`TRD_INSTALL`, 11 characters) can identify an ISO9660 -filesystem or a FAT filesystem on a GPT USB partition. Set it to the actual -label on the installation medium (Image Customizer ISO output uses `CDROM`). +`media.filesystemLabel` is a filesystem label, not a CD-ROM device name. When +omitted, the installer looks for either `TRD_INSTALL` (FAT-compatible, 11 +characters) or `CDROM` (Image Customizer's ISO label), and rejects ambiguity +if both are present. Set it explicitly for other labels or when booting from +an optical ISO alongside a separate installer USB. The older `cdromLabel` key is accepted as an alias; setting both is an error. Media is mounted read-only with filesystem auto-detection at `/run/trident/installer-media`. Media discovery waits up to 60 seconds for @@ -102,13 +103,11 @@ and gRPC logs, never a TUI, even when no graphical console exists. Autorun continues without an interactive console; missing inputs, repeat-install protection and other cases needing operator input emit an error and stop. Failures to open or write a serial console are logged and mirroring to that -console stops; they do not stop installation. Disable serial getty on a -logs-only installer ISO so its login prompt cannot mix with application logs. +console stops; they do not stop installation. On boots with +`trident.installer=1`, the RPM's getty drop-ins prevent serial login prompts +from mixing with the log stream and prevent tty1 getty from taking input. Firmware, kernel and systemd output during boot is separate from the installer's stream and may precede it; scrapers should match tagged log lines. -The ISO must also disable `getty@tty1.service` and suppress logind's automatic -virtual gettys (`NAutoVTs=0`, `ReserveVT=0`): stopping a getty before -`getty.target` starts does not prevent it from claiming tty1 later. No ordinary source picker or confirmation precedes autorun. Missing sources and failures keep recent logs visible in a red result frame; `D` opens the @@ -168,17 +167,23 @@ automatically reboot. ## ISO integration -Install `trident-installer`, then explicitly enable `trident-installer.service` -and `tridentd.socket` in the ISO recipe. Installing the RPM alone does not enable -the installer. Do not also enable `trident-install.service`. The daemon must be -built with `grpc-preview` for regular Install; repository RPM builds enable it. -Replace older installer startup units and autologin entrypoints rather than -running both installers on the same ISO. - -The ISO volume label must match configuration. Image Customizer's existing -`CDROM` label requires an override or relabelling. With full-OS initramfs, keep -SELinux disabled and include USB storage/UAS, CD-ROM/SCSI, and USB host-controller -drivers. QEMU's IDE CD-ROM does not validate BMC virtual-media support. +On a full-OS installer ISO, install `trident-installer`, add +`trident.installer=1` to its **ISO kernel command line**, and provide a COSI +under `cosi/` or a Host Configuration. The RPM pulls in Trident and common +installer tools, enables its service, starts the gRPC socket through the +service dependency, and suppresses tty1 and serial gettys on marked boots. +Without that flag the service skips startup and normal gettys are unchanged, +including on an installed OS that happens to have the RPM. + +Do not also enable `trident-install.service` or older autologin entrypoints. +The daemon must be built with `grpc-preview` for regular Install; repository +RPM builds enable it. For Image Customizer ISOs, `CDROM` is automatically +recognized; for GPT USB media, `TRD_INSTALL` is automatically recognized. +Specify `media.filesystemLabel` in the bootstrap TOML only to override those +labels or disambiguate multiple matching devices. With full-OS initramfs, +keep SELinux disabled and include USB storage/UAS, CD-ROM/SCSI, and USB +host-controller drivers. QEMU's IDE CD-ROM does not validate BMC virtual +media support. ## Validation diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index 3893b13a33..cbf5a71cf3 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -14,6 +14,7 @@ pub(super) const DEFAULT_HOST_CONFIGURATION: &str = "/etc/trident/config.yaml"; pub(super) const MEDIA_CONFIG: &str = "installer/installer.toml"; pub(super) const MEDIA_MOUNT: &str = "/run/trident/installer-media"; pub(super) const DEFAULT_MEDIA_LABEL: &str = "TRD_INSTALL"; +pub(super) const DEFAULT_MEDIA_LABELS: &[&str] = &[DEFAULT_MEDIA_LABEL, "CDROM"]; #[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase", deny_unknown_fields)] @@ -346,10 +347,8 @@ mod tests { fn packaged_configuration_matches_the_runtime_schema() { let config = Config::parse(include_str!("../../../packaging/etc/installer.toml")).unwrap(); config.require_autorun().unwrap(); - assert_eq!( - config.media.filesystem_label.as_deref(), - Some(DEFAULT_MEDIA_LABEL) - ); + assert!(config.media.filesystem_label.is_none()); + assert_eq!(DEFAULT_MEDIA_LABELS, ["TRD_INSTALL", "CDROM"]); assert!(config.autorun.reboot); } diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 6283b00c5f..debbc51f83 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -169,23 +169,26 @@ fn media_root( ); return Ok(Some(path.canonicalize()?)); } - let label = settings - .media - .filesystem_label - .as_deref() - .unwrap_or(config::DEFAULT_MEDIA_LABEL); + let label = settings.media.filesystem_label.as_deref(); progress("Looking for installer media by filesystem label")?; let deadline = Instant::now() + MEDIA_DISCOVERY_TIMEOUT; let mut attempts = 0; let devices = loop { - let devices = lsblk::devices_by_label(label)?; + let mut devices = + lsblk::find(|device| media_label_matches(device.label.as_deref(), label))? + .into_iter() + .map(|device| device.device_path()) + .collect::>(); + devices.sort(); + devices.dedup(); if !devices.is_empty() || !wait_for_media || Instant::now() >= deadline { break devices; } if attempts % 5 == 0 { - progress(&format!( - "Waiting for installation media labelled '{label}'" - ))?; + let labels = label + .map(str::to_owned) + .unwrap_or_else(|| config::DEFAULT_MEDIA_LABELS.join(" or ")); + progress(&format!("Waiting for installation media labelled {labels}"))?; } attempts += 1; thread::sleep(MEDIA_POLL_INTERVAL); @@ -226,6 +229,14 @@ fn media_root( Ok(Some(target.into())) } +/// Matches an explicit filesystem label or the supported optical and USB defaults. +fn media_label_matches(actual: Option<&str>, configured: Option<&str>) -> bool { + actual.is_some_and(|label| match configured { + Some(configured) => label == configured, + None => config::DEFAULT_MEDIA_LABELS.contains(&label), + }) +} + /// Accepts only credential-free HTTP(S) URLs for manually entered recovery sources. pub(super) fn remote_url(source: &str) -> Result { let url = Url::parse(source).context("Enter an absolute http:// or https:// URL")?; @@ -470,6 +481,18 @@ mod tests { use tar::Builder as TarBuilder; use tempfile::TempDir; + #[test] + /// Ensures automatic media selection supports optical and FAT labels without guessing. + fn default_media_labels_are_unambiguous_and_explicit_labels_stay_exact() { + assert!(media_label_matches(Some("TRD_INSTALL"), None)); + assert!(media_label_matches(Some("CDROM"), None)); + assert!(!media_label_matches(Some("OTHER"), None)); + assert!(!media_label_matches(None, None)); + assert!(media_label_matches(Some("CUSTOM"), Some("CUSTOM"))); + assert!(!media_label_matches(Some("CDROM"), Some("TRD_INSTALL"))); + config::choose_media(&[PathBuf::from("/dev/sr0"), PathBuf::from("/dev/sdb1")]).unwrap_err(); + } + #[test] /// Ensures recovery sources cannot silently fall back to local files or other schemes. fn remote_sources_are_explicit_and_limited() { diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index 0cb809fcb6..034ed9772e 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -19,7 +19,6 @@ serialMode = "logs" serialVerbosity = "debug" [media] -filesystemLabel = "TRD_INSTALL" cosiDirectory = "cosi/" [autorun] @@ -82,7 +81,7 @@ necessarily the value written in the packaged file. | `mode` | **None; required** | Set `"autorun"` (as the packaged file does). This is a startup mode, not the Install/StreamDisk selector. `"interactive"` is reserved and rejected. | | `serialMode` | `"logs"` | Sends plain installer and Trident log records to active serial consoles, without a serial TUI. `"ui"` is reserved and rejected. | | `serialVerbosity` | `"debug"` | Serial log filter: `"off"`, `"error"`, `"warn"`, `"info"`, `"debug"`, or `"trace"`. Independent of the on-screen log filter, which starts at Debug. | -| `media.filesystemLabel` | `"TRD_INSTALL"` | Exact filesystem label to find on optical media or a USB partition. `TRD_INSTALL` fits FAT's 11-character label limit; Image Customizer ISOs use `CDROM` instead. | +| `media.filesystemLabel` | Unset (auto) | Exact filesystem label when set. Otherwise find either `TRD_INSTALL` (FAT-compatible USB) or `CDROM` (Image Customizer ISO). If both labels are present, choose neither; specify a label to disambiguate. | | `media.cdromLabel` | — | Legacy alias for `filesystemLabel`. Do not specify both names. | | `media.mountPath` | Unset | Absolute path to an **already mounted** filesystem instead of label discovery. Cannot be combined with `filesystemLabel`; this path is not mounted or made read-only by the installer. | | `media.cosiDirectory` | `"cosi"` | Nonempty directory relative to the media root. Absolute paths and `..` are rejected. Only regular `.cosi` files directly inside it are considered, in filename order. | @@ -90,9 +89,10 @@ necessarily the value written in the packaged file. | `autorun.reboot` | `true` | Reboot after successful servicing **only if** Trident reports that work was performed and a reboot is required. | | `autorun.force` | `false` | On automatic COSI streaming, bypass the matching-filesystem-UUID repeat-install guard. Does not affect Host Configuration installs, manual recovery sources, or the mounted/read-only disk safety check. | -`filesystemLabel` discovery waits up to 60 seconds during autorun for late -USB/BMC device enumeration. It fails if multiple devices have the label. The -selected filesystem is mounted read-only at `/run/trident/installer-media`; +Media discovery waits up to 60 seconds during autorun for late USB/BMC device +enumeration. It fails if more than one device matches the selected/default +labels. The selected filesystem is mounted read-only at +`/run/trident/installer-media`; the live OS must support its filesystem. A manually selected remote recovery source does not wait for media. @@ -134,9 +134,8 @@ hostConfiguration = "installer/host.yaml" reboot = false ``` -The bootstrap `media.filesystemLabel` must already match the media for this -overlay to be found. Place the referenced `installer/host.yaml` on the same -media. +The bootstrap media selection must already match the media for this overlay +to be found. Place the referenced `installer/host.yaml` on the same media. The media file can change `serialVerbosity`, `media.cosiDirectory`, `mode`, and `autorun` options. It cannot change `serialMode`, @@ -144,3 +143,35 @@ and `autorun` options. It cannot change `serialMode`, those settings are needed to find the media in the first place. Other fields still undergo the same validation; setting `mode = "interactive"` remains unsupported. + +## Building an installer ISO + +On a full-OS Azure Linux ISO, install the `trident-installer` RPM and add +`trident.installer=1` to the **ISO boot kernel command line**. Include a +COSI in `cosi/`, or provide a Host Configuration as described above. For +example, the essential Image Customizer settings are: + +```yaml +os: + packages: + install: + - trident-installer +iso: + initramfsType: full-os + additionalFiles: + - source: cosi/payload.cosi + destination: /cosi/payload.cosi + kernelCommandLine: + extraCommandLine: + - trident.installer=1 +``` + +The RPM includes the installer TOML, Trident and common runtime tools. It +starts its service and gRPC socket on a marked installer boot and prevents +gettys from reclaiming tty1 or mixing login prompts with serial logs. +No separate service enablement, getty override, or label override is +necessary for a single `CDROM` ISO or `TRD_INSTALL` USB. On an unmarked +normal OS, the service does not run and gettys are unaffected. A Host +Configuration may require additional tools for optional features such as +RAID or encryption. See the [TUI installer ISO recipe](../../tests/images/tui-installer/image.yaml) +for a working full image definition. diff --git a/packaging/etc/installer.toml b/packaging/etc/installer.toml index fe88aa4ab4..961def17c9 100644 --- a/packaging/etc/installer.toml +++ b/packaging/etc/installer.toml @@ -3,7 +3,6 @@ serialMode = "logs" serialVerbosity = "debug" [media] -filesystemLabel = "TRD_INSTALL" cosiDirectory = "cosi/" [autorun] diff --git a/packaging/rpm/trident.spec b/packaging/rpm/trident.spec index 96243ffe2c..d420bf79d0 100644 --- a/packaging/rpm/trident.spec +++ b/packaging/rpm/trident.spec @@ -255,16 +255,25 @@ The Trident ACL components required to orchestrate servicing of ACL images. Summary: Trident Linux Installer console for installer media Requires: %{name} = %{version}-%{release} Requires: bash +Requires: device-mapper +Requires: squashfs-tools +Requires: veritysetup +Requires: curl +Requires: less +Requires: iproute Conflicts: %{name}-install-service %description installer Autorun installer, recovery console, and local demonstration UI. -Enable trident-installer.service explicitly in the installer ISO recipe. +The service starts only when the ISO boot command line has trident.installer=1. %files installer %license LICENSE NOTICE %{_bindir}/%{name}-installer %{_unitdir}/%{name}-installer.service +%{_unitdir}/multi-user.target.wants/%{name}-installer.service +%{_unitdir}/getty@tty1.service.d/%{name}-installer.conf +%{_unitdir}/serial-getty@.service.d/%{name}-installer.conf %config(noreplace) /etc/%{name}/installer.toml %preun installer @@ -337,6 +346,10 @@ install -D -m 755 target/release/%{name}-acl-agent %{buildroot}/%{_bindir}/%{nam install -D -m 755 target/release/%{name}-installer %{buildroot}/%{_bindir}/%{name}-installer install -D -m 644 packaging/systemd/%{name}-acl-agent.service %{buildroot}%{_unitdir}/%{name}-acl-agent.service install -D -m 644 packaging/systemd/%{name}-installer.service %{buildroot}%{_unitdir}/%{name}-installer.service +install -d %{buildroot}%{_unitdir}/multi-user.target.wants +ln -s ../%{name}-installer.service %{buildroot}%{_unitdir}/multi-user.target.wants/%{name}-installer.service +install -D -m 644 packaging/systemd/getty@tty1.service.d/%{name}-installer.conf %{buildroot}%{_unitdir}/getty@tty1.service.d/%{name}-installer.conf +install -D -m 644 packaging/systemd/serial-getty@.service.d/%{name}-installer.conf %{buildroot}%{_unitdir}/serial-getty@.service.d/%{name}-installer.conf install -D -m 644 packaging/etc/installer.toml %{buildroot}/etc/%{name}/installer.toml # Copy Trident SELinux policy module to /usr/share/selinux/packages diff --git a/packaging/systemd/getty@tty1.service.d/trident-installer.conf b/packaging/systemd/getty@tty1.service.d/trident-installer.conf new file mode 100644 index 0000000000..de9d71aec8 --- /dev/null +++ b/packaging/systemd/getty@tty1.service.d/trident-installer.conf @@ -0,0 +1,2 @@ +[Unit] +ConditionKernelCommandLine=!trident.installer=1 diff --git a/packaging/systemd/serial-getty@.service.d/trident-installer.conf b/packaging/systemd/serial-getty@.service.d/trident-installer.conf new file mode 100644 index 0000000000..de9d71aec8 --- /dev/null +++ b/packaging/systemd/serial-getty@.service.d/trident-installer.conf @@ -0,0 +1,2 @@ +[Unit] +ConditionKernelCommandLine=!trident.installer=1 diff --git a/packaging/systemd/trident-installer.service b/packaging/systemd/trident-installer.service index 8d9a1fd3c4..4e7a845467 100644 --- a/packaging/systemd/trident-installer.service +++ b/packaging/systemd/trident-installer.service @@ -1,5 +1,6 @@ [Unit] Description=Trident Linux Installer +ConditionKernelCommandLine=trident.installer=1 Wants=tridentd.socket systemd-udev-settle.service After=tridentd.socket systemd-udev-settle.service network.target Conflicts=trident-install.service diff --git a/test/README.md b/test/README.md index 3154fe30f6..b804428c4a 100644 --- a/test/README.md +++ b/test/README.md @@ -21,8 +21,9 @@ output; UEFI firmware output may still appear before the installer starts. With only a serial console active, autorun runs without a TUI and honors `autorun.reboot`; missing inputs or an already-present image produce tagged errors instead of waiting for a keyboard response. -The ISO disables `getty@tty1.service` and logind's automatic virtual gettys so -they cannot reclaim the installer's graphical keyboard after startup. The +The RPM's getty drop-ins skip serial and tty1 gettys only when the ISO boots +with `trident.installer=1`, so they cannot reclaim the installer's keyboard +or mix prompts into serial logs. The installer mirrors source-discovery stages to the serial log; an idle-looking progress screen can be diagnosed there without sending keys to the guest. @@ -62,13 +63,12 @@ replacement without changing the running guest's CD-ROM. The installer finds a filesystem by label, so it can read COSI files from an optical ISO9660 filesystem or a GPT USB partition (FAT32 in this fixture). -`filesystemLabel = "TRD_INSTALL"` fits FAT32's 11-character label limit. -Image Customizer ISO output is labelled `CDROM`. To build the USB test ISO, -temporarily set `filesystemLabel = "TRD_INSTALL"` in +By default it accepts `TRD_INSTALL` (FAT-compatible) or `CDROM` +(Image Customizer ISO). When both are attached, specify which to use. To +build the USB test ISO, temporarily add `filesystemLabel = "TRD_INSTALL"` in `test/installer/files/installer.toml`, run `python3 test/build.py --skip-rpms --output-dir test/.work/gpt-usb-ready`, -and restore `filesystemLabel = "CDROM"` afterward. Keep the two media labels -distinct to avoid ambiguous discovery. +and remove that line afterward. ```console python3 test/make_usb.py --cosi test/installer/cosi/payload.cosi diff --git a/test/installer/files/installer-logind.conf b/test/installer/files/installer-logind.conf deleted file mode 100644 index 6e1a22c65d..0000000000 --- a/test/installer/files/installer-logind.conf +++ /dev/null @@ -1,3 +0,0 @@ -[Login] -NAutoVTs=0 -ReserveVT=0 diff --git a/test/installer/files/installer-serial-getty.conf b/test/installer/files/installer-serial-getty.conf deleted file mode 100644 index 0721aef5e2..0000000000 --- a/test/installer/files/installer-serial-getty.conf +++ /dev/null @@ -1,2 +0,0 @@ -[Unit] -ConditionPathExists=!/etc/trident/installer.toml diff --git a/test/installer/files/installer.toml b/test/installer/files/installer.toml index 49d2a72b9b..96c7157d6d 100644 --- a/test/installer/files/installer.toml +++ b/test/installer/files/installer.toml @@ -3,7 +3,6 @@ serialMode = "logs" serialVerbosity = "debug" [media] -filesystemLabel = "CDROM" cosiDirectory = "cosi/" [autorun] diff --git a/test/installer/image.yaml b/test/installer/image.yaml index da0fb16469..acac1c1a94 100644 --- a/test/installer/image.yaml +++ b/test/installer/image.yaml @@ -35,25 +35,9 @@ config: packages: install: - trident-installer - - device-mapper - - squashfs-tools - - veritysetup - - curl - - less - - iproute - services: - enable: - - tridentd.socket - - trident-installer.service - disable: - - getty@tty1.service additionalFiles: - source: files/installer.toml destination: /etc/trident/installer.toml - - source: files/installer-logind.conf - destination: /etc/systemd/logind.conf.d/installer.conf - - source: files/installer-serial-getty.conf - destination: /etc/systemd/system/serial-getty@ttyS0.service.d/installer.conf storage: bootType: efi disks: @@ -87,3 +71,4 @@ config: - quiet - loglevel=0 - systemd.show_status=false + - trident.installer=1 diff --git a/tests/images/tui-installer/README.md b/tests/images/tui-installer/README.md index 04655730da..abf4ddb377 100644 --- a/tests/images/tui-installer/README.md +++ b/tests/images/tui-installer/README.md @@ -1,10 +1,12 @@ # TUI installer ISO Tailor builds an Azure Linux 3 ISO with the `trident-installer` RPM and Trident's -gRPC daemon. The installer boots in autorun mode, mounts the `CDROM`-labelled -ISO read-only, and streams the staged COSI. It does **not** enter demo mode: -boot only with a disposable target disk. The success screen remains visible -until an operator chooses Reboot. +gRPC daemon. The ISO recipe installs only the RPM and adds the +`trident.installer=1` boot flag; the RPM enables the service and excludes +competing gettys on that boot. The installer mounts the `CDROM`-labelled ISO +read-only and streams the staged COSI. It does **not** enter demo mode: boot +only with a disposable target disk. The packaged configuration reboots +automatically after successful installation when Trident requires it. Stage matching Trident RPMs (including `trident-installer`) in `rpms/` and an Azure Linux COSI in `cosi/payload.cosi`. These inputs are local and ignored by @@ -43,7 +45,7 @@ that address's localhost port over SSH. When finished, stop netlaunch and run `bin/virtdeploy clean --namespace tui-installer-demo` to remove only this VM, its disk, and its libvirt network. -The checked-in configuration uses `filesystemLabel = "CDROM"` to match Image -Customizer ISO output; change it if the image is copied to differently labelled -media. `trident-installer` is optional in the RPM build, but its package must -be present in `rpms/` for this image to build. +With no explicit `filesystemLabel`, `CDROM` and `TRD_INSTALL` are recognized. +If both media are present, specify one in the bootstrap TOML to disambiguate. +`trident-installer` is optional in the RPM build, but its package must be +present in `rpms/` for this image to build. diff --git a/tests/images/tui-installer/files/installer-logind.conf b/tests/images/tui-installer/files/installer-logind.conf deleted file mode 100644 index 6e1a22c65d..0000000000 --- a/tests/images/tui-installer/files/installer-logind.conf +++ /dev/null @@ -1,3 +0,0 @@ -[Login] -NAutoVTs=0 -ReserveVT=0 diff --git a/tests/images/tui-installer/files/installer-serial-getty.conf b/tests/images/tui-installer/files/installer-serial-getty.conf deleted file mode 100644 index 0721aef5e2..0000000000 --- a/tests/images/tui-installer/files/installer-serial-getty.conf +++ /dev/null @@ -1,2 +0,0 @@ -[Unit] -ConditionPathExists=!/etc/trident/installer.toml diff --git a/tests/images/tui-installer/files/installer.toml b/tests/images/tui-installer/files/installer.toml deleted file mode 100644 index 49d2a72b9b..0000000000 --- a/tests/images/tui-installer/files/installer.toml +++ /dev/null @@ -1,11 +0,0 @@ -mode = "autorun" -serialMode = "logs" -serialVerbosity = "debug" - -[media] -filesystemLabel = "CDROM" -cosiDirectory = "cosi/" - -[autorun] -reboot = false -force = false diff --git a/tests/images/tui-installer/image.yaml b/tests/images/tui-installer/image.yaml index a2093bce6e..4bfbd6409d 100644 --- a/tests/images/tui-installer/image.yaml +++ b/tests/images/tui-installer/image.yaml @@ -12,7 +12,6 @@ rpmSources: - rpms extraDependencies: - - files - cosi - rpms @@ -35,25 +34,6 @@ config: packages: install: - trident-installer - - device-mapper - - squashfs-tools - - veritysetup - - curl - - less - - iproute - services: - enable: - - tridentd.socket - - trident-installer.service - disable: - - getty@tty1.service - additionalFiles: - - source: files/installer.toml - destination: /etc/trident/installer.toml - - source: files/installer-logind.conf - destination: /etc/systemd/logind.conf.d/installer.conf - - source: files/installer-serial-getty.conf - destination: /etc/systemd/system/serial-getty@ttyS0.service.d/installer.conf storage: bootType: efi disks: @@ -87,3 +67,4 @@ config: - quiet - loglevel=0 - systemd.show_status=false + - trident.installer=1 From e3830f312b8ebbbc4e75fe711b479d7ff98cdb2b Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 5 Nov 2026 16:25:58 -0800 Subject: [PATCH 32/62] fix: Keep block device literals compatible while scanning labels Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/source.rs | 44 +++------- crates/osutils/src/lsblk.rs | 98 +++++++++++++++-------- docs/Reference/Installer-Configuration.md | 4 +- 3 files changed, 78 insertions(+), 68 deletions(-) diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index debbc51f83..5b813d8d4a 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -169,26 +169,25 @@ fn media_root( ); return Ok(Some(path.canonicalize()?)); } - let label = settings.media.filesystem_label.as_deref(); + let labels = settings + .media + .filesystem_label + .as_deref() + .map(|label| vec![label]) + .unwrap_or_else(|| config::DEFAULT_MEDIA_LABELS.to_vec()); progress("Looking for installer media by filesystem label")?; let deadline = Instant::now() + MEDIA_DISCOVERY_TIMEOUT; let mut attempts = 0; let devices = loop { - let mut devices = - lsblk::find(|device| media_label_matches(device.label.as_deref(), label))? - .into_iter() - .map(|device| device.device_path()) - .collect::>(); - devices.sort(); - devices.dedup(); + let devices = lsblk::devices_by_labels(&labels)?; if !devices.is_empty() || !wait_for_media || Instant::now() >= deadline { break devices; } if attempts % 5 == 0 { - let labels = label - .map(str::to_owned) - .unwrap_or_else(|| config::DEFAULT_MEDIA_LABELS.join(" or ")); - progress(&format!("Waiting for installation media labelled {labels}"))?; + progress(&format!( + "Waiting for installation media labelled {}", + labels.join(" or ") + ))?; } attempts += 1; thread::sleep(MEDIA_POLL_INTERVAL); @@ -229,14 +228,6 @@ fn media_root( Ok(Some(target.into())) } -/// Matches an explicit filesystem label or the supported optical and USB defaults. -fn media_label_matches(actual: Option<&str>, configured: Option<&str>) -> bool { - actual.is_some_and(|label| match configured { - Some(configured) => label == configured, - None => config::DEFAULT_MEDIA_LABELS.contains(&label), - }) -} - /// Accepts only credential-free HTTP(S) URLs for manually entered recovery sources. pub(super) fn remote_url(source: &str) -> Result { let url = Url::parse(source).context("Enter an absolute http:// or https:// URL")?; @@ -481,18 +472,6 @@ mod tests { use tar::Builder as TarBuilder; use tempfile::TempDir; - #[test] - /// Ensures automatic media selection supports optical and FAT labels without guessing. - fn default_media_labels_are_unambiguous_and_explicit_labels_stay_exact() { - assert!(media_label_matches(Some("TRD_INSTALL"), None)); - assert!(media_label_matches(Some("CDROM"), None)); - assert!(!media_label_matches(Some("OTHER"), None)); - assert!(!media_label_matches(None, None)); - assert!(media_label_matches(Some("CUSTOM"), Some("CUSTOM"))); - assert!(!media_label_matches(Some("CDROM"), Some("TRD_INSTALL"))); - config::choose_media(&[PathBuf::from("/dev/sr0"), PathBuf::from("/dev/sdb1")]).unwrap_err(); - } - #[test] /// Ensures recovery sources cannot silently fall back to local files or other schemes. fn remote_sources_are_explicit_and_limited() { @@ -631,7 +610,6 @@ mod tests { blkdev_type: BlockDeviceType::Disk, children: vec![BlockDevice { name: "sdb1".into(), - label: Some("TRD_INSTALL".into()), mountpoints: vec![config::MEDIA_MOUNT.into()], ..Default::default() }], diff --git a/crates/osutils/src/lsblk.rs b/crates/osutils/src/lsblk.rs index 31edba2532..b10bdcce6d 100644 --- a/crates/osutils/src/lsblk.rs +++ b/crates/osutils/src/lsblk.rs @@ -24,9 +24,6 @@ pub struct BlockDevice { /// Device name pub name: String, - /// Filesystem label - pub label: Option, - /// Filesystem type pub fstype: Option, @@ -163,37 +160,79 @@ pub enum PartitionTableType { /// structs. /// pub fn list() -> Result, Error> { - let result = Dependency::Lsblk + parse_lsblk_output(&lsblk_output()?) +} + +/// Reads the full device tree once for either block-device or label inspection. +fn lsblk_output() -> Result { + Dependency::Lsblk .cmd() .arg("--json") .arg("--output-all") .arg("--bytes") .output_and_check() - .context("Failed to execute lsblk")?; + .context("Failed to execute lsblk") +} - let parsed: Vec = parse_lsblk_output(result.as_str())?; +#[derive(Deserialize, Default)] +struct LabelledDevice { + name: String, + label: Option, + #[serde(default)] + children: Vec, +} - Ok(parsed) +#[derive(Deserialize)] +struct LabelledDevices { + blockdevices: Vec, } /// Finds all devices with a filesystem label, including devices nested below disks. pub fn devices_by_label(label: impl AsRef) -> Result, Error> { let label = label.as_ref(); ensure!(!label.is_empty(), "Filesystem label must not be empty"); + devices_by_labels(&[label]) +} + +/// Finds devices matching any of the given filesystem labels in one `lsblk` scan. +pub fn devices_by_labels(labels: &[&str]) -> Result, Error> { + ensure!( + !labels.is_empty() && labels.iter().all(|label| !label.is_empty()), + "Filesystem labels must not be empty" + ); let mut devices = Vec::new(); - collect_devices_by_label(&list()?, label, &mut devices); + collect_devices_by_labels( + &parse_labelled_devices(&lsblk_output()?)?, + labels, + &mut devices, + ); devices.sort(); devices.dedup(); Ok(devices) } -/// Adds paths of devices with an exact filesystem-label match, including descendants. -fn collect_devices_by_label(devices: &[BlockDevice], label: &str, matches: &mut Vec) { +/// Parses only the fields needed for label discovery without changing `BlockDevice`. +fn parse_labelled_devices(output: &str) -> Result, Error> { + let parsed: LabelledDevices = + serde_json::from_str(output).context("Failed to parse lsblk label output")?; + Ok(parsed.blockdevices) +} + +/// Adds paths of matching devices and their descendants. +fn collect_devices_by_labels( + devices: &[LabelledDevice], + labels: &[&str], + matches: &mut Vec, +) { for device in devices { - if device.label.as_deref() == Some(label) { - matches.push(device.device_path()); + if device + .label + .as_deref() + .is_some_and(|label| labels.contains(&label)) + { + matches.push(Path::new("/dev").join(&device.name)); } - collect_devices_by_label(&device.children, label, matches); + collect_devices_by_labels(&device.children, labels, matches); } } @@ -332,18 +371,17 @@ mod tests { use super::*; #[test] - /// Ensures optical and partition labels both match, and rejects an empty label. + /// Ensures optical and partition labels both match, and rejects empty label filters. fn label_scan_includes_cdrom_and_detects_duplicate_filesystem_labels() { let devices = vec![ - BlockDevice { + LabelledDevice { name: "sr0".into(), label: Some("CDROM".into()), - blkdev_type: BlockDeviceType::Rom, ..Default::default() }, - BlockDevice { + LabelledDevice { name: "sda".into(), - children: vec![BlockDevice { + children: vec![LabelledDevice { name: "sda1".into(), label: Some("CDROM".into()), ..Default::default() @@ -352,42 +390,42 @@ mod tests { }, ]; let mut matches = Vec::new(); - collect_devices_by_label(&devices, "CDROM", &mut matches); + collect_devices_by_labels(&devices, &["CDROM"], &mut matches); matches.sort(); assert_eq!( matches, [PathBuf::from("/dev/sda1"), PathBuf::from("/dev/sr0")] ); assert!(devices_by_label("").is_err()); + assert!(devices_by_labels(&[]).is_err()); + assert!(devices_by_labels(&["CDROM", ""]).is_err()); } #[test] /// Ensures optical-drive labels survive parsing the `lsblk` JSON tree. fn parses_optical_drive_label_from_lsblk_json() { - let devices = parse_lsblk_output( + let devices = parse_labelled_devices( r#"{"blockdevices":[{"name":"sr0","size":355497984,"type":"rom","label":"CDROM"}]}"#, ) .unwrap(); let mut matches = Vec::new(); - collect_devices_by_label(&devices, "CDROM", &mut matches); + collect_devices_by_labels(&devices, &["CDROM"], &mut matches); assert_eq!(matches, [PathBuf::from("/dev/sr0")]); } #[test] /// Ensures a GPT USB partition's label is found beneath its parent disk. fn parses_gpt_usb_partition_label_from_lsblk_json() { - let devices = parse_lsblk_output( - r#"{"blockdevices":[{"name":"sdb","size":1073741824,"type":"disk","pttype":"gpt","children":[{"name":"sdb1","size":1069547520,"type":"part","fstype":"vfat","label":"TRD_INSTALL"}]}]}"#, - ) - .unwrap(); + let output = r#"{"blockdevices":[{"name":"sdb","size":1073741824,"type":"disk","pttype":"gpt","children":[{"name":"sdb1","size":1069547520,"type":"part","fstype":"vfat","label":"TRD_INSTALL"}]}]}"#; + let devices = parse_labelled_devices(output).unwrap(); let mut matches = Vec::new(); - collect_devices_by_label(&devices, "TRD_INSTALL", &mut matches); + collect_devices_by_labels(&devices, &["TRD_INSTALL", "CDROM"], &mut matches); assert_eq!(matches, [PathBuf::from("/dev/sdb1")]); + let devices = parse_lsblk_output(output).unwrap(); assert_eq!( devices[0].partition_table_type, Some(PartitionTableType::Gpt) ); - assert!(devices[0].label.is_none()); } /// Output obtained from running `lsblk --json --bytes --output-all --paths /dev/sda` @@ -951,7 +989,6 @@ mod tests { fn test_parse_lsblk_output() { let expected_block_device_list = vec![BlockDevice { name: "/dev/sda".into(), - label: None, fstype: None, fssize: None, fsuuid: None, @@ -965,7 +1002,6 @@ mod tests { children: vec![ BlockDevice { name: "/dev/sda1".into(), - label: None, fstype: Some("vfat".into()), fssize: Some(ByteCount(52293632)), fsuuid: Some(OsUuid::Relaxed("6AA2-09D2".to_string())), @@ -983,7 +1019,6 @@ mod tests { }, BlockDevice { name: "/dev/sda2".into(), - label: None, fstype: Some("ext4".into()), fssize: Some(ByteCount(4143841280)), fsuuid: Some("f3c65403-dafc-4dbf-873a-c17d6fb98a2f".into()), @@ -1001,7 +1036,6 @@ mod tests { }, BlockDevice { name: "/dev/sda3".into(), - label: None, fstype: None, fssize: None, fsuuid: None, @@ -1019,7 +1053,6 @@ mod tests { }, BlockDevice { name: "/dev/sda4".into(), - label: None, fstype: Some("swap".into()), fssize: None, fsuuid: Some("91180e8c-096a-43d8-88b2-c24a5b5eaee6".into()), @@ -1037,7 +1070,6 @@ mod tests { }, BlockDevice { name: "/dev/sda5".into(), - label: None, fstype: Some("ext4".into()), fssize: Some(ByteCount(92500992)), fsuuid: Some("2253b2d0-62db-4ae9-8343-4023a361a9c3".into()), diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index 034ed9772e..4992e9700f 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -173,5 +173,5 @@ No separate service enablement, getty override, or label override is necessary for a single `CDROM` ISO or `TRD_INSTALL` USB. On an unmarked normal OS, the service does not run and gettys are unaffected. A Host Configuration may require additional tools for optional features such as -RAID or encryption. See the [TUI installer ISO recipe](../../tests/images/tui-installer/image.yaml) -for a working full image definition. +RAID or encryption. See `tests/images/tui-installer/image.yaml` in the +repository for a working full image definition. From 62e25f7779c60655d7b534cd23e42e0dc5532524 Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 5 Nov 2026 16:32:07 -0800 Subject: [PATCH 33/62] test: Cover simultaneous optical and USB installer labels Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/osutils/src/lsblk.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/osutils/src/lsblk.rs b/crates/osutils/src/lsblk.rs index b10bdcce6d..7deb406c3d 100644 --- a/crates/osutils/src/lsblk.rs +++ b/crates/osutils/src/lsblk.rs @@ -414,13 +414,16 @@ mod tests { } #[test] - /// Ensures a GPT USB partition's label is found beneath its parent disk. + /// Ensures optical and GPT USB labels are both found without choosing between them. fn parses_gpt_usb_partition_label_from_lsblk_json() { - let output = r#"{"blockdevices":[{"name":"sdb","size":1073741824,"type":"disk","pttype":"gpt","children":[{"name":"sdb1","size":1069547520,"type":"part","fstype":"vfat","label":"TRD_INSTALL"}]}]}"#; + let output = r#"{"blockdevices":[{"name":"sdb","size":1073741824,"type":"disk","pttype":"gpt","children":[{"name":"sdb1","size":1069547520,"type":"part","fstype":"vfat","label":"TRD_INSTALL"}]},{"name":"sr0","size":355497984,"type":"rom","label":"CDROM"}]}"#; let devices = parse_labelled_devices(output).unwrap(); let mut matches = Vec::new(); collect_devices_by_labels(&devices, &["TRD_INSTALL", "CDROM"], &mut matches); - assert_eq!(matches, [PathBuf::from("/dev/sdb1")]); + assert_eq!( + matches, + [PathBuf::from("/dev/sdb1"), PathBuf::from("/dev/sr0")] + ); let devices = parse_lsblk_output(output).unwrap(); assert_eq!( devices[0].partition_table_type, From 6cf368a75f70a4d32c776f7e167435325893fa72 Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 5 Nov 2026 17:02:43 -0800 Subject: [PATCH 34/62] refactor: Use shared trident-client in installer Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Cargo.lock | 4 +- crates/installer/Cargo.toml | 5 +- crates/installer/README.md | 3 +- crates/installer/src/client.rs | 506 ++++++++------------------------- crates/installer/src/demo.rs | 56 ++-- crates/installer/src/ui.rs | 96 ++++--- 6 files changed, 217 insertions(+), 453 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3044fc7e51..c15bfa75f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1737,10 +1737,8 @@ dependencies = [ "tar", "tempfile", "tokio", - "tokio-stream", "toml", - "tonic", - "trident-proto", + "trident-client", "trident_api", "url", ] diff --git a/crates/installer/Cargo.toml b/crates/installer/Cargo.toml index 8b7ef0d5b6..447cce7f04 100644 --- a/crates/installer/Cargo.toml +++ b/crates/installer/Cargo.toml @@ -24,16 +24,13 @@ serde_yaml = { workspace = true } tar = { workspace = true } tokio = { workspace = true } toml = { workspace = true } -tonic = { workspace = true } url = { workspace = true } # Local Crate Dependencies osutils = { path = "../osutils" } +trident-client = { path = "../trident-client", features = ["preview"] } trident_api = { path = "../trident_api" } -trident-proto = { path = "../trident-proto", features = ["grpc-preview"] } [dev-dependencies] indoc = { workspace = true } tempfile = { workspace = true } -tokio-stream = { workspace = true } -trident-proto = { path = "../trident-proto", features = ["server", "grpc-preview"] } diff --git a/crates/installer/README.md b/crates/installer/README.md index 6e3b02f96b..bf36141fe8 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -1,7 +1,8 @@ # Trident Linux Installer `installer` builds the `trident-installer` binary and optional RPM. It is an -autorun wrapper around Trident's gRPC API, not a partitioning engine. +autorun wrapper around Trident's gRPC API through the shared `trident-client` +crate, not a partitioning engine. ## Local demo diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index 7332cf1dd0..ae5c0197e2 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -1,26 +1,12 @@ -use std::time::Duration; - -use anyhow::{anyhow, bail, ensure, Context, Error}; +use anyhow::{anyhow, ensure, Error}; use tokio::{sync::mpsc::Sender, task}; -use tonic::{ - transport::{Channel, Endpoint}, - Code, Status, Streaming, -}; use url::Url; -use trident_proto::{ - v1::{ - servicing_response::Response as ResponseBody, - streaming_service_client::StreamingServiceClient, Completed, HostConfiguration, - RebootHandling, RebootManagement, RebootStatus, ServicingKind, ServicingResponse, - StatusCode, StreamDiskRequest, TridentError as RemoteError, - TridentErrorKind as ErrorCategory, - }, - v1preview::{ - install_service_client::InstallServiceClient, status_service_client::StatusServiceClient, - FinalizeInstallRequest, GetServicingStateRequest, InstallRequest, StageInstallRequest, - }, - TRIDENT_DEFAULT_SOCKET_URI, +use trident_api::status::ServicingType; +use trident_client::{ + Client, ClientError, Completion as ClientCompletion, ExecutionState, OperationOutcome, + RebootDisposition, RebootPolicy, RemoteError, RemoteErrorCategory, ServicingEvent, + ServicingOperation, }; use crate::{ @@ -28,9 +14,6 @@ use crate::{ source::{self, Plan, Request}, }; -const CONNECTION_TIMEOUT: Duration = Duration::from_secs(30); -const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); - /// Applies configured force only to autorun streams, while respecting explicit confirmation. fn stream_force_requested(automatic: bool, configured: bool, confirmed: bool) -> bool { confirmed || (automatic && configured) @@ -49,7 +32,7 @@ pub(super) enum Event { serial_verbosity: SerialVerbosity, stream_image: Option, }, - Response(ServicingResponse), + Response(ServicingEvent), Error { details: String, uncertain: bool, @@ -66,60 +49,41 @@ pub(super) enum Completion { Failure(String), } -/// Interprets Trident's final response, rejecting invalid or daemon-managed reboot states. -pub(super) fn completion(completed: Completed) -> Result { - match StatusCode::try_from(completed.status).context("Invalid final status code")? { - StatusCode::Success => { - ensure!( - completed.error.is_none(), - "Success response unexpectedly includes an error" - ); - let reboot = - RebootStatus::try_from(completed.reboot_status).context("Invalid reboot status")?; +/// Interprets a domain completion, enforcing the installer's caller-managed reboot contract. +pub(super) fn completion(outcome: OperationOutcome) -> Result { + match outcome { + OperationOutcome::Succeeded(ClientCompletion { + reboot, + servicing_type, + .. + }) => { ensure!( - matches!( - reboot, - RebootStatus::RebootRequired | RebootStatus::RebootNotRequired - ), + reboot != RebootDisposition::Started, "Daemon did not honour caller-managed reboot: {reboot:?}" ); - let performed = match completed.servicing_kind { - Some(kind) => { - ServicingKind::try_from(kind).context("Invalid servicing kind")? - != ServicingKind::NoneRequired - } - None => true, - }; Ok(Completion::Success { - reboot_required: reboot == RebootStatus::RebootRequired, - performed, + reboot_required: reboot == RebootDisposition::Required, + performed: servicing_type != Some(ServicingType::NoActiveServicing), }) } - StatusCode::Failure => Ok(Completion::Failure(match completed.error { - Some(error) => format_error(&error), - None => "Trident reported failure without error details".into(), - })), - StatusCode::Unspecified => bail!("Daemon returned an unspecified final status"), + OperationOutcome::Failed(error) => Ok(Completion::Failure(format_error(&error))), } } /// Renders a structured Trident error as readable multi-line diagnostics. fn format_error(error: &RemoteError) -> String { - let category = match ErrorCategory::try_from(error.kind) { - Ok(ErrorCategory::Unspecified) => "Unspecified", - Ok(ErrorCategory::ExecutionEnvironmentMisconfigurationError) => "Execution environment", - Ok(ErrorCategory::HealthChecksError) => "Health checks", - Ok(ErrorCategory::InitializationError) => "Initialization", - Ok(ErrorCategory::InternalError) => "Internal error", - Ok(ErrorCategory::InvalidInputError) => "Invalid input", - Ok(ErrorCategory::ServicingError) => "Servicing", - Ok(ErrorCategory::UnsupportedConfigurationError) => "Unsupported configuration", - Err(_) => "Unknown", + let category = match error.category { + RemoteErrorCategory::ExecutionEnvironmentMisconfiguration => "Execution environment", + RemoteErrorCategory::HealthChecks => "Health checks", + RemoteErrorCategory::Initialization => "Initialization", + RemoteErrorCategory::Internal => "Internal error", + RemoteErrorCategory::InvalidInput => "Invalid input", + RemoteErrorCategory::Servicing => "Servicing", + RemoteErrorCategory::UnsupportedConfiguration => "Unsupported configuration", }; let mut details = format!( - "{}\n\nCategory: {category} ({})\nCode: {}", + "{}\n\nCategory: {category}\nCode: {}", error.message.trim(), - error.kind, error.subkind ); if !error.error_message.is_empty() && !error.message.contains(&error.error_message) { @@ -131,10 +95,16 @@ fn format_error(error: &RemoteError) -> String { details } -/// Prepares a source, checks stream safety, and forwards Trident's gRPC responses to the UI. +/// Keeps invalid completions and unacknowledged writes uncertain, but not known remote failures. +fn operation_uncertain(error: &ClientError) -> bool { + !matches!(error, ClientError::RemoteFailure { .. }) + && error.execution_state() != ExecutionState::NotStarted +} + +/// Prepares a source, checks stream safety, and forwards Trident's domain events to the UI. /// /// After an uncertain previous operation, `require_idle` prevents a new write until the daemon -/// confirms it is idle. Ambiguous RPC failures are reported as uncertain instead of retrying. +/// answers a status request. Ambiguous RPC failures are reported as uncertain instead of retrying. pub(super) async fn execute( settings: Config, request: Request, @@ -145,7 +115,7 @@ pub(super) async fn execute( let automatic = matches!(request, Request::Autorun); let description = match &request { Request::Autorun => format!( - "Autorun: HC '{}' or first ISO COSI in '{}'", + "Autorun: HC '{}' or first media COSI in '{}'", settings .autorun .host_configuration @@ -218,8 +188,8 @@ pub(super) async fn execute( } } send(&tx, Event::Preparing("Connecting to Trident daemon".into())).await?; - let channel = match connect(TRIDENT_DEFAULT_SOCKET_URI).await { - Ok(channel) => channel, + let client = match Client::connect_default().await { + Ok(client) => client, Err(error) => { return send( &tx, @@ -232,35 +202,21 @@ pub(super) async fn execute( } }; if require_idle { - if let Err(error) = StatusServiceClient::new(channel.clone()) - .get_servicing_state(GetServicingStateRequest {}) - .await - { + if let Err(error) = client.servicing_state().await { return send(&tx, Event::Error { details: format!("The previous operation's outcome is unknown. Cannot establish that Trident is idle; refusing another write.\n{error}"), uncertain: true, }).await; } } - let started = tokio::time::timeout(REQUEST_TIMEOUT, start(channel, plan)) - .await - .unwrap_or_else(|_| { - Err(Status::deadline_exceeded( - "Timed out waiting for Trident to accept the request; outcome is unknown", - )) - }); - let response = match started { + let response = match start(&client, plan).await { Ok(response) => response, Err(error) => { - let uncertain = !matches!( - error.code(), - Code::InvalidArgument | Code::Unimplemented | Code::FailedPrecondition - ); return send( &tx, Event::Error { details: format!("gRPC request failed: {error}"), - uncertain, + uncertain: operation_uncertain(&error), }, ) .await; @@ -269,75 +225,44 @@ pub(super) async fn execute( receive(response, &tx).await } -/// Connects to the Trident daemon with a bounded connection timeout. -async fn connect(address: &str) -> Result { - Endpoint::new(address.to_owned())? - .connect_timeout(CONNECTION_TIMEOUT) - .connect() - .await - .with_context(|| format!("Failed to connect to Trident at '{address}'")) -} - -/// Starts the selected gRPC operation, leaving reboot control to the installer. -async fn start(channel: Channel, plan: Plan) -> Result, Status> { - let reboot = Some(RebootManagement { - handling: RebootHandling::CallerHandlesReboot.into(), - }); +/// Selects the shared client's typed RPC while leaving reboot control to the installer. +async fn start(client: &Client, plan: Plan) -> Result { match plan { - Plan::Stream { image } => Ok(StreamingServiceClient::new(channel) - .stream_disk(StreamDiskRequest { - image_url: image.to_string(), - image_hash: None, - reboot, - }) - .await? - .into_inner()), - Plan::Install { config, .. } => { - let config = serde_yaml::to_string(&config).map_err(|error| { - Status::internal(format!("Failed to serialize Host Configuration: {error}")) - })?; - Ok(InstallServiceClient::new(channel) - .install(InstallRequest { - stage: Some(StageInstallRequest { - config: Some(HostConfiguration { config }), - }), - finalize: Some(FinalizeInstallRequest { reboot }), - }) - .await? - .into_inner()) - } + Plan::Stream { image } => client.stream_disk(&image, None, RebootPolicy::Caller).await, + Plan::Install { config, .. } => client.install(&config, RebootPolicy::Caller).await, } } -/// Forwards a servicing stream until completion; missing final responses leave the result unknown. -async fn receive( - mut stream: Streaming, - tx: &Sender, -) -> Result<(), Error> { +/// Forwards domain events until a completed result; a broken stream leaves the outcome unknown. +async fn receive(mut operation: ServicingOperation, tx: &Sender) -> Result<(), Error> { loop { - let response = match stream.message().await { - Ok(Some(response)) => response, - Ok(None) => return send(tx, Event::Error { - details: "The gRPC stream ended without a final Completed response. Installation outcome is unknown; it may still be running.".into(), - uncertain: true, - }).await, - Err(error) => return send(tx, Event::Error { - details: format!("The gRPC stream failed; installation outcome is unknown.\n{error}"), - uncertain: true, - }).await, + let event = match operation.next_event().await { + Ok(Some(event)) => event, + Ok(None) => { + return send( + tx, + Event::Error { + details: "The gRPC stream ended without a final Completed response. Installation outcome is unknown; it may still be running.".into(), + uncertain: true, + }, + ) + .await; + } + Err(error) => { + return send( + tx, + Event::Error { + details: format!( + "The gRPC stream failed; installation outcome is unknown.\n{error}" + ), + uncertain: operation_uncertain(&error), + }, + ) + .await; + } }; - let completed = matches!(response.response, Some(ResponseBody::Completed(_))); - if response.response.is_none() { - return send( - tx, - Event::Error { - details: "gRPC response has no body; installation outcome is unknown".into(), - uncertain: true, - }, - ) - .await; - } - send(tx, Event::Response(response)).await?; + let completed = matches!(event, ServicingEvent::Completed(_)); + send(tx, Event::Response(event)).await?; if completed { return Ok(()); } @@ -355,27 +280,7 @@ pub(super) async fn send(tx: &Sender, event: Event) -> Result<(), Error> mod tests { use super::*; - use std::{ - sync::{Arc, Mutex}, - vec::IntoIter, - }; - - use tempfile::TempDir; - use tokio::{net::UnixListener, sync::mpsc}; - use tokio_stream::{wrappers::UnixListenerStream, Iter}; - use tonic::{async_trait, transport::Server, Request as TonicRequest, Response}; - use url::Url; - - use trident_api::config::{HostConfiguration as ApiHostConfiguration, ImageSha384, OsImage}; - use trident_proto::{ - v1::{ - streaming_service_server::{StreamingService, StreamingServiceServer}, - FileLocation, Log, LogLevel, Started, - }, - v1preview::install_service_server::{InstallService, InstallServiceServer}, - }; - - type ResponseStream = Iter>>; + use trident_client::{Operation, RpcCode, SourceLocation}; #[test] /// Ensures a configured force flag does not affect manually chosen streams. @@ -386,64 +291,61 @@ mod tests { assert!(stream_force_requested(false, false, true)); } - /// Builds a valid completed response for gRPC client tests. - fn successful() -> Completed { - Completed { - status: StatusCode::Success.into(), - reboot_status: RebootStatus::RebootRequired.into(), - servicing_kind: Some(ServicingKind::CleanInstall.into()), - ..Default::default() + /// Builds a valid domain completion for installer-specific result checks. + fn successful() -> ClientCompletion { + ClientCompletion { + reboot: RebootDisposition::Required, + servicing_type: Some(ServicingType::CleanInstall), + image_hash: None, } } #[test] - /// Ensures final responses reject invalid statuses and unexpected reboot behavior. + /// Ensures caller-managed reboot and no-op completions retain their installer semantics. fn final_response_is_strict() { assert_eq!( - completion(successful()).unwrap(), + completion(OperationOutcome::Succeeded(successful())).unwrap(), Completion::Success { reboot_required: true, - performed: true + performed: true, } ); - completion(Completed::default()).unwrap_err(); - completion(Completed { - status: 999, + completion(OperationOutcome::Succeeded(ClientCompletion { + reboot: RebootDisposition::Started, ..successful() - }) - .unwrap_err(); - completion(Completed { - reboot_status: RebootStatus::RebootStarted.into(), - ..successful() - }) + })) .unwrap_err(); assert_eq!( - completion(Completed { - servicing_kind: Some(ServicingKind::NoneRequired.into()), + completion(OperationOutcome::Succeeded(ClientCompletion { + servicing_type: Some(ServicingType::NoActiveServicing), ..successful() - }) + })) .unwrap(), Completion::Success { reboot_required: true, - performed: false + performed: false, } ); } - #[test] - /// Ensures structured errors retain useful diagnostics without debug formatting. - fn errors_are_readable_without_debug_dumps() { - let error = RemoteError { - kind: ErrorCategory::ServicingError.into(), + /// Builds a structured remote failure for formatting and execution-state tests. + fn remote_failure() -> RemoteError { + RemoteError { + category: RemoteErrorCategory::Servicing, subkind: "download-image".into(), message: "Installation failed\n Failed to download the image".into(), error_message: "Connection timed out".into(), - location: Some(FileLocation { + location: Some(SourceLocation { path: "source.rs".into(), line: 42, }), - }; - let formatted = format_error(&error); + } + } + + #[test] + /// Ensures domain errors remain readable without protobuf debug output. + fn errors_are_readable_without_debug_dumps() { + let formatted = format_error(&remote_failure()); assert!(formatted.contains("Installation failed\n Failed to download the image")); assert!(formatted.contains("Category: Servicing")); assert!(formatted.contains("Code: download-image")); @@ -451,189 +353,31 @@ mod tests { assert!(formatted.contains("Location: source.rs:42")); assert!(!formatted.contains("TridentError {")); assert!(!formatted.contains("\\n")); - } - - #[derive(Clone)] - struct Fake { - complete: bool, - } - - #[async_trait] - impl StreamingService for Fake { - type StreamDiskStream = ResponseStream; - - /// Returns a sample stream, optionally omitting completion to simulate a disconnect. - async fn stream_disk( - &self, - request: TonicRequest, - ) -> Result, Status> { - assert_eq!( - request.into_inner().reboot.unwrap().handling, - i32::from(RebootHandling::CallerHandlesReboot) - ); - let mut responses = vec![ - ServicingResponse { - response: Some(ResponseBody::Started(Started {})), - ..Default::default() - }, - ServicingResponse { - response: Some(ResponseBody::Log(Log { - level: LogLevel::Trace.into(), - message: "full detail".into(), - ..Default::default() - })), - ..Default::default() - }, - ]; - if self.complete { - responses.push(ServicingResponse { - response: Some(ResponseBody::Completed(successful())), - ..Default::default() - }); - } - - Ok(Response::new(tokio_stream::iter( - responses.into_iter().map(Ok).collect::>(), - ))) - } - } - - #[derive(Clone)] - struct FakeInstall { - request: Arc>>, - } - - #[async_trait] - impl InstallService for FakeInstall { - type InstallStream = ResponseStream; - type InstallStageStream = ResponseStream; - type InstallFinalizeStream = ResponseStream; - - /// Captures an install request and returns a completed response. - async fn install( - &self, - request: TonicRequest, - ) -> Result, Status> { - *self.request.lock().unwrap() = Some(request.into_inner()); - Ok(Response::new(tokio_stream::iter(vec![Ok( - ServicingResponse { - response: Some(ResponseBody::Completed(successful())), - ..Default::default() - }, - )]))) - } - - /// Rejects stage-only requests, which the fake does not support. - async fn install_stage( - &self, - _: TonicRequest, - ) -> Result, Status> { - Err(Status::unimplemented("test only supports full install")) - } - - /// Rejects finalize-only requests, which the fake does not support. - async fn install_finalize( - &self, - _: TonicRequest, - ) -> Result, Status> { - Err(Status::unimplemented("test only supports full install")) - } - } - - #[tokio::test] - /// Ensures the full Host Configuration reaches gRPC with caller-managed reboot. - async fn install_sends_full_configuration_and_caller_managed_reboot() { - let root = TempDir::new().unwrap(); - let path = root.path().join("trident.sock"); - let listener = UnixListener::bind(&path).unwrap(); - let captured = Arc::new(Mutex::new(None)); - let server = tokio::spawn( - Server::builder() - .add_service(InstallServiceServer::new(FakeInstall { - request: captured.clone(), - })) - .serve_with_incoming(UnixListenerStream::new(listener)), - ); - let image = OsImage { - url: Url::parse("file:///media/cosi/selected.cosi").unwrap(), - sha384: ImageSha384::new(&format!("sha384:{}", "ab".repeat(48))).unwrap(), - }; - let config = ApiHostConfiguration { - image: Some(image.clone()), - ..Default::default() - }; - let channel = connect(&format!("unix://{}", path.display())) - .await - .unwrap(); - let stream = start( - channel, - Plan::Install { - config: Box::new(config), - source: "test".into(), - }, - ) - .await - .unwrap(); - let (tx, mut rx) = mpsc::channel(4); - receive(stream, &tx).await.unwrap(); - assert!(matches!(rx.recv().await.unwrap(), Event::Response(_))); - let request = captured.lock().unwrap().take().unwrap(); assert_eq!( - request.finalize.unwrap().reboot.unwrap().handling, - i32::from(RebootHandling::CallerHandlesReboot) + completion(OperationOutcome::Failed(remote_failure())).unwrap(), + Completion::Failure(formatted) ); - let received: ApiHostConfiguration = - serde_yaml::from_str(&request.stage.unwrap().config.unwrap().config).unwrap(); - assert_eq!(received.image, Some(image)); - server.abort(); } - #[tokio::test] - /// Ensures a Unix gRPC stream retains trace logs and detects missing completion. - async fn real_unix_transport_preserves_trace_and_detects_missing_completion() { - for complete in [true, false] { - let root = TempDir::new().unwrap(); - let path = root.path().join("trident.sock"); - let listener = UnixListener::bind(&path).unwrap(); - let server = tokio::spawn( - Server::builder() - .add_service(StreamingServiceServer::new(Fake { complete })) - .serve_with_incoming(UnixListenerStream::new(listener)), - ); - let channel = connect(&format!("unix://{}", path.display())) - .await - .unwrap(); - let stream = start( - channel, - Plan::Stream { - image: Url::parse("https://example.com/image.cosi").unwrap(), - }, - ) - .await - .unwrap(); - let (tx, mut rx) = mpsc::channel(8); - receive(stream, &tx).await.unwrap(); - drop(tx); - let mut trace = false; - let mut completed = false; - let mut uncertain = false; - while let Some(event) = rx.recv().await { - match event { - Event::Response(response) => match response.response.unwrap() { - ResponseBody::Log(log) => trace = log.level() == LogLevel::Trace, - ResponseBody::Completed(_) => completed = true, - _ => {} - }, - Event::Error { - uncertain: value, .. - } => uncertain = value, - _ => {} - } - } - assert!(trace); - assert_eq!(completed, complete); - assert_eq!(uncertain, !complete); - server.abort(); - } + #[test] + /// Distinguishes rejected writes and known failures from ambiguous servicing outcomes. + fn execution_uncertainty_is_preserved() { + assert!(!operation_uncertain(&ClientError::RequestRejected { + operation: Operation::StreamDisk, + code: RpcCode::FailedPrecondition, + message: "not started".into(), + })); + assert!(operation_uncertain(&ClientError::MissingCompletion { + operation: Operation::StreamDisk, + })); + assert!(operation_uncertain(&ClientError::InvalidProtocol { + operation: Operation::StreamDisk, + details: "invalid final status".into(), + state: ExecutionState::Completed, + })); + assert!(!operation_uncertain(&ClientError::RemoteFailure { + operation: Operation::StreamDisk, + remote: remote_failure(), + })); } } diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs index 48de66803f..e531dc61ef 100644 --- a/crates/installer/src/demo.rs +++ b/crates/installer/src/demo.rs @@ -4,9 +4,10 @@ use anyhow::Error; use clap::ValueEnum; use tokio::{sync::mpsc::Sender, time}; -use trident_proto::v1::{ - servicing_response::Response as ResponseBody, Completed, Log, LogLevel, RebootStatus, - ServicingKind, ServicingResponse, Started, StatusCode, TridentError, TridentErrorKind, +use trident_api::status::ServicingType; +use trident_client::{ + Completion, LogLevel, LogRecord, OperationOutcome, RebootDisposition, RemoteError, + RemoteErrorCategory, ServicingEvent, }; use crate::{ @@ -47,7 +48,7 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err )).await, _ => {} } - response(&tx, ResponseBody::Started(Started {})).await?; + response(&tx, ServicingEvent::Started { timestamp: None }).await?; for (level, message) in [ (LogLevel::Info, "Loading image metadata"), (LogLevel::Debug, "Inspecting available block devices"), @@ -58,10 +59,13 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err time::sleep(DEMO_STEP).await; response( &tx, - ResponseBody::Log(Log { - level: level.into(), + ServicingEvent::Log(LogRecord { + timestamp: None, + level, message: message.into(), - ..Default::default() + module: String::new(), + target: String::new(), + location: None, }), ) .await?; @@ -76,38 +80,28 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err let failed = matches!(scenario, Scenario::Failure); response( &tx, - ResponseBody::Completed(Completed { - status: if failed { - StatusCode::Failure - } else { - StatusCode::Success - } - .into(), - error: failed.then(|| TridentError { - kind: TridentErrorKind::ServicingError.into(), + ServicingEvent::Completed(if failed { + OperationOutcome::Failed(RemoteError { + category: RemoteErrorCategory::Servicing, message: "DEMO: Installation failed\nFailed to stream the OS image\nConnection timed out" .into(), subkind: "demo-network-failure".into(), error_message: "Connection timed out".into(), - ..Default::default() - }), - reboot_status: RebootStatus::RebootRequired.into(), - servicing_kind: Some(ServicingKind::CleanInstall.into()), - ..Default::default() + location: None, + }) + } else { + OperationOutcome::Succeeded(Completion { + reboot: RebootDisposition::Required, + servicing_type: Some(ServicingType::CleanInstall), + image_hash: None, + }) }), ) .await } -/// Wraps a demo gRPC response in the same event shape used by a real install. -async fn response(tx: &Sender, response: ResponseBody) -> Result<(), Error> { - client::send( - tx, - Event::Response(ServicingResponse { - response: Some(response), - ..Default::default() - }), - ) - .await +/// Wraps a demo servicing event in the same shape used by a real install. +async fn response(tx: &Sender, response: ServicingEvent) -> Result<(), Error> { + client::send(tx, Event::Response(response)).await } diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 21d66deb6d..b404eddf25 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -34,7 +34,7 @@ use tokio::{ use url::Url; use osutils::{systemd, terminal as os_terminal}; -use trident_proto::v1::{servicing_response::Response as ResponseBody, LogLevel}; +use trident_client::{LogLevel, ServicingEvent}; use crate::{ client::{self, Completion, Event}, @@ -193,7 +193,12 @@ impl LogEntry { /// Prefixes each line of a diagnostic for plain output and serial mirrors. fn plain(&self) -> String { - let prefix = format!("{}{}:{}] ", self.prefix(), self.source.label(), self.level); + let prefix = format!( + "{}{}:{}] ", + self.prefix(), + self.source.label(), + level_label(self.level) + ); display_text(&self.message) .split('\n') .map(|line| format!("{prefix}{line}")) @@ -218,7 +223,7 @@ impl LogEntry { Style::default().fg(self.source.color(linux_vt)), ), Span::styled(":", surrounding), - Span::styled(self.level.to_string(), level.add_modifier(Modifier::BOLD)), + Span::styled(level_label(self.level), level.add_modifier(Modifier::BOLD)), Span::styled("] ", surrounding), Span::styled(self.message.as_str(), level), ]) @@ -369,19 +374,19 @@ impl Model { ) } Event::Error { details, uncertain } => self.fail(details, uncertain), - Event::Response(response) => match response.response { - Some(ResponseBody::Started(_)) => { + Event::Response(response) => match response { + ServicingEvent::Started { .. } => { self.activity = "Servicing started".into(); self.log(LogSource::Inst, LogLevel::Info, self.activity.clone()) } - Some(ResponseBody::Log(log)) => { - let level = log.level(); + ServicingEvent::Log(log) => { + let level = log.level; if level == LogLevel::Info { self.activity = display_text(&log.message); } self.log(LogSource::Trident, level, log.message) } - Some(ResponseBody::Completed(completed)) => match client::completion(completed) { + ServicingEvent::Completed(completed) => match client::completion(completed) { Ok(Completion::Success { reboot_required, performed, @@ -412,10 +417,6 @@ impl Model { self.fail(details, true) } }, - None => { - let details = "Response has no body; installation outcome is unknown"; - self.fail(details.into(), true) - } }, } } @@ -1121,11 +1122,11 @@ fn display_text(text: &str) -> String { .replace('\t', " ") } -/// Compares an RPC severity against the chosen filter, treating unspecified as a warning. +/// Compares a Trident severity against the chosen display filter. fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { let level = match level { LogLevel::Error => Level::Error, - LogLevel::Warn | LogLevel::Unspecified => Level::Warn, + LogLevel::Warn => Level::Warn, LogLevel::Info => Level::Info, LogLevel::Debug => Level::Debug, LogLevel::Trace => Level::Trace, @@ -1133,11 +1134,22 @@ fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { level.to_level_filter() <= verbosity } -/// Selects the severity color, treating unspecified severity as a warning. +/// Preserves the uppercase severity names used by TUI and serial records. +fn level_label(level: LogLevel) -> &'static str { + match level { + LogLevel::Error => "ERROR", + LogLevel::Warn => "WARN", + LogLevel::Info => "INFO", + LogLevel::Debug => "DEBUG", + LogLevel::Trace => "TRACE", + } +} + +/// Selects the severity color for Trident and installer records. fn log_color(level: LogLevel) -> Color { match level { LogLevel::Error => COLOR_ERROR, - LogLevel::Warn | LogLevel::Unspecified => COLOR_WARN, + LogLevel::Warn => COLOR_WARN, LogLevel::Info => COLOR_INFO, LogLevel::Debug => COLOR_DEBUG, LogLevel::Trace => COLOR_TRACE, @@ -1479,7 +1491,10 @@ mod tests { use ratatui::backend::TestBackend; use tempfile::TempDir; - use trident_proto::v1::{Completed, RebootStatus, ServicingResponse, StatusCode}; + + use trident_client::{ + Completion as ClientCompletion, LogRecord, OperationOutcome, RebootDisposition, + }; /// A failed serial destination is removed without blocking healthy mirrors. #[test] @@ -1804,20 +1819,36 @@ mod tests { assert_eq!(model.logs.len(), 1); } + /// Domain logs retain Trident's source tag and update the displayed activity. + #[test] + fn trident_domain_log_event_updates_activity_and_serial_record() { + let mut model = Model::new(); + let entry = model.event(Event::Response(ServicingEvent::Log(LogRecord { + timestamp: None, + level: LogLevel::Info, + message: "Preparing storage".into(), + module: String::new(), + target: String::new(), + location: None, + }))); + assert_eq!(entry.source, LogSource::Trident); + assert_eq!(model.activity, "Preparing storage"); + assert_eq!(entry.plain(), "00:00 [TRIDENT:INFO] Preparing storage"); + } + /// The autorun default schedules a reboot when servicing requires one. #[test] fn headless_autorun_keeps_automatic_reboot_enabled_by_default() { let config = Config::parse("mode = 'autorun'\nserialMode = 'logs'").unwrap(); let mut model = Model::new(); model.reboot = config.autorun.reboot; - model.event(Event::Response(ServicingResponse { - response: Some(ResponseBody::Completed(Completed { - status: StatusCode::Success.into(), - reboot_status: RebootStatus::RebootRequired.into(), - ..Default::default() - })), - ..Default::default() - })); + model.event(Event::Response(ServicingEvent::Completed( + OperationOutcome::Succeeded(ClientCompletion { + reboot: RebootDisposition::Required, + servicing_type: None, + image_hash: None, + }), + ))); assert!(model.reboot_at.is_some()); } @@ -1845,14 +1876,13 @@ mod tests { LogLevel::Debug, "Earlier disk action".into(), ); - let result = model.event(Event::Response(ServicingResponse { - response: Some(ResponseBody::Completed(Completed { - status: StatusCode::Success.into(), - reboot_status: RebootStatus::RebootNotRequired.into(), - ..Default::default() - })), - ..Default::default() - })); + let result = model.event(Event::Response(ServicingEvent::Completed( + OperationOutcome::Succeeded(ClientCompletion { + reboot: RebootDisposition::NotRequired, + servicing_type: None, + image_hash: None, + }), + ))); assert_eq!(result.result_color, Some(Color::Rgb(93, 200, 148))); assert_eq!( result.plain(), From 96bc413ad997076eecde0ceba168fbbaef3b7ee0 Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 5 Nov 2026 17:15:59 -0800 Subject: [PATCH 35/62] style: Prefer equality for installer enum variants Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/client.rs | 5 +++-- crates/installer/src/demo.rs | 6 +++--- crates/installer/src/source.rs | 4 ++-- crates/installer/src/ui.rs | 7 ++++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index ae5c0197e2..d68734727b 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, ensure, Error}; +use log::Level; use tokio::{sync::mpsc::Sender, task}; use url::Url; @@ -22,7 +23,7 @@ fn stream_force_requested(automatic: bool, configured: bool, confirmed: bool) -> #[derive(Debug)] pub(super) enum Event { InstallerLog { - level: log::Level, + level: Level, message: String, }, Preparing(String), @@ -112,7 +113,7 @@ pub(super) async fn execute( require_idle: bool, tx: Sender, ) -> Result<(), Error> { - let automatic = matches!(request, Request::Autorun); + let automatic = request == Request::Autorun; let description = match &request { Request::Autorun => format!( "Autorun: HC '{}' or first media COSI in '{}'", diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs index e531dc61ef..002ba91233 100644 --- a/crates/installer/src/demo.rs +++ b/crates/installer/src/demo.rs @@ -17,7 +17,7 @@ use crate::{ const DEMO_STEP: Duration = Duration::from_millis(500); -#[derive(Debug, Clone, Copy, ValueEnum)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] pub(super) enum Scenario { Success, Failure, @@ -71,13 +71,13 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err .await?; } time::sleep(DEMO_STEP).await; - if matches!(scenario, Scenario::Disconnect) { + if scenario == Scenario::Disconnect { return client::send(&tx, Event::Error { details: "DEMO: Connection lost before Completed. Outcome unknown; another write must wait until Trident is confirmed idle.".into(), uncertain: true, }).await; } - let failed = matches!(scenario, Scenario::Failure); + let failed = scenario == Scenario::Failure; response( &tx, ServicingEvent::Completed(if failed { diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 5b813d8d4a..b0bee7fcf6 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -32,7 +32,7 @@ const MAX_METADATA_BYTES: u64 = 16 * 1024 * 1024; const METADATA_PATH: &str = "metadata.json"; const STREAM_DISK_PREFIXES: &[&str] = &["sd", "nvme", "vd", "hd", "mmcblk"]; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub(super) enum Request { Autorun, HostConfiguration(String), @@ -68,7 +68,7 @@ pub(super) fn prepare_with_progress( request: &Request, mut progress: impl FnMut(&str) -> Result<(), Error>, ) -> Result<(Config, Plan), Error> { - let root = media_root(settings, matches!(request, Request::Autorun), &mut progress)?; + let root = media_root(settings, request == &Request::Autorun, &mut progress)?; prepare_from_media_with_progress(settings, request, root.as_deref(), &mut progress) } diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index b404eddf25..8d9c172377 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -639,6 +639,7 @@ impl Model { } } +#[derive(Debug, PartialEq, Eq)] enum Action { Start(Request, bool), Shell, @@ -2044,15 +2045,15 @@ mod tests { model.fail("Complete diagnostic information".into(), false); assert!(model.key(enter(), false).is_none()); assert_eq!(model.screen, Screen::Recovery); - assert!(matches!(model.key(enter(), false), Some(Action::Shell))); + assert_eq!(model.key(enter(), false), Some(Action::Shell)); assert_eq!(model.screen, Screen::Recovery); assert_eq!(model.details, "Complete diagnostic information"); model.screen = Screen::Success; model.selected = 1; - assert!(matches!(model.key(enter(), false), Some(Action::Shell))); + assert_eq!(model.key(enter(), false), Some(Action::Shell)); assert_eq!(model.screen, Screen::Success); model.selected = 0; - assert!(matches!(model.key(enter(), false), Some(Action::Reboot))); + assert_eq!(model.key(enter(), false), Some(Action::Reboot)); } /// Force reinstall opens with Cancel selected and needs a second confirmation. From 50e067762e1ced18ee92949b2215b6429a3152ae Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 6 Nov 2026 16:57:50 -0800 Subject: [PATCH 36/62] refactor: Adapt installer to latest trident-client API Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/client.rs | 167 ++++++++++++++++++++++----------- crates/installer/src/demo.rs | 11 +-- crates/installer/src/ui.rs | 83 ++++++++++++---- 3 files changed, 181 insertions(+), 80 deletions(-) diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index d68734727b..de66a6de47 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -1,13 +1,14 @@ -use anyhow::{anyhow, ensure, Error}; +use std::borrow::Cow; + +use anyhow::{anyhow, bail, Error}; use log::Level; use tokio::{sync::mpsc::Sender, task}; use url::Url; -use trident_api::status::ServicingType; use trident_client::{ - Client, ClientError, Completion as ClientCompletion, ExecutionState, OperationOutcome, - RebootDisposition, RebootPolicy, RemoteError, RemoteErrorCategory, ServicingEvent, - ServicingOperation, + Client, ClientError, Completion as ClientCompletion, ExecutionState, RebootDisposition, + RebootPolicy, RemoteError, RemoteErrorCategory, ServicingEvent, ServicingKind, + ServicingOperation, ServicingState, }; use crate::{ @@ -51,36 +52,59 @@ pub(super) enum Completion { } /// Interprets a domain completion, enforcing the installer's caller-managed reboot contract. -pub(super) fn completion(outcome: OperationOutcome) -> Result { +pub(super) fn completion( + outcome: Result, +) -> Result { match outcome { - OperationOutcome::Succeeded(ClientCompletion { + Ok(ClientCompletion { reboot, - servicing_type, + servicing_kind, .. }) => { - ensure!( - reboot != RebootDisposition::Started, - "Daemon did not honour caller-managed reboot: {reboot:?}" - ); + let reboot_required = match reboot { + RebootDisposition::NotRequired => false, + RebootDisposition::Required => true, + RebootDisposition::Started => { + bail!("Daemon did not honour caller-managed reboot: {reboot:?}") + } + _ => bail!("Unsupported daemon reboot disposition: {reboot:?}"), + }; + let performed = match servicing_kind { + None => true, + Some(ServicingKind::NoneRequired) => false, + Some( + ServicingKind::RuntimeUpdate + | ServicingKind::AbUpdate + | ServicingKind::CleanInstall + | ServicingKind::ManualRollbackAb + | ServicingKind::ManualRollbackRuntime, + ) => true, + Some(other) => bail!("Unsupported daemon servicing kind: {other:?}"), + }; Ok(Completion::Success { - reboot_required: reboot == RebootDisposition::Required, - performed: servicing_type != Some(ServicingType::NoActiveServicing), + reboot_required, + performed, }) } - OperationOutcome::Failed(error) => Ok(Completion::Failure(format_error(&error))), + Err(error) => Ok(Completion::Failure(format_error(&error))), } } /// Renders a structured Trident error as readable multi-line diagnostics. fn format_error(error: &RemoteError) -> String { let category = match error.category { - RemoteErrorCategory::ExecutionEnvironmentMisconfiguration => "Execution environment", - RemoteErrorCategory::HealthChecks => "Health checks", - RemoteErrorCategory::Initialization => "Initialization", - RemoteErrorCategory::Internal => "Internal error", - RemoteErrorCategory::InvalidInput => "Invalid input", - RemoteErrorCategory::Servicing => "Servicing", - RemoteErrorCategory::UnsupportedConfiguration => "Unsupported configuration", + RemoteErrorCategory::ExecutionEnvironmentMisconfiguration => { + Cow::Borrowed("Execution environment") + } + RemoteErrorCategory::HealthChecks => Cow::Borrowed("Health checks"), + RemoteErrorCategory::Initialization => Cow::Borrowed("Initialization"), + RemoteErrorCategory::Internal => Cow::Borrowed("Internal error"), + RemoteErrorCategory::InvalidInput => Cow::Borrowed("Invalid input"), + RemoteErrorCategory::Servicing => Cow::Borrowed("Servicing"), + RemoteErrorCategory::UnsupportedConfiguration => Cow::Borrowed("Unsupported configuration"), + RemoteErrorCategory::Unspecified => Cow::Borrowed("Unspecified"), + RemoteErrorCategory::Unknown(value) => Cow::Owned(format!("Unknown ({value})")), + other => Cow::Owned(format!("{other:?}")), }; let mut details = format!( "{}\n\nCategory: {category}\nCode: {}", @@ -98,8 +122,25 @@ fn format_error(error: &RemoteError) -> String { /// Keeps invalid completions and unacknowledged writes uncertain, but not known remote failures. fn operation_uncertain(error: &ClientError) -> bool { - !matches!(error, ClientError::RemoteFailure { .. }) - && error.execution_state() != ExecutionState::NotStarted + error.remote_error().is_none() && error.execution_state() != ExecutionState::NotStarted +} + +/// Rejects unrecognized states before retrying an operation with an unknown prior outcome. +fn recognized_servicing_state(state: ServicingState) -> bool { + matches!( + state, + ServicingState::NotProvisioned + | ServicingState::CleanInstallStaged + | ServicingState::AbUpdateStaged + | ServicingState::ManualRollbackAbStaged + | ServicingState::ManualRollbackRuntimeStaged + | ServicingState::RuntimeUpdateStaged + | ServicingState::CleanInstallFinalized + | ServicingState::AbUpdateFinalized + | ServicingState::ManualRollbackAbFinalized + | ServicingState::Provisioned + | ServicingState::AbUpdateHealthCheckFailed + ) } /// Prepares a source, checks stream safety, and forwards Trident's domain events to the UI. @@ -203,11 +244,20 @@ pub(super) async fn execute( } }; if require_idle { - if let Err(error) = client.servicing_state().await { - return send(&tx, Event::Error { - details: format!("The previous operation's outcome is unknown. Cannot establish that Trident is idle; refusing another write.\n{error}"), - uncertain: true, - }).await; + match client.servicing_state().await { + Ok(state) if recognized_servicing_state(state) => {} + Ok(state) => { + return send(&tx, Event::Error { + details: format!("The previous operation's outcome is unknown. Trident returned an unsupported servicing state {state:?}; refusing another write."), + uncertain: true, + }).await; + } + Err(error) => { + return send(&tx, Event::Error { + details: format!("The previous operation's outcome is unknown. Cannot establish that Trident is idle; refusing another write.\n{error}"), + uncertain: true, + }).await; + } } } let response = match start(&client, plan).await { @@ -281,7 +331,7 @@ pub(super) async fn send(tx: &Sender, event: Event) -> Result<(), Error> mod tests { use super::*; - use trident_client::{Operation, RpcCode, SourceLocation}; + use trident_client::SourceLocation; #[test] /// Ensures a configured force flag does not affect manually chosen streams. @@ -296,7 +346,7 @@ mod tests { fn successful() -> ClientCompletion { ClientCompletion { reboot: RebootDisposition::Required, - servicing_type: Some(ServicingType::CleanInstall), + servicing_kind: Some(ServicingKind::CleanInstall), image_hash: None, } } @@ -305,20 +355,20 @@ mod tests { /// Ensures caller-managed reboot and no-op completions retain their installer semantics. fn final_response_is_strict() { assert_eq!( - completion(OperationOutcome::Succeeded(successful())).unwrap(), + completion(Ok(successful())).unwrap(), Completion::Success { reboot_required: true, performed: true, } ); - completion(OperationOutcome::Succeeded(ClientCompletion { + completion(Ok(ClientCompletion { reboot: RebootDisposition::Started, ..successful() })) .unwrap_err(); assert_eq!( - completion(OperationOutcome::Succeeded(ClientCompletion { - servicing_type: Some(ServicingType::NoActiveServicing), + completion(Ok(ClientCompletion { + servicing_kind: Some(ServicingKind::NoneRequired), ..successful() })) .unwrap(), @@ -327,6 +377,11 @@ mod tests { performed: false, } ); + completion(Ok(ClientCompletion { + servicing_kind: Some(ServicingKind::Unknown(42)), + ..successful() + })) + .unwrap_err(); } /// Builds a structured remote failure for formatting and execution-state tests. @@ -355,30 +410,32 @@ mod tests { assert!(!formatted.contains("TridentError {")); assert!(!formatted.contains("\\n")); assert_eq!( - completion(OperationOutcome::Failed(remote_failure())).unwrap(), + completion(Err(remote_failure())).unwrap(), Completion::Failure(formatted) ); + let category = RemoteError { + category: RemoteErrorCategory::Unknown(42), + ..remote_failure() + }; + assert!(format_error(&category).contains("Category: Unknown (42)")); + } + + /// Connection errors cannot be mistaken for an accepted servicing request. + #[tokio::test] + async fn connection_errors_do_not_mark_a_write_uncertain() { + let error = Client::connect("not an endpoint").await.unwrap_err(); + assert_eq!(error.execution_state(), ExecutionState::NotStarted); + assert!(!operation_uncertain(&error)); } #[test] - /// Distinguishes rejected writes and known failures from ambiguous servicing outcomes. - fn execution_uncertainty_is_preserved() { - assert!(!operation_uncertain(&ClientError::RequestRejected { - operation: Operation::StreamDisk, - code: RpcCode::FailedPrecondition, - message: "not started".into(), - })); - assert!(operation_uncertain(&ClientError::MissingCompletion { - operation: Operation::StreamDisk, - })); - assert!(operation_uncertain(&ClientError::InvalidProtocol { - operation: Operation::StreamDisk, - details: "invalid final status".into(), - state: ExecutionState::Completed, - })); - assert!(!operation_uncertain(&ClientError::RemoteFailure { - operation: Operation::StreamDisk, - remote: remote_failure(), - })); + /// Unknown servicing states cannot clear the uncertainty of a previous write. + fn unknown_servicing_state_is_not_accepted_for_retry() { + assert!(recognized_servicing_state(ServicingState::NotProvisioned)); + assert!(recognized_servicing_state( + ServicingState::CleanInstallFinalized + )); + assert!(!recognized_servicing_state(ServicingState::Unspecified)); + assert!(!recognized_servicing_state(ServicingState::Unknown(42))); } } diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs index 002ba91233..fdbc319ec0 100644 --- a/crates/installer/src/demo.rs +++ b/crates/installer/src/demo.rs @@ -4,10 +4,9 @@ use anyhow::Error; use clap::ValueEnum; use tokio::{sync::mpsc::Sender, time}; -use trident_api::status::ServicingType; use trident_client::{ - Completion, LogLevel, LogRecord, OperationOutcome, RebootDisposition, RemoteError, - RemoteErrorCategory, ServicingEvent, + Completion, LogLevel, LogRecord, RebootDisposition, RemoteError, RemoteErrorCategory, + ServicingEvent, ServicingKind, }; use crate::{ @@ -81,7 +80,7 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err response( &tx, ServicingEvent::Completed(if failed { - OperationOutcome::Failed(RemoteError { + Err(RemoteError { category: RemoteErrorCategory::Servicing, message: "DEMO: Installation failed\nFailed to stream the OS image\nConnection timed out" @@ -91,9 +90,9 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err location: None, }) } else { - OperationOutcome::Succeeded(Completion { + Ok(Completion { reboot: RebootDisposition::Required, - servicing_type: Some(ServicingType::CleanInstall), + servicing_kind: Some(ServicingKind::CleanInstall), image_hash: None, }) }), diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 8d9c172377..c7bf74eca3 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -1,4 +1,5 @@ use std::{ + borrow::Cow, collections::VecDeque, fs::{File, OpenOptions}, io::{self, IsTerminal, Write}, @@ -417,6 +418,10 @@ impl Model { self.fail(details, true) } }, + _ => self.fail( + "Unsupported Trident servicing event; installation outcome is unknown".into(), + true, + ), }, } } @@ -1127,22 +1132,26 @@ fn display_text(text: &str) -> String { fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { let level = match level { LogLevel::Error => Level::Error, - LogLevel::Warn => Level::Warn, + LogLevel::Warn | LogLevel::Unspecified | LogLevel::Unknown(_) => Level::Warn, LogLevel::Info => Level::Info, LogLevel::Debug => Level::Debug, LogLevel::Trace => Level::Trace, + _ => Level::Warn, }; level.to_level_filter() <= verbosity } /// Preserves the uppercase severity names used by TUI and serial records. -fn level_label(level: LogLevel) -> &'static str { +fn level_label(level: LogLevel) -> Cow<'static, str> { match level { - LogLevel::Error => "ERROR", - LogLevel::Warn => "WARN", - LogLevel::Info => "INFO", - LogLevel::Debug => "DEBUG", - LogLevel::Trace => "TRACE", + LogLevel::Error => Cow::Borrowed("ERROR"), + LogLevel::Warn => Cow::Borrowed("WARN"), + LogLevel::Info => Cow::Borrowed("INFO"), + LogLevel::Debug => Cow::Borrowed("DEBUG"), + LogLevel::Trace => Cow::Borrowed("TRACE"), + LogLevel::Unspecified => Cow::Borrowed("UNSPECIFIED"), + LogLevel::Unknown(value) => Cow::Owned(format!("UNKNOWN({value})")), + _ => Cow::Borrowed("UNSUPPORTED"), } } @@ -1150,10 +1159,11 @@ fn level_label(level: LogLevel) -> &'static str { fn log_color(level: LogLevel) -> Color { match level { LogLevel::Error => COLOR_ERROR, - LogLevel::Warn => COLOR_WARN, + LogLevel::Warn | LogLevel::Unspecified | LogLevel::Unknown(_) => COLOR_WARN, LogLevel::Info => COLOR_INFO, LogLevel::Debug => COLOR_DEBUG, LogLevel::Trace => COLOR_TRACE, + _ => COLOR_WARN, } } @@ -1494,7 +1504,7 @@ mod tests { use tempfile::TempDir; use trident_client::{ - Completion as ClientCompletion, LogRecord, OperationOutcome, RebootDisposition, + Completion as ClientCompletion, LogRecord, RebootDisposition, ServicingKind, }; /// A failed serial destination is removed without blocking healthy mirrors. @@ -1837,22 +1847,57 @@ mod tests { assert_eq!(entry.plain(), "00:00 [TRIDENT:INFO] Preparing storage"); } + /// Unknown daemon severities remain identifiable in serial output and visible as warnings. + #[test] + fn unknown_log_level_is_not_silently_discarded() { + let mut model = Model::new(); + let entry = model.event(Event::Response(ServicingEvent::Log(LogRecord { + timestamp: None, + level: LogLevel::Unknown(42), + message: "New severity".into(), + module: String::new(), + target: String::new(), + location: None, + }))); + assert_eq!(entry.plain(), "00:00 [TRIDENT:UNKNOWN(42)] New severity"); + assert!(log_visible(entry.level, LevelFilter::Warn)); + assert_eq!(log_color(entry.level), COLOR_WARN); + } + /// The autorun default schedules a reboot when servicing requires one. #[test] fn headless_autorun_keeps_automatic_reboot_enabled_by_default() { let config = Config::parse("mode = 'autorun'\nserialMode = 'logs'").unwrap(); let mut model = Model::new(); model.reboot = config.autorun.reboot; - model.event(Event::Response(ServicingEvent::Completed( - OperationOutcome::Succeeded(ClientCompletion { + model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { reboot: RebootDisposition::Required, - servicing_type: None, + servicing_kind: None, image_hash: None, - }), - ))); + }, + )))); assert!(model.reboot_at.is_some()); } + /// Unknown servicing kinds fail rather than triggering an automatic reboot. + #[test] + fn unknown_completion_kind_does_not_claim_success() { + let mut model = Model::new(); + model.reboot = true; + let entry = model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::Required, + servicing_kind: Some(ServicingKind::Unknown(42)), + image_hash: None, + }, + )))); + assert!(model.failed); + assert!(model.uncertain); + assert!(model.reboot_at.is_none()); + assert!(entry.plain().contains("Unsupported daemon servicing kind")); + } + /// Missing inputs produce a tagged failure record suitable for serial output. #[test] fn errors_for_missing_inputs_are_tagged_for_serial_without_a_tui() { @@ -1877,13 +1922,13 @@ mod tests { LogLevel::Debug, "Earlier disk action".into(), ); - let result = model.event(Event::Response(ServicingEvent::Completed( - OperationOutcome::Succeeded(ClientCompletion { + let result = model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { reboot: RebootDisposition::NotRequired, - servicing_type: None, + servicing_kind: None, image_hash: None, - }), - ))); + }, + )))); assert_eq!(result.result_color, Some(Color::Rgb(93, 200, 148))); assert_eq!( result.plain(), From 7023923700bf21a36a2322178f9c0e77530972e3 Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 6 Nov 2026 17:28:06 -0800 Subject: [PATCH 37/62] fix: Set Linux terminal type for installer shell Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 6 ++++-- packaging/systemd/trident-installer.service | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index bf36141fe8..405b80d4ea 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -118,8 +118,10 @@ log entry. Continue opens recovery: Shell, Stream COSI URL, remote HC URL, or Shutdown. Shell `exit` returns to the originating screen. The recovery shell is a root login Bash, so it reads `/etc/profile` and the root profile (if present); on the installer ISO, -`/etc/profile` gives root a red prompt. Installation continues while a shell -is open; automatic reboot waits. The success screen offers Reboot and Shell. +`/etc/profile` gives root a red prompt. The ISO service sets `TERM=linux` +for tty1 so terminal tools such as `clear` work in the shell. Installation +continues while a shell is open; automatic reboot waits. The success screen +offers Reboot and Shell. Before each recovery attempt the baked configuration is reloaded, so shell edits take effect. Invalid or missing configuration never supplies implicit defaults for an installation. diff --git a/packaging/systemd/trident-installer.service b/packaging/systemd/trident-installer.service index 4e7a845467..59a1f6e3b8 100644 --- a/packaging/systemd/trident-installer.service +++ b/packaging/systemd/trident-installer.service @@ -7,6 +7,7 @@ Conflicts=trident-install.service [Service] Type=simple +Environment=TERM=linux ExecStart=/usr/bin/trident-installer --system-console StandardInput=null StandardOutput=journal From 451d7b24954379c36312653821821fc775ce0537 Mon Sep 17 00:00:00 2001 From: Paco Date: Sat, 7 Nov 2026 11:06:22 -0800 Subject: [PATCH 38/62] chore: Focus installer PR on product changes Remove the standalone ISO and VM test tooling from this PR while retaining installer inline tests. Drop the reference to the removed image recipe. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/Reference/Installer-Configuration.md | 3 +- test/.gitignore | 7 - test/README.md | 130 -------- test/build.py | 230 --------------- test/installer/files/installer.toml | 10 - test/installer/image.yaml | 74 ----- test/make_usb.py | 86 ------ test/run.py | 342 ---------------------- test/tailor.yaml | 8 - test/test_scripts.py | 179 ----------- tests/images/.gitignore | 1 - tests/images/tailor.yaml | 8 - tests/images/tui-installer/.gitignore | 2 - tests/images/tui-installer/README.md | 51 ---- tests/images/tui-installer/image.yaml | 70 ----- 15 files changed, 1 insertion(+), 1200 deletions(-) delete mode 100644 test/.gitignore delete mode 100644 test/README.md delete mode 100755 test/build.py delete mode 100644 test/installer/files/installer.toml delete mode 100644 test/installer/image.yaml delete mode 100644 test/make_usb.py delete mode 100755 test/run.py delete mode 100644 test/tailor.yaml delete mode 100644 test/test_scripts.py delete mode 100644 tests/images/.gitignore delete mode 100644 tests/images/tailor.yaml delete mode 100644 tests/images/tui-installer/.gitignore delete mode 100644 tests/images/tui-installer/README.md delete mode 100644 tests/images/tui-installer/image.yaml diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index 4992e9700f..5016ea813f 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -173,5 +173,4 @@ No separate service enablement, getty override, or label override is necessary for a single `CDROM` ISO or `TRD_INSTALL` USB. On an unmarked normal OS, the service does not run and gettys are unaffected. A Host Configuration may require additional tools for optional features such as -RAID or encryption. See `tests/images/tui-installer/image.yaml` in the -repository for a working full image definition. +RAID or encryption. diff --git a/test/.gitignore b/test/.gitignore deleted file mode 100644 index 81ed2ece65..0000000000 --- a/test/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -/artifacts/ -/.work/ -/.vm/ -/.tailor/ -/installer/.rendered/ -/installer/cosi/ -/installer/rpms/ diff --git a/test/README.md b/test/README.md deleted file mode 100644 index b804428c4a..0000000000 --- a/test/README.md +++ /dev/null @@ -1,130 +0,0 @@ -# Real installer ISO test - -Build an Azure Linux installer ISO containing the optional `trident-installer` -RPM from this branch and an existing **non-ACL** COSI. The COSI is reused, not -rebuilt. - -```console -python3 test/build.py --cosi /path/to/regular.cosi -python3 test/run.py --display gtk -``` - -The payload is copied into the ISO's `cosi/` folder. No HC is supplied, so -autorun chooses StreamDisk. `reboot = false` leaves the success screen visible; -use its Reboot button to check what the installed disk boots. -`serialMode = "logs"` routes INST and TRIDENT log records to the serial -console as `MM:SS [SOURCE:LEVEL] message`, up to `serialVerbosity` (default -`debug`), independently of TUI verbosity. Multiline messages repeat the prefix -on each line instead of printing a literal `\n`. -This ISO suppresses the serial getty prompt and normal kernel/systemd status -output; UEFI firmware output may still appear before the installer starts. -With only a serial console active, autorun runs without a TUI and honors -`autorun.reboot`; missing inputs or an already-present image produce tagged -errors instead of waiting for a keyboard response. -The RPM's getty drop-ins skip serial and tty1 gettys only when the ISO boots -with `trident.installer=1`, so they cannot reclaim the installer's keyboard -or mix prompts into serial logs. The -installer mirrors source-discovery stages to the serial log; an idle-looking -progress screen can be diagnosed there without sending keys to the guest. - -The generated ISO is `test/artifacts/installer_amd64_iso.iso`. This local build -uses a pre-existing regular Azure Linux 3.0.20260909 COSI (175 MB compressed, -4 GiB original disk). The launcher creates an 8 GiB disposable disk. - -The launcher follows the ACL test runner's UEFI setup, but is Python. It creates -a new sparse qcow2 disk and private firmware variables for every run. No host -disk or shared host directory is exposed to the guest. Per-device `bootindex` -makes the hard disk first: a blank or non-bootable disk falls through to the ISO. -For this cached `regular.cosi`, the installer successfully streams the image, -but a subsequent UEFI boot reports a missing `\\EFI\\BOOT\\grubx64.efi` even -after ejecting the ISO. This fixture verifies installation, **not** a bootable -target OS; choose a known-bootable COSI to test both. If the ISO remains attached, -the repeat-install guard prevents a second stream. - -```console -python3 test/run.py --display gtk --keep -python3 test/run.py --dry-run -``` - -`--keep` retains the disk, firmware variables and serial log for inspection. -Otherwise that run's private `.vm/` directory is removed after QEMU exits. -The launcher follows tagged INST and TRIDENT serial records in the same -terminal, including TRACE when `serialVerbosity = "trace"`, without forwarding -firmware/GRUB escape sequences that could redraw your terminal. The complete -raw serial capture stays -in the indicated `serial.log` file while the VM runs; use `--keep` to retain it -after exit. GTK uses a separate graphical window for the interactive console. -Do not rebuild over an ISO still attached to a running VM. Use -`python3 test/build.py --output-dir test/.work/next-iso` and then pass -`--iso test/.work/next-iso/installer_amd64_iso.iso` to `run.py` to test a -replacement without changing the running guest's CD-ROM. - -## GPT USB installation media - -The installer finds a filesystem by label, so it can read COSI files from an -optical ISO9660 filesystem or a GPT USB partition (FAT32 in this fixture). -By default it accepts `TRD_INSTALL` (FAT-compatible) or `CDROM` -(Image Customizer ISO). When both are attached, specify which to use. To -build the USB test ISO, temporarily add `filesystemLabel = "TRD_INSTALL"` in -`test/installer/files/installer.toml`, run -`python3 test/build.py --skip-rpms --output-dir test/.work/gpt-usb-ready`, -and remove that line afterward. - -```console -python3 test/make_usb.py --cosi test/installer/cosi/payload.cosi -python3 test/run.py --iso test/.work/gpt-usb-ready/installer_amd64_iso.iso \ - --usb-media test/.work/installer-usb.raw --vnc --keep -``` - -`make_usb.py` creates only a local, sparse 768 MiB GPT/FAT32 image under -`test/.work`. It uses Docker's cached builder for `sfdisk` and mtools to -populate `cosi/payload.cosi`; it does not repartition a host device. -`run.py` attaches it as a read-only USB mass-storage device. The USB is smaller -than the COSI's 4 GiB original disk, so Trident cannot select it as an install -target. A larger USB that could be the smallest eligible disk is rejected by -the installer preflight rather than being overwritten; the StreamDisk RPC -does not yet accept a target disk override. -This fixture exercises a Rufus-style GPT/FAT32 layout, not Rufus itself or -NTFS; non-FAT media require filesystem support in the live OS. - -## Over SSH, including from a Wayland desktop - -On the VM host: - -```console -./test/run.py --vnc --keep -``` - -On your local desktop, open a second terminal: - -```console -ssh -N -L 5901:127.0.0.1:5901 USER@HOST -``` - -Connect a VNC client such as GNOME Connections or Remmina to -`vnc://127.0.0.1:5901`. QEMU binds VNC to loopback only; the SSH tunnel carries -the connection. The guest stays paused until the viewer connects, so there is -no setup race. The screen is initially black while the guest is paused; boot -begins automatically when the viewer connects. No X11 forwarding or desktop -environment changes are needed. -Use `--vnc 2` and tunnel TCP 5902 if display 1 is already occupied. - -## Build prerequisites - -Build prerequisites: Python 3.11+, Docker, Tailor and the cached -`azl3/trident-builder:latest` image. Trident binaries are built inside Azure -Linux rather than using workstation binaries with potentially incompatible -glibc. The USB image fixture also needs mtools (`mformat`, `mmd`, `mcopy`). -The normal repository RPM spec packages them. Tailor runs Image -Customizer 1.6 on an Azure Linux 3 minimal base. - -Runtime prerequisites: QEMU, `qemu-img`, and matching OVMF code/variables. -KVM is used when available; otherwise QEMU emulation is used. - -```console -python3 test/build.py --skip-rpms -python3 test/build.py --cosi /path/to/regular.cosi --prepare-only -``` - -Payloads, RPMs, ISO artifacts and VM disks are ignored by Git. This tests QEMU's -IDE CD-ROM path, not USB/BMC virtual-media detection. diff --git a/test/build.py b/test/build.py deleted file mode 100755 index b08edc6157..0000000000 --- a/test/build.py +++ /dev/null @@ -1,230 +0,0 @@ -#!/usr/bin/env python3 -import argparse -import datetime -import json -from pathlib import Path -import shlex -import shutil -import subprocess -import tarfile -import tempfile -import tomllib - -HERE = Path(__file__).resolve().parent -REPO = HERE.parent -IMAGE = HERE / "installer" -WORK = HERE / ".work" -BUILDER = "azl3/trident-builder:latest" -GIB = 1024**3 - - -def run(command, **kwargs): - """Run a build command and fail if it exits unsuccessfully.""" - print("+", shlex.join(map(str, command)), flush=True) - subprocess.run(list(map(str, command)), check=True, **kwargs) - - -def metadata(path): - """Validate that the fixture is a supported non-ACL Azure Linux COSI.""" - with tarfile.open(path, "r:") as archive: - member = archive.getmember("metadata.json") - if not member.isfile() or member.size > 16 * 1024**2: - raise ValueError("COSI metadata is not a bounded regular file") - info = json.load(archive.extractfile(member)) - release = info.get("osRelease", "") - if isinstance(release, dict): - release = json.dumps(release) - if "azurelinux" not in release.lower(): - raise ValueError("This fixture requires a regular Azure Linux COSI") - if any( - name in release.lower() - for name in ( - "azurelinux-container-host", - "azure container linux", - '"variantid": "acl"', - ) - ): - raise ValueError("ACL is deliberately excluded from this fixture") - if info.get("version") != "1.2" or not (info.get("disk") or {}).get("size"): - raise ValueError("The fixture needs a COSI v1.2 image with disk metadata") - if info.get("osArch") != "x86_64": - raise ValueError("The fixture requires an x86_64 payload") - return info - - -def stage_cosi(path): - """Copy the validated COSI into the ISO recipe and record its disk size.""" - path = path.resolve(strict=True) - info = metadata(path) - destination = IMAGE / "cosi/payload.cosi" - destination.parent.mkdir(parents=True, exist_ok=True) - if path != destination: - shutil.copy2(path, destination) - WORK.mkdir(exist_ok=True) - (WORK / "payload.json").write_text( - json.dumps( - { - "source": str(path), - "diskBytes": info["disk"]["size"], - "recommendedDiskGiB": max( - 8, (info["disk"]["size"] + GIB - 1) // GIB + 1 - ), - "osRelease": info["osRelease"], - }, - indent=2, - ) - + "\n" - ) - print(f"Payload: {path} ({info['disk']['size'] // GIB} GiB original disk)") - - -def build_rpms(): - """Build the Trident RPMs in a container and stage the installer sub-package.""" - run(["make", ".cargo/config", "OVERRIDE_RUST_FEED=true"], cwd=REPO) - version = tomllib.loads((REPO / "crates/trident/Cargo.toml").read_text())[ - "package" - ]["version"] - commit = subprocess.check_output( - ["git", "rev-parse", "--short", "HEAD"], cwd=REPO, text=True - ).strip() - release = f"dev.{datetime.date.today():%Y%m%d}99.v{commit}.azl3" - product_version = f"{version}-{release}" - run( - [ - "docker", - "run", - "--rm", - "-e", - f"TRIDENT_VERSION={product_version}", - "-v", - f"{REPO}:/work", - "-w", - "/work", - BUILDER, - "cargo", - "build", - "--locked", - "--release", - "--target-dir", - "target/azl3", - "--features", - "trident/dangerous-options,trident/grpc-preview", - "-p", - "trident", - "-p", - "trident-acl-agent", - "-p", - "installer", - ] - ) - tag = f"trident-installer-test-rpms:{commit}" - with tempfile.TemporaryDirectory(prefix="rpm-context-", dir=WORK) as temporary: - context = Path(temporary) - shutil.copytree(REPO / "packaging", context / "packaging") - for name in ["LICENSE", "NOTICE"]: - shutil.copy2(REPO / name, context / name) - (context / "bin").mkdir() - for name in ["trident", "trident-acl-agent", "trident-installer"]: - shutil.copy2(REPO / "target/azl3/release" / name, context / "bin" / name) - run( - [ - "docker", - "build", - "-t", - tag, - "--build-arg", - f"TRIDENT_VERSION={product_version}", - "--build-arg", - f"RPM_VER={version}", - "--build-arg", - f"RPM_REL={release}", - "-f", - str(context / "packaging/docker/Dockerfile.azl"), - str(context), - ] - ) - container = subprocess.check_output( - ["docker", "create", tag], text=True - ).strip() - try: - run( - [ - "docker", - "cp", - f"{container}:/work/trident-rpms.tar.gz", - str(context / "rpms.tar.gz"), - ] - ) - finally: - run(["docker", "rm", "-v", container]) - extracted = context / "extracted" - extracted.mkdir() - with tarfile.open(context / "rpms.tar.gz") as archive: - archive.extractall(extracted, filter="data") - destination = IMAGE / "rpms" - destination.mkdir(exist_ok=True) - for old in destination.glob("*.rpm"): - old.unlink() - for rpm in extracted.rglob("*.rpm"): - shutil.copy2(rpm, destination / rpm.name) - installer = list(destination.glob("trident-installer-*.rpm")) - if len(installer) != 1: - raise ValueError("RPM build did not produce exactly one installer package") - print(f"Installer RPM: {installer[0]}") - - -def main(): - """Parse fixture options and build or validate the installer ISO.""" - parser = argparse.ArgumentParser( - description="Build a Tailor ISO with the current Trident Linux Installer" - ) - parser.add_argument("--cosi", type=Path, help="Existing non-ACL Azure Linux COSI") - parser.add_argument( - "--skip-rpms", - action="store_true", - help="Reuse RPMs already staged by this script", - ) - parser.add_argument( - "--prepare-only", - action="store_true", - help="Stage/validate inputs without building the ISO", - ) - parser.add_argument( - "--output-dir", - type=Path, - default=HERE / "artifacts", - help="Build output directory (use a different path while an older ISO is attached to a VM)", - ) - args = parser.parse_args() - for tool in ["docker", "tailor", "make"]: - if not shutil.which(tool): - parser.error(f"{tool} is required") - payload = args.cosi or IMAGE / "cosi/payload.cosi" - if not payload.is_file(): - parser.error("Supply --cosi /path/to/an/existing/non-ACL.cosi") - stage_cosi(payload) - if args.prepare_only: - return - if not args.skip_rpms: - build_rpms() - if not list((IMAGE / "rpms").glob("trident-installer-*.rpm")): - parser.error("No installer RPM staged; run without --skip-rpms") - run(["tailor", "--manifest", str(HERE / "tailor.yaml"), "validate"], cwd=HERE) - run( - [ - "tailor", - "--manifest", - str(HERE / "tailor.yaml"), - "build", - "installer", - "--output-dir", - str(args.output_dir.resolve()), - "--log-dir", - str(WORK / "logs"), - ], - cwd=HERE, - ) - - -if __name__ == "__main__": - main() diff --git a/test/installer/files/installer.toml b/test/installer/files/installer.toml deleted file mode 100644 index 96c7157d6d..0000000000 --- a/test/installer/files/installer.toml +++ /dev/null @@ -1,10 +0,0 @@ -mode = "autorun" -serialMode = "logs" -serialVerbosity = "debug" - -[media] -cosiDirectory = "cosi/" - -[autorun] -reboot = false -force = false diff --git a/test/installer/image.yaml b/test/installer/image.yaml deleted file mode 100644 index acac1c1a94..0000000000 --- a/test/installer/image.yaml +++ /dev/null @@ -1,74 +0,0 @@ -name: installer - -outputs: - - format: iso - -base: - azureLinux: - version: "3.0" - variant: minimal-os - -rpmSources: - - rpms - -extraDependencies: - - files - - cosi - - rpms - -config: - previewFeatures: - - input-image-oci - os: - hostname: trident-installer-test - bootloader: - resetType: hard-reset - selinux: - mode: disabled - kernelCommandLine: - extraCommandLine: - - console=tty0 - - console=ttyS0,115200n8 - - quiet - - loglevel=0 - - systemd.show_status=false - packages: - install: - - trident-installer - additionalFiles: - - source: files/installer.toml - destination: /etc/trident/installer.toml - storage: - bootType: efi - disks: - - partitionTableType: gpt - maxSize: 4G - partitions: - - id: esp - type: esp - size: 8M - - id: rootfs - size: grow - filesystems: - - deviceId: esp - type: fat32 - mountPoint: - path: /boot/efi - options: umask=0077 - - deviceId: rootfs - type: ext4 - mountPoint: - path: / - iso: - initramfsType: full-os - additionalFiles: - - source: cosi/payload.cosi - destination: /cosi/payload.cosi - kernelCommandLine: - extraCommandLine: - - console=tty0 - - console=ttyS0,115200n8 - - quiet - - loglevel=0 - - systemd.show_status=false - - trident.installer=1 diff --git a/test/make_usb.py b/test/make_usb.py deleted file mode 100644 index 988c8e2ffc..0000000000 --- a/test/make_usb.py +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env python3 -import argparse -import json -import os -from pathlib import Path -import shutil -import subprocess -import tempfile - -from build import BUILDER, GIB, HERE, IMAGE, metadata, run - -LABEL = "TRD_INSTALL" -DEFAULT_SIZE_MIB = 768 -PARTITION_RESERVE_MIB = 68 -SECTOR_SIZE = 512 - - -def build_usb(cosi, output, size_mib): - """Create a new GPT/FAT32 image smaller than the COSI's target disk.""" - source = cosi.resolve(strict=True) - info = metadata(source) - if size_mib < 512: - raise ValueError("A FAT32 USB test image needs at least 512 MiB") - if size_mib * 1024**2 >= info["disk"]["size"]: - raise ValueError("USB test media must be smaller than the COSI disk size") - if output.exists() or output.is_symlink(): - raise FileExistsError(f"Refusing to overwrite existing USB image: {output}") - - output.parent.mkdir(parents=True, exist_ok=True) - with tempfile.TemporaryDirectory(prefix="usb-media-", dir=output.parent) as scratch: - image = Path(scratch) / "usb.raw" - with image.open("wb") as file: - file.truncate(size_mib * 1024**2) - volume = f"{scratch}:/media" - base = [ - "docker", - "run", - "--rm", - "-i", - "--user", - f"{os.getuid()}:{os.getgid()}", - "-v", - volume, - BUILDER, - ] - subprocess.run( - base + ["sfdisk", "/media/usb.raw"], - input=f"label: gpt\n,{size_mib - PARTITION_RESERVE_MIB}M,U\n", - check=True, - text=True, - ) - result = subprocess.run( - base + ["sfdisk", "--json", "/media/usb.raw"], - check=True, - capture_output=True, - text=True, - ) - table = json.loads(result.stdout)["partitiontable"] - if table["label"] != "gpt" or len(table["partitions"]) != 1: - raise ValueError("Expected one GPT partition on USB test media") - part = table["partitions"][0] - device = f"{image}@@{part['start'] * SECTOR_SIZE}" - run(["mformat", "-i", device, "-T", part["size"], "-F", "-v", LABEL, "::"]) - run(["mmd", "-i", device, "::/cosi"]) - run(["mcopy", "-i", device, source, "::/cosi/payload.cosi"]) - shutil.move(image, output) - print(f"GPT/FAT32 installer USB: {output} ({size_mib} MiB, label {LABEL})") - - -def main(): - """Validate image-builder arguments and create a disposable USB fixture.""" - parser = argparse.ArgumentParser( - description="Create a GPT/FAT32 USB image for installer VM testing" - ) - parser.add_argument("--cosi", type=Path, default=IMAGE / "cosi/payload.cosi") - parser.add_argument("--output", type=Path, default=HERE / ".work/installer-usb.raw") - parser.add_argument("--size-mib", type=int, default=DEFAULT_SIZE_MIB) - args = parser.parse_args() - for tool in ["docker", "mformat", "mmd", "mcopy"]: - if shutil.which(tool) is None: - parser.error(f"{tool} is required") - build_usb(args.cosi, args.output.resolve(), args.size_mib) - - -if __name__ == "__main__": - main() diff --git a/test/run.py b/test/run.py deleted file mode 100755 index b40c614133..0000000000 --- a/test/run.py +++ /dev/null @@ -1,342 +0,0 @@ -#!/usr/bin/env python3 -import argparse -import contextlib -import json -import os -from pathlib import Path -import re -import select -import shlex -import shutil -import socket -import subprocess -import sys -import tempfile -import time - -HERE = Path(__file__).resolve().parent -QMP_STARTUP_TIMEOUT_SECONDS = 30 -SERIAL_POLL_SECONDS = 0.1 -INSTALLER_RECORD = re.compile(r"^\d{2,}:[0-5]\d \[(?:INST|TRIDENT):[A-Z]+\] ") -CODE_CANDIDATES = [ - Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), - Path("/usr/share/OVMF/OVMF_CODE.fd"), - Path("/usr/share/edk2/ovmf/OVMF_CODE.fd"), - Path("/usr/share/qemu/edk2-x86_64-code.fd"), -] - - -def command(iso, scratch, code, memory, cpus, display, vnc=None, usb_media=None): - """Construct QEMU arguments with disposable storage and read-only install media.""" - acceleration = ( - ["-enable-kvm", "-cpu", "host"] if os.access("/dev/kvm", os.W_OK) else [] - ) - launch = [ - "qemu-system-x86_64", - *acceleration, - "-m", - str(memory), - "-smp", - str(cpus), - "-machine", - "q35", - "-drive", - f"if=pflash,format=raw,unit=0,readonly=on,file={code}", - "-drive", - f"if=pflash,format=raw,unit=1,file={scratch / 'vars.fd'}", - "-drive", - f"if=none,id=hd0,file={scratch / 'disk.qcow2'},format=qcow2", - "-device", - "virtio-blk-pci,drive=hd0,bootindex=1", - "-drive", - f"if=none,id=cd0,file={iso},media=cdrom,readonly=on", - "-device", - "ide-cd,bus=ide.0,drive=cd0,bootindex=2", - "-nic", - "user,model=virtio-net-pci", - "-display", - "none" if vnc is not None else display, - "-serial", - f"file:{scratch / 'serial.log'}", - ] - if vnc is not None: - launch.extend( - [ - "-S", - "-qmp", - f"unix:{scratch / 'monitor.sock'},server=on,wait=off", - "-vnc", - f"127.0.0.1:{vnc}", - ] - ) - if usb_media is not None: - launch.extend( - [ - "-device", - "qemu-xhci,id=usb", - "-drive", - f"if=none,id=installer-usb,file={usb_media},format=raw,readonly=on", - "-device", - "usb-storage,bus=usb.0,drive=installer-usb,bootindex=3", - ] - ) - return launch - - -class Qmp: - def __init__(self, connection, process): - """Track the QMP connection and viewer state for a paused guest.""" - self.connection = connection - self.process = process - self.pending = b"" - self.viewer_connected = False - - def receive(self): - """Read a QMP response or event, updating the VNC connection state.""" - while b"\n" not in self.pending: - if self.process.poll() is not None: - raise RuntimeError("QEMU exited while waiting for a VNC viewer") - if not select.select([self.connection], [], [], 1)[0]: - continue - data = self.connection.recv(4096) - if not data: - raise RuntimeError("QEMU monitor disconnected") - self.pending += data - line, self.pending = self.pending.split(b"\n", 1) - message = json.loads(line) - if message.get("event") == "VNC_INITIALIZED": - self.viewer_connected = True - elif message.get("event") == "VNC_DISCONNECTED": - self.viewer_connected = False - return message - - def execute(self, command): - """Send a QMP command and wait for its matching response.""" - request = {"execute": command, "id": command} - self.connection.sendall(json.dumps(request).encode() + b"\n") - while True: - response = self.receive() - if response.get("id") == command: - if "error" in response: - raise RuntimeError(f"QEMU {command}: {response['error']}") - return response["return"] - - -def wait_for_vnc_viewer(process, monitor_path, on_ready): - """Resume the paused guest only after the VNC monitor reports a viewer.""" - deadline = time.monotonic() + QMP_STARTUP_TIMEOUT_SECONDS - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as connection: - while True: - if process.poll() is not None: - raise RuntimeError("QEMU exited before the VNC monitor was ready") - try: - connection.connect(str(monitor_path)) - break - except (FileNotFoundError, ConnectionRefusedError): - if time.monotonic() >= deadline: - raise RuntimeError("QEMU monitor did not become ready") - time.sleep(0.1) - qmp = Qmp(connection, process) - if "QMP" not in qmp.receive(): - raise RuntimeError("Invalid greeting from QEMU monitor") - qmp.execute("qmp_capabilities") - status = qmp.execute("query-vnc") - if not status.get("enabled"): - raise RuntimeError("QEMU VNC display is not enabled") - on_ready() - if not status.get("clients"): - while not qmp.viewer_connected: - qmp.receive() - print("VNC viewer connected. Starting the guest now.", flush=True) - qmp.execute("cont") - - -def follow_serial(process, path, output=sys.stdout, interval=SERIAL_POLL_SECONDS): - """Print new installer records from serial, excluding firmware and escape codes.""" - stream = None - pending = "" - try: - while True: - if stream is None and path.exists(): - stream = path.open(encoding="utf-8", errors="replace") - if stream is not None: - pending += stream.read() - while "\n" in pending: - line, pending = pending.split("\n", 1) - line = line.rstrip("\r") - if INSTALLER_RECORD.match(line): - print(line, file=output, flush=True) - status = process.poll() - if status is not None: - if pending and INSTALLER_RECORD.match(pending): - print(pending.rstrip("\r"), file=output, flush=True) - return status - time.sleep(interval) - finally: - if stream is not None: - stream.close() - - -def main(): - """Launch a disposable VM, optionally wait for VNC, and follow serial logs.""" - parser = argparse.ArgumentParser( - description="Boot the installer ISO in a disposable UEFI VM" - ) - parser.add_argument("--iso", type=Path) - parser.add_argument( - "--usb-media", - type=Path, - help="Attach a GPT/FAT USB image read-only; never pass a host block device", - ) - displays = parser.add_mutually_exclusive_group() - displays.add_argument("--display", default="gtk", choices=["gtk", "sdl", "none"]) - displays.add_argument( - "--vnc", - type=int, - nargs="?", - const=1, - metavar="DISPLAY", - help="Headless VNC on localhost; display 1 is TCP 5901 (default)", - ) - parser.add_argument("--memory", type=int, default=6144, help="Guest RAM in MiB") - parser.add_argument("--cpus", type=int, default=2) - parser.add_argument( - "--disk-size", - type=int, - help="Fresh qcow2 size in GiB; derived from payload by default", - ) - parser.add_argument( - "--keep", - action="store_true", - help="Keep the disk, firmware variables and serial log", - ) - parser.add_argument( - "--dry-run", - action="store_true", - help="Print launch command without creating or booting a VM", - ) - args = parser.parse_args() - if args.vnc is not None and not 0 <= args.vnc <= 99: - parser.error("--vnc display must be between 0 and 99") - for tool in ["qemu-system-x86_64", "qemu-img"]: - if not shutil.which(tool): - parser.error(f"{tool} is required") - images = sorted((HERE / "artifacts").glob("installer*.iso")) - iso = args.iso or (images[0] if len(images) == 1 else None) - if iso is None or not iso.is_file(): - parser.error("Build the ISO first, or provide --iso PATH") - iso = iso.resolve() - usb_media = args.usb_media.resolve() if args.usb_media is not None else None - if usb_media is not None and not usb_media.is_file(): - parser.error("--usb-media must be a regular image file, not a physical device") - if any("," in str(path) for path in (iso, usb_media) if path is not None): - parser.error("QEMU drive paths containing commas are not supported") - code = next((path for path in CODE_CANDIDATES if path.is_file()), None) - if code is None: - parser.error("OVMF firmware is required") - variables = code.with_name(code.name.replace("CODE", "VARS")) - if not variables.is_file(): - parser.error(f"Matching OVMF variables not found: {variables}") - metadata_path = HERE / ".work/payload.json" - recommended = ( - json.loads(metadata_path.read_text())["recommendedDiskGiB"] - if metadata_path.exists() - else 8 - ) - disk_size = recommended if args.disk_size is None else args.disk_size - if disk_size < recommended or args.memory < 1024 or args.cpus < 1: - parser.error(f"Use at least {recommended} GiB disk, 1024 MiB RAM, and one CPU") - if args.dry_run: - print( - shlex.join( - command( - iso, - HERE / ".vm/NEW-DISPOSABLE-VM", - code, - args.memory, - args.cpus, - args.display, - args.vnc, - usb_media, - ) - ) - ) - return - state = HERE / ".vm" - state.mkdir(exist_ok=True) - scratch = Path(tempfile.mkdtemp(prefix="installer-", dir=state)) - try: - shutil.copy2(variables, scratch / "vars.fd") - os.chmod(scratch / "vars.fd", 0o600) - subprocess.run( - [ - "qemu-img", - "create", - "-f", - "qcow2", - str(scratch / "disk.qcow2"), - f"{disk_size}G", - ], - check=True, - ) - launch = command( - iso, - scratch, - code, - args.memory, - args.cpus, - args.display, - args.vnc, - usb_media, - ) - print( - f"ISO: {iso}\nDisposable disk: {scratch / 'disk.qcow2'}\nSerial log: {scratch / 'serial.log'}", - flush=True, - ) - print( - "Disk-first UEFI boot: a blank or non-bootable disk falls through to the ISO.", - flush=True, - ) - if args.vnc is not None: - port = 5900 + args.vnc - process = subprocess.Popen(launch) - try: - if args.vnc is not None: - - def show_vnc_instructions(): - """Print loopback VNC connection instructions before guest boot.""" - print( - f"Guest paused; VNC on localhost:{port} only. Tunnel from your local machine:\n" - f" ssh -N -L {port}:127.0.0.1:{port} USER@HOST\n" - f"Connect to vnc://127.0.0.1:{port}; boot begins after the viewer connects.", - flush=True, - ) - - wait_for_vnc_viewer( - process, scratch / "monitor.sock", show_vnc_instructions - ) - print( - "Following installer serial records here (full capture: serial.log).", - flush=True, - ) - status = follow_serial(process, scratch / "serial.log") - finally: - if process.poll() is None: - process.terminate() - try: - process.wait(timeout=5) - except subprocess.TimeoutExpired: - process.kill() - process.wait() - if status: - raise subprocess.CalledProcessError(status, launch) - finally: - if args.keep: - print(f"Kept VM files: {scratch}") - else: - shutil.rmtree(scratch) - - -if __name__ == "__main__": - with contextlib.suppress(KeyboardInterrupt): - main() diff --git a/test/tailor.yaml b/test/tailor.yaml deleted file mode 100644 index 36a874321c..0000000000 --- a/test/tailor.yaml +++ /dev/null @@ -1,8 +0,0 @@ -schemaVersion: 1 - -toolchains: - default: ic - entries: - - name: ic - container: mcr.microsoft.com/azurelinux/imagecustomizer - tag: "1.6" diff --git a/test/test_scripts.py b/test/test_scripts.py deleted file mode 100644 index ad87cb534e..0000000000 --- a/test/test_scripts.py +++ /dev/null @@ -1,179 +0,0 @@ -import importlib.util -import io -import json -from pathlib import Path -import socket -import tempfile -import threading -import time -import unittest - -HERE = Path(__file__).resolve().parent -SPEC = importlib.util.spec_from_file_location("installer_vm", HERE / "run.py") -VM = importlib.util.module_from_spec(SPEC) -SPEC.loader.exec_module(VM) - - -class VmCommandTests(unittest.TestCase): - def test_usb_media_is_a_read_only_gpt_image_with_lower_boot_priority(self): - """The USB fixture is attached read-only behind the disposable disk.""" - image = Path("/tmp/disposable-installer-usb.raw") - launch = VM.command( - Path("/tmp/installer-test.iso"), - Path("/tmp/installer-test-owned"), - Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), - 6144, - 2, - "gtk", - usb_media=image, - ) - drives = [ - launch[index + 1] for index, value in enumerate(launch) if value == "-drive" - ] - self.assertIn(f"file={image},format=raw,readonly=on", drives[-1]) - self.assertIn("qemu-xhci,id=usb", launch) - self.assertIn("usb-storage,bus=usb.0,drive=installer-usb,bootindex=3", launch) - self.assertNotIn("-virtfs", launch) - - def test_follows_new_serial_records_without_replaying_firmware_escape_codes(self): - """Only complete and newly appended installer records reach the console.""" - - class RunningProcess: - def __init__(self): - """Create a flag for simulated guest completion.""" - self.finished = threading.Event() - - def poll(self): - """Return an exit status only after the guest finishes.""" - return 0 if self.finished.is_set() else None - - process = RunningProcess() - with tempfile.TemporaryDirectory() as directory: - serial = Path(directory) / "serial.log" - output = io.StringIO() - - def guest(): - """Write firmware noise followed by incremental installer records.""" - time.sleep(0.03) - with serial.open("w") as log: - log.write("\x1b[2JWelcome to firmware\r\n") - log.write("00:00 [INST:INFO] Installer starting\r\n") - log.flush() - time.sleep(0.03) - log.write("00:01 [TRIDENT:TRACE] first\r\n") - log.write("00:01 [TRIDENT:TRACE] second\r\n") - log.flush() - process.finished.set() - - writer = threading.Thread(target=guest) - writer.start() - self.assertEqual( - VM.follow_serial(process, serial, output=output, interval=0.005), 0 - ) - writer.join(timeout=3) - self.assertFalse(writer.is_alive()) - self.assertEqual( - output.getvalue(), - "00:00 [INST:INFO] Installer starting\n" - "00:01 [TRIDENT:TRACE] first\n" - "00:01 [TRIDENT:TRACE] second\n", - ) - - def test_vnc_is_headless_and_bound_to_loopback(self): - """VNC mode pauses boot and binds only the loopback interface.""" - launch = VM.command( - Path("/tmp/installer-test.iso"), - Path("/tmp/installer-test-owned"), - Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), - 6144, - 2, - "gtk", - vnc=1, - ) - self.assertEqual(launch[launch.index("-display") + 1], "none") - self.assertEqual(launch[launch.index("-vnc") + 1], "127.0.0.1:1") - self.assertIn("-S", launch) - self.assertEqual( - launch[launch.index("-qmp") + 1], - "unix:/tmp/installer-test-owned/monitor.sock,server=on,wait=off", - ) - self.assertNotIn("gtk", launch) - - def test_vnc_connection_resumes_paused_guest_after_viewer_handshake(self): - """A viewer handshake must precede the QMP continue command.""" - - class RunningProcess: - def poll(self): - """Simulate a still-running guest.""" - return None - - with tempfile.TemporaryDirectory() as directory: - monitor = Path(directory) / "monitor.sock" - commands = [] - ready = [] - - def monitor_server(): - """Serve the minimal QMP handshake and a viewer connection event.""" - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listener: - listener.bind(str(monitor)) - listener.listen(1) - connection, _ = listener.accept() - with connection, connection.makefile("r") as reader: - connection.sendall( - b'{"QMP":{"version":{},"capabilities":[]}}\r\n' - ) - for expected in ["qmp_capabilities", "query-vnc", "cont"]: - request = json.loads(reader.readline()) - commands.append(request["execute"]) - if expected == "query-vnc": - connection.sendall( - b'{"return":{"enabled":true,"clients":[]},"id":"query-vnc"}\r\n' - ) - connection.sendall(b'{"event":"VNC_INITIALIZED"}\r\n') - else: - response = {"return": {}, "id": expected} - connection.sendall( - json.dumps(response).encode() + b"\r\n" - ) - - server = threading.Thread(target=monitor_server, daemon=True) - server.start() - VM.wait_for_vnc_viewer( - RunningProcess(), monitor, lambda: ready.append(True) - ) - server.join(timeout=3) - self.assertFalse(server.is_alive()) - self.assertEqual(commands, ["qmp_capabilities", "query-vnc", "cont"]) - self.assertEqual(ready, [True]) - - def test_vm_exposes_only_new_disposable_storage(self): - """Only the new qcow2 disk may be a writable guest storage device.""" - scratch = Path("/tmp/installer-test-owned") - launch = VM.command( - Path("/tmp/installer-test.iso"), - scratch, - Path("/usr/share/OVMF/OVMF_CODE_4M.fd"), - 6144, - 2, - "gtk", - ) - drives = [ - launch[index + 1] for index, value in enumerate(launch) if value == "-drive" - ] - self.assertEqual(len(drives), 4) - self.assertIn(f"file={scratch}/disk.qcow2", drives[2]) - self.assertIn("readonly=on", drives[0]) - self.assertIn("readonly=on", drives[3]) - self.assertNotIn("-virtfs", launch) - self.assertNotIn("-fsdev", launch) - self.assertIn("virtio-blk-pci,drive=hd0,bootindex=1", launch) - self.assertIn("ide-cd,bus=ide.0,drive=cd0,bootindex=2", launch) - self.assertIn("gtk", launch) - self.assertNotIn("-vnc", launch) - self.assertEqual( - launch[launch.index("-serial") + 1], f"file:{scratch}/serial.log" - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/images/.gitignore b/tests/images/.gitignore deleted file mode 100644 index 40a4bdbe07..0000000000 --- a/tests/images/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/.tailor/ diff --git a/tests/images/tailor.yaml b/tests/images/tailor.yaml deleted file mode 100644 index 36a874321c..0000000000 --- a/tests/images/tailor.yaml +++ /dev/null @@ -1,8 +0,0 @@ -schemaVersion: 1 - -toolchains: - default: ic - entries: - - name: ic - container: mcr.microsoft.com/azurelinux/imagecustomizer - tag: "1.6" diff --git a/tests/images/tui-installer/.gitignore b/tests/images/tui-installer/.gitignore deleted file mode 100644 index 85bebb1a42..0000000000 --- a/tests/images/tui-installer/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/cosi/ -/rpms/ diff --git a/tests/images/tui-installer/README.md b/tests/images/tui-installer/README.md deleted file mode 100644 index abf4ddb377..0000000000 --- a/tests/images/tui-installer/README.md +++ /dev/null @@ -1,51 +0,0 @@ -# TUI installer ISO - -Tailor builds an Azure Linux 3 ISO with the `trident-installer` RPM and Trident's -gRPC daemon. The ISO recipe installs only the RPM and adds the -`trident.installer=1` boot flag; the RPM enables the service and excludes -competing gettys on that boot. The installer mounts the `CDROM`-labelled ISO -read-only and streams the staged COSI. It does **not** enter demo mode: boot -only with a disposable target disk. The packaged configuration reboots -automatically after successful installation when Trident requires it. - -Stage matching Trident RPMs (including `trident-installer`) in `rpms/` and an -Azure Linux COSI in `cosi/payload.cosi`. These inputs are local and ignored by -Git. Use matching RPMs from `bin/RPMS/` after building the Trident RPMs, -and a COSI such as `artifacts/trident-testimage.cosi` from the existing image -builder. From the repository root: - -```console -tailor --manifest tests/images/tailor.yaml validate -tailor --manifest tests/images/tailor.yaml build tui-installer \ - --output-dir tests/images/artifacts -``` - -The generated ISO is `tests/images/artifacts/tui-installer_amd64_iso.iso`. -Build this image with Tailor, not the legacy `testimages.py` CLI. -Use the existing virt-deploy and netlaunch tools to boot it in a VM with a new -disposable disk (at least as large as the COSI's target disk): - -```console -mkdir -p test/.work -bin/virtdeploy create-one --namespace tui-installer-demo \ - --network 192.168.244.0/24 --disks 8 --mem 6 \ - --no-secure-boot --no-tpm \ - --netlaunch test/.work/tui-installer-netlaunch.yaml -bin/netlaunch --port 46119 \ - --config test/.work/tui-installer-netlaunch.yaml \ - --iso tests/images/artifacts/tui-installer_amd64_iso.iso -``` - -The ISO-only `--port` mode needs netlaunch with the fix in #909; older versions -exit on the firmware's first HTTP probe before the ISO can boot. Leave -netlaunch serving until the installer has loaded. In another terminal, run -`virsh --connect qemu:///system domdisplay tui-installer-demo-vm-0` to find -the SPICE address. Connect with a SPICE viewer; for a remote VM host, tunnel -that address's localhost port over SSH. When finished, stop netlaunch and run -`bin/virtdeploy clean --namespace tui-installer-demo` to remove only this VM, -its disk, and its libvirt network. - -With no explicit `filesystemLabel`, `CDROM` and `TRD_INSTALL` are recognized. -If both media are present, specify one in the bootstrap TOML to disambiguate. -`trident-installer` is optional in the RPM build, but its package must be -present in `rpms/` for this image to build. diff --git a/tests/images/tui-installer/image.yaml b/tests/images/tui-installer/image.yaml deleted file mode 100644 index 4bfbd6409d..0000000000 --- a/tests/images/tui-installer/image.yaml +++ /dev/null @@ -1,70 +0,0 @@ -name: tui-installer - -outputs: - - format: iso - -base: - azureLinux: - version: "3.0" - variant: minimal-os - -rpmSources: - - rpms - -extraDependencies: - - cosi - - rpms - -config: - previewFeatures: - - input-image-oci - os: - hostname: trident-installer - bootloader: - resetType: hard-reset - selinux: - mode: disabled - kernelCommandLine: - extraCommandLine: - - console=tty0 - - console=ttyS0,115200n8 - - quiet - - loglevel=0 - - systemd.show_status=false - packages: - install: - - trident-installer - storage: - bootType: efi - disks: - - partitionTableType: gpt - maxSize: 4G - partitions: - - id: esp - type: esp - size: 8M - - id: rootfs - size: grow - filesystems: - - deviceId: esp - type: fat32 - mountPoint: - path: /boot/efi - options: umask=0077 - - deviceId: rootfs - type: ext4 - mountPoint: - path: / - iso: - initramfsType: full-os - additionalFiles: - - source: cosi/payload.cosi - destination: /cosi/payload.cosi - kernelCommandLine: - extraCommandLine: - - console=tty0 - - console=ttyS0,115200n8 - - quiet - - loglevel=0 - - systemd.show_status=false - - trident.installer=1 From 0952686b1a799f0dd896a972737c281284264269 Mon Sep 17 00:00:00 2001 From: Paco Date: Sat, 7 Nov 2026 17:41:25 -0800 Subject: [PATCH 39/62] packaging: Require only installer runtime dependencies Keep Trident and Bash for the daemon and recovery shell. Leave image-specific tools and recovery conveniences to ISO recipes instead of pulling them into every installer RPM. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 6 ++++-- docs/Reference/Installer-Configuration.md | 4 ++-- packaging/rpm/trident.spec | 6 ------ 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 405b80d4ea..d083f7168c 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -172,11 +172,13 @@ automatically reboot. On a full-OS installer ISO, install `trident-installer`, add `trident.installer=1` to its **ISO kernel command line**, and provide a COSI -under `cosi/` or a Host Configuration. The RPM pulls in Trident and common -installer tools, enables its service, starts the gRPC socket through the +under `cosi/` or a Host Configuration. The RPM pulls in Trident and Bash for +the recovery shell, enables its service, starts the gRPC socket through the service dependency, and suppresses tty1 and serial gettys on marked boots. Without that flag the service skips startup and normal gettys are unchanged, including on an installed OS that happens to have the RPM. +Include optional tools in the ISO when the image or Host Configuration needs +them (for example, `veritysetup` for verity-enabled storage). Do not also enable `trident-install.service` or older autologin entrypoints. The daemon must be built with `grpc-preview` for regular Install; repository diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index 5016ea813f..8da0af311a 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -166,11 +166,11 @@ iso: - trident.installer=1 ``` -The RPM includes the installer TOML, Trident and common runtime tools. It +The RPM includes the installer TOML and requires Trident and Bash. It starts its service and gRPC socket on a marked installer boot and prevents gettys from reclaiming tty1 or mixing login prompts with serial logs. No separate service enablement, getty override, or label override is necessary for a single `CDROM` ISO or `TRD_INSTALL` USB. On an unmarked normal OS, the service does not run and gettys are unaffected. A Host Configuration may require additional tools for optional features such as -RAID or encryption. +RAID, encryption, or verity. diff --git a/packaging/rpm/trident.spec b/packaging/rpm/trident.spec index d420bf79d0..cec8aeb9e8 100644 --- a/packaging/rpm/trident.spec +++ b/packaging/rpm/trident.spec @@ -255,12 +255,6 @@ The Trident ACL components required to orchestrate servicing of ACL images. Summary: Trident Linux Installer console for installer media Requires: %{name} = %{version}-%{release} Requires: bash -Requires: device-mapper -Requires: squashfs-tools -Requires: veritysetup -Requires: curl -Requires: less -Requires: iproute Conflicts: %{name}-install-service %description installer From b27962d4f6e8c2f0cd333f05459a8d0301d7cfd7 Mon Sep 17 00:00:00 2001 From: Paco Date: Mon, 9 Nov 2026 16:00:03 -0800 Subject: [PATCH 40/62] fix: Address installer PR review findings Bind StreamDisk to preflighted COSI metadata, redact source descriptions, keep remote stream recovery independent of local media, and make serial mirrors nonblocking. Avoid activating installer dependencies on unmarked boots and regenerate third-party notices. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Cargo.lock | 1 + NOTICE | 268 ++++++++++++++++++++ crates/installer/Cargo.toml | 1 + crates/installer/README.md | 25 +- crates/installer/src/client.rs | 87 +++++-- crates/installer/src/main.rs | 4 +- crates/installer/src/source.rs | 184 +++++++++++--- crates/osutils/src/systemd.rs | 10 + crates/osutils/src/terminal.rs | 26 +- docs/Reference/Installer-Configuration.md | 9 +- packaging/systemd/trident-installer.service | 3 +- 11 files changed, 543 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c15bfa75f4..46a3c3524c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1734,6 +1734,7 @@ dependencies = [ "serde", "serde_json", "serde_yaml", + "sha2", "tar", "tempfile", "tokio", diff --git a/NOTICE b/NOTICE index bddc349267..edea8b7fdd 100644 --- a/NOTICE +++ b/NOTICE @@ -2651,6 +2651,35 @@ The `cfg_aliases!` macro uses a lot of the code from [`tectonic_cfg_support::tar -------------------------------------------------------------------------------- MIT License (MIT) +Used by: + - instability 0.3.10 + +# MIT License + +Copyright (c) 2020 Stephen M. Coakley +Copyright (c) The Ratatui Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +-------------------------------------------------------------------------------- +MIT License (MIT) + Used by: - schemars_derive 0.8.21 @@ -3383,6 +3412,7 @@ Used by: - heck 0.5.0 - unicode-normalization 0.1.24 - unicode-segmentation 1.12.0 + - unicode-width 0.1.14 - unicode-width 0.2.0 - unicode-xid 0.2.6 @@ -4488,7 +4518,9 @@ DEALINGS IN THE SOFTWARE. MIT License (MIT) Used by: + - signal-hook 0.3.18 - signal-hook 0.4.3 + - signal-hook-mio 0.2.5 - signal-hook-registry 1.4.7 Copyright (c) 2017 tokio-jsonrpc developers @@ -4863,6 +4895,38 @@ DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- MIT License (MIT) +Used by: + - unicode-truncate 1.1.0 + +Copyright (c) 2019 Aetf + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +-------------------------------------------------------------------------------- +MIT License (MIT) + Used by: - slab 0.4.9 @@ -5422,6 +5486,11 @@ Used by: - env_logger 0.10.2 - env_logger 0.11.5 - is_terminal_polyfill 1.70.1 + - serde_spanned 0.6.9 + - toml 0.8.23 + - toml_datetime 0.6.11 + - toml_edit 0.22.27 + - toml_write 0.1.2 Copyright (c) Individual contributors @@ -5705,6 +5774,34 @@ DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- MIT License (MIT) +Used by: + - lru 0.12.5 + +MIT License + +Copyright (c) 2016 Jerome Froelich + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +-------------------------------------------------------------------------------- +MIT License (MIT) + Used by: - textwrap 0.16.2 @@ -5874,6 +5971,34 @@ SOFTWARE. -------------------------------------------------------------------------------- MIT License (MIT) +Used by: + - static_assertions 1.1.0 + +MIT License + +Copyright (c) 2017 Nikolai Vazquez + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +-------------------------------------------------------------------------------- +MIT License (MIT) + Used by: - gpt 4.1.0 @@ -6290,6 +6415,62 @@ SOFTWARE. -------------------------------------------------------------------------------- MIT License (MIT) +Used by: + - compact_str 0.8.2 + +MIT License + +Copyright (c) 2021 Parker Timmerman + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +-------------------------------------------------------------------------------- +MIT License (MIT) + +Used by: + - castaway 0.2.4 + +MIT License + +Copyright (c) 2021 Stephen M. Coakley + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +-------------------------------------------------------------------------------- +MIT License (MIT) + Used by: - jsonptr 0.6.3 @@ -6613,6 +6794,34 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI -------------------------------------------------------------------------------- MIT License (MIT) +Used by: + - crossterm 0.28.1 + +MIT License + +Copyright (c) 2019 Timon + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +-------------------------------------------------------------------------------- +MIT License (MIT) + Used by: - jsonpath-rust 0.7.5 @@ -6660,6 +6869,7 @@ Used by: - linux-raw-sys 0.4.14 - once_cell 1.20.2 - parking 2.2.1 + - paste 1.0.15 - pest 2.7.14 - pest_derive 2.7.14 - pest_generator 2.7.14 @@ -6748,6 +6958,7 @@ MIT License (MIT) Used by: - systemd-journal-logger 2.2.2 + - winnow 0.7.15 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -7278,6 +7489,35 @@ SOFTWARE. -------------------------------------------------------------------------------- MIT License (MIT) +Used by: + - ratatui 0.29.0 + +The MIT License (MIT) + +Copyright (c) 2016-2022 Florian Dehau +Copyright (c) 2023-2024 The Ratatui Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +-------------------------------------------------------------------------------- +MIT License (MIT) + Used by: - same-file 1.0.6 @@ -7553,6 +7793,34 @@ SOFTWARE. -------------------------------------------------------------------------------- MIT License (MIT) +Used by: + - cassowary 0.3.0 + +The MIT License (MIT) + +Copyright (c) 2016 Dylan Ede + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +-------------------------------------------------------------------------------- +MIT License (MIT) + Used by: - quick-xml 0.41.0 diff --git a/crates/installer/Cargo.toml b/crates/installer/Cargo.toml index 447cce7f04..fa4e0c7caa 100644 --- a/crates/installer/Cargo.toml +++ b/crates/installer/Cargo.toml @@ -21,6 +21,7 @@ reqwest = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } serde_yaml = { workspace = true } +sha2 = { workspace = true } tar = { workspace = true } tokio = { workspace = true } toml = { workspace = true } diff --git a/crates/installer/README.md b/crates/installer/README.md index d083f7168c..81f1b27a89 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -63,13 +63,14 @@ The older `cdromLabel` key is accepted as an alias; setting both is an error. Media is mounted read-only with filesystem auto-detection at `/run/trident/installer-media`. Media discovery waits up to 60 seconds for USB/BMC block-device enumeration during autorun before reporting a missing -source; manually supplied recovery URLs do not wait. Other filesystem types -require corresponding kernel mount support on the live OS. Duplicate -labels are rejected. Optional -`installer/installer.toml` on the mounted media can -override mode, image directory, and autorun options, but not bootstrap media -discovery or `serialMode`. Relative HC paths are media-relative; absolute paths and `file://` -refer to the live filesystem. HTTP(S) HC sources are supported. +source. Explicit remote Stream COSI recovery bypasses local media discovery, +configuration overlays, and COSI scanning, including when media is broken. +Other filesystem types require corresponding kernel mount support on the live +OS. Duplicate labels are rejected. Optional `installer/installer.toml` on the +mounted media can override mode, image directory, and autorun options, but not +bootstrap media discovery or `serialMode`. Relative HC paths are media-relative; +absolute paths and `file://` refer to the live filesystem. HTTP(S) HC sources +are supported. With an HC, use Install. Otherwise stream the first regular `.cosi` file in the configured folder, sorted by filename, without recursing or following @@ -107,6 +108,8 @@ Failures to open or write a serial console are logged and mirroring to that console stops; they do not stop installation. On boots with `trident.installer=1`, the RPM's getty drop-ins prevent serial login prompts from mixing with the log stream and prevent tty1 getty from taking input. +Serial mirrors are nonblocking; a console that cannot accept a record is +disabled rather than delaying installation or reboot. Firmware, kernel and systemd output during boot is separate from the installer's stream and may precede it; scrapers should match tagged log lines. @@ -155,6 +158,9 @@ checks. It does not change HC installs or manually supplied recovery URLs. **With force enabled, an ISO left in the boot path can reimage the machine on every boot.** Remove or deprioritize the media after installation. Mounted or read-only potential stream targets are never force-overwritten. +For StreamDisk, the daemon checks the SHA-384 of the metadata inspected by +the installer before selecting the target disk; changed metadata stops the +operation instead of bypassing this safety check. If the installer USB itself is the smallest disk fitting the COSI, streaming stops rather than overwriting it; use a USB smaller than the COSI's original disk size or an HC selecting an explicit target. The current StreamDisk RPC @@ -173,8 +179,9 @@ automatically reboot. On a full-OS installer ISO, install `trident-installer`, add `trident.installer=1` to its **ISO kernel command line**, and provide a COSI under `cosi/` or a Host Configuration. The RPM pulls in Trident and Bash for -the recovery shell, enables its service, starts the gRPC socket through the -service dependency, and suppresses tty1 and serial gettys on marked boots. +the recovery shell, enables its service, starts the gRPC socket and waits for +udev from the running installer only on marked boots, and suppresses tty1 and +serial gettys on those boots. Without that flag the service skips startup and normal gettys are unchanged, including on an installed OS that happens to have the RPM. Include optional tools in the ISO when the image or Host Configuration needs diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index de66a6de47..9a80e924ab 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -8,7 +8,7 @@ use url::Url; use trident_client::{ Client, ClientError, Completion as ClientCompletion, ExecutionState, RebootDisposition, RebootPolicy, RemoteError, RemoteErrorCategory, ServicingEvent, ServicingKind, - ServicingOperation, ServicingState, + ServicingOperation, ServicingState, Sha384Hash, }; use crate::{ @@ -143,6 +143,25 @@ fn recognized_servicing_state(state: ServicingState) -> bool { ) } +/// Describes the requested operation without exposing credentials in source URLs. +fn request_description(settings: &Config, request: &Request) -> String { + match request { + Request::Autorun => format!( + "Autorun: HC '{}' or first media COSI in '{}'", + source::display_source( + settings + .autorun + .host_configuration + .as_deref() + .unwrap_or(DEFAULT_HOST_CONFIGURATION) + ), + settings.media.cosi_directory.display() + ), + Request::HostConfiguration(url) => format!("Install from {}", source::display_source(url)), + Request::Stream(url) => format!("StreamDisk from {}", source::display_url(url)), + } +} + /// Prepares a source, checks stream safety, and forwards Trident's domain events to the UI. /// /// After an uncertain previous operation, `require_idle` prevents a new write until the daemon @@ -155,19 +174,7 @@ pub(super) async fn execute( tx: Sender, ) -> Result<(), Error> { let automatic = request == Request::Autorun; - let description = match &request { - Request::Autorun => format!( - "Autorun: HC '{}' or first media COSI in '{}'", - settings - .autorun - .host_configuration - .as_deref() - .unwrap_or(DEFAULT_HOST_CONFIGURATION), - settings.media.cosi_directory.display() - ), - Request::HostConfiguration(url) => format!("Install from {url}"), - Request::Stream(url) => format!("StreamDisk from {url}"), - }; + let description = request_description(&settings, &request); send( &tx, Event::Prepared { @@ -213,7 +220,7 @@ pub(super) async fn execute( }, ) .await?; - if let Plan::Stream { image } = &plan { + let stream_hash: Option = if let Plan::Stream { image } = &plan { send( &tx, Event::Preparing("Checking image metadata and available disks".into()), @@ -222,13 +229,15 @@ pub(super) async fn execute( let image = image.clone(); let force = stream_force_requested(automatic, settings.autorun.force, force); match task::spawn_blocking(move || source::stream_preflight(&image, force)).await? { - Ok(true) => return send(&tx, Event::AlreadyPresent( + Ok((true, _)) => return send(&tx, Event::AlreadyPresent( "Every COSI filesystem UUID was found on an attached disk.\nNo installation was started.".into() )).await, - Ok(false) => {} + Ok((false, hash)) => Some(hash), Err(error) => return send(&tx, Event::Error { details: format!("{error:#}"), uncertain: false }).await, } - } + } else { + None + }; send(&tx, Event::Preparing("Connecting to Trident daemon".into())).await?; let client = match Client::connect_default().await { Ok(client) => client, @@ -260,7 +269,20 @@ pub(super) async fn execute( } } } - let response = match start(&client, plan).await { + let response = match (plan, stream_hash) { + (Plan::Stream { image }, Some(hash)) => { + client + .stream_disk(&image, Some(&hash), RebootPolicy::Caller) + .await + } + (Plan::Install { config, .. }, None) => client.install(&config, RebootPolicy::Caller).await, + _ => { + return Err(anyhow!( + "Installer plan and preflight hash are inconsistent" + )) + } + }; + let response = match response { Ok(response) => response, Err(error) => { return send( @@ -276,14 +298,6 @@ pub(super) async fn execute( receive(response, &tx).await } -/// Selects the shared client's typed RPC while leaving reboot control to the installer. -async fn start(client: &Client, plan: Plan) -> Result { - match plan { - Plan::Stream { image } => client.stream_disk(&image, None, RebootPolicy::Caller).await, - Plan::Install { config, .. } => client.install(&config, RebootPolicy::Caller).await, - } -} - /// Forwards domain events until a completed result; a broken stream leaves the outcome unknown. async fn receive(mut operation: ServicingOperation, tx: &Sender) -> Result<(), Error> { loop { @@ -333,6 +347,25 @@ mod tests { use trident_client::SourceLocation; + #[test] + /// Autorun and recovery descriptions must not disclose signed source URLs. + fn request_descriptions_redact_secrets() { + let url = "https://user:password@example.com/image.cosi?token=secret#fragment"; + let mut settings = Config::default(); + settings.autorun.host_configuration = Some(url.into()); + for request in [ + Request::Autorun, + Request::HostConfiguration(url.into()), + Request::Stream(Url::parse(url).unwrap()), + ] { + let description = request_description(&settings, &request); + assert!(description.contains("https://example.com/image.cosi")); + for secret in ["user", "password", "token=secret", "fragment"] { + assert!(!description.contains(secret), "{description}"); + } + } + } + #[test] /// Ensures a configured force flag does not affect manually chosen streams. fn only_automatic_streams_use_configured_force() { diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index 3d1a3cfb79..e74cf18bab 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -105,6 +105,8 @@ async fn run(args: Args) -> Result<(), Error> { ); fs::set_permissions(LOG_PATH, Permissions::from_mode(PRIVATE_FILE_MODE))?; if args.system_console { + systemd::start_unit("tridentd.socket")?; + systemd::start_unit("systemd-udev-settle.service")?; let consoles = terminal::active_consoles()?; if let Some(graphical) = consoles.iter().find(|path| path.ends_with("tty1")) { systemd::stop_unit("getty@tty1.service")?; @@ -112,7 +114,7 @@ async fn run(args: Args) -> Result<(), Error> { linux_vt = true; } for path in consoles.iter().filter(|path| !path.ends_with("tty1")) { - match OpenOptions::new().write(true).open(path) { + match terminal::open_nonblocking_console(path) { Ok(terminal) => output.mirrors.push((path.clone(), terminal)), Err(error) => warn!("Could not mirror status to '{}': {error}", path.display()), } diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index b0bee7fcf6..a1cdf63cc5 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -10,6 +10,7 @@ use std::{ use anyhow::{anyhow, bail, ensure, Context, Error}; use reqwest::blocking::Client; use serde::Deserialize; +use sha2::{Digest, Sha384}; use tar::Archive; use url::Url; @@ -19,7 +20,9 @@ use osutils::{ lsblk::{self, BlockDevice, BlockDeviceType}, mount, mountpoint, }; -use trident_api::{config::HostConfiguration, constants::MOUNT_OPTION_READ_ONLY}; +use trident_api::{ + config::HostConfiguration, constants::MOUNT_OPTION_READ_ONLY, primitives::hash::Sha384Hash, +}; use crate::config::{self, Config}; @@ -54,12 +57,34 @@ impl Plan { /// Describes the selected source for the progress window. pub(super) fn description(&self) -> String { match self { - Self::Install { source, .. } => format!("Install from {source}"), - Self::Stream { image } => format!("StreamDisk from {image}"), + Self::Install { source, .. } => format!("Install from {}", display_source(source)), + Self::Stream { image } => format!("StreamDisk from {}", display_url(image)), } } } +/// Removes URL credentials and signed query data from diagnostic source names. +pub(super) fn display_url(source: &Url) -> String { + let mut url = source.clone(); + if (url.password().is_some() && url.set_password(None).is_err()) + || (!url.username().is_empty() && url.set_username("").is_err()) + { + return "".into(); + } + url.set_query(None); + url.set_fragment(None); + url.to_string() +} + +/// Preserves local path descriptions without exposing URL secrets. +pub(super) fn display_source(source: &str) -> String { + match Url::parse(source) { + Ok(url) => display_url(&url), + Err(_) if source.contains("://") => "".into(), + Err(_) => source.to_owned(), + } +} + /// Resolves installation media and returns an install or stream plan with progress callbacks. /// /// Only autorun waits for media enumeration; explicit recovery sources proceed immediately. @@ -68,6 +93,9 @@ pub(super) fn prepare_with_progress( request: &Request, mut progress: impl FnMut(&str) -> Result<(), Error>, ) -> Result<(Config, Plan), Error> { + if matches!(request, Request::Stream(_)) { + return prepare_from_media_with_progress(settings, request, None, &mut progress); + } let root = media_root(settings, request == &Request::Autorun, &mut progress)?; prepare_from_media_with_progress(settings, request, root.as_deref(), &mut progress) } @@ -89,6 +117,15 @@ fn prepare_from_media_with_progress( root: Option<&Path>, progress: &mut impl FnMut(&str) -> Result<(), Error>, ) -> Result<(Config, Plan), Error> { + if let Request::Stream(image) = request { + settings.require_autorun()?; + return Ok(( + settings.clone(), + Plan::Stream { + image: image.clone(), + }, + )); + } progress("Reading installer configuration")?; let settings = match root { Some(root) => settings.overlay(root)?, @@ -101,20 +138,11 @@ fn prepare_from_media_with_progress( None => Vec::new(), }; progress("Checking Host Configuration")?; - let source = match request { - Request::HostConfiguration(source) => { - remote_url(source)?; - Some(source.clone()) - } - Request::Stream(source) => { - return Ok(( - settings, - Plan::Stream { - image: source.clone(), - }, - )); - } - Request::Autorun => match &settings.autorun.host_configuration { + let source = if let Request::HostConfiguration(source) = request { + remote_url(source)?; + Some(source.clone()) + } else { + match &settings.autorun.host_configuration { Some(source) => Some(source.clone()), None => match fs::metadata(config::DEFAULT_HOST_CONFIGURATION) { Ok(_) => Some(config::DEFAULT_HOST_CONFIGURATION.into()), @@ -123,7 +151,7 @@ fn prepare_from_media_with_progress( return Err(error).context("Failed to inspect default Host Configuration") } }, - }, + } }; let plan = match source { Some(source) => { @@ -286,14 +314,22 @@ fn reader(url: &Url) -> Result, Error> { format!("Failed to open '{}'", path.display()) })?)) } - "http" | "https" => Ok(Box::new( - Client::builder() + "http" | "https" => { + let response = Client::builder() .timeout(SOURCE_TIMEOUT) .build()? .get(url.clone()) - .send()? - .error_for_status()?, - )), + .send() + .and_then(reqwest::blocking::Response::error_for_status) + .map_err(|error| { + anyhow!( + "Failed to fetch '{}': {}", + display_url(url), + error.without_url() + ) + })?; + Ok(Box::new(response)) + } _ => bail!("Unsupported source scheme '{}'", url.scheme()), } } @@ -351,8 +387,8 @@ struct ImageMetadata { fs_uuid: Option, } -/// Extracts and checks COSI disk metadata within a bounded archive read. -fn metadata(reader: impl Read) -> Result { +/// Extracts and hashes the raw COSI metadata within a bounded archive read. +fn metadata(reader: impl Read) -> Result<(Metadata, Sha384Hash), Error> { let mut archive = Archive::new(reader.take(MAX_METADATA_BYTES)); for entry in archive.entries().context("Invalid COSI archive")? { let mut entry = entry?; @@ -365,8 +401,10 @@ fn metadata(reader: impl Read) -> Result { entry.size() <= MAX_METADATA_BYTES, "COSI metadata is too large" ); + let mut raw = Vec::new(); + entry.read_to_end(&mut raw)?; let metadata: Metadata = - serde_json::from_reader(&mut entry).context("Invalid COSI metadata")?; + serde_json::from_slice(&raw).context("Invalid COSI metadata")?; ensure!( metadata.disk.as_ref().is_some_and(|disk| disk.size > 0), "StreamDisk requires COSI disk metadata" @@ -383,17 +421,20 @@ fn metadata(reader: impl Read) -> Result { major == "1" && minor.parse::()? >= 2, "StreamDisk requires COSI v1.2 or newer" ); - return Ok(metadata); + return Ok(( + metadata, + Sha384Hash::from(format!("{:x}", Sha384::digest(&raw))), + )); } } bail!("COSI metadata.json was not found within the metadata read limit") } -/// Checks disk eligibility and reports whether a complete matching image is already present. +/// Checks disk eligibility and returns the metadata hash to bind the daemon's fetch. /// /// `force` bypasses only the repeat-install guard, not the installer-media safety check. -pub(super) fn stream_preflight(image: &Url, force: bool) -> Result { - let metadata = metadata(reader(image)?)?; +pub(super) fn stream_preflight(image: &Url, force: bool) -> Result<(bool, Sha384Hash), Error> { + let (metadata, hash) = metadata(reader(image)?)?; let devices = lsblk::list().context("Failed to inspect attached disks")?; let required = metadata .disk @@ -402,7 +443,7 @@ pub(super) fn stream_preflight(image: &Url, force: bool) -> Result .size; ensure_stream_target_safe(&devices, required)?; if force { - return Ok(false); + return Ok((false, hash)); } let expected = metadata .images @@ -415,7 +456,7 @@ pub(super) fn stream_preflight(image: &Url, force: bool) -> Result !expected.is_empty(), "COSI has no filesystem UUIDs for repeat-install protection" ); - Ok(contains_image(&devices, &expected)) + Ok((contains_image(&devices, &expected), hash)) } /// Refuses streaming if Trident could choose a mounted or read-only smallest fitting disk. @@ -486,6 +527,60 @@ mod tests { } } + #[test] + /// Keeps signed source credentials out of operator-visible descriptions. + fn source_descriptions_redact_url_secrets() { + let url = + Url::parse("https://user:password@example.com/image.cosi?token=secret#key").unwrap(); + assert_eq!(display_url(&url), "https://example.com/image.cosi"); + assert_eq!( + display_source(url.as_str()), + "https://example.com/image.cosi" + ); + assert_eq!( + Plan::Stream { image: url.clone() }.description(), + "StreamDisk from https://example.com/image.cosi" + ); + assert_eq!( + Plan::Install { + config: Box::default(), + source: url.to_string() + } + .description(), + "Install from https://example.com/image.cosi" + ); + assert_eq!( + display_source("/etc/trident/config.yaml"), + "/etc/trident/config.yaml" + ); + assert_eq!( + display_url(&Url::parse("file:///media/image.cosi").unwrap()), + "file:///media/image.cosi" + ); + assert_eq!( + display_source("https://bad URL?token=secret"), + "" + ); + } + + #[test] + /// Explicit stream recovery ignores broken media mounts, overlays and COSI directories. + fn explicit_stream_ignores_local_media() { + let root = TempDir::new().unwrap(); + fs::create_dir(root.path().join("installer")).unwrap(); + fs::write(root.path().join(config::MEDIA_CONFIG), "invalid TOML").unwrap(); + let mut settings = Config::default(); + settings.media.mount_path = Some("/nonexistent-installer-media".into()); + let url = Url::parse("https://example.com/image.cosi?token=secret").unwrap(); + let (_, plan) = + prepare_from_media(&settings, &Request::Stream(url.clone()), Some(root.path())) + .unwrap(); + assert!(matches!(plan, Plan::Stream { image } if image == url)); + let (_, plan) = + prepare_with_progress(&settings, &Request::Stream(url.clone()), |_| Ok(())).unwrap(); + assert!(matches!(plan, Plan::Stream { image } if image == url)); + } + #[test] /// Ensures the image marker needs a COSI and leaves other image URLs untouched. fn marker_requires_an_image_and_does_not_touch_other_urls() { @@ -584,6 +679,31 @@ mod tests { metadata(Cursor::new(builder.into_inner().unwrap())).unwrap_err(); } + #[test] + /// Hashes the original metadata bytes rather than re-serialized JSON. + fn preflight_metadata_hash_matches_daemon_contract() { + let raw = br#"{"version":"1.2","disk":{"size":4294967296},"images":[{"fsUuid":"uuid-1"}]}"#; + let inspect = |raw: &[u8]| { + let mut builder = TarBuilder::new(Vec::new()); + let mut header = tar::Header::new_gnu(); + header.set_size(raw.len() as u64); + header.set_cksum(); + builder + .append_data(&mut header, METADATA_PATH, raw) + .unwrap(); + builder.finish().unwrap(); + metadata(Cursor::new(builder.into_inner().unwrap())).unwrap() + }; + let (parsed, hash) = inspect(raw); + assert_eq!(parsed.disk.unwrap().size, 4294967296); + assert_eq!(hash, Sha384Hash::from(format!("{:x}", Sha384::digest(raw)))); + + let padded = + br#"{ "version":"1.2","disk":{"size":4294967296},"images":[{"fsUuid":"uuid-1"}]}"#; + let (_, padded_hash) = inspect(padded); + assert_ne!(hash, padded_hash); + } + #[test] /// Ensures repeat-install detection requires every expected filesystem UUID. fn repeat_guard_requires_the_complete_nonempty_uuid_set() { diff --git a/crates/osutils/src/systemd.rs b/crates/osutils/src/systemd.rs index 7be4b7f8e9..2d68b996cd 100644 --- a/crates/osutils/src/systemd.rs +++ b/crates/osutils/src/systemd.rs @@ -52,6 +52,16 @@ pub fn stop_unit(unit: impl AsRef) -> Result<(), Error> { .with_context(|| format!("Failed to stop unit '{unit}'")) } +/// Starts the named systemd unit and waits for its start job to finish. +pub fn start_unit(unit: impl AsRef) -> Result<(), Error> { + let unit = unit.as_ref(); + Dependency::Systemctl + .cmd() + .args(["start", unit]) + .run_and_check() + .with_context(|| format!("Failed to start unit '{unit}'")) +} + /// Requests a reboot through systemd and reports command failures. pub fn reboot() -> Result<(), Error> { Dependency::Systemctl.cmd().arg("reboot").run_and_check()?; diff --git a/crates/osutils/src/terminal.rs b/crates/osutils/src/terminal.rs index 2658b8c25c..dd7860c135 100644 --- a/crates/osutils/src/terminal.rs +++ b/crates/osutils/src/terminal.rs @@ -1,7 +1,10 @@ use std::{ fs::{self, File, OpenOptions}, io::{self, IsTerminal, Write}, - os::{fd::AsRawFd, unix::fs::FileTypeExt}, + os::{ + fd::AsRawFd, + unix::fs::{FileTypeExt, OpenOptionsExt}, + }, path::{Path, PathBuf}, }; @@ -80,6 +83,16 @@ pub fn preferred_console(consoles: &[PathBuf]) -> Result<&Path, Error> { .context("No console available") } +/// Opens a serial console without allowing flow control to block a caller's event loop. +pub fn open_nonblocking_console(path: impl AsRef) -> Result { + let path = path.as_ref(); + OpenOptions::new() + .write(true) + .custom_flags(libc::O_NONBLOCK | libc::O_NOCTTY) + .open(path) + .with_context(|| format!("Failed to open console '{}'", path.display())) +} + /// Claims a character terminal as the controlling TTY and redirects stdin/stdout to it. /// /// The caller must stop competing getty units before attaching. @@ -154,6 +167,17 @@ mod tests { console_paths("").unwrap_err(); } + #[test] + /// Serial mirrors must not block the installer when a console stops draining. + fn serial_console_is_nonblocking() { + let terminal = nix::pty::openpty(None, None).unwrap(); + let path = unistd::ttyname(&terminal.slave).unwrap(); + let mirror = open_nonblocking_console(path).unwrap(); + let flags = unsafe { libc::fcntl(mirror.as_raw_fd(), libc::F_GETFL) }; + assert!(flags >= 0); + assert_ne!(flags & libc::O_NONBLOCK, 0); + } + #[test] /// Ensures VT palette slots are encoded correctly and out-of-range slots fail. fn linux_vt_palette_sequences_set_rgb_slots_and_reset() { diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index 8da0af311a..fea034e703 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -112,6 +112,8 @@ Configuration image URL of `installer://image` fails if no COSI is available. Authored disk paths and image checksums remain unchanged. Even with an explicit Host Configuration source, autorun checks for media first (up to the 60-second label-discovery wait) so it can resolve `installer://image`. +Explicit remote Stream COSI recovery bypasses local media discovery and +overlays, so it remains available when the local media cannot be read. Without a Host Configuration, the first regular COSI in `cosiDirectory` is streamed. If neither source is available, the graphical console offers @@ -166,9 +168,10 @@ iso: - trident.installer=1 ``` -The RPM includes the installer TOML and requires Trident and Bash. It -starts its service and gRPC socket on a marked installer boot and prevents -gettys from reclaiming tty1 or mixing login prompts with serial logs. +The RPM includes the installer TOML and requires Trident and Bash. It starts +its service on a marked installer boot; the installer starts the gRPC socket +and waits for udev before servicing. It prevents gettys from reclaiming tty1 +or mixing login prompts with serial logs. No separate service enablement, getty override, or label override is necessary for a single `CDROM` ISO or `TRD_INSTALL` USB. On an unmarked normal OS, the service does not run and gettys are unaffected. A Host diff --git a/packaging/systemd/trident-installer.service b/packaging/systemd/trident-installer.service index 59a1f6e3b8..747e678ee1 100644 --- a/packaging/systemd/trident-installer.service +++ b/packaging/systemd/trident-installer.service @@ -1,8 +1,7 @@ [Unit] Description=Trident Linux Installer ConditionKernelCommandLine=trident.installer=1 -Wants=tridentd.socket systemd-udev-settle.service -After=tridentd.socket systemd-udev-settle.service network.target +After=network.target Conflicts=trident-install.service [Service] From 8350b8506dffe9fa0a2c634734f7c86f086e2130 Mon Sep 17 00:00:00 2001 From: Paco Date: Tue, 10 Nov 2026 18:20:20 -0800 Subject: [PATCH 41/62] chore: Drop obsolete label lookup after rebase Keep the merged client Makefile target unchanged and use the installer lsblk label lookup without retaining an unused blkid wrapper. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Makefile | 2 +- crates/osutils/src/blkid.rs | 29 ----------------------------- 2 files changed, 1 insertion(+), 30 deletions(-) diff --git a/Makefile b/Makefile index b3654b829d..a0f643de89 100644 --- a/Makefile +++ b/Makefile @@ -142,7 +142,7 @@ format: .PHONY: test test: .cargo/config cargo test --all --no-fail-fast - cargo test -p trident-client --all-features --no-fail-fast + cargo test --locked -p trident-client --all-features --no-fail-fast COVERAGE_EXCLUDED_FILES_REGEX='crates/docbuilder|crates/pytest|crates/setsail|target|/mnt/vss' diff --git a/crates/osutils/src/blkid.rs b/crates/osutils/src/blkid.rs index 07153efc1b..7132ef8a23 100644 --- a/crates/osutils/src/blkid.rs +++ b/crates/osutils/src/blkid.rs @@ -48,35 +48,6 @@ pub fn get_filesystem_label(device_path: impl AsRef) -> Result) -> Result, Error> { - let label = label.as_ref(); - ensure!(!label.is_empty(), "Filesystem label must not be empty"); - let output = Dependency::Blkid - .cmd() - .args([ - "-c", - "/dev/null", - "-t", - &format!("LABEL={label}"), - "-o", - "device", - ]) - .output()?; - // blkid uses status 2 when its search has no matches. - if output.code() == Some(2) { - return Ok(Vec::new()); - } - output.check()?; - let mut devices = output - .output() - .lines() - .map(PathBuf::from) - .collect::>(); - devices.sort(); - devices.dedup(); - Ok(devices) -} - #[cfg(feature = "functional-test")] #[cfg_attr(not(test), allow(unused_imports, dead_code))] mod functional_test { From 5a8f87ca3191aaecf0b774ed9a6ead1149f5e717 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 11 Nov 2026 14:50:20 -0800 Subject: [PATCH 42/62] fix: Address installer recovery and progress review Reapply media configuration on forced retries, prevent power actions during active servicing, scroll the TUI by actual wrapped rows, and retry transient remote source failures while networking starts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/Cargo.toml | 2 +- crates/installer/README.md | 12 +- crates/installer/src/client.rs | 19 +- crates/installer/src/demo.rs | 10 +- crates/installer/src/source.rs | 128 +++++++++-- crates/installer/src/ui.rs | 246 ++++++++++++++++++---- docs/Reference/Installer-Configuration.md | 4 +- 7 files changed, 350 insertions(+), 71 deletions(-) diff --git a/crates/installer/Cargo.toml b/crates/installer/Cargo.toml index fa4e0c7caa..1ba2dc7b95 100644 --- a/crates/installer/Cargo.toml +++ b/crates/installer/Cargo.toml @@ -16,7 +16,7 @@ clap = { workspace = true } crossterm = { workspace = true } env_logger = { workspace = true } log = { workspace = true } -ratatui = { workspace = true } +ratatui = { workspace = true, features = ["unstable-rendered-line-info"] } reqwest = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/crates/installer/README.md b/crates/installer/README.md index 81f1b27a89..77ee2d6d07 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -70,7 +70,8 @@ OS. Duplicate labels are rejected. Optional `installer/installer.toml` on the mounted media can override mode, image directory, and autorun options, but not bootstrap media discovery or `serialMode`. Relative HC paths are media-relative; absolute paths and `file://` refer to the live filesystem. HTTP(S) HC sources -are supported. +are supported. Remote connection and timeout failures are retried for at most +60 seconds while networking comes up; HTTP errors fail immediately. With an HC, use Install. Otherwise stream the first regular `.cosi` file in the configured folder, sorted by filename, without recursing or following @@ -124,7 +125,8 @@ originating screen. The recovery shell is a root login Bash, so it reads `/etc/profile` gives root a red prompt. The ISO service sets `TERM=linux` for tty1 so terminal tools such as `clear` work in the shell. Installation continues while a shell is open; automatic reboot waits. The success screen -offers Reboot and Shell. +offers Reboot and Shell. While servicing is active, Ctrl-C remains in the +installation menu and power actions are blocked. Before each recovery attempt the baked configuration is reloaded, so shell edits take effect. Invalid or missing configuration never supplies implicit defaults for an installation. @@ -144,7 +146,8 @@ terminals use the RGB values directly. `V` opens a live verbosity picker (Off, Error, Warn, Info, Debug, Trace) beside the operation; this only filters the display, not serial or stored diagnostics. The private diagnostic file retains -all levels even when serialVerbosity filters them. PgUp/PgDn scroll. +all levels even when serialVerbosity filters them. PgUp/PgDn scroll by wrapped +terminal rows, keeping the latest result visible. TRACE output can expose low-level inputs; restrict access to BMC serial captures. A missing final Completed response means unknown outcome, not success. Another write requires a successful Trident status query @@ -152,6 +155,9 @@ establishing that the daemon is idle. Streaming checks COSI filesystem UUIDs before writing. A complete match opens the already-present screen with a separately confirmed Force reinstall action. +For media autorun, a confirmed retry keeps the original COSI and re-applies +the media-local configuration; a manually entered stream retains its URL and +does not apply media overrides. `autorun.force` defaults to false. Setting it to true skips the matching-UUID check on automatic COSI streams, without weakening the mounted/read-only target checks. It does not change HC installs or manually supplied recovery URLs. diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index 9a80e924ab..62f7b64b9c 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -39,7 +39,10 @@ pub(super) enum Event { details: String, uncertain: bool, }, - AlreadyPresent(String), + AlreadyPresent { + details: String, + automatic: bool, + }, } #[derive(Debug, PartialEq, Eq)] @@ -158,7 +161,9 @@ fn request_description(settings: &Config, request: &Request) -> String { settings.media.cosi_directory.display() ), Request::HostConfiguration(url) => format!("Install from {}", source::display_source(url)), - Request::Stream(url) => format!("StreamDisk from {}", source::display_url(url)), + Request::Stream(url) | Request::StreamMedia(url) => { + format!("StreamDisk from {}", source::display_url(url)) + } } } @@ -173,7 +178,7 @@ pub(super) async fn execute( require_idle: bool, tx: Sender, ) -> Result<(), Error> { - let automatic = request == Request::Autorun; + let automatic = matches!(request, Request::Autorun | Request::StreamMedia(_)); let description = request_description(&settings, &request); send( &tx, @@ -229,9 +234,10 @@ pub(super) async fn execute( let image = image.clone(); let force = stream_force_requested(automatic, settings.autorun.force, force); match task::spawn_blocking(move || source::stream_preflight(&image, force)).await? { - Ok((true, _)) => return send(&tx, Event::AlreadyPresent( - "Every COSI filesystem UUID was found on an attached disk.\nNo installation was started.".into() - )).await, + Ok((true, _)) => return send(&tx, Event::AlreadyPresent { + details: "Every COSI filesystem UUID was found on an attached disk.\nNo installation was started.".into(), + automatic, + }).await, Ok((false, hash)) => Some(hash), Err(error) => return send(&tx, Event::Error { details: format!("{error:#}"), uncertain: false }).await, } @@ -357,6 +363,7 @@ mod tests { Request::Autorun, Request::HostConfiguration(url.into()), Request::Stream(Url::parse(url).unwrap()), + Request::StreamMedia(Url::parse(url).unwrap()), ] { let description = request_description(&settings, &request); assert!(description.contains("https://example.com/image.cosi")); diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs index fdbc319ec0..84ddcf09d7 100644 --- a/crates/installer/src/demo.rs +++ b/crates/installer/src/demo.rs @@ -3,6 +3,7 @@ use std::time::Duration; use anyhow::Error; use clap::ValueEnum; use tokio::{sync::mpsc::Sender, time}; +use url::Url; use trident_client::{ Completion, LogLevel, LogRecord, RebootDisposition, RemoteError, RemoteErrorCategory, @@ -33,7 +34,7 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err description: "DEMO: StreamDisk from ISO image".into(), reboot: false, serial_verbosity: SerialVerbosity::Debug, - stream_image: None, + stream_image: Some(Url::parse("file:///demo/image.cosi")?), }, ) .await?; @@ -42,9 +43,10 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err details: "DEMO: No Host Configuration and no COSI images in cosi/.\nTry the simulated shell or a remote-source recovery action.\nNo network requests or machine operations will be performed.".into(), uncertain: false, }).await, - Scenario::AlreadyPresent => return client::send(&tx, Event::AlreadyPresent( - "DEMO: Matching COSI filesystem UUIDs found. No writes performed.".into() - )).await, + Scenario::AlreadyPresent => return client::send(&tx, Event::AlreadyPresent { + details: "DEMO: Matching COSI filesystem UUIDs found. No writes performed.".into(), + automatic: true, + }).await, _ => {} } response(&tx, ServicingEvent::Started { timestamp: None }).await?; diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index a1cdf63cc5..d656a10731 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -8,6 +8,7 @@ use std::{ }; use anyhow::{anyhow, bail, ensure, Context, Error}; +use log::warn; use reqwest::blocking::Client; use serde::Deserialize; use sha2::{Digest, Sha384}; @@ -28,6 +29,8 @@ use crate::config::{self, Config}; pub(super) const IMAGE_MARKER: &str = "installer://image"; const SOURCE_TIMEOUT: Duration = Duration::from_secs(30); +const REMOTE_RETRY_TIMEOUT: Duration = Duration::from_secs(60); +const REMOTE_RETRY_INTERVAL: Duration = Duration::from_secs(2); const MEDIA_DISCOVERY_TIMEOUT: Duration = Duration::from_secs(60); const MEDIA_POLL_INTERVAL: Duration = Duration::from_secs(2); const MAX_HOST_CONFIGURATION_BYTES: u64 = 2 * 1024 * 1024; @@ -40,6 +43,7 @@ pub(super) enum Request { Autorun, HostConfiguration(String), Stream(Url), + StreamMedia(Url), } #[derive(Debug, Clone)] @@ -132,6 +136,18 @@ fn prepare_from_media_with_progress( None => settings.clone(), }; settings.require_autorun()?; + if let Request::StreamMedia(image) = request { + ensure!( + root.is_some(), + "Cannot retry a media stream without installer media" + ); + return Ok(( + settings, + Plan::Stream { + image: image.clone(), + }, + )); + } progress("Scanning media for COSI images")?; let images = match root { Some(root) => config::discover_images(root, &settings.media.cosi_directory)?, @@ -315,19 +331,47 @@ fn reader(url: &Url) -> Result, Error> { })?)) } "http" | "https" => { - let response = Client::builder() - .timeout(SOURCE_TIMEOUT) - .build()? - .get(url.clone()) - .send() - .and_then(reqwest::blocking::Response::error_for_status) - .map_err(|error| { - anyhow!( - "Failed to fetch '{}': {}", - display_url(url), - error.without_url() - ) - })?; + let client = Client::builder().build()?; + let deadline = Instant::now() + REMOTE_RETRY_TIMEOUT; + let response = loop { + let remaining = deadline.saturating_duration_since(Instant::now()); + let response = client + .get(url.clone()) + .timeout(SOURCE_TIMEOUT.min(remaining)) + .send(); + match response { + Ok(response) => { + break response.error_for_status().map_err(|error| { + anyhow!( + "Failed to fetch '{}': {}", + display_url(url), + error.without_url() + ) + })? + } + Err(error) + if (error.is_connect() || error.is_timeout()) + && Instant::now() < deadline => + { + warn!( + "Remote source '{}' unavailable ({}); retrying while network starts", + display_url(url), + error.without_url() + ); + thread::sleep( + REMOTE_RETRY_INTERVAL + .min(deadline.saturating_duration_since(Instant::now())), + ); + } + Err(error) => { + return Err(anyhow!( + "Failed to fetch '{}': {}", + display_url(url), + error.without_url() + )); + } + } + }; Ok(Box::new(response)) } _ => bail!("Unsupported source scheme '{}'", url.scheme()), @@ -507,7 +551,10 @@ fn contains_image(devices: &[BlockDevice], expected: &HashSet) -> bool { mod tests { use super::*; - use std::io::Cursor; + use std::{ + io::{Cursor, Write}, + net::TcpListener, + }; use indoc::indoc; use tar::Builder as TarBuilder; @@ -527,6 +574,37 @@ mod tests { } } + #[test] + /// Remote sources recover when the network becomes reachable after startup. + fn remote_source_retries_until_listener_is_ready() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + drop(listener); + let url = Url::parse(&format!("http://{address}/host.yaml")).unwrap(); + let initial = Client::builder() + .timeout(Duration::from_secs(2)) + .build() + .unwrap() + .get(url.clone()) + .send() + .unwrap_err(); + assert!(initial.is_connect(), "{initial}"); + let server = thread::spawn(move || { + thread::sleep(Duration::from_millis(200)); + let listener = TcpListener::bind(address).unwrap(); + let (mut socket, _) = listener.accept().unwrap(); + let mut request = [0; 512]; + assert_ne!(socket.read(&mut request).unwrap(), 0); + socket + .write_all( + b"HTTP/1.1 200 OK\r\nContent-Length: 16\r\nConnection: close\r\n\r\nmode = 'autorun'", + ) + .unwrap(); + }); + assert_eq!(read_text(&url).unwrap(), "mode = 'autorun'"); + server.join().unwrap(); + } + #[test] /// Keeps signed source credentials out of operator-visible descriptions. fn source_descriptions_redact_url_secrets() { @@ -581,6 +659,28 @@ mod tests { assert!(matches!(plan, Plan::Stream { image } if image == url)); } + #[test] + /// Force retry keeps the original COSI while reloading its media settings. + fn force_media_stream_reapplies_overlay_without_switching_to_install() { + let root = TempDir::new().unwrap(); + fs::create_dir(root.path().join("installer")).unwrap(); + fs::write( + root.path().join(config::MEDIA_CONFIG), + "[autorun]\nreboot = false\nhostConfiguration = 'https://example.test/host.yaml'\n", + ) + .unwrap(); + let image = Url::parse("file:///run/trident/installer-media/cosi/image.cosi").unwrap(); + let (settings, plan) = prepare_from_media( + &Config::default(), + &Request::StreamMedia(image.clone()), + Some(root.path()), + ) + .unwrap(); + assert!(!settings.autorun.reboot); + assert!(matches!(plan, Plan::Stream { image: selected } if selected == image)); + prepare_from_media(&Config::default(), &Request::StreamMedia(image), None).unwrap_err(); + } + #[test] /// Ensures the image marker needs a COSI and leaves other image URLs untouched. fn marker_requires_an_image_and_does_not_touch_other_urls() { diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index c7bf74eca3..5da23b3658 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -249,6 +249,7 @@ struct Model { reboot: bool, reboot_at: Option, stream_image: Option, + stream_from_autorun: bool, started: Instant, failed: bool, error_details_open: bool, @@ -275,6 +276,7 @@ impl Model { reboot: true, reboot_at: None, stream_image: None, + stream_from_autorun: false, started: Instant::now(), failed: false, error_details_open: false, @@ -361,10 +363,12 @@ impl Model { self.reboot = reboot; self.serial_verbosity = serial_verbosity.filter(); self.stream_image = stream_image; + self.stream_from_autorun = false; self.log(LogSource::Inst, LogLevel::Info, description) } - Event::AlreadyPresent(details) => { + Event::AlreadyPresent { details, automatic } => { self.details = details.clone(); + self.stream_from_autorun = automatic; self.reboot_at = None; self.screen = Screen::AlreadyPresent; self.selected = 0; @@ -454,7 +458,7 @@ impl Model { /// Updates navigation for a key press and returns an action only when one is requested. /// URL entry and the verbosity picker consume keys before screen-level shortcuts. - fn key(&mut self, key: KeyEvent, demo: bool) -> Option { + fn key(&mut self, key: KeyEvent, demo: bool, worker_active: bool) -> Option { if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { return None; } @@ -523,7 +527,7 @@ impl Model { return None; } if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') { - self.screen = if self.screen == Screen::Progress { + self.screen = if worker_active || self.screen == Screen::Progress { Screen::Menu } else { Screen::Recovery @@ -591,7 +595,7 @@ impl Model { KeyCode::Up | KeyCode::BackTab => self.selected = (self.selected + count - 1) % count, KeyCode::Down | KeyCode::Tab => self.selected = (self.selected + 1) % count, KeyCode::Enter => { - return match (self.screen, self.selected) { + let action = match (self.screen, self.selected) { (Screen::Recovery, 0) | (Screen::Success, 1) | (Screen::AlreadyPresent, 2) @@ -619,13 +623,16 @@ impl Model { self.selected = 0; None } - (Screen::Force, 1) => Some(Action::Start( - self.stream_image - .clone() - .map(Request::Stream) - .unwrap_or(Request::Autorun), - true, - )), + (Screen::Force, 1) => self.stream_image.clone().map(|image| { + Action::Start( + if self.stream_from_autorun { + Request::StreamMedia(image) + } else { + Request::Stream(image) + }, + true, + ) + }), (Screen::Menu, 0) => { self.screen = Screen::Progress; None @@ -636,7 +643,13 @@ impl Model { None } _ => None, - } + }; + return if worker_active && matches!(action, Some(Action::Reboot | Action::Poweroff)) + { + Some(Action::BlockedPower) + } else { + action + }; } _ => {} } @@ -648,6 +661,7 @@ impl Model { enum Action { Start(Request, bool), Shell, + BlockedPower, Reboot, Poweroff, Quit, @@ -888,7 +902,7 @@ pub(super) async fn run( }, }; output.record(&event)?; - let already_present = matches!(event, Event::AlreadyPresent(_)); + let already_present = matches!(event, Event::AlreadyPresent { .. }); let entry = model.event(event); output.announce(&entry, model.serial_verbosity); if display.is_none() @@ -1022,7 +1036,9 @@ pub(super) async fn run( } else if key.code == KeyCode::Enter { printed_screen = None; } - } else if let Some(action) = model.key(key, scenario.is_some()) { + } else if let Some(action) = + model.key(key, scenario.is_some(), worker.is_some()) + { match action { Action::Quit => return Ok(()), Action::Shell if scenario.is_some() => { @@ -1061,6 +1077,19 @@ pub(super) async fn run( )); } } + Action::BlockedPower => { + model.screen = Screen::Menu; + model.selected = 0; + model.activity = + "Servicing is still active; power actions are unavailable" + .into(); + let warning = model.log( + LogSource::Inst, + LogLevel::Warn, + model.activity.clone(), + ); + output.announce(&warning, model.serial_verbosity); + } Action::Reboot | Action::Poweroff if scenario.is_some() => { model.reboot_at = None; model.details = "DEMO: Machine power action suppressed".into(); @@ -1303,16 +1332,7 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { logs.push(result); } } - let visible = usize::from(area.height.saturating_sub(2 + header_lines)); - let end = logs - .len() - .saturating_sub(usize::from(model.scroll).min(logs.len().saturating_sub(1))); - let start = end.saturating_sub(visible); - lines.extend( - logs[start..end] - .iter() - .map(|entry| entry.styled(model.linux_vt)), - ); + lines.extend(logs.into_iter().map(|entry| entry.styled(model.linux_vt))); let block = Block::default() .borders(Borders::ALL) .border_set(ASCII_BORDER) @@ -1322,9 +1342,15 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { title, Style::default().fg(theme_color(color, model.linux_vt)), )); - Paragraph::new(lines) + let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false }); + let visible_rows = usize::from(area.height.saturating_sub(2)); + let total_rows = paragraph.line_count(area.width.saturating_sub(2).max(1)); + let offset = total_rows + .saturating_sub(visible_rows) + .saturating_sub(usize::from(model.scroll)); + paragraph .block(block) - .wrap(Wrap { trim: false }) + .scroll((u16::try_from(offset).unwrap_or(u16::MAX), 0)) } /// Draws the current screen, actions, log controls, and any pending reboot countdown. @@ -1643,17 +1669,33 @@ mod tests { ); assert_eq!(model.verbosity, LevelFilter::Debug); assert!(!log_visible(LogLevel::Trace, model.verbosity)); - model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), true); + model.key( + KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), + true, + false, + ); assert!(model.verbosity_open); - model.key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), true); - model.key(enter(), true); + model.key( + KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), + true, + false, + ); + model.key(enter(), true, false); assert_eq!(model.verbosity, LevelFilter::Trace); assert!(!model.verbosity_open); assert!(log_visible(LogLevel::Trace, model.verbosity)); assert_eq!(model.logs.len(), 2); - model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), true); - model.key(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE), true); - model.key(enter(), true); + model.key( + KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), + true, + false, + ); + model.key( + KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE), + true, + false, + ); + model.key(enter(), true, false); assert_eq!(model.verbosity, LevelFilter::Off); assert!(!log_visible(LogLevel::Error, model.verbosity)); assert_eq!(model.logs.len(), 2); @@ -1963,9 +2005,21 @@ mod tests { rows[result_row][rendered[result_row].find("SUCCESS:").unwrap()].fg, Color::Rgb(93, 200, 148) ); - assert!(rendered + model.key( + KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), + true, + false, + ); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + let earlier = terminal + .backend() + .buffer() + .content .iter() - .any(|row| row.contains("Earlier disk action"))); + .map(|cell| cell.symbol()) + .collect::(); + assert!(earlier.contains("Earlier disk action")); + model.scroll = 0; model.verbosity = LevelFilter::Off; terminal.draw(|frame| render(frame, &model, true)).unwrap(); let text = terminal @@ -1979,6 +2033,60 @@ mod tests { assert!(!text.contains("Earlier disk action")); } + /// Wrapped diagnostics and activity cannot hide the latest result at 80x24. + #[test] + fn wrapped_log_rows_keep_the_result_visible_and_scrollable() { + let mut model = Model::new(); + model.log( + LogSource::Trident, + LogLevel::Info, + "Long preceding diagnostic ".repeat(40), + ); + model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::NotRequired, + servicing_kind: Some(ServicingKind::CleanInstall), + image_hash: None, + }, + )))); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + let visible_text = |terminal: &Terminal| { + terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::() + }; + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + assert!( + visible_text(&terminal).contains("SUCCESS: Installation"), + "{}", + visible_text(&terminal) + ); + model.key( + KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), + true, + false, + ); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + assert!(!visible_text(&terminal).contains("SUCCESS: Installation")); + model.key( + KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE), + true, + false, + ); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + assert!(visible_text(&terminal).contains("SUCCESS: Installation")); + + model.screen = Screen::Progress; + model.activity = "Scanning media ".repeat(60); + model.log(LogSource::Inst, LogLevel::Info, "Latest activity".into()); + terminal.draw(|frame| render(frame, &model, true)).unwrap(); + assert!(visible_text(&terminal).contains("Latest activity")); + } + /// Failures show prior logs first while still allowing full diagnostic details. #[test] fn failure_shows_logs_by_default_and_keeps_full_details_accessible() { @@ -2020,7 +2128,11 @@ mod tests { Color::Rgb(255, 107, 115) ); } - model.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE), true); + model.key( + KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE), + true, + false, + ); assert!(model.error_details_open); terminal.draw(|frame| render(frame, &model, true)).unwrap(); let text = terminal @@ -2031,7 +2143,7 @@ mod tests { .map(|cell| cell.symbol()) .collect::(); assert!(text.contains("Underlying transport failure")); - model.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), true); + model.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), true, false); assert_eq!(model.screen, Screen::Error); assert!(!model.error_details_open); } @@ -2088,17 +2200,17 @@ mod tests { fn error_continue_and_shell_keep_the_originating_screen() { let mut model = Model::new(); model.fail("Complete diagnostic information".into(), false); - assert!(model.key(enter(), false).is_none()); + assert!(model.key(enter(), false, false).is_none()); assert_eq!(model.screen, Screen::Recovery); - assert_eq!(model.key(enter(), false), Some(Action::Shell)); + assert_eq!(model.key(enter(), false, false), Some(Action::Shell)); assert_eq!(model.screen, Screen::Recovery); assert_eq!(model.details, "Complete diagnostic information"); model.screen = Screen::Success; model.selected = 1; - assert_eq!(model.key(enter(), false), Some(Action::Shell)); + assert_eq!(model.key(enter(), false, false), Some(Action::Shell)); assert_eq!(model.screen, Screen::Success); model.selected = 0; - assert_eq!(model.key(enter(), false), Some(Action::Reboot)); + assert_eq!(model.key(enter(), false, false), Some(Action::Reboot)); } /// Force reinstall opens with Cancel selected and needs a second confirmation. @@ -2107,13 +2219,63 @@ mod tests { let mut model = Model::new(); model.screen = Screen::AlreadyPresent; model.selected = 1; - assert!(model.key(enter(), false).is_none()); + assert!(model.key(enter(), false, false).is_none()); assert_eq!(model.screen, Screen::Force); assert_eq!(model.selected, 0); - assert!(model.key(enter(), false).is_none()); + assert!(model.key(enter(), false, false).is_none()); assert_eq!(model.screen, Screen::AlreadyPresent); } + /// Retrying an automatic stream re-applies media overrides; manual streams keep their URL. + #[test] + fn force_retry_preserves_the_original_stream_source() { + let image = Url::parse("file:///run/trident/installer-media/cosi/image.cosi").unwrap(); + let mut model = Model::new(); + model.stream_image = Some(image.clone()); + model.event(Event::AlreadyPresent { + details: "Image already present".into(), + automatic: true, + }); + model.screen = Screen::Force; + model.selected = 1; + assert_eq!( + model.key(enter(), false, false), + Some(Action::Start(Request::StreamMedia(image), true)) + ); + + let image = Url::parse("https://example.test/image.cosi").unwrap(); + model.stream_image = Some(image.clone()); + model.event(Event::AlreadyPresent { + details: "Remote image already present".into(), + automatic: false, + }); + model.screen = Screen::Force; + model.selected = 1; + assert_eq!( + model.key(enter(), false, false), + Some(Action::Start(Request::Stream(image), true)) + ); + } + + /// Repeated interrupts and power choices cannot shut down an active installation. + #[test] + fn active_worker_cannot_reach_a_power_action() { + let mut model = Model::new(); + let interrupt = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL); + model.key(interrupt, false, true); + assert_eq!(model.screen, Screen::Menu); + model.key(interrupt, false, true); + assert_eq!(model.screen, Screen::Menu); + + model.screen = Screen::Recovery; + model.selected = 3; + assert_eq!(model.key(enter(), false, true), Some(Action::BlockedPower)); + assert_eq!(model.key(enter(), false, false), Some(Action::Poweroff)); + model.screen = Screen::Success; + model.selected = 0; + assert_eq!(model.key(enter(), false, true), Some(Action::BlockedPower)); + } + /// Standard-size result and recovery screens expose their expected actions. #[test] fn screens_render_at_80_by_24_with_visible_actions() { diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index fea034e703..08f6c8b798 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -86,7 +86,7 @@ necessarily the value written in the packaged file. | `media.mountPath` | Unset | Absolute path to an **already mounted** filesystem instead of label discovery. Cannot be combined with `filesystemLabel`; this path is not mounted or made read-only by the installer. | | `media.cosiDirectory` | `"cosi"` | Nonempty directory relative to the media root. Absolute paths and `..` are rejected. Only regular `.cosi` files directly inside it are considered, in filename order. | | `autorun.hostConfiguration` | Unset | Host Configuration source. When omitted, `/etc/trident/config.yaml` is used **if it exists**; otherwise autorun looks for a COSI. | -| `autorun.reboot` | `true` | Reboot after successful servicing **only if** Trident reports that work was performed and a reboot is required. | +| `autorun.reboot` | `true` | Reboot after successful servicing **only if** Trident reports that work was performed and a reboot is required. Confirmed Force retries of media streams re-apply the media-local value. | | `autorun.force` | `false` | On automatic COSI streaming, bypass the matching-filesystem-UUID repeat-install guard. Does not affect Host Configuration installs, manual recovery sources, or the mounted/read-only disk safety check. | Media discovery waits up to 60 seconds during autorun for late USB/BMC device @@ -107,6 +107,8 @@ source does not wait for media. | File URL | `"file:///etc/trident/config.yaml"` | A file in the live installer OS. | | HTTP(S) URL | `"https://example.com/host.yaml"` | Fetched over the network. | +Remote connection and timeout errors are retried for up to 60 seconds while +networking starts; HTTP response errors fail immediately. Host Configuration input must be valid YAML and is limited to 2 MiB. A Host Configuration image URL of `installer://image` fails if no COSI is available. Authored disk paths and image checksums remain unchanged. Even with an From 12785441a478503e3261d09a4583bdba9f1f090f Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 11 Nov 2026 15:01:17 -0800 Subject: [PATCH 43/62] fix: Block power actions after uncertain servicing Keep shutdown unavailable after a disconnected gRPC stream until a completed result or a successful daemon servicing-state read confirms that no write is active. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 3 +- crates/installer/src/client.rs | 5 ++- crates/installer/src/ui.rs | 71 ++++++++++++++++++++++++++++------ 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 77ee2d6d07..cd4c30a719 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -151,7 +151,8 @@ terminal rows, keeping the latest result visible. TRACE output can expose low-level inputs; restrict access to BMC serial captures. A missing final Completed response means unknown outcome, not success. Another write requires a successful Trident status query -establishing that the daemon is idle. +establishing that the daemon is idle; power actions remain unavailable until +that check or a confirmed completion resolves the uncertain outcome. Streaming checks COSI filesystem UUIDs before writing. A complete match opens the already-present screen with a separately confirmed Force reinstall action. diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index 62f7b64b9c..c626cacfd0 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -35,6 +35,7 @@ pub(super) enum Event { stream_image: Option, }, Response(ServicingEvent), + IdleConfirmed, Error { details: String, uncertain: bool, @@ -260,7 +261,9 @@ pub(super) async fn execute( }; if require_idle { match client.servicing_state().await { - Ok(state) if recognized_servicing_state(state) => {} + Ok(state) if recognized_servicing_state(state) => { + send(&tx, Event::IdleConfirmed).await?; + } Ok(state) => { return send(&tx, Event::Error { details: format!("The previous operation's outcome is unknown. Trident returned an unsupported servicing state {state:?}; refusing another write."), diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 5da23b3658..9258c752cf 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -366,6 +366,14 @@ impl Model { self.stream_from_autorun = false; self.log(LogSource::Inst, LogLevel::Info, description) } + Event::IdleConfirmed => { + self.uncertain = false; + self.log( + LogSource::Inst, + LogLevel::Info, + "Trident confirmed that no servicing is active".into(), + ) + } Event::AlreadyPresent { details, automatic } => { self.details = details.clone(); self.stream_from_autorun = automatic; @@ -433,12 +441,17 @@ impl Model { /// Lists actions in keyboard-selection order for the current screen. fn choices(&self) -> Vec<&'static str> { match self.screen { - Screen::Recovery => vec![ - "Shell", - "Stream COSI from URL", - "Install from Host Configuration URL", - "Shutdown", - ], + Screen::Recovery => { + let mut actions = vec![ + "Shell", + "Stream COSI from URL", + "Install from Host Configuration URL", + ]; + if !self.uncertain { + actions.push("Shutdown"); + } + actions + } Screen::Success => vec!["Reboot", "Shell"], Screen::AlreadyPresent => vec!["Reboot", "Force reinstall", "Shell"], Screen::Force => vec!["Cancel", "Force reinstall and erase"], @@ -644,7 +657,8 @@ impl Model { } _ => None, }; - return if worker_active && matches!(action, Some(Action::Reboot | Action::Poweroff)) + return if (worker_active || self.uncertain) + && matches!(action, Some(Action::Reboot | Action::Poweroff)) { Some(Action::BlockedPower) } else { @@ -1078,11 +1092,13 @@ pub(super) async fn run( } } Action::BlockedPower => { - model.screen = Screen::Menu; + model.screen = if worker.is_some() { + Screen::Menu + } else { + Screen::Recovery + }; model.selected = 0; - model.activity = - "Servicing is still active; power actions are unavailable" - .into(); + model.activity = "Servicing may still be active; power actions are unavailable until Trident confirms it is idle".into(); let warning = model.log( LogSource::Inst, LogLevel::Warn, @@ -2276,6 +2292,39 @@ mod tests { assert_eq!(model.key(enter(), false, true), Some(Action::BlockedPower)); } + /// Losing the client stream must not make shutdown safe until Trident confirms idleness. + #[test] + fn uncertain_outcome_blocks_power_after_worker_exits() { + let mut model = Model::new(); + model.event(Event::Error { + details: "gRPC stream disconnected; servicing may continue".into(), + uncertain: true, + }); + model.screen = Screen::Recovery; + model.selected = 3; + assert!(!model.choices().contains(&"Shutdown")); + assert_eq!(model.key(enter(), false, false), Some(Action::BlockedPower)); + + model.event(Event::IdleConfirmed); + assert!(!model.uncertain); + assert!(model.choices().contains(&"Shutdown")); + assert_eq!(model.key(enter(), false, false), Some(Action::Poweroff)); + + model.event(Event::Error { + details: "Another stream disconnected".into(), + uncertain: true, + }); + model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::NotRequired, + servicing_kind: Some(ServicingKind::CleanInstall), + image_hash: None, + }, + )))); + assert!(!model.uncertain); + assert_eq!(model.key(enter(), false, false), Some(Action::Reboot)); + } + /// Standard-size result and recovery screens expose their expected actions. #[test] fn screens_render_at_80_by_24_with_visible_actions() { From b6039285d6dbfa0e5228dff87939a7728b0f90b0 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 11 Nov 2026 15:39:55 -0800 Subject: [PATCH 44/62] fix: Never stop autorun on matching filesystem UUIDs Remove the premature Already Present/Force flow while interactive mode remains unimplemented. Preserve media-target and COSI metadata-hash safety, and continue parsing legacy force settings as no-ops for compatibility. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 27 ++-- crates/installer/src/client.rs | 54 ++----- crates/installer/src/config.rs | 7 +- crates/installer/src/demo.rs | 14 +- crates/installer/src/source.rs | 132 +++++------------ crates/installer/src/ui.rs | 169 ++++------------------ docs/Reference/Installer-Configuration.md | 14 +- packaging/etc/installer.toml | 1 - 8 files changed, 106 insertions(+), 312 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index cd4c30a719..85ec448889 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -10,7 +10,6 @@ crate, not a partitioning engine. cargo run -p installer -- --demo cargo run -p installer -- --demo failure cargo run -p installer -- --demo no-images -cargo run -p installer -- --demo already-present cargo run -p installer -- --demo disconnect ``` @@ -43,7 +42,6 @@ cosiDirectory = "cosi/" # If omitted, use /etc/trident/config.yaml when that file exists. # hostConfiguration = "installer/host-configuration.yaml" reboot = true -force = false ``` Unknown fields and conflicting media selectors are errors. `interactive` is @@ -103,8 +101,8 @@ On the ISO, `--system-console` uses the graphical virtual console for local keyboard/monitor and BMC KVM, independent of `console=` order. With `serialMode = "logs"`, active serial consoles receive only formatted installer and gRPC logs, never a TUI, even when no graphical console exists. Autorun -continues without an interactive console; missing inputs, repeat-install -protection and other cases needing operator input emit an error and stop. +continues without an interactive console; missing inputs and failures +emit an error and stop. Failures to open or write a serial console are logged and mirroring to that console stops; they do not stop installation. On boots with `trident.installer=1`, the RPM's getty drop-ins prevent serial login prompts @@ -154,17 +152,12 @@ outcome, not success. Another write requires a successful Trident status query establishing that the daemon is idle; power actions remain unavailable until that check or a confirmed completion resolves the uncertain outcome. -Streaming checks COSI filesystem UUIDs before writing. A complete match opens -the already-present screen with a separately confirmed Force reinstall action. -For media autorun, a confirmed retry keeps the original COSI and re-applies -the media-local configuration; a manually entered stream retains its URL and -does not apply media overrides. -`autorun.force` defaults to false. Setting it to true skips the matching-UUID -check on automatic COSI streams, without weakening the mounted/read-only target -checks. It does not change HC installs or manually supplied recovery URLs. -**With force enabled, an ISO left in the boot path can reimage the machine on -every boot.** Remove or deprioritize the media after installation. -Mounted or read-only potential stream targets are never force-overwritten. +Autorun does not compare filesystem UUIDs or confirm repeat installations. +**An ISO left in the boot path can reimage the machine on every boot.** Remove +or deprioritize the media after installation. The former `autorun.force` key +is accepted for compatibility but ignored; interactive mode, where an operator +could confirm a repeat install, is not implemented yet. +Mounted or read-only potential stream targets are still refused. For StreamDisk, the daemon checks the SHA-384 of the metadata inspected by the installer before selecting the target disk; changed metadata stops the operation instead of bypassing this safety check. @@ -172,13 +165,13 @@ If the installer USB itself is the smallest disk fitting the COSI, streaming stops rather than overwriting it; use a USB smaller than the COSI's original disk size or an HC selecting an explicit target. The current StreamDisk RPC cannot exclude a specific disk. -**HC installation intentionally has no repeat-install/Force guard:** the HC is +**HC installation also has no repeat-install guard:** the HC is the operator's authority to overwrite its selected disks. Remove ISO media or correct boot order to avoid repeated HC-driven installations. `autorun.reboot` defaults to true. Successful installations requiring reboot show a short countdown, then reboot; false leaves the success screen. Errors, -unknown outcomes, already-present images, and no-servicing results never +unknown outcomes, and no-servicing results never automatically reboot. ## ISO integration diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index c626cacfd0..a7180f355f 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -3,7 +3,6 @@ use std::borrow::Cow; use anyhow::{anyhow, bail, Error}; use log::Level; use tokio::{sync::mpsc::Sender, task}; -use url::Url; use trident_client::{ Client, ClientError, Completion as ClientCompletion, ExecutionState, RebootDisposition, @@ -16,11 +15,6 @@ use crate::{ source::{self, Plan, Request}, }; -/// Applies configured force only to autorun streams, while respecting explicit confirmation. -fn stream_force_requested(automatic: bool, configured: bool, confirmed: bool) -> bool { - confirmed || (automatic && configured) -} - #[derive(Debug)] pub(super) enum Event { InstallerLog { @@ -32,7 +26,6 @@ pub(super) enum Event { description: String, reboot: bool, serial_verbosity: SerialVerbosity, - stream_image: Option, }, Response(ServicingEvent), IdleConfirmed, @@ -40,10 +33,6 @@ pub(super) enum Event { details: String, uncertain: bool, }, - AlreadyPresent { - details: String, - automatic: bool, - }, } #[derive(Debug, PartialEq, Eq)] @@ -162,9 +151,7 @@ fn request_description(settings: &Config, request: &Request) -> String { settings.media.cosi_directory.display() ), Request::HostConfiguration(url) => format!("Install from {}", source::display_source(url)), - Request::Stream(url) | Request::StreamMedia(url) => { - format!("StreamDisk from {}", source::display_url(url)) - } + Request::Stream(url) => format!("StreamDisk from {}", source::display_url(url)), } } @@ -175,11 +162,9 @@ fn request_description(settings: &Config, request: &Request) -> String { pub(super) async fn execute( settings: Config, request: Request, - force: bool, require_idle: bool, tx: Sender, ) -> Result<(), Error> { - let automatic = matches!(request, Request::Autorun | Request::StreamMedia(_)); let description = request_description(&settings, &request); send( &tx, @@ -187,7 +172,6 @@ pub(super) async fn execute( description, reboot: settings.autorun.reboot, serial_verbosity: settings.serial_verbosity, - stream_image: None, }, ) .await?; @@ -219,10 +203,6 @@ pub(super) async fn execute( description: plan.description(), reboot: settings.autorun.reboot, serial_verbosity: settings.serial_verbosity, - stream_image: match &plan { - Plan::Stream { image } => Some(image.clone()), - Plan::Install { .. } => None, - }, }, ) .await?; @@ -233,14 +213,18 @@ pub(super) async fn execute( ) .await?; let image = image.clone(); - let force = stream_force_requested(automatic, settings.autorun.force, force); - match task::spawn_blocking(move || source::stream_preflight(&image, force)).await? { - Ok((true, _)) => return send(&tx, Event::AlreadyPresent { - details: "Every COSI filesystem UUID was found on an attached disk.\nNo installation was started.".into(), - automatic, - }).await, - Ok((false, hash)) => Some(hash), - Err(error) => return send(&tx, Event::Error { details: format!("{error:#}"), uncertain: false }).await, + match task::spawn_blocking(move || source::stream_preflight(&image)).await? { + Ok(hash) => Some(hash), + Err(error) => { + return send( + &tx, + Event::Error { + details: format!("{error:#}"), + uncertain: false, + }, + ) + .await + } } } else { None @@ -354,6 +338,8 @@ pub(super) async fn send(tx: &Sender, event: Event) -> Result<(), Error> mod tests { use super::*; + use url::Url; + use trident_client::SourceLocation; #[test] @@ -366,7 +352,6 @@ mod tests { Request::Autorun, Request::HostConfiguration(url.into()), Request::Stream(Url::parse(url).unwrap()), - Request::StreamMedia(Url::parse(url).unwrap()), ] { let description = request_description(&settings, &request); assert!(description.contains("https://example.com/image.cosi")); @@ -376,15 +361,6 @@ mod tests { } } - #[test] - /// Ensures a configured force flag does not affect manually chosen streams. - fn only_automatic_streams_use_configured_force() { - assert!(!stream_force_requested(true, false, false)); - assert!(stream_force_requested(true, true, false)); - assert!(!stream_force_requested(false, true, false)); - assert!(stream_force_requested(false, false, true)); - } - /// Builds a valid domain completion for installer-specific result checks. fn successful() -> ClientCompletion { ClientCompletion { diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index cbf5a71cf3..fefe7cc5ab 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -106,12 +106,13 @@ pub(super) struct Autorun { pub host_configuration: Option, #[serde(default = "default_reboot")] pub reboot: bool, + /// Accepted for compatibility with older installer media; autorun does not check UUIDs. #[serde(default)] pub force: bool, } impl Default for Autorun { - /// Enables reboot after a successful install but never forces a repeat stream. + /// Enables reboot after a successful install. fn default() -> Self { Self { host_configuration: None, @@ -315,8 +316,8 @@ mod tests { } #[test] - /// Ensures force is false by default and accepts only a boolean override. - fn autorun_force_is_opt_in() { + /// Older media can still specify the now-ignored force flag. + fn legacy_force_setting_remains_compatible() { let config = Config::parse("mode = 'autorun'\n[autorun]\nforce = true").unwrap(); assert!(config.autorun.force); let config = Config::parse("mode = 'autorun'\n[autorun]\nforce = false").unwrap(); diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs index 84ddcf09d7..a4715b9aac 100644 --- a/crates/installer/src/demo.rs +++ b/crates/installer/src/demo.rs @@ -3,7 +3,6 @@ use std::time::Duration; use anyhow::Error; use clap::ValueEnum; use tokio::{sync::mpsc::Sender, time}; -use url::Url; use trident_client::{ Completion, LogLevel, LogRecord, RebootDisposition, RemoteError, RemoteErrorCategory, @@ -22,7 +21,6 @@ pub(super) enum Scenario { Success, Failure, NoImages, - AlreadyPresent, Disconnect, } @@ -34,20 +32,14 @@ pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Err description: "DEMO: StreamDisk from ISO image".into(), reboot: false, serial_verbosity: SerialVerbosity::Debug, - stream_image: Some(Url::parse("file:///demo/image.cosi")?), }, ) .await?; - match scenario { - Scenario::NoImages => return client::send(&tx, Event::Error { + if scenario == Scenario::NoImages { + return client::send(&tx, Event::Error { details: "DEMO: No Host Configuration and no COSI images in cosi/.\nTry the simulated shell or a remote-source recovery action.\nNo network requests or machine operations will be performed.".into(), uncertain: false, - }).await, - Scenario::AlreadyPresent => return client::send(&tx, Event::AlreadyPresent { - details: "DEMO: Matching COSI filesystem UUIDs found. No writes performed.".into(), - automatic: true, - }).await, - _ => {} + }).await; } response(&tx, ServicingEvent::Started { timestamp: None }).await?; for (level, message) in [ diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index d656a10731..588b6a7027 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -1,5 +1,4 @@ use std::{ - collections::HashSet, fs::{self, File}, io::{ErrorKind, Read}, path::{Path, PathBuf}, @@ -10,7 +9,7 @@ use std::{ use anyhow::{anyhow, bail, ensure, Context, Error}; use log::warn; use reqwest::blocking::Client; -use serde::Deserialize; +use serde::{de::IgnoredAny, Deserialize}; use sha2::{Digest, Sha384}; use tar::Archive; use url::Url; @@ -43,7 +42,6 @@ pub(super) enum Request { Autorun, HostConfiguration(String), Stream(Url), - StreamMedia(Url), } #[derive(Debug, Clone)] @@ -136,18 +134,6 @@ fn prepare_from_media_with_progress( None => settings.clone(), }; settings.require_autorun()?; - if let Request::StreamMedia(image) = request { - ensure!( - root.is_some(), - "Cannot retry a media stream without installer media" - ); - return Ok(( - settings, - Plan::Stream { - image: image.clone(), - }, - )); - } progress("Scanning media for COSI images")?; let images = match root { Some(root) => config::discover_images(root, &settings.media.cosi_directory)?, @@ -411,13 +397,13 @@ pub(super) fn resolve_host_configuration( Ok(config) } -// Only the repeat-install preflight needs these fields; Trident validates the full COSI. +// Only disk sizing and the metadata hash are needed here; Trident validates the full COSI. #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] struct Metadata { version: String, disk: Option, - images: Vec, + images: Vec, } #[derive(Debug, Deserialize)] @@ -425,12 +411,6 @@ struct DiskMetadata { size: u64, } -#[derive(Debug, Deserialize)] -#[serde(rename_all = "camelCase")] -struct ImageMetadata { - fs_uuid: Option, -} - /// Extracts and hashes the raw COSI metadata within a bounded archive read. fn metadata(reader: impl Read) -> Result<(Metadata, Sha384Hash), Error> { let mut archive = Archive::new(reader.take(MAX_METADATA_BYTES)); @@ -475,32 +455,25 @@ fn metadata(reader: impl Read) -> Result<(Metadata, Sha384Hash), Error> { } /// Checks disk eligibility and returns the metadata hash to bind the daemon's fetch. -/// -/// `force` bypasses only the repeat-install guard, not the installer-media safety check. -pub(super) fn stream_preflight(image: &Url, force: bool) -> Result<(bool, Sha384Hash), Error> { +pub(super) fn stream_preflight(image: &Url) -> Result { let (metadata, hash) = metadata(reader(image)?)?; let devices = lsblk::list().context("Failed to inspect attached disks")?; + checked_stream_hash(&metadata, hash, &devices) +} + +/// Keeps target-media safety independent of previously installed filesystem UUIDs. +fn checked_stream_hash( + metadata: &Metadata, + hash: Sha384Hash, + devices: &[BlockDevice], +) -> Result { let required = metadata .disk .as_ref() .context("COSI disk metadata is missing")? .size; - ensure_stream_target_safe(&devices, required)?; - if force { - return Ok((false, hash)); - } - let expected = metadata - .images - .iter() - .filter_map(|image| image.fs_uuid.as_deref()) - .filter(|uuid| !uuid.is_empty()) - .map(str::to_ascii_lowercase) - .collect::>(); - ensure!( - !expected.is_empty(), - "COSI has no filesystem UUIDs for repeat-install protection" - ); - Ok((contains_image(&devices, &expected), hash)) + ensure_stream_target_safe(devices, required)?; + Ok(hash) } /// Refuses streaming if Trident could choose a mounted or read-only smallest fitting disk. @@ -531,22 +504,6 @@ fn ensure_stream_target_safe(devices: &[BlockDevice], required: u64) -> Result<( Ok(()) } -/// Checks whether every filesystem UUID in the COSI is present on attached devices. -fn contains_image(devices: &[BlockDevice], expected: &HashSet) -> bool { - /// Collects filesystem UUIDs recursively, normalizing case for matching. - fn collect(devices: &[BlockDevice], present: &mut HashSet) { - for device in devices { - if let Some(uuid) = &device.fsuuid { - present.insert(uuid.to_string().to_ascii_lowercase()); - } - collect(&device.children, present); - } - } - let mut present = HashSet::new(); - collect(devices, &mut present); - expected.iter().all(|uuid| present.contains(uuid)) -} - #[cfg(test)] mod tests { use super::*; @@ -659,28 +616,6 @@ mod tests { assert!(matches!(plan, Plan::Stream { image } if image == url)); } - #[test] - /// Force retry keeps the original COSI while reloading its media settings. - fn force_media_stream_reapplies_overlay_without_switching_to_install() { - let root = TempDir::new().unwrap(); - fs::create_dir(root.path().join("installer")).unwrap(); - fs::write( - root.path().join(config::MEDIA_CONFIG), - "[autorun]\nreboot = false\nhostConfiguration = 'https://example.test/host.yaml'\n", - ) - .unwrap(); - let image = Url::parse("file:///run/trident/installer-media/cosi/image.cosi").unwrap(); - let (settings, plan) = prepare_from_media( - &Config::default(), - &Request::StreamMedia(image.clone()), - Some(root.path()), - ) - .unwrap(); - assert!(!settings.autorun.reboot); - assert!(matches!(plan, Plan::Stream { image: selected } if selected == image)); - prepare_from_media(&Config::default(), &Request::StreamMedia(image), None).unwrap_err(); - } - #[test] /// Ensures the image marker needs a COSI and leaves other image URLs untouched. fn marker_requires_an_image_and_does_not_touch_other_urls() { @@ -782,7 +717,7 @@ mod tests { #[test] /// Hashes the original metadata bytes rather than re-serialized JSON. fn preflight_metadata_hash_matches_daemon_contract() { - let raw = br#"{"version":"1.2","disk":{"size":4294967296},"images":[{"fsUuid":"uuid-1"}]}"#; + let raw = br#"{"version":"1.2","disk":{"size":4294967296},"images":[{}]}"#; let inspect = |raw: &[u8]| { let mut builder = TarBuilder::new(Vec::new()); let mut header = tar::Header::new_gnu(); @@ -798,27 +733,38 @@ mod tests { assert_eq!(parsed.disk.unwrap().size, 4294967296); assert_eq!(hash, Sha384Hash::from(format!("{:x}", Sha384::digest(raw)))); - let padded = - br#"{ "version":"1.2","disk":{"size":4294967296},"images":[{"fsUuid":"uuid-1"}]}"#; + let padded = br#"{ "version":"1.2","disk":{"size":4294967296},"images":[{}]}"#; let (_, padded_hash) = inspect(padded); assert_ne!(hash, padded_hash); } #[test] - /// Ensures repeat-install detection requires every expected filesystem UUID. - fn repeat_guard_requires_the_complete_nonempty_uuid_set() { - let devices = vec![BlockDevice { + /// Autorun still streams to a safe disk with matching UUIDs already present. + fn existing_filesystem_uuids_do_not_block_stream_preflight() { + let raw = br#"{"version":"1.2","disk":{"size":4294967296},"images":[{"fsUuid":"existing-root"}]}"#; + let mut builder = TarBuilder::new(Vec::new()); + let mut header = tar::Header::new_gnu(); + header.set_size(raw.len() as u64); + header.set_cksum(); + builder + .append_data(&mut header, METADATA_PATH, &raw[..]) + .unwrap(); + builder.finish().unwrap(); + let (metadata, hash) = metadata(Cursor::new(builder.into_inner().unwrap())).unwrap(); + let disk = BlockDevice { + name: "vda".into(), + size: 8 * 1024 * 1024 * 1024, + blkdev_type: BlockDeviceType::Disk, children: vec![BlockDevice { - fsuuid: Some("uuid-1".into()), + fsuuid: Some("existing-root".into()), ..Default::default() }], ..Default::default() - }]; - assert!(contains_image(&devices, &HashSet::from(["uuid-1".into()]))); - assert!(!contains_image( - &devices, - &HashSet::from(["uuid-1".into(), "uuid-2".into()]) - )); + }; + assert_eq!( + checked_stream_hash(&metadata, hash.clone(), &[disk]).unwrap(), + hash + ); } #[test] diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 9258c752cf..7433b4b6f7 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -32,7 +32,6 @@ use tokio::{ task::{self, JoinHandle}, time, }; -use url::Url; use osutils::{systemd, terminal as os_terminal}; use trident_client::{LogLevel, ServicingEvent}; @@ -142,8 +141,6 @@ enum Screen { Error, Recovery, Success, - AlreadyPresent, - Force, StreamUrl, HostConfigUrl, Menu, @@ -248,8 +245,6 @@ struct Model { uncertain: bool, reboot: bool, reboot_at: Option, - stream_image: Option, - stream_from_autorun: bool, started: Instant, failed: bool, error_details_open: bool, @@ -275,8 +270,6 @@ impl Model { uncertain: false, reboot: true, reboot_at: None, - stream_image: None, - stream_from_autorun: false, started: Instant::now(), failed: false, error_details_open: false, @@ -357,13 +350,10 @@ impl Model { description, reboot, serial_verbosity, - stream_image, } => { self.source = description.clone(); self.reboot = reboot; self.serial_verbosity = serial_verbosity.filter(); - self.stream_image = stream_image; - self.stream_from_autorun = false; self.log(LogSource::Inst, LogLevel::Info, description) } Event::IdleConfirmed => { @@ -374,18 +364,6 @@ impl Model { "Trident confirmed that no servicing is active".into(), ) } - Event::AlreadyPresent { details, automatic } => { - self.details = details.clone(); - self.stream_from_autorun = automatic; - self.reboot_at = None; - self.screen = Screen::AlreadyPresent; - self.selected = 0; - self.result_log( - LogLevel::Warn, - format!("ALREADY PRESENT: {details}"), - COLOR_WARN, - ) - } Event::Error { details, uncertain } => self.fail(details, uncertain), Event::Response(response) => match response { ServicingEvent::Started { .. } => { @@ -453,8 +431,6 @@ impl Model { actions } Screen::Success => vec!["Reboot", "Shell"], - Screen::AlreadyPresent => vec!["Reboot", "Force reinstall", "Shell"], - Screen::Force => vec!["Cancel", "Force reinstall and erase"], Screen::Menu => vec!["Return to installation", "Shell"], Screen::Error => vec!["Continue to recovery", "Shell"], _ => Vec::new(), @@ -465,7 +441,7 @@ impl Model { fn showing_logs(&self) -> bool { matches!( self.screen, - Screen::Progress | Screen::Menu | Screen::Success | Screen::AlreadyPresent + Screen::Progress | Screen::Menu | Screen::Success ) || self.screen == Screen::Error && !self.error_details_open } @@ -518,14 +494,11 @@ impl Model { None } KeyCode::Enter => match source::remote_url(self.url.trim()) { - Ok(url) => Some(Action::Start( - if self.screen == Screen::StreamUrl { - Request::Stream(url) - } else { - Request::HostConfiguration(url.to_string()) - }, - false, - )), + Ok(url) => Some(Action::Start(if self.screen == Screen::StreamUrl { + Request::Stream(url) + } else { + Request::HostConfiguration(url.to_string()) + })), Err(error) => { self.fail(format!("{error:#}"), false); None @@ -592,7 +565,6 @@ impl Model { self.screen = match self.screen { Screen::Progress => Screen::Menu, Screen::Menu => Screen::Progress, - Screen::Force => Screen::AlreadyPresent, Screen::Error => Screen::Recovery, other => other, }; @@ -611,7 +583,6 @@ impl Model { let action = match (self.screen, self.selected) { (Screen::Recovery, 0) | (Screen::Success, 1) - | (Screen::AlreadyPresent, 2) | (Screen::Menu, 1) | (Screen::Error, 1) => Some(Action::Shell), (Screen::Recovery, 1) => { @@ -625,27 +596,7 @@ impl Model { None } (Screen::Recovery, 3) => Some(Action::Poweroff), - (Screen::Success, 0) | (Screen::AlreadyPresent, 0) => Some(Action::Reboot), - (Screen::AlreadyPresent, 1) => { - self.screen = Screen::Force; - self.selected = 0; - None - } - (Screen::Force, 0) => { - self.screen = Screen::AlreadyPresent; - self.selected = 0; - None - } - (Screen::Force, 1) => self.stream_image.clone().map(|image| { - Action::Start( - if self.stream_from_autorun { - Request::StreamMedia(image) - } else { - Request::Stream(image) - }, - true, - ) - }), + (Screen::Success, 0) => Some(Action::Reboot), (Screen::Menu, 0) => { self.screen = Screen::Progress; None @@ -673,7 +624,7 @@ impl Model { #[derive(Debug, PartialEq, Eq)] enum Action { - Start(Request, bool), + Start(Request), Shell, BlockedPower, Reboot, @@ -841,19 +792,18 @@ impl Drop for Display { fn spawn_operation( config_path: Option, request: Request, - force: bool, uncertain: bool, scenario: Option, tx: mpsc::Sender, ) -> JoinHandle> { task::spawn(async move { match scenario { - Some(scenario) => demo::run(if force { Scenario::Success } else { scenario }, tx).await, + Some(scenario) => demo::run(scenario, tx).await, None => { let path = config_path .context("Live installation requires an explicit configuration path")?; match Config::read(path) { - Ok(settings) => client::execute(settings, request, force, uncertain, tx).await, + Ok(settings) => client::execute(settings, request, uncertain, tx).await, Err(error) => { client::send( &tx, @@ -893,7 +843,6 @@ pub(super) async fn run( config_path.clone(), Request::Autorun, false, - false, scenario, tx.clone(), )); @@ -916,7 +865,6 @@ pub(super) async fn run( }, }; output.record(&event)?; - let already_present = matches!(event, Event::AlreadyPresent { .. }); let entry = model.event(event); output.announce(&entry, model.serial_verbosity); if display.is_none() @@ -926,14 +874,6 @@ pub(super) async fn run( { println!("{}", entry.plain()); } - if !interactive && already_present { - model.failed = true; - let explanation = - "Already installed; remove the media or confirm a force reinstall on a graphical console"; - model.details.push_str(&format!("\n{explanation}")); - let error = model.log(LogSource::Inst, LogLevel::Error, explanation.into()); - output.announce(&error, model.serial_verbosity); - } } if worker.as_ref().is_some_and(JoinHandle::is_finished) { if let Some(finished) = worker.take() { @@ -1069,7 +1009,7 @@ pub(super) async fn run( shell = Some(task::spawn_blocking(move || os_terminal::shell(&tty))); } - Action::Start(request, force) => { + Action::Start(request) => { if worker.is_some() { let failure = model.fail("An installer operation is still active. Wait for it to finish.".into(), true); output.announce(&failure, model.serial_verbosity); @@ -1084,7 +1024,6 @@ pub(super) async fn run( worker = Some(spawn_operation( config_path.clone(), request, - force, model.uncertain, recovery_demo, tx.clone(), @@ -1219,8 +1158,6 @@ fn title(screen: Screen) -> &'static str { Screen::Error => "INSTALLATION ERROR", Screen::Recovery => "RECOVERY", Screen::Success => "INSTALLATION COMPLETE", - Screen::AlreadyPresent => "IMAGE ALREADY PRESENT", - Screen::Force => "CONFIRM FORCE REINSTALL", Screen::StreamUrl => "STREAM COSI FROM URL", Screen::HostConfigUrl => "INSTALL FROM HOST CONFIGURATION URL", Screen::Menu => "INSTALLER MENU", @@ -1318,7 +1255,6 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { let (title, color) = match model.screen { Screen::Success => ("SUCCESS - Installation complete", COLOR_SUCCESS), Screen::Error => ("FAILURE - Installation stopped", COLOR_ERROR), - Screen::AlreadyPresent => ("ALREADY PRESENT - No write", COLOR_WARN), _ => ("Activity / logs", COLOR_FRAME), }; let mut lines = Vec::new(); @@ -1339,10 +1275,7 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { .iter() .filter(|entry| entry.result_color.is_some() || log_visible(entry.level, model.verbosity)) .collect::>(); - if matches!( - model.screen, - Screen::Success | Screen::Error | Screen::AlreadyPresent - ) { + if matches!(model.screen, Screen::Success | Screen::Error) { if let Some(index) = logs.iter().rposition(|entry| entry.result_color.is_some()) { let result = logs.remove(index); logs.push(result); @@ -1387,7 +1320,6 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { Constraint::Length(header_height(frame.area())), Constraint::Length(match model.screen { Screen::Recovery => 0, - Screen::AlreadyPresent => 1, _ => 2, }), Constraint::Min(3), @@ -1403,7 +1335,6 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { match model.screen { Screen::Error => COLOR_ERROR, Screen::Success => COLOR_SUCCESS, - Screen::AlreadyPresent => COLOR_WARN, _ => COLOR_BRAND, }, model.linux_vt, @@ -1429,16 +1360,22 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { } let content = match model.screen { _ if model.showing_logs() => log_panel(model, areas[2]), - Screen::StreamUrl | Screen::HostConfigUrl => Paragraph::new(format!("Enter an http:// or https:// URL:\n\n{}\n\nEnter: start Esc: back", display_text(&model.url))) - .block(block("Remote source", model.linux_vt)).wrap(Wrap { trim: false }), - Screen::Force => Paragraph::new("Force reinstall will erase the disk selected by Trident.\n\nExisting partitions and files will be lost.\nCancel is selected by default.") - .block(block("Destructive action", model.linux_vt)).wrap(Wrap { trim: false }), + Screen::StreamUrl | Screen::HostConfigUrl => Paragraph::new(format!( + "Enter an http:// or https:// URL:\n\n{}\n\nEnter: start Esc: back", + display_text(&model.url) + )) + .block(block("Remote source", model.linux_vt)) + .wrap(Wrap { trim: false }), Screen::Error => Paragraph::new(display_text(&model.details)) - .block(block("Full error details / Esc to return", model.linux_vt) - .border_style(Style::default().fg(theme_color(COLOR_ERROR, model.linux_vt)))) - .wrap(Wrap { trim: false }).scroll((model.scroll, 0)), + .block( + block("Full error details / Esc to return", model.linux_vt) + .border_style(Style::default().fg(theme_color(COLOR_ERROR, model.linux_vt))), + ) + .wrap(Wrap { trim: false }) + .scroll((model.scroll, 0)), _ => Paragraph::new(display_text(&model.details)) - .block(block("Result", model.linux_vt)).wrap(Wrap { trim: false }), + .block(block("Result", model.linux_vt)) + .wrap(Wrap { trim: false }), }; frame.render_widget(content, areas[2]); let choices = choices @@ -2199,8 +2136,7 @@ mod tests { for (text, diagnostic) in [("", "mode"), ("mode = 'not-a-mode'", "not-a-mode")] { fs::write(&path, text).unwrap(); let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); - let worker = - spawn_operation(Some(path.clone()), Request::Autorun, false, false, None, tx); + let worker = spawn_operation(Some(path.clone()), Request::Autorun, false, None, tx); let Event::Error { details, uncertain } = rx.recv().await.unwrap() else { panic!("Invalid settings must fail before preparing a source"); }; @@ -2229,50 +2165,6 @@ mod tests { assert_eq!(model.key(enter(), false, false), Some(Action::Reboot)); } - /// Force reinstall opens with Cancel selected and needs a second confirmation. - #[test] - fn force_is_never_the_default_and_requires_confirmation() { - let mut model = Model::new(); - model.screen = Screen::AlreadyPresent; - model.selected = 1; - assert!(model.key(enter(), false, false).is_none()); - assert_eq!(model.screen, Screen::Force); - assert_eq!(model.selected, 0); - assert!(model.key(enter(), false, false).is_none()); - assert_eq!(model.screen, Screen::AlreadyPresent); - } - - /// Retrying an automatic stream re-applies media overrides; manual streams keep their URL. - #[test] - fn force_retry_preserves_the_original_stream_source() { - let image = Url::parse("file:///run/trident/installer-media/cosi/image.cosi").unwrap(); - let mut model = Model::new(); - model.stream_image = Some(image.clone()); - model.event(Event::AlreadyPresent { - details: "Image already present".into(), - automatic: true, - }); - model.screen = Screen::Force; - model.selected = 1; - assert_eq!( - model.key(enter(), false, false), - Some(Action::Start(Request::StreamMedia(image), true)) - ); - - let image = Url::parse("https://example.test/image.cosi").unwrap(); - model.stream_image = Some(image.clone()); - model.event(Event::AlreadyPresent { - details: "Remote image already present".into(), - automatic: false, - }); - model.screen = Screen::Force; - model.selected = 1; - assert_eq!( - model.key(enter(), false, false), - Some(Action::Start(Request::Stream(image), true)) - ); - } - /// Repeated interrupts and power choices cannot shut down an active installation. #[test] fn active_worker_cannot_reach_a_power_action() { @@ -2328,12 +2220,7 @@ mod tests { /// Standard-size result and recovery screens expose their expected actions. #[test] fn screens_render_at_80_by_24_with_visible_actions() { - for screen in [ - Screen::Recovery, - Screen::Success, - Screen::Error, - Screen::AlreadyPresent, - ] { + for screen in [Screen::Recovery, Screen::Success, Screen::Error] { let mut model = Model::new(); model.screen = screen; model.details = "Diagnostic or result details".into(); diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index 08f6c8b798..df2de0f5e7 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -23,7 +23,6 @@ cosiDirectory = "cosi/" [autorun] reboot = true -force = false ``` There is **no implicit default for `mode`** in a TOML file: omitting it is an @@ -86,8 +85,8 @@ necessarily the value written in the packaged file. | `media.mountPath` | Unset | Absolute path to an **already mounted** filesystem instead of label discovery. Cannot be combined with `filesystemLabel`; this path is not mounted or made read-only by the installer. | | `media.cosiDirectory` | `"cosi"` | Nonempty directory relative to the media root. Absolute paths and `..` are rejected. Only regular `.cosi` files directly inside it are considered, in filename order. | | `autorun.hostConfiguration` | Unset | Host Configuration source. When omitted, `/etc/trident/config.yaml` is used **if it exists**; otherwise autorun looks for a COSI. | -| `autorun.reboot` | `true` | Reboot after successful servicing **only if** Trident reports that work was performed and a reboot is required. Confirmed Force retries of media streams re-apply the media-local value. | -| `autorun.force` | `false` | On automatic COSI streaming, bypass the matching-filesystem-UUID repeat-install guard. Does not affect Host Configuration installs, manual recovery sources, or the mounted/read-only disk safety check. | +| `autorun.reboot` | `true` | Reboot after successful servicing **only if** Trident reports that work was performed and a reboot is required. | +| `autorun.force` | `false` | Legacy key accepted for compatibility but ignored. Autorun never compares installed filesystem UUIDs or asks for repeat-install confirmation. | Media discovery waits up to 60 seconds during autorun for late USB/BMC device enumeration. It fails if more than one device matches the selected/default @@ -119,10 +118,11 @@ overlays, so it remains available when the local media cannot be read. Without a Host Configuration, the first regular COSI in `cosiDirectory` is streamed. If neither source is available, the graphical console offers -recovery; a headless run reports an error instead. Even with -`autorun.force = true`, streaming refuses a mounted or read-only disk that -Trident could select as its smallest fitting target, such as a large -installer USB. +recovery; a headless run reports an error instead. **Autorun streams on every +boot from the installer media, even after a successful install:** remove or +deprioritize the media before rebooting into the installed OS. The separate +media safety check still refuses a mounted or read-only disk that Trident +could select as its smallest fitting target, such as a large installer USB. ## Optional configuration on the media diff --git a/packaging/etc/installer.toml b/packaging/etc/installer.toml index 961def17c9..b947c02ba2 100644 --- a/packaging/etc/installer.toml +++ b/packaging/etc/installer.toml @@ -7,4 +7,3 @@ cosiDirectory = "cosi/" [autorun] reboot = true -force = false From 065c721668aba0f5601acc0d3dc05f069e0af84d Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 11 Nov 2026 15:48:21 -0800 Subject: [PATCH 45/62] fix: Keep shell exit status separate from install result A nonzero recovery-shell exit is a warning in the installer diagnostics and serial mirror; it does not replace a successful servicing result or cancel its pending reboot. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 5 ++-- crates/installer/src/ui.rs | 52 ++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index 85ec448889..bf19e7dec0 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -118,8 +118,9 @@ scrollable full error details and Esc returns to the logs. Successful installs keep their logs in a green result frame. Each result is also the last highlighted log entry. Continue opens recovery: Shell, Stream COSI URL, remote HC URL, or Shutdown. Shell `exit` returns to the -originating screen. The recovery shell is a root login Bash, so it reads -`/etc/profile` and the root profile (if present); on the installer ISO, +originating screen; a nonzero shell exit is logged without changing the +installation result or reboot decision. The recovery shell is a root login +Bash, so it reads `/etc/profile` and the root profile (if present); on the installer ISO, `/etc/profile` gives root a red prompt. The ISO service sets `TERM=linux` for tty1 so terminal tools such as `clear` work in the shell. Installation continues while a shell is open; automatic reboot waits. The success screen diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 7433b4b6f7..f15028ffac 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -29,7 +29,7 @@ use ratatui::{ use tokio::{ signal::unix::{self, SignalKind}, sync::mpsc, - task::{self, JoinHandle}, + task::{self, JoinError, JoinHandle}, time, }; @@ -820,6 +820,21 @@ fn spawn_operation( }) } +/// Reports shell failures without changing the result of an independent installation. +fn shell_result_event(result: Result, JoinError>) -> Option { + match result { + Ok(Ok(())) => None, + Ok(Err(error)) => Some(Event::InstallerLog { + level: Level::Warn, + message: format!("Recovery shell failed: {error:#}"), + }), + Err(error) => Some(Event::InstallerLog { + level: Level::Warn, + message: format!("Recovery shell task terminated: {error}"), + }), + } +} + /// Runs the installer event loop, starting autorun and offering recovery on a terminal. /// Noninteractive runs return after work completes and fail if recovery needs user input; /// `plain` selects text output instead of the full-screen display. @@ -893,15 +908,10 @@ pub(super) async fn run( } if shell.as_ref().is_some_and(JoinHandle::is_finished) { if let Some(finished) = shell.take() { - let failure = match finished.await { - Ok(Ok(())) => None, - Ok(Err(error)) => Some(model.fail(format!("Shell failed: {error:#}"), false)), - Err(error) => { - Some(model.fail(format!("Shell task terminated: {error}"), false)) - } - }; - if let Some(failure) = failure { - output.announce(&failure, model.serial_verbosity); + if let Some(event) = shell_result_event(finished.await) { + output.record(&event)?; + let warning = model.event(event); + output.announce(&warning, model.serial_verbosity); } } if let Some(display) = &mut display { @@ -1875,6 +1885,28 @@ mod tests { assert!(model.reboot_at.is_some()); } + /// A failed recovery shell cannot erase a successful servicing result or its reboot. + #[test] + fn shell_exit_failure_is_only_a_warning() { + let mut model = Model::new(); + model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::Required, + servicing_kind: Some(ServicingKind::CleanInstall), + image_hash: None, + }, + )))); + let reboot_at = model.reboot_at; + assert!(reboot_at.is_some()); + assert!(shell_result_event(Ok(Ok(()))).is_none()); + let warning = model.event(shell_result_event(Ok(Err(anyhow!("exit 1")))).unwrap()); + assert_eq!(warning.level, LogLevel::Warn); + assert!(warning.plain().contains("Recovery shell failed: exit 1")); + assert_eq!(model.screen, Screen::Success); + assert!(!model.failed); + assert_eq!(model.reboot_at, reboot_at); + } + /// Unknown servicing kinds fail rather than triggering an automatic reboot. #[test] fn unknown_completion_kind_does_not_claim_success() { From c182236b2cd2034244537e7bc7eb5d3bfd839644 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 11 Nov 2026 16:51:21 -0800 Subject: [PATCH 46/62] fix: Redact signed image URLs from installer diagnostics Strip URL credentials, queries and fragments from daemon logs and structured errors before storing installer diagnostics or forwarding them to the TUI and serial consoles. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Cargo.lock | 1 + crates/installer/Cargo.toml | 1 + crates/installer/README.md | 7 ++- crates/installer/src/source.rs | 29 +++++++++++ crates/installer/src/ui.rs | 92 +++++++++++++++++++++++++++++++++- 5 files changed, 127 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 46a3c3524c..1d11e8e302 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1730,6 +1730,7 @@ dependencies = [ "log", "osutils", "ratatui", + "regex", "reqwest", "serde", "serde_json", diff --git a/crates/installer/Cargo.toml b/crates/installer/Cargo.toml index 1ba2dc7b95..0b18713d38 100644 --- a/crates/installer/Cargo.toml +++ b/crates/installer/Cargo.toml @@ -17,6 +17,7 @@ crossterm = { workspace = true } env_logger = { workspace = true } log = { workspace = true } ratatui = { workspace = true, features = ["unstable-rendered-line-info"] } +regex = { workspace = true } reqwest = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/crates/installer/README.md b/crates/installer/README.md index bf19e7dec0..4702a101c2 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -131,8 +131,11 @@ edits take effect. Invalid or missing configuration never supplies implicit defaults for an installation. Display filtering never removes daemon log records. All received responses are -also appended to the private `/var/log/trident-installer.log`. Installer events -and daemon records use `MM:SS [INST:LEVEL] message` and +also appended to the private `/var/log/trident-installer.log`. HTTP(S) URLs in +received diagnostics have credentials, queries and fragments removed before +display, serial mirroring or installer-log persistence; Trident's own journal +is separate. Installer events and daemon records use +`MM:SS [INST:LEVEL] message` and `MM:SS [TRIDENT:LEVEL] message`, respectively. `serialVerbosity` controls the serial stream independently of TUI verbosity; each line of a multiline message gets its own `MM:SS [SOURCE:LEVEL]` prefix. The TUI defaults to Debug: errors red, warnings diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 588b6a7027..ce124e7031 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -2,12 +2,14 @@ use std::{ fs::{self, File}, io::{ErrorKind, Read}, path::{Path, PathBuf}, + sync::OnceLock, thread, time::{Duration, Instant}, }; use anyhow::{anyhow, bail, ensure, Context, Error}; use log::warn; +use regex::{Captures, Regex}; use reqwest::blocking::Client; use serde::{de::IgnoredAny, Deserialize}; use sha2::{Digest, Sha384}; @@ -87,6 +89,27 @@ pub(super) fn display_source(source: &str) -> String { } } +/// Removes credentials and signed query data from URLs embedded in diagnostics. +pub(super) fn redact_message_urls(message: &str) -> String { + static URLS: OnceLock = OnceLock::new(); + let urls = URLS.get_or_init(|| { + Regex::new(r#"(?i)https?://[^\s"<>]+"#).expect("invariant: diagnostic URL pattern is valid") + }); + urls.replace_all(message, |capture: &Captures<'_>| { + let matched = capture + .get(0) + .expect("invariant: entire URL match") + .as_str(); + let url = matched.trim_end_matches(['.', ',', ')', ']', ';', '\'']); + let suffix = &matched[url.len()..]; + let description = Url::parse(url) + .map(|url| display_url(&url)) + .unwrap_or_else(|_| "".into()); + format!("{description}{suffix}") + }) + .into_owned() +} + /// Resolves installation media and returns an install or stream plan with progress callbacks. /// /// Only autorun waits for media enumeration; explicit recovery sources proceed immediately. @@ -596,6 +619,12 @@ mod tests { display_source("https://bad URL?token=secret"), "" ); + assert_eq!( + redact_message_urls( + "Loaded 'https://u:p@example.test/image.cosi?sig=sec'ret#fragment', done" + ), + "Loaded 'https://example.test/image.cosi', done" + ); } #[test] diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index f15028ffac..e7bc09911a 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -835,6 +835,30 @@ fn shell_result_event(result: Result, JoinError>) -> Option Event { + match &mut event { + Event::InstallerLog { message, .. } | Event::Preparing(message) => { + *message = source::redact_message_urls(message); + } + Event::Prepared { description, .. } => { + *description = source::redact_message_urls(description); + } + Event::Error { details, .. } => { + *details = source::redact_message_urls(details); + } + Event::Response(ServicingEvent::Log(record)) => { + record.message = source::redact_message_urls(&record.message); + } + Event::Response(ServicingEvent::Completed(Err(error))) => { + error.message = source::redact_message_urls(&error.message); + error.error_message = source::redact_message_urls(&error.error_message); + } + _ => {} + } + event +} + /// Runs the installer event loop, starting autorun and offering recovery on a terminal. /// Noninteractive runs return after work completes and fail if recovery needs user input; /// `plain` selects text output instead of the full-screen display. @@ -879,6 +903,7 @@ pub(super) async fn run( Err(_) => break, }, }; + let event = redact_event(event); output.record(&event)?; let entry = model.event(event); output.announce(&entry, model.serial_verbosity); @@ -909,6 +934,7 @@ pub(super) async fn run( if shell.as_ref().is_some_and(JoinHandle::is_finished) { if let Some(finished) = shell.take() { if let Some(event) = shell_result_event(finished.await) { + let event = redact_event(event); output.record(&event)?; let warning = model.event(event); output.announce(&warning, model.serial_verbosity); @@ -1493,7 +1519,8 @@ mod tests { use tempfile::TempDir; use trident_client::{ - Completion as ClientCompletion, LogRecord, RebootDisposition, ServicingKind, + Completion as ClientCompletion, LogRecord, RebootDisposition, RemoteError, + RemoteErrorCategory, ServicingKind, }; /// A failed serial destination is removed without blocking healthy mirrors. @@ -1852,6 +1879,69 @@ mod tests { assert_eq!(entry.plain(), "00:00 [TRIDENT:INFO] Preparing storage"); } + /// Trident's COSI-loading messages must not expose signed URLs on any installer output. + #[test] + fn daemon_image_urls_are_redacted_before_recording_or_mirroring() { + let root = TempDir::new().unwrap(); + let diagnostic = root.path().join("installer.log"); + let serial = root.path().join("serial.log"); + let mut output = Output { + log: Some(File::create(&diagnostic).unwrap()), + mirrors: vec![(serial.clone(), File::create(&serial).unwrap())], + }; + let mut model = Model::new(); + for message in [ + "Attempting to load COSI file from 'https://user:password@example.test/image.cosi?sig=secret#fragment'", + "Loaded COSI file from 'https://user:password@example.test/image.cosi?sig=secret#fragment' with hash 'abc'", + ] { + let event = redact_event(Event::Response(ServicingEvent::Log(LogRecord { + timestamp: None, + level: LogLevel::Info, + message: message.into(), + module: String::new(), + target: String::new(), + location: None, + }))); + output.record(&event).unwrap(); + let entry = model.event(event); + output.announce(&entry, model.serial_verbosity); + assert!(entry.message.contains("https://example.test/image.cosi")); + for secret in ["user", "password", "sig=secret", "fragment"] { + assert!(!entry.message.contains(secret), "{entry:?}"); + } + } + for text in [ + fs::read_to_string(diagnostic).unwrap(), + fs::read_to_string(serial).unwrap(), + ] { + assert!(text.contains("https://example.test/image.cosi"), "{text}"); + for secret in ["user", "password", "sig=secret", "fragment"] { + assert!(!text.contains(secret), "{text}"); + } + } + } + + /// Daemon failures cannot reintroduce a signed URL through structured error details. + #[test] + fn daemon_image_errors_redact_signed_urls() { + let event = redact_event(Event::Response(ServicingEvent::Completed(Err( + RemoteError { + category: RemoteErrorCategory::Servicing, + message: "Failed to load 'https://user:password@example.test/image.cosi?sig=secret#fragment'".into(), + error_message: "HTTP failure at https://user:password@example.test/image.cosi?sig=secret#fragment".into(), + subkind: "image-load-failed".into(), + location: None, + }, + )))); + let mut model = Model::new(); + let entry = model.event(event); + assert!(model.details.contains("https://example.test/image.cosi")); + for secret in ["user", "password", "sig=secret", "fragment"] { + assert!(!model.details.contains(secret), "{}", model.details); + assert!(!entry.plain().contains(secret)); + } + } + /// Unknown daemon severities remain identifiable in serial output and visible as warnings. #[test] fn unknown_log_level_is_not_silently_discarded() { From 275b27ae8ed47ac5cde171242690951ed315ed79 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 11 Nov 2026 16:56:40 -0800 Subject: [PATCH 47/62] fix: Drain queued installer keyboard events per frame Process bounded batches of ready terminal events so pasted recovery URLs and their Enter key are handled promptly, yielding before input can starve other installer events and stopping when the recovery shell takes control. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/ui.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index e7bc09911a..24799544cb 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -45,6 +45,7 @@ use crate::{ }; const FRAME_INTERVAL: Duration = Duration::from_millis(100); +const INPUT_EVENTS_PER_FRAME: usize = 256; const REBOOT_DELAY: Duration = Duration::from_secs(5); const EVENT_CAPACITY: usize = 64; const LOG_CAPACITY: usize = 500; @@ -1016,7 +1017,10 @@ pub(super) async fn run( } printed_screen = Some((model.screen, demo_shell.is_some(), model.verbosity_open)); } - if event::poll(Duration::ZERO)? { + for _ in 0..INPUT_EVENTS_PER_FRAME { + if !event::poll(Duration::ZERO)? { + break; + } let event = event::read()?; if let TerminalEvent::Key(key) = event { if let Some(shell) = &mut demo_shell { @@ -1034,6 +1038,7 @@ pub(super) async fn run( Action::Shell if scenario.is_some() => { demo_shell = Some(DemoShell::default()); printed_screen = None; + break; } Action::Shell => { if let Some(display) = &mut display { @@ -1044,6 +1049,7 @@ pub(super) async fn run( OpenOptions::new().read(true).write(true).open("/dev/tty")?; shell = Some(task::spawn_blocking(move || os_terminal::shell(&tty))); + break; } Action::Start(request) => { if worker.is_some() { From 158d6070833e7178545f44c57a0f9767cc4660f4 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 11 Nov 2026 17:22:41 -0800 Subject: [PATCH 48/62] fix: Wait for queued installer results before releasing worker Keep power actions guarded until all worker events are applied and accept ./metadata.json in COSI archives, matching Trident archive entry handling. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/source.rs | 15 +++++++------ crates/installer/src/ui.rs | 40 +++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index ce124e7031..729366306d 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -439,7 +439,8 @@ fn metadata(reader: impl Read) -> Result<(Metadata, Sha384Hash), Error> { let mut archive = Archive::new(reader.take(MAX_METADATA_BYTES)); for entry in archive.entries().context("Invalid COSI archive")? { let mut entry = entry?; - if entry.path()?.as_ref() == Path::new(METADATA_PATH) { + let path = entry.path()?; + if path.strip_prefix("./").unwrap_or(&path) == Path::new(METADATA_PATH) { ensure!( entry.header().entry_type().is_file(), "COSI metadata must be a regular file" @@ -747,23 +748,23 @@ mod tests { /// Hashes the original metadata bytes rather than re-serialized JSON. fn preflight_metadata_hash_matches_daemon_contract() { let raw = br#"{"version":"1.2","disk":{"size":4294967296},"images":[{}]}"#; - let inspect = |raw: &[u8]| { + let inspect = |raw: &[u8], path: &str| { let mut builder = TarBuilder::new(Vec::new()); let mut header = tar::Header::new_gnu(); header.set_size(raw.len() as u64); header.set_cksum(); - builder - .append_data(&mut header, METADATA_PATH, raw) - .unwrap(); + builder.append_data(&mut header, path, raw).unwrap(); builder.finish().unwrap(); metadata(Cursor::new(builder.into_inner().unwrap())).unwrap() }; - let (parsed, hash) = inspect(raw); + let (parsed, hash) = inspect(raw, METADATA_PATH); assert_eq!(parsed.disk.unwrap().size, 4294967296); assert_eq!(hash, Sha384Hash::from(format!("{:x}", Sha384::digest(raw)))); + let (_, prefixed_hash) = inspect(raw, "./metadata.json"); + assert_eq!(hash, prefixed_hash); let padded = br#"{ "version":"1.2","disk":{"size":4294967296},"images":[{}]}"#; - let (_, padded_hash) = inspect(padded); + let (_, padded_hash) = inspect(padded, METADATA_PATH); assert_ne!(hash, padded_hash); } diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 24799544cb..5356d958b4 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -916,7 +916,7 @@ pub(super) async fn run( println!("{}", entry.plain()); } } - if worker.as_ref().is_some_and(JoinHandle::is_finished) { + if rx.is_empty() && worker.as_ref().is_some_and(JoinHandle::is_finished) { if let Some(finished) = worker.take() { let failure = match finished.await { Ok(Ok(())) => None, @@ -2345,6 +2345,44 @@ mod tests { assert_eq!(model.key(enter(), false, false), Some(Action::Reboot)); } + /// An enqueued completion must be handled before the worker stops blocking power actions. + #[tokio::test] + async fn pending_completion_keeps_worker_active() { + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let worker = task::spawn(async move { + tx.send(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::NotRequired, + servicing_kind: Some(ServicingKind::CleanInstall), + image_hash: None, + }, + )))) + .await + .unwrap(); + Ok::<(), Error>(()) + }); + time::timeout(Duration::from_secs(1), async { + while !worker.is_finished() { + task::yield_now().await; + } + }) + .await + .unwrap(); + + let mut model = Model::new(); + model.screen = Screen::Recovery; + model.selected = 3; + assert!(!rx.is_empty()); + assert_eq!( + model.key(enter(), false, !(rx.is_empty() && worker.is_finished())), + Some(Action::BlockedPower) + ); + model.event(rx.recv().await.unwrap()); + assert!(rx.is_empty()); + assert_eq!(model.screen, Screen::Success); + worker.await.unwrap().unwrap(); + } + /// Standard-size result and recovery screens expose their expected actions. #[test] fn screens_render_at_80_by_24_with_visible_actions() { From 13fae4574e93b23c656e2ddd8597c3e4f8224f48 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 11 Nov 2026 18:57:59 -0800 Subject: [PATCH 49/62] fix: Decode local COSI file URLs before opening paths Use Url::to_file_path in Trident FileReader so installer-selected image names containing spaces or URL-sensitive characters work in both Install and StreamDisk. Reject non-local file URL hosts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/trident/src/io_utils/file_reader.rs | 31 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/crates/trident/src/io_utils/file_reader.rs b/crates/trident/src/io_utils/file_reader.rs index d3e54ddd62..5967bdfc26 100644 --- a/crates/trident/src/io_utils/file_reader.rs +++ b/crates/trident/src/io_utils/file_reader.rs @@ -8,7 +8,7 @@ use std::{ #[cfg(test)] use std::io::Cursor; -use anyhow::{bail, ensure, Error}; +use anyhow::{anyhow, bail, ensure, Error}; use log::debug; use url::Url; @@ -45,9 +45,10 @@ impl FileReader { pub(crate) fn new(source: &Url, timeout: Duration) -> Result { Ok(match source.scheme() { "file" => { - // Load from local file - debug!("Loading file: '{}'", source.path()); - let path = PathBuf::from(source.path()); + let path = source + .to_file_path() + .map_err(|()| anyhow!("Invalid local file URL"))?; + debug!("Loading file: '{}'", path.display()); ensure!( path.exists(), format!("Path '{}' does not exist", path.display()), @@ -140,7 +141,7 @@ mod tests { use std::io::{SeekFrom, Write}; use log::trace; - use tempfile::NamedTempFile; + use tempfile::{NamedTempFile, TempDir}; #[test] fn test_file_reader_factory_file() { @@ -200,6 +201,26 @@ mod tests { check_section(0, 13); // Hello, World! } + #[test] + fn file_url_decodes_escaped_local_path() { + let directory = TempDir::new().unwrap(); + let path = directory.path().join("installer image #1.cosi"); + std::fs::write(&path, b"local COSI").unwrap(); + let url = Url::from_file_path(&path).unwrap(); + assert!(url.path().contains("%20")); + assert!(url.path().contains("%23")); + + let reader = FileReader::new(&url, Duration::from_secs(5)).unwrap(); + let mut bytes = Vec::new(); + reader.reader().unwrap().read_to_end(&mut bytes).unwrap(); + assert_eq!(bytes, b"local COSI"); + FileReader::new( + &Url::parse("file://remote.example/installer%20image.cosi").unwrap(), + Duration::from_secs(5), + ) + .unwrap_err(); + } + #[test] fn test_file_reader_http_file() { env_logger::builder() From ee5a1d6a0dca1ab81cc81d87d6d656a927874642 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 11 Nov 2026 19:06:07 -0800 Subject: [PATCH 50/62] test: Cover queued disconnect after full installer event batch Show that a worker cannot release its power guard while an uncertain terminal event waits behind a full channel; after delivery, the uncertain state continues to block shutdown. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/ui.rs | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 5356d958b4..8a372e6d33 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -2383,6 +2383,51 @@ mod tests { worker.await.unwrap().unwrap(); } + /// An uncertain terminal event queued behind a full batch must block shutdown. + #[tokio::test] + async fn pending_disconnect_cannot_release_power_guard() { + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let worker = task::spawn(async move { + for _ in 0..EVENT_CAPACITY { + tx.send(Event::Preparing("Still writing".into())) + .await + .unwrap(); + } + tx.send(Event::Error { + details: "gRPC stream disconnected".into(), + uncertain: true, + }) + .await + .unwrap(); + Ok::<(), Error>(()) + }); + for _ in 0..EVENT_CAPACITY { + rx.recv().await.unwrap(); + } + time::timeout(Duration::from_secs(1), async { + while !worker.is_finished() { + task::yield_now().await; + } + }) + .await + .unwrap(); + + let mut model = Model::new(); + model.screen = Screen::Recovery; + model.selected = 3; + assert!(!rx.is_empty()); + assert_eq!( + model.key(enter(), false, !(rx.is_empty() && worker.is_finished())), + Some(Action::BlockedPower) + ); + model.event(rx.recv().await.unwrap()); + assert!(model.uncertain); + model.screen = Screen::Recovery; + model.selected = 3; + assert_eq!(model.key(enter(), false, false), Some(Action::BlockedPower)); + worker.await.unwrap().unwrap(); + } + /// Standard-size result and recovery screens expose their expected actions. #[test] fn screens_render_at_80_by_24_with_visible_actions() { From 1a79a3bfad6a79b60ff7b84b0a24524f64312286 Mon Sep 17 00:00:00 2001 From: Paco Date: Thu, 12 Nov 2026 13:36:55 -0800 Subject: [PATCH 51/62] fix: Check worker completion before pending result queue Do not observe an empty queue before the worker can enqueue its final disconnect. The event queue now drains before the worker guard is released, with a deterministic test of the former interleaving. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/ui.rs | 46 +++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index 8a372e6d33..b27cfd4223 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -916,7 +916,8 @@ pub(super) async fn run( println!("{}", entry.plain()); } } - if rx.is_empty() && worker.as_ref().is_some_and(JoinHandle::is_finished) { + // A finished worker cannot enqueue a terminal event after the queue check. + if worker.as_ref().is_some_and(JoinHandle::is_finished) && rx.is_empty() { if let Some(finished) = worker.take() { let failure = match finished.await { Ok(Ok(())) => None, @@ -1523,6 +1524,7 @@ mod tests { use ratatui::backend::TestBackend; use tempfile::TempDir; + use tokio::sync::oneshot; use trident_client::{ Completion as ClientCompletion, LogRecord, RebootDisposition, RemoteError, @@ -2428,6 +2430,48 @@ mod tests { worker.await.unwrap().unwrap(); } + /// Rechecking an empty queue before worker completion misses a late disconnect. + #[tokio::test] + async fn disconnect_between_worker_and_queue_checks_stays_guarded() { + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let (release, ready) = oneshot::channel::<()>(); + let worker = task::spawn(async move { + ready.await.unwrap(); + tx.send(Event::Error { + details: "gRPC stream disconnected".into(), + uncertain: true, + }) + .await + .unwrap(); + Ok::<(), Error>(()) + }); + let previously_empty = rx.is_empty(); + release.send(()).unwrap(); + time::timeout(Duration::from_secs(1), async { + while !worker.is_finished() { + task::yield_now().await; + } + }) + .await + .unwrap(); + assert!(previously_empty && worker.is_finished()); + assert!(!rx.is_empty()); + + let mut model = Model::new(); + model.screen = Screen::Recovery; + model.selected = 3; + assert_eq!( + model.key(enter(), false, !(worker.is_finished() && rx.is_empty())), + Some(Action::BlockedPower) + ); + model.event(rx.recv().await.unwrap()); + assert!(model.uncertain); + model.screen = Screen::Recovery; + model.selected = 3; + assert_eq!(model.key(enter(), false, false), Some(Action::BlockedPower)); + worker.await.unwrap().unwrap(); + } + /// Standard-size result and recovery screens expose their expected actions. #[test] fn screens_render_at_80_by_24_with_visible_actions() { From 20932e3cd99bae52845684adb7db6dc6f32e29a1 Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 13 Nov 2026 14:35:03 -0800 Subject: [PATCH 52/62] refactor: Remove installer demo mode Keep the installer focused on live autorun and recovery. Remove simulated shell, source and power actions while retaining line-oriented --plain output and real unit coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 19 +- crates/installer/src/demo.rs | 100 ------ crates/installer/src/main.rs | 56 ++-- crates/installer/src/ui.rs | 384 +++++----------------- docs/Reference/Installer-Configuration.md | 4 +- 5 files changed, 114 insertions(+), 449 deletions(-) delete mode 100644 crates/installer/src/demo.rs diff --git a/crates/installer/README.md b/crates/installer/README.md index 4702a101c2..c33b87ae14 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -4,21 +4,6 @@ autorun wrapper around Trident's gRPC API through the shared `trident-client` crate, not a partitioning engine. -## Local demo - -```console -cargo run -p installer -- --demo -cargo run -p installer -- --demo failure -cargo run -p installer -- --demo no-images -cargo run -p installer -- --demo disconnect -``` - -The demo never mounts media, inspects disks, contacts Trident, or changes machine -power state. Reboot, Shutdown and Shell are all simulated; no real shell or -command is launched. Press `Q` to quit. Use `--plain` -for line-oriented output; without a terminal the demo prints its events and -exits when complete. - ## Configuration Read `/etc/trident/installer.toml` or the path supplied with `--config`. @@ -143,8 +128,8 @@ amber, INFO pale neutral, DEBUG steel gray and TRACE slate gray; INST labels are muted slate and TRIDENT labels seafoam. The wordmark remains cyan and success is green. On the graphical Linux virtual terminal, the installer programs its 16 ANSI palette slots to A1 RGB values, then restores the -original palette when opening a shell or leaving the UI. Workstation demo -terminals use the RGB values directly. +original palette when opening a shell or leaving the UI. Non-VT terminals use +the RGB values directly. `V` opens a live verbosity picker (Off, Error, Warn, Info, Debug, Trace) beside the operation; this only filters the display, not serial or stored diagnostics. The private diagnostic file retains diff --git a/crates/installer/src/demo.rs b/crates/installer/src/demo.rs deleted file mode 100644 index a4715b9aac..0000000000 --- a/crates/installer/src/demo.rs +++ /dev/null @@ -1,100 +0,0 @@ -use std::time::Duration; - -use anyhow::Error; -use clap::ValueEnum; -use tokio::{sync::mpsc::Sender, time}; - -use trident_client::{ - Completion, LogLevel, LogRecord, RebootDisposition, RemoteError, RemoteErrorCategory, - ServicingEvent, ServicingKind, -}; - -use crate::{ - client::{self, Event}, - config::SerialVerbosity, -}; - -const DEMO_STEP: Duration = Duration::from_millis(500); - -#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] -pub(super) enum Scenario { - Success, - Failure, - NoImages, - Disconnect, -} - -/// Emits a simulated servicing sequence without connecting to Trident or touching disks. -pub(super) async fn run(scenario: Scenario, tx: Sender) -> Result<(), Error> { - client::send( - &tx, - Event::Prepared { - description: "DEMO: StreamDisk from ISO image".into(), - reboot: false, - serial_verbosity: SerialVerbosity::Debug, - }, - ) - .await?; - if scenario == Scenario::NoImages { - return client::send(&tx, Event::Error { - details: "DEMO: No Host Configuration and no COSI images in cosi/.\nTry the simulated shell or a remote-source recovery action.\nNo network requests or machine operations will be performed.".into(), - uncertain: false, - }).await; - } - response(&tx, ServicingEvent::Started { timestamp: None }).await?; - for (level, message) in [ - (LogLevel::Info, "Loading image metadata"), - (LogLevel::Debug, "Inspecting available block devices"), - (LogLevel::Info, "Preparing storage"), - (LogLevel::Trace, "Example detailed diagnostic record"), - (LogLevel::Info, "Streaming the OS image"), - ] { - time::sleep(DEMO_STEP).await; - response( - &tx, - ServicingEvent::Log(LogRecord { - timestamp: None, - level, - message: message.into(), - module: String::new(), - target: String::new(), - location: None, - }), - ) - .await?; - } - time::sleep(DEMO_STEP).await; - if scenario == Scenario::Disconnect { - return client::send(&tx, Event::Error { - details: "DEMO: Connection lost before Completed. Outcome unknown; another write must wait until Trident is confirmed idle.".into(), - uncertain: true, - }).await; - } - let failed = scenario == Scenario::Failure; - response( - &tx, - ServicingEvent::Completed(if failed { - Err(RemoteError { - category: RemoteErrorCategory::Servicing, - message: - "DEMO: Installation failed\nFailed to stream the OS image\nConnection timed out" - .into(), - subkind: "demo-network-failure".into(), - error_message: "Connection timed out".into(), - location: None, - }) - } else { - Ok(Completion { - reboot: RebootDisposition::Required, - servicing_kind: Some(ServicingKind::CleanInstall), - image_hash: None, - }) - }), - ) - .await -} - -/// Wraps a demo servicing event in the same shape used by a real install. -async fn response(tx: &Sender, response: ServicingEvent) -> Result<(), Error> { - client::send(tx, Event::Response(response)).await -} diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index e74cf18bab..43ffe30e84 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -15,11 +15,10 @@ use tokio::sync::mpsc::UnboundedSender; use osutils::{systemd, terminal}; -use crate::{client::Event, config::DEFAULT_CONFIG, demo::Scenario, ui::Output}; +use crate::{client::Event, config::DEFAULT_CONFIG, ui::Output}; mod client; mod config; -mod demo; mod source; mod ui; @@ -38,11 +37,9 @@ static INSTALLER_LOG_TX: OnceLock> = OnceLock::new(); struct Args { #[arg(long, default_value = DEFAULT_CONFIG)] config: PathBuf, - #[arg(long, num_args = 0..=1, default_missing_value = "success", value_enum)] - demo: Option, #[arg(long)] plain: bool, - #[arg(long, conflicts_with = "demo")] + #[arg(long)] system_console: bool, } @@ -94,33 +91,30 @@ async fn run(args: Args) -> Result<(), Error> { log: None, mirrors: Vec::new(), }; - if args.demo.is_none() { - output.log = Some( - OpenOptions::new() - .create(true) - .append(true) - .mode(PRIVATE_FILE_MODE) - .open(LOG_PATH) - .context("Failed to open private installer diagnostic log; run as root")?, - ); - fs::set_permissions(LOG_PATH, Permissions::from_mode(PRIVATE_FILE_MODE))?; - if args.system_console { - systemd::start_unit("tridentd.socket")?; - systemd::start_unit("systemd-udev-settle.service")?; - let consoles = terminal::active_consoles()?; - if let Some(graphical) = consoles.iter().find(|path| path.ends_with("tty1")) { - systemd::stop_unit("getty@tty1.service")?; - let _terminal = terminal::attach(graphical)?; - linux_vt = true; - } - for path in consoles.iter().filter(|path| !path.ends_with("tty1")) { - match terminal::open_nonblocking_console(path) { - Ok(terminal) => output.mirrors.push((path.clone(), terminal)), - Err(error) => warn!("Could not mirror status to '{}': {error}", path.display()), - } + output.log = Some( + OpenOptions::new() + .create(true) + .append(true) + .mode(PRIVATE_FILE_MODE) + .open(LOG_PATH) + .context("Failed to open private installer diagnostic log; run as root")?, + ); + fs::set_permissions(LOG_PATH, Permissions::from_mode(PRIVATE_FILE_MODE))?; + if args.system_console { + systemd::start_unit("tridentd.socket")?; + systemd::start_unit("systemd-udev-settle.service")?; + let consoles = terminal::active_consoles()?; + if let Some(graphical) = consoles.iter().find(|path| path.ends_with("tty1")) { + systemd::stop_unit("getty@tty1.service")?; + let _terminal = terminal::attach(graphical)?; + linux_vt = true; + } + for path in consoles.iter().filter(|path| !path.ends_with("tty1")) { + match terminal::open_nonblocking_console(path) { + Ok(terminal) => output.mirrors.push((path.clone(), terminal)), + Err(error) => warn!("Could not mirror status to '{}': {error}", path.display()), } } } - let config_path = args.demo.is_none().then_some(args.config); - ui::run(config_path, args.demo, args.plain, linux_vt, output).await + ui::run(args.config, args.plain, linux_vt, output).await } diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs index b27cfd4223..c97b152766 100644 --- a/crates/installer/src/ui.rs +++ b/crates/installer/src/ui.rs @@ -39,7 +39,6 @@ use trident_client::{LogLevel, ServicingEvent}; use crate::{ client::{self, Completion, Event}, config::{Config, SerialVerbosity}, - demo::{self, Scenario}, source::{self, Request}, APPLICATION_NAME, INSTALLER_LOG_TX, }; @@ -50,7 +49,6 @@ const REBOOT_DELAY: Duration = Duration::from_secs(5); const EVENT_CAPACITY: usize = 64; const LOG_CAPACITY: usize = 500; const DISPLAY_LOG_CHARACTERS: usize = 4096; -const DEMO_SHELL_HISTORY: usize = 8; const COLOR_BACKGROUND: Color = Color::Rgb(11, 17, 24); const COLOR_FRAME: Color = Color::Rgb(53, 69, 83); const COLOR_BRAND: Color = Color::Rgb(75, 216, 220); @@ -124,7 +122,6 @@ const TRIDENT_WORDMARK: [&str; 8] = [ r#" 888 888 888 Y88b 888 Y8b. 888 888 Y88b."#, r#" 888 888 888 "Y88888 "Y8888 888 888 "Y888"#, ]; -const DEMO_SHELL_NOTICE: &str = "This shell is simulated. Commands are never executed.\nNo network, disk or power operations are performed.\nType exit or press Esc to return to the installer."; const ASCII_BORDER: BorderSet = BorderSet { top_left: "+", top_right: "+", @@ -448,7 +445,7 @@ impl Model { /// Updates navigation for a key press and returns an action only when one is requested. /// URL entry and the verbosity picker consume keys before screen-level shortcuts. - fn key(&mut self, key: KeyEvent, demo: bool, worker_active: bool) -> Option { + fn key(&mut self, key: KeyEvent, worker_active: bool) -> Option { if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { return None; } @@ -531,7 +528,6 @@ impl Model { } } } - KeyCode::Char('q') if demo => return Some(Action::Quit), KeyCode::Char('s') => return Some(Action::Shell), KeyCode::Char('v' | 'V') => { self.verbosity_selected = VERBOSITY_LEVELS @@ -630,59 +626,6 @@ enum Action { BlockedPower, Reboot, Poweroff, - Quit, -} - -#[derive(Default)] -struct DemoShell { - command: String, - responses: VecDeque, -} - -impl DemoShell { - /// Edits simulated input without executing it; returns true on exit or Escape. - fn key(&mut self, key: KeyEvent) -> bool { - if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { - return false; - } - if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') { - self.command.clear(); - return false; - } - match key.code { - KeyCode::Esc => return true, - KeyCode::Backspace => { - self.command.pop(); - } - KeyCode::Char(c) if !c.is_control() => self.command.push(c), - KeyCode::Enter => { - if self.command.trim() == "exit" { - return true; - } - if self.responses.len() == DEMO_SHELL_HISTORY { - self.responses.pop_front(); - } - self.responses - .push_back("DEMO: Command not executed".into()); - self.command.clear(); - } - _ => {} - } - false - } - - /// Includes the simulation warning, recent responses, and sanitized prompt input. - fn text(&self) -> String { - format!( - "{DEMO_SHELL_NOTICE}\n\n{}\n\ndemo$ {}", - self.responses - .iter() - .cloned() - .collect::>() - .join("\n"), - display_text(&self.command) - ) - } } pub(super) struct Output { @@ -788,34 +731,26 @@ impl Drop for Display { } } -/// Starts a demo run or reloads live configuration for each installation attempt. +/// Reloads live configuration for each installation attempt. /// Configuration read errors are reported as events rather than worker failures. fn spawn_operation( - config_path: Option, + config_path: PathBuf, request: Request, uncertain: bool, - scenario: Option, tx: mpsc::Sender, ) -> JoinHandle> { task::spawn(async move { - match scenario { - Some(scenario) => demo::run(scenario, tx).await, - None => { - let path = config_path - .context("Live installation requires an explicit configuration path")?; - match Config::read(path) { - Ok(settings) => client::execute(settings, request, uncertain, tx).await, - Err(error) => { - client::send( - &tx, - Event::Error { - details: format!("{error:#}"), - uncertain, - }, - ) - .await - } - } + match Config::read(config_path) { + Ok(settings) => client::execute(settings, request, uncertain, tx).await, + Err(error) => { + client::send( + &tx, + Event::Error { + details: format!("{error:#}"), + uncertain, + }, + ) + .await } } }) @@ -864,8 +799,7 @@ fn redact_event(mut event: Event) -> Event { /// Noninteractive runs return after work completes and fail if recovery needs user input; /// `plain` selects text output instead of the full-screen display. pub(super) async fn run( - config_path: Option, - scenario: Option, + config_path: PathBuf, plain: bool, linux_vt: bool, mut output: Output, @@ -883,7 +817,6 @@ pub(super) async fn run( config_path.clone(), Request::Autorun, false, - scenario, tx.clone(), )); let interactive = io::stdin().is_terminal() && io::stdout().is_terminal(); @@ -893,7 +826,6 @@ pub(super) async fn run( None }; let mut shell: Option>> = None; - let mut demo_shell: Option = None; let mut printed_screen = None; loop { for _ in 0..EVENT_CAPACITY { @@ -908,11 +840,7 @@ pub(super) async fn run( output.record(&event)?; let entry = model.event(event); output.announce(&entry, model.serial_verbosity); - if display.is_none() - && shell.is_none() - && demo_shell.is_none() - && log_visible(entry.level, model.verbosity) - { + if display.is_none() && shell.is_none() && log_visible(entry.level, model.verbosity) { println!("{}", entry.plain()); } } @@ -959,19 +887,17 @@ pub(super) async fn run( .is_some_and(|deadline| Instant::now() >= deadline) { model.reboot_at = None; - if scenario.is_none() { - let reboot = model.log( - LogSource::Inst, - LogLevel::Info, - "Rebooting after successful installation".into(), - ); - output.announce(&reboot, model.serial_verbosity); - match systemd::reboot() { - Ok(()) => return Ok(()), - Err(error) => { - let failure = model.fail(format!("Failed to reboot: {error:#}"), false); - output.announce(&failure, model.serial_verbosity); - } + let reboot = model.log( + LogSource::Inst, + LogLevel::Info, + "Rebooting after successful installation".into(), + ); + output.announce(&reboot, model.serial_verbosity); + match systemd::reboot() { + Ok(()) => return Ok(()), + Err(error) => { + let failure = model.fail(format!("Failed to reboot: {error:#}"), false); + output.announce(&failure, model.serial_verbosity); } } } @@ -987,16 +913,9 @@ pub(super) async fn run( } } else { if let Some(display) = &mut display { - display.terminal.draw(|frame| match &demo_shell { - Some(shell) => render_demo_shell(frame, shell), - None => render(frame, &model, scenario.is_some()), - })?; - } else if printed_screen - != Some((model.screen, demo_shell.is_some(), model.verbosity_open)) - { - if let Some(shell) = &demo_shell { - println!("\nSIMULATED SHELL\n{}", shell.text()); - } else if model.verbosity_open { + display.terminal.draw(|frame| render(frame, &model))?; + } else if printed_screen != Some((model.screen, model.verbosity_open)) { + if model.verbosity_open { println!("\nLOG VERBOSITY (display only)"); for (index, level) in VERBOSITY_LEVELS.iter().enumerate() { println!("{}: {level}", index + 1); @@ -1016,7 +935,7 @@ pub(super) async fn run( } println!("Number + Enter: select; S + Enter: shell; Esc: menu; V: verbosity"); } - printed_screen = Some((model.screen, demo_shell.is_some(), model.verbosity_open)); + printed_screen = Some((model.screen, model.verbosity_open)); } for _ in 0..INPUT_EVENTS_PER_FRAME { if !event::poll(Duration::ZERO)? { @@ -1024,23 +943,8 @@ pub(super) async fn run( } let event = event::read()?; if let TerminalEvent::Key(key) = event { - if let Some(shell) = &mut demo_shell { - if shell.key(key) { - demo_shell = None; - printed_screen = None; - } else if key.code == KeyCode::Enter { - printed_screen = None; - } - } else if let Some(action) = - model.key(key, scenario.is_some(), worker.is_some()) - { + if let Some(action) = model.key(key, worker.is_some()) { match action { - Action::Quit => return Ok(()), - Action::Shell if scenario.is_some() => { - demo_shell = Some(DemoShell::default()); - printed_screen = None; - break; - } Action::Shell => { if let Some(display) = &mut display { display.suspend()?; @@ -1063,12 +967,10 @@ pub(super) async fn run( model.reboot_at = None; model.scroll = 0; model.selected = 0; - let recovery_demo = scenario.map(|_| Scenario::Success); worker = Some(spawn_operation( config_path.clone(), request, model.uncertain, - recovery_demo, tx.clone(), )); } @@ -1088,12 +990,6 @@ pub(super) async fn run( ); output.announce(&warning, model.serial_verbosity); } - Action::Reboot | Action::Poweroff if scenario.is_some() => { - model.reboot_at = None; - model.details = "DEMO: Machine power action suppressed".into(); - model.screen = Screen::Success; - model.selected = 0; - } Action::Reboot => { let action = model.log( LogSource::Inst, @@ -1135,12 +1031,8 @@ pub(super) async fn run( if signal.is_none() { return Err(anyhow!("Installer interrupt handler closed")); } - if let Some(shell) = &mut demo_shell { - shell.command.clear(); - } else { - model.screen = if worker.is_some() { Screen::Menu } else { Screen::Recovery }; - model.selected = 0; - } + model.screen = if worker.is_some() { Screen::Menu } else { Screen::Recovery }; + model.selected = 0; } _ = termination.recv() => return Err(anyhow!("Installer stopped; servicing may continue in Trident")), } @@ -1235,15 +1127,8 @@ fn header_height(viewport: Rect) -> u16 { } } -/// Draws the wordmark when space permits, plus status and the demo warning. -fn render_header( - frame: &mut Frame, - area: Rect, - state: &str, - demo: bool, - state_color: Color, - linux_vt: bool, -) { +/// Draws the wordmark when space permits, plus the installer status. +fn render_header(frame: &mut Frame, area: Rect, state: &str, state_color: Color, linux_vt: bool) { let brand = Style::default() .fg(theme_color(COLOR_BRAND, linux_vt)) .add_modifier(Modifier::BOLD); @@ -1259,40 +1144,13 @@ fn render_header( lines.push(Line::default()); } lines.push(Line::from(Span::styled(APPLICATION_NAME, brand))); - let mut status = vec![Span::styled( + lines.push(Line::from(Span::styled( state, Style::default().fg(theme_color(state_color, linux_vt)), - )]; - if demo { - status.push(Span::styled( - " / DEMO: ALL ACTIONS SIMULATED", - Style::default().fg(theme_color(COLOR_BRAND, linux_vt)), - )); - } - lines.push(Line::from(status)); + ))); frame.render_widget(Paragraph::new(lines).alignment(Alignment::Center), area); } -/// Draws a visibly simulated shell without presenting it as a real terminal. -fn render_demo_shell(frame: &mut Frame, shell: &DemoShell) { - frame.render_widget( - Block::default().style(Style::default().bg(COLOR_BACKGROUND)), - frame.area(), - ); - let areas = Layout::vertical([ - Constraint::Length(header_height(frame.area())), - Constraint::Min(4), - ]) - .split(frame.area()); - render_header(frame, areas[0], "SIMULATED SHELL", true, COLOR_BRAND, false); - frame.render_widget( - Paragraph::new(shell.text()) - .block(block("No commands are executed", false)) - .wrap(Wrap { trim: false }), - areas[1], - ); -} - /// Filters and scrolls retained logs, moving the latest result to the end on result screens. fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { let (title, color) = match model.screen { @@ -1346,7 +1204,7 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { } /// Draws the current screen, actions, log controls, and any pending reboot countdown. -fn render(frame: &mut Frame, model: &Model, demo: bool) { +fn render(frame: &mut Frame, model: &Model) { frame.render_widget( Block::default().style(Style::default().bg(theme_color(COLOR_BACKGROUND, model.linux_vt))), frame.area(), @@ -1374,7 +1232,6 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { frame, areas[0], title(model.screen), - demo, match model.screen { Screen::Error => COLOR_ERROR, Screen::Success => COLOR_SUCCESS, @@ -1455,13 +1312,12 @@ fn render(frame: &mut Frame, model: &Model, demo: bool) { "Enter: start Esc: return to recovery".to_owned() } else { format!( - "Arrows: choose Enter: select S: shell V: logs{} Esc: back{}", + "Arrows: choose Enter: select S: shell V: logs{} Esc: back", if model.screen == Screen::Error { " D: details" } else { "" }, - if demo { " Q: quit" } else { "" } ) }; frame.render_widget( @@ -1581,49 +1437,11 @@ mod tests { KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) } - /// Demo commands never reach the host, and `exit` closes the simulation. - #[test] - fn demo_shell_never_executes_commands_and_exit_returns() { - let root = TempDir::new().unwrap(); - let marker = root.path().join("must-not-exist"); - let mut shell = DemoShell::default(); - for c in format!("touch {}", marker.display()).chars() { - assert!(!shell.key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE))); - } - assert!(!shell.key(enter())); - assert!(!marker.exists()); - assert!(shell.text().contains("DEMO: Command not executed")); - for c in "exit".chars() { - assert!(!shell.key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE))); - } - assert!(shell.key(enter())); - } - - /// The demo shell advertises that it cannot execute commands. - #[test] - fn demo_shell_is_visibly_simulated() { - let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal - .draw(|frame| render_demo_shell(frame, &DemoShell::default())) - .unwrap(); - let text = terminal - .backend() - .buffer() - .content - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(text.contains("SIMULATED SHELL"), "{text}"); - assert!(text.contains("Commands are never executed"), "{text}"); - } - /// The full-size header and background keep their intended colors and spacing. #[test] fn wordmark_and_background_use_clear_neutrals_palette_at_80_by_24() { let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal - .draw(|frame| render(frame, &Model::new(), true)) - .unwrap(); + terminal.draw(|frame| render(frame, &Model::new())).unwrap(); let buffer = terminal.backend().buffer(); assert_eq!(buffer.content[0].bg, Color::Rgb(11, 17, 24)); assert_eq!(buffer.content[14 * 80].fg, Color::Rgb(53, 69, 83)); @@ -1652,7 +1470,7 @@ mod tests { .map(|cell| cell.symbol()) .collect::(); assert!(text.contains(APPLICATION_NAME), "{text}"); - assert!(text.contains("ALL ACTIONS SIMULATED"), "{text}"); + assert!(text.contains("INSTALLING"), "{text}"); } /// Changing display verbosity hides entries without deleting retained logs. @@ -1667,33 +1485,17 @@ mod tests { ); assert_eq!(model.verbosity, LevelFilter::Debug); assert!(!log_visible(LogLevel::Trace, model.verbosity)); - model.key( - KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), - true, - false, - ); + model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), false); assert!(model.verbosity_open); - model.key( - KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), - true, - false, - ); - model.key(enter(), true, false); + model.key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), false); + model.key(enter(), false); assert_eq!(model.verbosity, LevelFilter::Trace); assert!(!model.verbosity_open); assert!(log_visible(LogLevel::Trace, model.verbosity)); assert_eq!(model.logs.len(), 2); - model.key( - KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), - true, - false, - ); - model.key( - KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE), - true, - false, - ); - model.key(enter(), true, false); + model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), false); + model.key(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE), false); + model.key(enter(), false); assert_eq!(model.verbosity, LevelFilter::Off); assert!(!log_visible(LogLevel::Error, model.verbosity)); assert_eq!(model.logs.len(), 2); @@ -1726,7 +1528,7 @@ mod tests { model.log(LogSource::Trident, level, text.into()); } let mut terminal = Terminal::new(TestBackend::new(80, 32)).unwrap(); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); let buffer = terminal.backend().buffer(); for (_, message, expected) in records { let row = buffer @@ -1770,7 +1572,7 @@ mod tests { model.log(LogSource::Trident, level, message.into()); } let mut terminal = Terminal::new(TestBackend::new(80, 32)).unwrap(); - terminal.draw(|frame| render(frame, &model, false)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); let buffer = terminal.backend().buffer(); for (message, expected) in [ ("info-vt", Color::White), @@ -1798,7 +1600,7 @@ mod tests { let mut model = Model::new(); model.source = "StreamDisk from file:///media/cosi/payload.cosi".into(); let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); let row = terminal.backend().buffer().content[12 * 80..13 * 80] .iter() .map(|cell| cell.symbol()) @@ -2061,7 +1863,7 @@ mod tests { 00:00 [INST:INFO] Remove the installation media before booting the installed OS." ); let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); let rows = terminal .backend() .buffer() @@ -2088,12 +1890,8 @@ mod tests { rows[result_row][rendered[result_row].find("SUCCESS:").unwrap()].fg, Color::Rgb(93, 200, 148) ); - model.key( - KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), - true, - false, - ); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + model.key(KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), false); + terminal.draw(|frame| render(frame, &model)).unwrap(); let earlier = terminal .backend() .buffer() @@ -2104,7 +1902,7 @@ mod tests { assert!(earlier.contains("Earlier disk action")); model.scroll = 0; model.verbosity = LevelFilter::Off; - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); let text = terminal .backend() .buffer() @@ -2142,31 +1940,23 @@ mod tests { .map(|cell| cell.symbol()) .collect::() }; - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); assert!( visible_text(&terminal).contains("SUCCESS: Installation"), "{}", visible_text(&terminal) ); - model.key( - KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), - true, - false, - ); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + model.key(KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), false); + terminal.draw(|frame| render(frame, &model)).unwrap(); assert!(!visible_text(&terminal).contains("SUCCESS: Installation")); - model.key( - KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE), - true, - false, - ); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + model.key(KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE), false); + terminal.draw(|frame| render(frame, &model)).unwrap(); assert!(visible_text(&terminal).contains("SUCCESS: Installation")); model.screen = Screen::Progress; model.activity = "Scanning media ".repeat(60); model.log(LogSource::Inst, LogLevel::Info, "Latest activity".into()); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); assert!(visible_text(&terminal).contains("Latest activity")); } @@ -2181,7 +1971,7 @@ mod tests { }); assert_eq!(result.result_color, Some(Color::Rgb(255, 107, 115))); let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); let text = terminal .backend() .buffer() @@ -2211,13 +2001,9 @@ mod tests { Color::Rgb(255, 107, 115) ); } - model.key( - KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE), - true, - false, - ); + model.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE), false); assert!(model.error_details_open); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); let text = terminal .backend() .buffer() @@ -2226,7 +2012,7 @@ mod tests { .map(|cell| cell.symbol()) .collect::(); assert!(text.contains("Underlying transport failure")); - model.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), true, false); + model.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), false); assert_eq!(model.screen, Screen::Error); assert!(!model.error_details_open); } @@ -2245,7 +2031,7 @@ mod tests { let mut model = Model::new(); model.screen = Screen::Success; let mut terminal = Terminal::new(TestBackend::new(40, 16)).unwrap(); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); let text = terminal .backend() .buffer() @@ -2266,7 +2052,7 @@ mod tests { for (text, diagnostic) in [("", "mode"), ("mode = 'not-a-mode'", "not-a-mode")] { fs::write(&path, text).unwrap(); let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); - let worker = spawn_operation(Some(path.clone()), Request::Autorun, false, None, tx); + let worker = spawn_operation(path.clone(), Request::Autorun, false, tx); let Event::Error { details, uncertain } = rx.recv().await.unwrap() else { panic!("Invalid settings must fail before preparing a source"); }; @@ -2282,17 +2068,17 @@ mod tests { fn error_continue_and_shell_keep_the_originating_screen() { let mut model = Model::new(); model.fail("Complete diagnostic information".into(), false); - assert!(model.key(enter(), false, false).is_none()); + assert!(model.key(enter(), false).is_none()); assert_eq!(model.screen, Screen::Recovery); - assert_eq!(model.key(enter(), false, false), Some(Action::Shell)); + assert_eq!(model.key(enter(), false), Some(Action::Shell)); assert_eq!(model.screen, Screen::Recovery); assert_eq!(model.details, "Complete diagnostic information"); model.screen = Screen::Success; model.selected = 1; - assert_eq!(model.key(enter(), false, false), Some(Action::Shell)); + assert_eq!(model.key(enter(), false), Some(Action::Shell)); assert_eq!(model.screen, Screen::Success); model.selected = 0; - assert_eq!(model.key(enter(), false, false), Some(Action::Reboot)); + assert_eq!(model.key(enter(), false), Some(Action::Reboot)); } /// Repeated interrupts and power choices cannot shut down an active installation. @@ -2300,18 +2086,18 @@ mod tests { fn active_worker_cannot_reach_a_power_action() { let mut model = Model::new(); let interrupt = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL); - model.key(interrupt, false, true); + model.key(interrupt, true); assert_eq!(model.screen, Screen::Menu); - model.key(interrupt, false, true); + model.key(interrupt, true); assert_eq!(model.screen, Screen::Menu); model.screen = Screen::Recovery; model.selected = 3; - assert_eq!(model.key(enter(), false, true), Some(Action::BlockedPower)); - assert_eq!(model.key(enter(), false, false), Some(Action::Poweroff)); + assert_eq!(model.key(enter(), true), Some(Action::BlockedPower)); + assert_eq!(model.key(enter(), false), Some(Action::Poweroff)); model.screen = Screen::Success; model.selected = 0; - assert_eq!(model.key(enter(), false, true), Some(Action::BlockedPower)); + assert_eq!(model.key(enter(), true), Some(Action::BlockedPower)); } /// Losing the client stream must not make shutdown safe until Trident confirms idleness. @@ -2325,12 +2111,12 @@ mod tests { model.screen = Screen::Recovery; model.selected = 3; assert!(!model.choices().contains(&"Shutdown")); - assert_eq!(model.key(enter(), false, false), Some(Action::BlockedPower)); + assert_eq!(model.key(enter(), false), Some(Action::BlockedPower)); model.event(Event::IdleConfirmed); assert!(!model.uncertain); assert!(model.choices().contains(&"Shutdown")); - assert_eq!(model.key(enter(), false, false), Some(Action::Poweroff)); + assert_eq!(model.key(enter(), false), Some(Action::Poweroff)); model.event(Event::Error { details: "Another stream disconnected".into(), @@ -2344,7 +2130,7 @@ mod tests { }, )))); assert!(!model.uncertain); - assert_eq!(model.key(enter(), false, false), Some(Action::Reboot)); + assert_eq!(model.key(enter(), false), Some(Action::Reboot)); } /// An enqueued completion must be handled before the worker stops blocking power actions. @@ -2376,7 +2162,7 @@ mod tests { model.selected = 3; assert!(!rx.is_empty()); assert_eq!( - model.key(enter(), false, !(rx.is_empty() && worker.is_finished())), + model.key(enter(), !(rx.is_empty() && worker.is_finished())), Some(Action::BlockedPower) ); model.event(rx.recv().await.unwrap()); @@ -2419,14 +2205,14 @@ mod tests { model.selected = 3; assert!(!rx.is_empty()); assert_eq!( - model.key(enter(), false, !(rx.is_empty() && worker.is_finished())), + model.key(enter(), !(rx.is_empty() && worker.is_finished())), Some(Action::BlockedPower) ); model.event(rx.recv().await.unwrap()); assert!(model.uncertain); model.screen = Screen::Recovery; model.selected = 3; - assert_eq!(model.key(enter(), false, false), Some(Action::BlockedPower)); + assert_eq!(model.key(enter(), false), Some(Action::BlockedPower)); worker.await.unwrap().unwrap(); } @@ -2461,14 +2247,14 @@ mod tests { model.screen = Screen::Recovery; model.selected = 3; assert_eq!( - model.key(enter(), false, !(worker.is_finished() && rx.is_empty())), + model.key(enter(), !(worker.is_finished() && rx.is_empty())), Some(Action::BlockedPower) ); model.event(rx.recv().await.unwrap()); assert!(model.uncertain); model.screen = Screen::Recovery; model.selected = 3; - assert_eq!(model.key(enter(), false, false), Some(Action::BlockedPower)); + assert_eq!(model.key(enter(), false), Some(Action::BlockedPower)); worker.await.unwrap().unwrap(); } @@ -2480,7 +2266,7 @@ mod tests { model.screen = screen; model.details = "Diagnostic or result details".into(); let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal.draw(|frame| render(frame, &model, true)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); let text = terminal .backend() .buffer() diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index df2de0f5e7..8e808f5652 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -8,8 +8,8 @@ title: Installer Configuration `trident-installer` reads `/etc/trident/installer.toml` by default. Pass `--config PATH` to use another file. This TOML file controls the **installer**; the [Host Configuration](./Host-Configuration/Host-Configuration.md) is a -separate YAML document describing what Trident installs. A live installation -requires a readable TOML file. `--demo` does not read it. +separate YAML document describing what Trident installs. Installation requires +a readable TOML file. The packaged `/etc/trident/installer.toml` explicitly sets: From de66f705615b36b9b01cc10c3d05dbfc6562e920 Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 13 Nov 2026 14:41:57 -0800 Subject: [PATCH 53/62] refactor: Split installer UI into focused modules Keep runtime orchestration, state, rendering, serial output and theme in documented modules with local tests and narrow visibility. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/ui.rs | 2286 ----------------------------- crates/installer/src/ui/mod.rs | 678 +++++++++ crates/installer/src/ui/model.rs | 804 ++++++++++ crates/installer/src/ui/output.rs | 144 ++ crates/installer/src/ui/render.rs | 715 +++++++++ crates/installer/src/ui/theme.rs | 67 + 6 files changed, 2408 insertions(+), 2286 deletions(-) delete mode 100644 crates/installer/src/ui.rs create mode 100644 crates/installer/src/ui/mod.rs create mode 100644 crates/installer/src/ui/model.rs create mode 100644 crates/installer/src/ui/output.rs create mode 100644 crates/installer/src/ui/render.rs create mode 100644 crates/installer/src/ui/theme.rs diff --git a/crates/installer/src/ui.rs b/crates/installer/src/ui.rs deleted file mode 100644 index c97b152766..0000000000 --- a/crates/installer/src/ui.rs +++ /dev/null @@ -1,2286 +0,0 @@ -use std::{ - borrow::Cow, - collections::VecDeque, - fs::{File, OpenOptions}, - io::{self, IsTerminal, Write}, - path::PathBuf, - time::{Duration, Instant}, -}; - -use anyhow::{anyhow, Context, Error}; -use crossterm::{ - cursor::MoveTo, - event::{self, Event as TerminalEvent, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}, - execute, - terminal::{ - self, Clear as ClearTerminal, ClearType, EnterAlternateScreen, LeaveAlternateScreen, - }, -}; -use log::{error, warn, Level, LevelFilter}; -use ratatui::{ - backend::CrosstermBackend, - layout::{Alignment, Constraint, Layout, Rect}, - style::{Color, Modifier, Style}, - symbols::border::Set as BorderSet, - text::{Line, Span}, - widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Wrap}, - Frame, Terminal, -}; -use tokio::{ - signal::unix::{self, SignalKind}, - sync::mpsc, - task::{self, JoinError, JoinHandle}, - time, -}; - -use osutils::{systemd, terminal as os_terminal}; -use trident_client::{LogLevel, ServicingEvent}; - -use crate::{ - client::{self, Completion, Event}, - config::{Config, SerialVerbosity}, - source::{self, Request}, - APPLICATION_NAME, INSTALLER_LOG_TX, -}; - -const FRAME_INTERVAL: Duration = Duration::from_millis(100); -const INPUT_EVENTS_PER_FRAME: usize = 256; -const REBOOT_DELAY: Duration = Duration::from_secs(5); -const EVENT_CAPACITY: usize = 64; -const LOG_CAPACITY: usize = 500; -const DISPLAY_LOG_CHARACTERS: usize = 4096; -const COLOR_BACKGROUND: Color = Color::Rgb(11, 17, 24); -const COLOR_FRAME: Color = Color::Rgb(53, 69, 83); -const COLOR_BRAND: Color = Color::Rgb(75, 216, 220); -const COLOR_INST: Color = Color::Rgb(171, 186, 196); -const COLOR_TRIDENT: Color = Color::Rgb(114, 185, 165); -const COLOR_ERROR: Color = Color::Rgb(255, 107, 115); -const COLOR_WARN: Color = Color::Rgb(245, 182, 93); -const COLOR_INFO: Color = Color::Rgb(229, 234, 236); -const COLOR_DEBUG: Color = Color::Rgb(167, 180, 189); -const COLOR_TRACE: Color = Color::Rgb(99, 115, 129); -const COLOR_SUCCESS: Color = Color::Rgb(93, 200, 148); -// Linux VT maps RGB SGR values into 16 colours, so give each UI role its own ANSI slot. -const VT_PALETTE: &[(u8, Color, Color)] = &[ - (0, COLOR_BACKGROUND, Color::Black), - (1, COLOR_ERROR, Color::Red), - (2, COLOR_TRIDENT, Color::Green), - (3, COLOR_WARN, Color::Yellow), - (4, COLOR_FRAME, Color::Blue), - (5, COLOR_INST, Color::Magenta), - (6, COLOR_BRAND, Color::Cyan), - (7, COLOR_DEBUG, Color::Gray), - (8, COLOR_TRACE, Color::DarkGray), - (10, COLOR_SUCCESS, Color::LightGreen), - (15, COLOR_INFO, Color::White), -]; - -/// Maps known theme RGB colors to distinct ANSI roles on Linux VTs. -/// Other colors and non-VT terminals retain the original value. -fn theme_color(color: Color, linux_vt: bool) -> Color { - if !linux_vt { - return color; - } - VT_PALETTE - .iter() - .find(|(_, rgb, _)| *rgb == color) - .map(|(_, _, ansi)| *ansi) - .unwrap_or(color) -} - -/// Builds the Linux VT palette entries, rejecting any theme slot without an RGB value. -fn vt_rgb_palette() -> Result, io::Error> { - VT_PALETTE - .iter() - .map(|(slot, color, _)| match color { - Color::Rgb(red, green, blue) => Ok((*slot, *red, *green, *blue)), - other => Err(io::Error::new( - io::ErrorKind::InvalidInput, - format!("Linux VT palette requires RGB values, got {other:?}"), - )), - }) - .collect() -} -const WORDMARK_HEADER_HEIGHT: u16 = 12; -const VERBOSITY_LEVELS: [LevelFilter; 6] = [ - LevelFilter::Off, - LevelFilter::Error, - LevelFilter::Warn, - LevelFilter::Info, - LevelFilter::Debug, - LevelFilter::Trace, -]; -const COMPACT_HEADER_HEIGHT: u16 = 2; -const MIN_WORDMARK_TERMINAL_HEIGHT: u16 = 24; -const TRIDENT_WORDMARK: [&str; 8] = [ - r#"88888888888 d8b 888 888"#, - r#" 888 Y8P 888 888"#, - r#" 888 888 888"#, - r#" 888 888d888 888 .d88888 .d88b. 88888b. 888888"#, - r#" 888 888P" 888 d88" 888 d8P Y8b 888 "88b 888"#, - r#" 888 888 888 888 888 88888888 888 888 888"#, - r#" 888 888 888 Y88b 888 Y8b. 888 888 Y88b."#, - r#" 888 888 888 "Y88888 "Y8888 888 888 "Y888"#, -]; -const ASCII_BORDER: BorderSet = BorderSet { - top_left: "+", - top_right: "+", - bottom_left: "+", - bottom_right: "+", - vertical_left: "|", - vertical_right: "|", - horizontal_top: "-", - horizontal_bottom: "-", -}; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum Screen { - Progress, - Error, - Recovery, - Success, - StreamUrl, - HostConfigUrl, - Menu, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum LogSource { - Inst, - Trident, -} - -impl LogSource { - /// Returns the source tag used in both displayed and mirrored diagnostics. - fn label(self) -> &'static str { - match self { - Self::Inst => "INST", - Self::Trident => "TRIDENT", - } - } - - /// Uses the source's theme color or its Linux VT equivalent. - fn color(self, linux_vt: bool) -> Color { - theme_color( - match self { - Self::Inst => COLOR_INST, - Self::Trident => COLOR_TRIDENT, - }, - linux_vt, - ) - } -} - -#[derive(Debug, Clone)] -struct LogEntry { - elapsed: Duration, - source: LogSource, - level: LogLevel, - message: String, - result_color: Option, -} - -impl LogEntry { - /// Formats elapsed time as a minutes-and-seconds prefix without wrapping at an hour. - fn prefix(&self) -> String { - let seconds = self.elapsed.as_secs(); - format!("{:02}:{:02} [", seconds / 60, seconds % 60) - } - - /// Prefixes each line of a diagnostic for plain output and serial mirrors. - fn plain(&self) -> String { - let prefix = format!( - "{}{}:{}] ", - self.prefix(), - self.source.label(), - level_label(self.level) - ); - display_text(&self.message) - .split('\n') - .map(|line| format!("{prefix}{line}")) - .collect::>() - .join("\n") - } - - /// Styles the source and severity separately, highlighting result records as a whole. - fn styled(&self, linux_vt: bool) -> Line<'_> { - let level = Style::default().fg(theme_color( - self.result_color.unwrap_or(log_color(self.level)), - linux_vt, - )); - let surrounding = self - .result_color - .map(|color| Style::default().fg(theme_color(color, linux_vt))) - .unwrap_or_default(); - Line::from(vec![ - Span::styled(self.prefix(), surrounding), - Span::styled( - self.source.label(), - Style::default().fg(self.source.color(linux_vt)), - ), - Span::styled(":", surrounding), - Span::styled(level_label(self.level), level.add_modifier(Modifier::BOLD)), - Span::styled("] ", surrounding), - Span::styled(self.message.as_str(), level), - ]) - } -} - -#[derive(Debug)] -struct Model { - screen: Screen, - source: String, - activity: String, - details: String, - logs: VecDeque, - verbosity: LevelFilter, - serial_verbosity: LevelFilter, - verbosity_open: bool, - verbosity_selected: usize, - selected: usize, - scroll: u16, - url: String, - uncertain: bool, - reboot: bool, - reboot_at: Option, - started: Instant, - failed: bool, - error_details_open: bool, - linux_vt: bool, -} - -impl Model { - /// Starts on the progress screen with default display and serial log levels. - fn new() -> Self { - Self { - screen: Screen::Progress, - source: String::new(), - activity: "Preparing installer".into(), - details: String::new(), - logs: VecDeque::new(), - verbosity: LevelFilter::Debug, - serial_verbosity: SerialVerbosity::Debug.filter(), - verbosity_open: false, - verbosity_selected: 4, - selected: 0, - scroll: 0, - url: String::new(), - uncertain: false, - reboot: true, - reboot_at: None, - started: Instant::now(), - failed: false, - error_details_open: false, - linux_vt: false, - } - } - - /// Records a failure, preserving any earlier uncertainty and cancelling automatic reboot. - fn fail(&mut self, details: String, uncertain: bool) -> LogEntry { - self.details = details.clone(); - self.uncertain |= uncertain; - self.reboot_at = None; - self.failed = true; - self.scroll = 0; - self.selected = 0; - self.screen = Screen::Error; - self.error_details_open = false; - self.result_log(LogLevel::Error, format!("FAILURE: {details}"), COLOR_ERROR) - } - - /// Retains a bounded, single-line display copy while returning the full diagnostic. - fn log(&mut self, source: LogSource, level: LogLevel, message: String) -> LogEntry { - let entry = LogEntry { - elapsed: self.started.elapsed(), - source, - level, - message: display_text(&message), - result_color: None, - }; - let mut characters = entry.message.chars(); - let mut displayed = characters - .by_ref() - .take(DISPLAY_LOG_CHARACTERS) - .collect::() - .replace('\n', " "); - if characters.next().is_some() { - displayed.push_str(" [Display shortened; full record preserved in diagnostic log]"); - } - if self.logs.len() == LOG_CAPACITY { - self.logs.pop_front(); - } - self.logs.push_back(LogEntry { - message: displayed, - ..entry.clone() - }); - entry - } - - /// Marks an installer result record with a color even when logs are filtered. - fn result_log(&mut self, level: LogLevel, message: String, color: Color) -> LogEntry { - let mut entry = self.log(LogSource::Inst, level, message); - entry.result_color = Some(color); - if let Some(last) = self.logs.back_mut() { - last.result_color = Some(color); - } - entry - } - - /// Applies a worker event to the current screen and returns its diagnostic record. - fn event(&mut self, event: Event) -> LogEntry { - match event { - Event::InstallerLog { level, message } => self.log( - LogSource::Inst, - match level { - Level::Error => LogLevel::Error, - Level::Warn => LogLevel::Warn, - Level::Info => LogLevel::Info, - Level::Debug => LogLevel::Debug, - Level::Trace => LogLevel::Trace, - }, - message, - ), - Event::Preparing(activity) => { - self.activity = activity.clone(); - self.log(LogSource::Inst, LogLevel::Info, activity) - } - Event::Prepared { - description, - reboot, - serial_verbosity, - } => { - self.source = description.clone(); - self.reboot = reboot; - self.serial_verbosity = serial_verbosity.filter(); - self.log(LogSource::Inst, LogLevel::Info, description) - } - Event::IdleConfirmed => { - self.uncertain = false; - self.log( - LogSource::Inst, - LogLevel::Info, - "Trident confirmed that no servicing is active".into(), - ) - } - Event::Error { details, uncertain } => self.fail(details, uncertain), - Event::Response(response) => match response { - ServicingEvent::Started { .. } => { - self.activity = "Servicing started".into(); - self.log(LogSource::Inst, LogLevel::Info, self.activity.clone()) - } - ServicingEvent::Log(log) => { - let level = log.level; - if level == LogLevel::Info { - self.activity = display_text(&log.message); - } - self.log(LogSource::Trident, level, log.message) - } - ServicingEvent::Completed(completed) => match client::completion(completed) { - Ok(Completion::Success { - reboot_required, - performed, - }) => { - self.uncertain = false; - self.failed = false; - self.screen = Screen::Success; - self.selected = 0; - self.details = if performed { - "Installation completed successfully.\nRemove the installation media before booting the installed OS.".into() - } else { - "Trident reported that no servicing was performed.".into() - }; - self.reboot_at = (self.reboot && reboot_required && performed) - .then(|| Instant::now() + REBOOT_DELAY); - self.result_log( - LogLevel::Info, - format!("SUCCESS: {}", self.details), - COLOR_SUCCESS, - ) - } - Ok(Completion::Failure(details)) => { - self.uncertain = false; - self.fail(details, false) - } - Err(error) => { - let details = format!("{error:#}"); - self.fail(details, true) - } - }, - _ => self.fail( - "Unsupported Trident servicing event; installation outcome is unknown".into(), - true, - ), - }, - } - } - - /// Lists actions in keyboard-selection order for the current screen. - fn choices(&self) -> Vec<&'static str> { - match self.screen { - Screen::Recovery => { - let mut actions = vec![ - "Shell", - "Stream COSI from URL", - "Install from Host Configuration URL", - ]; - if !self.uncertain { - actions.push("Shutdown"); - } - actions - } - Screen::Success => vec!["Reboot", "Shell"], - Screen::Menu => vec!["Return to installation", "Shell"], - Screen::Error => vec!["Continue to recovery", "Shell"], - _ => Vec::new(), - } - } - - /// Shows the log panel on result screens unless full error details are open. - fn showing_logs(&self) -> bool { - matches!( - self.screen, - Screen::Progress | Screen::Menu | Screen::Success - ) || self.screen == Screen::Error && !self.error_details_open - } - - /// Updates navigation for a key press and returns an action only when one is requested. - /// URL entry and the verbosity picker consume keys before screen-level shortcuts. - fn key(&mut self, key: KeyEvent, worker_active: bool) -> Option { - if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { - return None; - } - if self.verbosity_open { - match key.code { - KeyCode::Esc => self.verbosity_open = false, - KeyCode::Up | KeyCode::BackTab => { - self.verbosity_selected = (self.verbosity_selected + VERBOSITY_LEVELS.len() - - 1) - % VERBOSITY_LEVELS.len(); - } - KeyCode::Down | KeyCode::Tab => { - self.verbosity_selected = - (self.verbosity_selected + 1) % VERBOSITY_LEVELS.len(); - } - KeyCode::Char(digit) if digit.is_ascii_digit() => { - if let Some(index) = digit.to_digit(10).and_then(|value| value.checked_sub(1)) { - if (index as usize) < VERBOSITY_LEVELS.len() { - self.verbosity_selected = index as usize; - } - } - } - KeyCode::Enter => { - self.verbosity = VERBOSITY_LEVELS[self.verbosity_selected]; - self.verbosity_open = false; - self.scroll = 0; - } - _ => {} - } - return None; - } - if matches!(self.screen, Screen::StreamUrl | Screen::HostConfigUrl) { - return match key.code { - KeyCode::Esc => { - self.screen = Screen::Recovery; - None - } - KeyCode::Backspace => { - self.url.pop(); - None - } - KeyCode::Char(c) if !c.is_control() => { - self.url.push(c); - None - } - KeyCode::Enter => match source::remote_url(self.url.trim()) { - Ok(url) => Some(Action::Start(if self.screen == Screen::StreamUrl { - Request::Stream(url) - } else { - Request::HostConfiguration(url.to_string()) - })), - Err(error) => { - self.fail(format!("{error:#}"), false); - None - } - }, - _ => None, - }; - } - if self.screen == Screen::Error && matches!(key.code, KeyCode::Char('d' | 'D')) { - self.error_details_open = !self.error_details_open; - self.scroll = 0; - return None; - } - if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') { - self.screen = if worker_active || self.screen == Screen::Progress { - Screen::Menu - } else { - Screen::Recovery - }; - self.selected = 0; - return None; - } - let count = self.choices().len(); - match key.code { - KeyCode::Char(digit) if digit.is_ascii_digit() => { - if let Some(index) = digit.to_digit(10).and_then(|value| value.checked_sub(1)) { - if (index as usize) < count { - self.selected = index as usize; - } - } - } - KeyCode::Char('s') => return Some(Action::Shell), - KeyCode::Char('v' | 'V') => { - self.verbosity_selected = VERBOSITY_LEVELS - .iter() - .position(|level| *level == self.verbosity) - .expect("invariant: all log levels are present in the verbosity picker"); - self.verbosity_open = true; - return None; - } - KeyCode::PageUp => { - self.scroll = if self.showing_logs() { - self.scroll.saturating_add(5) - } else { - self.scroll.saturating_sub(5) - }; - return None; - } - KeyCode::PageDown => { - self.scroll = if self.showing_logs() { - self.scroll.saturating_sub(5) - } else { - self.scroll.saturating_add(5) - }; - return None; - } - KeyCode::Esc => { - if self.screen == Screen::Error && self.error_details_open { - self.error_details_open = false; - self.scroll = 0; - return None; - } - self.screen = match self.screen { - Screen::Progress => Screen::Menu, - Screen::Menu => Screen::Progress, - Screen::Error => Screen::Recovery, - other => other, - }; - self.selected = 0; - return None; - } - _ => {} - } - if count == 0 { - return None; - } - match key.code { - KeyCode::Up | KeyCode::BackTab => self.selected = (self.selected + count - 1) % count, - KeyCode::Down | KeyCode::Tab => self.selected = (self.selected + 1) % count, - KeyCode::Enter => { - let action = match (self.screen, self.selected) { - (Screen::Recovery, 0) - | (Screen::Success, 1) - | (Screen::Menu, 1) - | (Screen::Error, 1) => Some(Action::Shell), - (Screen::Recovery, 1) => { - self.url.clear(); - self.screen = Screen::StreamUrl; - None - } - (Screen::Recovery, 2) => { - self.url.clear(); - self.screen = Screen::HostConfigUrl; - None - } - (Screen::Recovery, 3) => Some(Action::Poweroff), - (Screen::Success, 0) => Some(Action::Reboot), - (Screen::Menu, 0) => { - self.screen = Screen::Progress; - None - } - (Screen::Error, 0) => { - self.screen = Screen::Recovery; - self.selected = 0; - None - } - _ => None, - }; - return if (worker_active || self.uncertain) - && matches!(action, Some(Action::Reboot | Action::Poweroff)) - { - Some(Action::BlockedPower) - } else { - action - }; - } - _ => {} - } - None - } -} - -#[derive(Debug, PartialEq, Eq)] -enum Action { - Start(Request), - Shell, - BlockedPower, - Reboot, - Poweroff, -} - -pub(super) struct Output { - pub log: Option, - pub mirrors: Vec<(PathBuf, File)>, -} - -impl Output { - /// Persists the complete event in the diagnostic file, if one is configured. - fn record(&mut self, event: &Event) -> Result<(), Error> { - if let Some(log) = &mut self.log { - writeln!(log, "{event:#?}").context("Failed to preserve installer diagnostics")?; - log.flush()?; - } - Ok(()) - } - - /// Sends visible records to serial mirrors, dropping mirrors that fail to write. - fn announce(&mut self, entry: &LogEntry, verbosity: LevelFilter) { - if !log_visible(entry.level, verbosity) { - return; - } - self.mirrors.retain_mut(|(path, mirror)| { - if let Err(error) = writeln!(mirror, "{}", entry.plain()) { - warn!( - "Stopped mirroring installer status to '{}': {error}", - path.display() - ); - return false; - } - true - }); - } -} - -struct Display { - terminal: Terminal>, - active: bool, - linux_vt: bool, -} - -impl Display { - /// Creates a terminal display and enters its alternate screen. - fn new(linux_vt: bool) -> Result { - let mut display = Self { - terminal: Terminal::new(CrosstermBackend::new(io::stdout()))?, - active: false, - linux_vt, - }; - display.resume()?; - Ok(display) - } - - /// Restores raw-mode drawing and the VT palette after a shell exits. - fn resume(&mut self) -> Result<(), Error> { - terminal::enable_raw_mode()?; - self.active = true; - if self.linux_vt { - os_terminal::set_vt_palette(&mut io::stdout(), &vt_rgb_palette()?)?; - } - execute!(self.terminal.backend_mut(), EnterAlternateScreen)?; - self.terminal.clear()?; - Ok(()) - } - - /// Leaves raw mode and the alternate screen so an interactive shell can use the terminal. - /// On Linux VTs, also clears the old UI and restores the original palette. - fn suspend(&mut self) -> Result<(), Error> { - let restore = (|| -> Result<(), Error> { - terminal::disable_raw_mode()?; - execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; - if self.linux_vt { - // Linux VT leaves the old TUI visible when exiting its alternate screen. - execute!( - self.terminal.backend_mut(), - ClearTerminal(ClearType::All), - MoveTo(0, 0) - )?; - } - self.terminal.show_cursor()?; - Ok(()) - })(); - let palette = if self.linux_vt { - os_terminal::reset_vt_palette(&mut io::stdout()) - } else { - Ok(()) - }; - self.active = false; - restore?; - palette?; - Ok(()) - } -} - -impl Drop for Display { - /// Attempts terminal restoration if the display was not explicitly suspended. - fn drop(&mut self) { - if self.active { - if let Err(error) = self.suspend() { - error!("Failed to restore terminal: {error:#}"); - } - } - } -} - -/// Reloads live configuration for each installation attempt. -/// Configuration read errors are reported as events rather than worker failures. -fn spawn_operation( - config_path: PathBuf, - request: Request, - uncertain: bool, - tx: mpsc::Sender, -) -> JoinHandle> { - task::spawn(async move { - match Config::read(config_path) { - Ok(settings) => client::execute(settings, request, uncertain, tx).await, - Err(error) => { - client::send( - &tx, - Event::Error { - details: format!("{error:#}"), - uncertain, - }, - ) - .await - } - } - }) -} - -/// Reports shell failures without changing the result of an independent installation. -fn shell_result_event(result: Result, JoinError>) -> Option { - match result { - Ok(Ok(())) => None, - Ok(Err(error)) => Some(Event::InstallerLog { - level: Level::Warn, - message: format!("Recovery shell failed: {error:#}"), - }), - Err(error) => Some(Event::InstallerLog { - level: Level::Warn, - message: format!("Recovery shell task terminated: {error}"), - }), - } -} - -/// Sanitizes received diagnostics before persistence, display, or serial mirroring. -fn redact_event(mut event: Event) -> Event { - match &mut event { - Event::InstallerLog { message, .. } | Event::Preparing(message) => { - *message = source::redact_message_urls(message); - } - Event::Prepared { description, .. } => { - *description = source::redact_message_urls(description); - } - Event::Error { details, .. } => { - *details = source::redact_message_urls(details); - } - Event::Response(ServicingEvent::Log(record)) => { - record.message = source::redact_message_urls(&record.message); - } - Event::Response(ServicingEvent::Completed(Err(error))) => { - error.message = source::redact_message_urls(&error.message); - error.error_message = source::redact_message_urls(&error.error_message); - } - _ => {} - } - event -} - -/// Runs the installer event loop, starting autorun and offering recovery on a terminal. -/// Noninteractive runs return after work completes and fail if recovery needs user input; -/// `plain` selects text output instead of the full-screen display. -pub(super) async fn run( - config_path: PathBuf, - plain: bool, - linux_vt: bool, - mut output: Output, -) -> Result<(), Error> { - let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); - let (log_tx, mut log_rx) = mpsc::unbounded_channel(); - INSTALLER_LOG_TX - .set(log_tx) - .map_err(|_| anyhow!("Installer log receiver already initialized"))?; - let mut interrupts = unix::signal(SignalKind::interrupt())?; - let mut termination = unix::signal(SignalKind::terminate())?; - let mut model = Model::new(); - model.linux_vt = linux_vt; - let mut worker = Some(spawn_operation( - config_path.clone(), - Request::Autorun, - false, - tx.clone(), - )); - let interactive = io::stdin().is_terminal() && io::stdout().is_terminal(); - let mut display = if interactive && !plain { - Some(Display::new(linux_vt)?) - } else { - None - }; - let mut shell: Option>> = None; - let mut printed_screen = None; - loop { - for _ in 0..EVENT_CAPACITY { - let event = match rx.try_recv() { - Ok(event) => event, - Err(_) => match log_rx.try_recv() { - Ok(event) => event, - Err(_) => break, - }, - }; - let event = redact_event(event); - output.record(&event)?; - let entry = model.event(event); - output.announce(&entry, model.serial_verbosity); - if display.is_none() && shell.is_none() && log_visible(entry.level, model.verbosity) { - println!("{}", entry.plain()); - } - } - // A finished worker cannot enqueue a terminal event after the queue check. - if worker.as_ref().is_some_and(JoinHandle::is_finished) && rx.is_empty() { - if let Some(finished) = worker.take() { - let failure = match finished.await { - Ok(Ok(())) => None, - Ok(Err(error)) => { - Some(model.fail(format!("Installer worker failed: {error:#}"), true)) - } - Err(error) => { - Some(model.fail(format!("Installer worker terminated: {error}"), true)) - } - }; - if let Some(failure) = failure { - output.announce(&failure, model.serial_verbosity); - } - } - } - if shell.as_ref().is_some_and(JoinHandle::is_finished) { - if let Some(finished) = shell.take() { - if let Some(event) = shell_result_event(finished.await) { - let event = redact_event(event); - output.record(&event)?; - let warning = model.event(event); - output.announce(&warning, model.serial_verbosity); - } - } - if let Some(display) = &mut display { - display.resume()?; - } - printed_screen = None; - } - if shell.is_some() { - tokio::select! { - _ = time::sleep(FRAME_INTERVAL) => {} - _ = termination.recv() => return Err(anyhow!("Installer stopped; servicing may continue in Trident")), - } - continue; - } - if model - .reboot_at - .is_some_and(|deadline| Instant::now() >= deadline) - { - model.reboot_at = None; - let reboot = model.log( - LogSource::Inst, - LogLevel::Info, - "Rebooting after successful installation".into(), - ); - output.announce(&reboot, model.serial_verbosity); - match systemd::reboot() { - Ok(()) => return Ok(()), - Err(error) => { - let failure = model.fail(format!("Failed to reboot: {error:#}"), false); - output.announce(&failure, model.serial_verbosity); - } - } - } - if !interactive { - if worker.is_none() && rx.is_empty() && log_rx.is_empty() && model.reboot_at.is_none() { - if model.failed { - return Err(anyhow!( - "{}\nRecovery requires an interactive terminal.", - model.details - )); - } - return Ok(()); - } - } else { - if let Some(display) = &mut display { - display.terminal.draw(|frame| render(frame, &model))?; - } else if printed_screen != Some((model.screen, model.verbosity_open)) { - if model.verbosity_open { - println!("\nLOG VERBOSITY (display only)"); - for (index, level) in VERBOSITY_LEVELS.iter().enumerate() { - println!("{}: {level}", index + 1); - } - println!("Number + Enter: apply; Esc: cancel"); - } else { - println!( - "\n{}\n{}", - title(model.screen), - display_text(&model.details) - ); - for (index, choice) in model.choices().iter().enumerate() { - println!("{}: {choice}", index + 1); - } - if matches!(model.screen, Screen::StreamUrl | Screen::HostConfigUrl) { - println!("Enter an http:// or https:// URL, then press Enter"); - } - println!("Number + Enter: select; S + Enter: shell; Esc: menu; V: verbosity"); - } - printed_screen = Some((model.screen, model.verbosity_open)); - } - for _ in 0..INPUT_EVENTS_PER_FRAME { - if !event::poll(Duration::ZERO)? { - break; - } - let event = event::read()?; - if let TerminalEvent::Key(key) = event { - if let Some(action) = model.key(key, worker.is_some()) { - match action { - Action::Shell => { - if let Some(display) = &mut display { - display.suspend()?; - } - println!("\nShell: type exit to return. Installation continues; automatic reboot waits."); - let tty = - OpenOptions::new().read(true).write(true).open("/dev/tty")?; - shell = - Some(task::spawn_blocking(move || os_terminal::shell(&tty))); - break; - } - Action::Start(request) => { - if worker.is_some() { - let failure = model.fail("An installer operation is still active. Wait for it to finish.".into(), true); - output.announce(&failure, model.serial_verbosity); - } else { - model.screen = Screen::Progress; - model.activity = "Preparing installation".into(); - model.started = Instant::now(); - model.reboot_at = None; - model.scroll = 0; - model.selected = 0; - worker = Some(spawn_operation( - config_path.clone(), - request, - model.uncertain, - tx.clone(), - )); - } - } - Action::BlockedPower => { - model.screen = if worker.is_some() { - Screen::Menu - } else { - Screen::Recovery - }; - model.selected = 0; - model.activity = "Servicing may still be active; power actions are unavailable until Trident confirms it is idle".into(); - let warning = model.log( - LogSource::Inst, - LogLevel::Warn, - model.activity.clone(), - ); - output.announce(&warning, model.serial_verbosity); - } - Action::Reboot => { - let action = model.log( - LogSource::Inst, - LogLevel::Info, - "Reboot requested".into(), - ); - output.announce(&action, model.serial_verbosity); - if let Err(error) = systemd::reboot() { - let failure = - model.fail(format!("Failed to reboot: {error:#}"), false); - output.announce(&failure, model.serial_verbosity); - } else { - return Ok(()); - } - } - Action::Poweroff => { - let action = model.log( - LogSource::Inst, - LogLevel::Info, - "Shutdown requested".into(), - ); - output.announce(&action, model.serial_verbosity); - if let Err(error) = systemd::poweroff() { - let failure = model - .fail(format!("Failed to shut down: {error:#}"), false); - output.announce(&failure, model.serial_verbosity); - } else { - return Ok(()); - } - } - } - } - } - } - } - tokio::select! { - _ = time::sleep(FRAME_INTERVAL) => {} - signal = interrupts.recv() => { - if signal.is_none() { - return Err(anyhow!("Installer interrupt handler closed")); - } - model.screen = if worker.is_some() { Screen::Menu } else { Screen::Recovery }; - model.selected = 0; - } - _ = termination.recv() => return Err(anyhow!("Installer stopped; servicing may continue in Trident")), - } - } -} - -/// Removes terminal control characters while retaining newlines and expanding tabs. -fn display_text(text: &str) -> String { - text.chars() - .filter(|c| !c.is_control() || *c == '\n' || *c == '\t') - .collect::() - .replace('\t', " ") -} - -/// Compares a Trident severity against the chosen display filter. -fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { - let level = match level { - LogLevel::Error => Level::Error, - LogLevel::Warn | LogLevel::Unspecified | LogLevel::Unknown(_) => Level::Warn, - LogLevel::Info => Level::Info, - LogLevel::Debug => Level::Debug, - LogLevel::Trace => Level::Trace, - _ => Level::Warn, - }; - level.to_level_filter() <= verbosity -} - -/// Preserves the uppercase severity names used by TUI and serial records. -fn level_label(level: LogLevel) -> Cow<'static, str> { - match level { - LogLevel::Error => Cow::Borrowed("ERROR"), - LogLevel::Warn => Cow::Borrowed("WARN"), - LogLevel::Info => Cow::Borrowed("INFO"), - LogLevel::Debug => Cow::Borrowed("DEBUG"), - LogLevel::Trace => Cow::Borrowed("TRACE"), - LogLevel::Unspecified => Cow::Borrowed("UNSPECIFIED"), - LogLevel::Unknown(value) => Cow::Owned(format!("UNKNOWN({value})")), - _ => Cow::Borrowed("UNSUPPORTED"), - } -} - -/// Selects the severity color for Trident and installer records. -fn log_color(level: LogLevel) -> Color { - match level { - LogLevel::Error => COLOR_ERROR, - LogLevel::Warn | LogLevel::Unspecified | LogLevel::Unknown(_) => COLOR_WARN, - LogLevel::Info => COLOR_INFO, - LogLevel::Debug => COLOR_DEBUG, - LogLevel::Trace => COLOR_TRACE, - _ => COLOR_WARN, - } -} - -/// Provides the short status heading for each installer screen. -fn title(screen: Screen) -> &'static str { - match screen { - Screen::Progress => "INSTALLING", - Screen::Error => "INSTALLATION ERROR", - Screen::Recovery => "RECOVERY", - Screen::Success => "INSTALLATION COMPLETE", - Screen::StreamUrl => "STREAM COSI FROM URL", - Screen::HostConfigUrl => "INSTALL FROM HOST CONFIGURATION URL", - Screen::Menu => "INSTALLER MENU", - } -} - -/// Gives panels a consistent ASCII border and VT-aware background. -fn block(title: &str, linux_vt: bool) -> Block<'_> { - Block::default() - .borders(Borders::ALL) - .border_set(ASCII_BORDER) - .style(Style::default().bg(theme_color(COLOR_BACKGROUND, linux_vt))) - .border_style(Style::default().fg(theme_color(COLOR_FRAME, linux_vt))) - .title(title) -} - -/// Finds the widest wordmark row for layout and centering. -fn wordmark_width() -> usize { - TRIDENT_WORDMARK - .iter() - .fold(0, |width, row| width.max(row.len())) -} - -/// Reserves wordmark space only when both the terminal width and height fit it. -fn header_height(viewport: Rect) -> u16 { - if usize::from(viewport.width) >= wordmark_width() - && viewport.height >= MIN_WORDMARK_TERMINAL_HEIGHT - { - WORDMARK_HEADER_HEIGHT - } else { - COMPACT_HEADER_HEIGHT - } -} - -/// Draws the wordmark when space permits, plus the installer status. -fn render_header(frame: &mut Frame, area: Rect, state: &str, state_color: Color, linux_vt: bool) { - let brand = Style::default() - .fg(theme_color(COLOR_BRAND, linux_vt)) - .add_modifier(Modifier::BOLD); - let mut lines = Vec::new(); - if area.height >= WORDMARK_HEADER_HEIGHT { - lines.push(Line::default()); - let width = wordmark_width(); - lines.extend( - TRIDENT_WORDMARK - .iter() - .map(|row| Line::from(Span::styled(format!("{row: Paragraph<'_> { - let (title, color) = match model.screen { - Screen::Success => ("SUCCESS - Installation complete", COLOR_SUCCESS), - Screen::Error => ("FAILURE - Installation stopped", COLOR_ERROR), - _ => ("Activity / logs", COLOR_FRAME), - }; - let mut lines = Vec::new(); - let header_lines = if matches!(model.screen, Screen::Progress | Screen::Menu) { - 2 - } else { - 0 - }; - if header_lines > 0 { - lines.push(Line::from(Span::styled( - model.activity.clone(), - Style::default().fg(theme_color(COLOR_BRAND, model.linux_vt)), - ))); - lines.push(Line::default()); - } - let mut logs = model - .logs - .iter() - .filter(|entry| entry.result_color.is_some() || log_visible(entry.level, model.verbosity)) - .collect::>(); - if matches!(model.screen, Screen::Success | Screen::Error) { - if let Some(index) = logs.iter().rposition(|entry| entry.result_color.is_some()) { - let result = logs.remove(index); - logs.push(result); - } - } - lines.extend(logs.into_iter().map(|entry| entry.styled(model.linux_vt))); - let block = Block::default() - .borders(Borders::ALL) - .border_set(ASCII_BORDER) - .style(Style::default().bg(theme_color(COLOR_BACKGROUND, model.linux_vt))) - .border_style(Style::default().fg(theme_color(color, model.linux_vt))) - .title(Span::styled( - title, - Style::default().fg(theme_color(color, model.linux_vt)), - )); - let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false }); - let visible_rows = usize::from(area.height.saturating_sub(2)); - let total_rows = paragraph.line_count(area.width.saturating_sub(2).max(1)); - let offset = total_rows - .saturating_sub(visible_rows) - .saturating_sub(usize::from(model.scroll)); - paragraph - .block(block) - .scroll((u16::try_from(offset).unwrap_or(u16::MAX), 0)) -} - -/// Draws the current screen, actions, log controls, and any pending reboot countdown. -fn render(frame: &mut Frame, model: &Model) { - frame.render_widget( - Block::default().style(Style::default().bg(theme_color(COLOR_BACKGROUND, model.linux_vt))), - frame.area(), - ); - let choices = model.choices(); - let action_height = if choices.is_empty() { - 0 - } else { - u16::try_from(choices.len()) - .expect("invariant: static action lists have at most four items") - + 2 - }; - let areas = Layout::vertical([ - Constraint::Length(header_height(frame.area())), - Constraint::Length(match model.screen { - Screen::Recovery => 0, - _ => 2, - }), - Constraint::Min(3), - Constraint::Length(action_height), - Constraint::Length(2), - ]) - .split(frame.area()); - render_header( - frame, - areas[0], - title(model.screen), - match model.screen { - Screen::Error => COLOR_ERROR, - Screen::Success => COLOR_SUCCESS, - _ => COLOR_BRAND, - }, - model.linux_vt, - ); - if areas[1].width > 22 { - let operation = - Layout::horizontal([Constraint::Min(0), Constraint::Length(21)]).split(areas[1]); - frame.render_widget( - Paragraph::new(display_text(&model.source)).wrap(Wrap { trim: false }), - operation[0], - ); - frame.render_widget( - Paragraph::new(format!("Logs: {} [V]", model.verbosity)) - .alignment(Alignment::Right) - .style(Style::default().fg(theme_color(COLOR_BRAND, model.linux_vt))), - operation[1], - ); - } else { - frame.render_widget( - Paragraph::new(display_text(&model.source)).wrap(Wrap { trim: false }), - areas[1], - ); - } - let content = match model.screen { - _ if model.showing_logs() => log_panel(model, areas[2]), - Screen::StreamUrl | Screen::HostConfigUrl => Paragraph::new(format!( - "Enter an http:// or https:// URL:\n\n{}\n\nEnter: start Esc: back", - display_text(&model.url) - )) - .block(block("Remote source", model.linux_vt)) - .wrap(Wrap { trim: false }), - Screen::Error => Paragraph::new(display_text(&model.details)) - .block( - block("Full error details / Esc to return", model.linux_vt) - .border_style(Style::default().fg(theme_color(COLOR_ERROR, model.linux_vt))), - ) - .wrap(Wrap { trim: false }) - .scroll((model.scroll, 0)), - _ => Paragraph::new(display_text(&model.details)) - .block(block("Result", model.linux_vt)) - .wrap(Wrap { trim: false }), - }; - frame.render_widget(content, areas[2]); - let choices = choices - .iter() - .enumerate() - .map(|(index, choice)| { - ListItem::new(format!( - "{} {choice}", - if index == model.selected { ">" } else { " " } - )) - .style(if index == model.selected { - Style::default() - .fg(theme_color(COLOR_BRAND, model.linux_vt)) - .add_modifier(Modifier::BOLD) - } else { - Style::default() - }) - }) - .collect::>(); - frame.render_widget( - List::new(choices).block(block("Actions", model.linux_vt)), - areas[3], - ); - let countdown = model - .reboot_at - .map(|deadline| { - format!( - "Automatic reboot in {}s; Shell holds reboot", - deadline.saturating_duration_since(Instant::now()).as_secs() - ) - }) - .unwrap_or_else(|| format!("Elapsed {}s", model.started.elapsed().as_secs())); - let controls = if matches!(model.screen, Screen::StreamUrl | Screen::HostConfigUrl) { - "Enter: start Esc: return to recovery".to_owned() - } else { - format!( - "Arrows: choose Enter: select S: shell V: logs{} Esc: back", - if model.screen == Screen::Error { - " D: details" - } else { - "" - }, - ) - }; - frame.render_widget( - Paragraph::new(format!("{controls}\n{countdown}")) - .style(Style::default().fg(theme_color(COLOR_BRAND, model.linux_vt))), - areas[4], - ); - if model.verbosity_open { - render_verbosity_picker(frame, model); - } -} - -/// Overlays the display-only log filter without modifying the stored diagnostics. -fn render_verbosity_picker(frame: &mut Frame, model: &Model) { - let viewport = frame.area(); - let width = viewport.width.min(44); - let height = viewport.height.min(10); - let area = Rect::new( - viewport.x + (viewport.width - width) / 2, - viewport.y + (viewport.height - height) / 2, - width, - height, - ); - frame.render_widget(Clear, area); - let rows = VERBOSITY_LEVELS - .iter() - .enumerate() - .map(|(index, level)| { - Line::from(Span::styled( - format!( - "{} {level}", - if index == model.verbosity_selected { - ">" - } else { - " " - } - ), - if index == model.verbosity_selected { - Style::default() - .fg(theme_color(COLOR_BRAND, model.linux_vt)) - .add_modifier(Modifier::BOLD) - } else { - Style::default() - }, - )) - }) - .chain([Line::default(), Line::from("Enter: apply Esc: cancel")]) - .collect::>(); - frame.render_widget( - Paragraph::new(rows).block(block("Log verbosity / display only", model.linux_vt)), - area, - ); -} - -#[cfg(test)] -mod tests { - use super::*; - - use std::fs; - - use ratatui::backend::TestBackend; - use tempfile::TempDir; - use tokio::sync::oneshot; - - use trident_client::{ - Completion as ClientCompletion, LogRecord, RebootDisposition, RemoteError, - RemoteErrorCategory, ServicingKind, - }; - - /// A failed serial destination is removed without blocking healthy mirrors. - #[test] - fn unavailable_serial_mirror_does_not_stop_the_installer() { - let root = TempDir::new().unwrap(); - let transcript = root.path().join("serial.log"); - let mut output = Output { - log: None, - mirrors: vec![ - ( - PathBuf::from("/dev/full"), - OpenOptions::new().write(true).open("/dev/full").unwrap(), - ), - ( - transcript.clone(), - OpenOptions::new() - .create(true) - .truncate(true) - .write(true) - .open(&transcript) - .unwrap(), - ), - ], - }; - let entry = LogEntry { - elapsed: Duration::from_secs(125), - source: LogSource::Inst, - level: LogLevel::Info, - message: "Preparing installer".into(), - result_color: None, - }; - output.announce(&entry, LevelFilter::Debug); - assert_eq!(output.mirrors.len(), 1); - output.announce( - &LogEntry { - message: "Still running".into(), - ..entry - }, - LevelFilter::Debug, - ); - assert_eq!( - fs::read_to_string(transcript).unwrap(), - "02:05 [INST:INFO] Preparing installer\n02:05 [INST:INFO] Still running\n" - ); - } - - /// Creates an unmodified Enter press for navigation tests. - fn enter() -> KeyEvent { - KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) - } - - /// The full-size header and background keep their intended colors and spacing. - #[test] - fn wordmark_and_background_use_clear_neutrals_palette_at_80_by_24() { - let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal.draw(|frame| render(frame, &Model::new())).unwrap(); - let buffer = terminal.backend().buffer(); - assert_eq!(buffer.content[0].bg, Color::Rgb(11, 17, 24)); - assert_eq!(buffer.content[14 * 80].fg, Color::Rgb(53, 69, 83)); - let padding = (80 - wordmark_width()) / 2; - assert!(buffer.content[..80].iter().all(|cell| cell.symbol() == " ")); - for (row_index, expected) in TRIDENT_WORDMARK.iter().enumerate() { - let row_index = row_index + 1; - let row = buffer.content[row_index * 80..(row_index + 1) * 80] - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert_eq!(&row[padding..padding + expected.len()], *expected); - for column in padding..padding + expected.len() { - assert_eq!( - buffer.content[row_index * 80 + column].fg, - Color::Rgb(75, 216, 220) - ); - } - assert!(buffer.content[9 * 80..10 * 80] - .iter() - .all(|cell| cell.symbol() == " ")); - } - let text = buffer - .content - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(text.contains(APPLICATION_NAME), "{text}"); - assert!(text.contains("INSTALLING"), "{text}"); - } - - /// Changing display verbosity hides entries without deleting retained logs. - #[test] - fn live_verbosity_picker_filters_without_discarding_logs() { - let mut model = Model::new(); - model.log(LogSource::Inst, LogLevel::Info, "ordinary event".into()); - model.log( - LogSource::Trident, - LogLevel::Trace, - "diagnostic detail".into(), - ); - assert_eq!(model.verbosity, LevelFilter::Debug); - assert!(!log_visible(LogLevel::Trace, model.verbosity)); - model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), false); - assert!(model.verbosity_open); - model.key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), false); - model.key(enter(), false); - assert_eq!(model.verbosity, LevelFilter::Trace); - assert!(!model.verbosity_open); - assert!(log_visible(LogLevel::Trace, model.verbosity)); - assert_eq!(model.logs.len(), 2); - model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), false); - model.key(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE), false); - model.key(enter(), false); - assert_eq!(model.verbosity, LevelFilter::Off); - assert!(!log_visible(LogLevel::Error, model.verbosity)); - assert_eq!(model.logs.len(), 2); - } - - /// Preparation events replace the initial idle activity with the current step. - #[test] - fn preparation_steps_replace_the_idle_status() { - let mut model = Model::new(); - let step = "Looking for installer media by filesystem label"; - let entry = model.event(Event::Preparing(step.into())); - assert_eq!(entry.source, LogSource::Inst); - assert_eq!(entry.message, step); - assert_eq!(model.activity, step); - } - - /// RPC log severities and the Trident source receive independent colors. - #[test] - fn rpc_logs_are_coloured_by_severity() { - let mut model = Model::new(); - model.verbosity = LevelFilter::Trace; - let records = [ - (LogLevel::Error, "error-line", Color::Rgb(255, 107, 115)), - (LogLevel::Warn, "warn-line", Color::Rgb(245, 182, 93)), - (LogLevel::Info, "info-line", Color::Rgb(229, 234, 236)), - (LogLevel::Debug, "debug-line", Color::Rgb(167, 180, 189)), - (LogLevel::Trace, "trace-line", Color::Rgb(99, 115, 129)), - ]; - for (level, text, _) in records { - model.log(LogSource::Trident, level, text.into()); - } - let mut terminal = Terminal::new(TestBackend::new(80, 32)).unwrap(); - terminal.draw(|frame| render(frame, &model)).unwrap(); - let buffer = terminal.backend().buffer(); - for (_, message, expected) in records { - let row = buffer - .content - .chunks(80) - .find(|row| { - row.iter() - .map(|cell| cell.symbol()) - .collect::() - .contains(message) - }) - .unwrap(); - let text = row.iter().map(|cell| cell.symbol()).collect::(); - assert_eq!(row[text.find(message).unwrap()].fg, expected); - let source = text.find("TRIDENT").unwrap(); - assert_eq!(row[source].fg, Color::Rgb(114, 185, 165)); - } - } - - /// VT palette slots distinguish log severities without changing RGB-mode colors. - #[test] - fn linux_vt_palette_uses_distinct_slots_for_a1_log_levels() { - let entries = vt_rgb_palette().unwrap(); - assert!(entries.contains(&(15, 229, 234, 236))); - assert!(entries.contains(&(7, 167, 180, 189))); - assert!(entries.contains(&(8, 99, 115, 129))); - assert_eq!(theme_color(COLOR_INFO, true), Color::White); - assert_eq!(theme_color(COLOR_DEBUG, true), Color::Gray); - assert_eq!(theme_color(COLOR_TRACE, true), Color::DarkGray); - assert_eq!(theme_color(COLOR_SUCCESS, true), Color::LightGreen); - assert_eq!(theme_color(COLOR_DEBUG, false), Color::Rgb(167, 180, 189)); - - let mut model = Model::new(); - model.linux_vt = true; - model.verbosity = LevelFilter::Trace; - for (level, message) in [ - (LogLevel::Info, "info-vt"), - (LogLevel::Debug, "debug-vt"), - (LogLevel::Trace, "trace-vt"), - ] { - model.log(LogSource::Trident, level, message.into()); - } - let mut terminal = Terminal::new(TestBackend::new(80, 32)).unwrap(); - terminal.draw(|frame| render(frame, &model)).unwrap(); - let buffer = terminal.backend().buffer(); - for (message, expected) in [ - ("info-vt", Color::White), - ("debug-vt", Color::Gray), - ("trace-vt", Color::DarkGray), - ] { - let row = buffer - .content - .chunks(80) - .find(|row| { - row.iter() - .map(|cell| cell.symbol()) - .collect::() - .contains(message) - }) - .unwrap(); - let text = row.iter().map(|cell| cell.symbol()).collect::(); - assert_eq!(row[text.find(message).unwrap()].fg, expected); - } - } - - /// The source and verbosity indicator both fit on the standard-width status line. - #[test] - fn operation_and_verbosity_share_one_line() { - let mut model = Model::new(); - model.source = "StreamDisk from file:///media/cosi/payload.cosi".into(); - let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal.draw(|frame| render(frame, &model)).unwrap(); - let row = terminal.backend().buffer().content[12 * 80..13 * 80] - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(row.contains("StreamDisk from file://"), "{row}"); - assert!(row.contains("Logs: DEBUG [V]"), "{row}"); - } - - /// Serial filtering preserves line prefixes and does not change display filtering. - #[test] - fn serial_verbosity_filters_independently_of_display() { - let mut model = Model::new(); - let root = TempDir::new().unwrap(); - let serial = root.path().join("serial.log"); - let mut output = Output { - log: None, - mirrors: vec![(serial.clone(), File::create(&serial).unwrap())], - }; - for level in [ - LogLevel::Error, - LogLevel::Warn, - LogLevel::Info, - LogLevel::Debug, - LogLevel::Trace, - ] { - let entry = model.log(LogSource::Trident, level, "first\nsecond".into()); - output.announce(&entry, SerialVerbosity::Debug.filter()); - } - let text = fs::read_to_string(&serial).unwrap(); - let lines = text.lines().collect::>(); - assert_eq!(lines.len(), 8); - assert!(lines.iter().all(|line| line.starts_with("00:00 [TRIDENT:"))); - assert_eq!(lines[0], "00:00 [TRIDENT:ERROR] first"); - assert_eq!(lines[1], "00:00 [TRIDENT:ERROR] second"); - assert!(!text.contains("\\n")); - let trace = model.log(LogSource::Trident, LogLevel::Trace, "trace detail".into()); - output.announce(&trace, SerialVerbosity::Trace.filter()); - let text = fs::read_to_string(serial).unwrap(); - assert!(text - .lines() - .last() - .unwrap() - .ends_with("[TRIDENT:TRACE] trace detail")); - assert!(!log_visible(LogLevel::Trace, model.verbosity)); - assert_eq!(model.logs.len(), 6); - } - - /// Installer logger events retain their own source tag and severity styling. - #[test] - fn installer_logger_records_use_inst_source_and_colour() { - let mut model = Model::new(); - let entry = model.event(Event::InstallerLog { - level: Level::Debug, - message: "Loaded installer settings".into(), - }); - assert_eq!(entry.source, LogSource::Inst); - assert_eq!( - entry.plain(), - "00:00 [INST:DEBUG] Loaded installer settings" - ); - assert_eq!( - entry.styled(false).spans[1].style.fg, - Some(Color::Rgb(171, 186, 196)) - ); - assert_eq!( - entry.styled(false).spans[3].style.fg, - Some(Color::Rgb(167, 180, 189)) - ); - assert_eq!(model.logs.len(), 1); - } - - /// Domain logs retain Trident's source tag and update the displayed activity. - #[test] - fn trident_domain_log_event_updates_activity_and_serial_record() { - let mut model = Model::new(); - let entry = model.event(Event::Response(ServicingEvent::Log(LogRecord { - timestamp: None, - level: LogLevel::Info, - message: "Preparing storage".into(), - module: String::new(), - target: String::new(), - location: None, - }))); - assert_eq!(entry.source, LogSource::Trident); - assert_eq!(model.activity, "Preparing storage"); - assert_eq!(entry.plain(), "00:00 [TRIDENT:INFO] Preparing storage"); - } - - /// Trident's COSI-loading messages must not expose signed URLs on any installer output. - #[test] - fn daemon_image_urls_are_redacted_before_recording_or_mirroring() { - let root = TempDir::new().unwrap(); - let diagnostic = root.path().join("installer.log"); - let serial = root.path().join("serial.log"); - let mut output = Output { - log: Some(File::create(&diagnostic).unwrap()), - mirrors: vec![(serial.clone(), File::create(&serial).unwrap())], - }; - let mut model = Model::new(); - for message in [ - "Attempting to load COSI file from 'https://user:password@example.test/image.cosi?sig=secret#fragment'", - "Loaded COSI file from 'https://user:password@example.test/image.cosi?sig=secret#fragment' with hash 'abc'", - ] { - let event = redact_event(Event::Response(ServicingEvent::Log(LogRecord { - timestamp: None, - level: LogLevel::Info, - message: message.into(), - module: String::new(), - target: String::new(), - location: None, - }))); - output.record(&event).unwrap(); - let entry = model.event(event); - output.announce(&entry, model.serial_verbosity); - assert!(entry.message.contains("https://example.test/image.cosi")); - for secret in ["user", "password", "sig=secret", "fragment"] { - assert!(!entry.message.contains(secret), "{entry:?}"); - } - } - for text in [ - fs::read_to_string(diagnostic).unwrap(), - fs::read_to_string(serial).unwrap(), - ] { - assert!(text.contains("https://example.test/image.cosi"), "{text}"); - for secret in ["user", "password", "sig=secret", "fragment"] { - assert!(!text.contains(secret), "{text}"); - } - } - } - - /// Daemon failures cannot reintroduce a signed URL through structured error details. - #[test] - fn daemon_image_errors_redact_signed_urls() { - let event = redact_event(Event::Response(ServicingEvent::Completed(Err( - RemoteError { - category: RemoteErrorCategory::Servicing, - message: "Failed to load 'https://user:password@example.test/image.cosi?sig=secret#fragment'".into(), - error_message: "HTTP failure at https://user:password@example.test/image.cosi?sig=secret#fragment".into(), - subkind: "image-load-failed".into(), - location: None, - }, - )))); - let mut model = Model::new(); - let entry = model.event(event); - assert!(model.details.contains("https://example.test/image.cosi")); - for secret in ["user", "password", "sig=secret", "fragment"] { - assert!(!model.details.contains(secret), "{}", model.details); - assert!(!entry.plain().contains(secret)); - } - } - - /// Unknown daemon severities remain identifiable in serial output and visible as warnings. - #[test] - fn unknown_log_level_is_not_silently_discarded() { - let mut model = Model::new(); - let entry = model.event(Event::Response(ServicingEvent::Log(LogRecord { - timestamp: None, - level: LogLevel::Unknown(42), - message: "New severity".into(), - module: String::new(), - target: String::new(), - location: None, - }))); - assert_eq!(entry.plain(), "00:00 [TRIDENT:UNKNOWN(42)] New severity"); - assert!(log_visible(entry.level, LevelFilter::Warn)); - assert_eq!(log_color(entry.level), COLOR_WARN); - } - - /// The autorun default schedules a reboot when servicing requires one. - #[test] - fn headless_autorun_keeps_automatic_reboot_enabled_by_default() { - let config = Config::parse("mode = 'autorun'\nserialMode = 'logs'").unwrap(); - let mut model = Model::new(); - model.reboot = config.autorun.reboot; - model.event(Event::Response(ServicingEvent::Completed(Ok( - ClientCompletion { - reboot: RebootDisposition::Required, - servicing_kind: None, - image_hash: None, - }, - )))); - assert!(model.reboot_at.is_some()); - } - - /// A failed recovery shell cannot erase a successful servicing result or its reboot. - #[test] - fn shell_exit_failure_is_only_a_warning() { - let mut model = Model::new(); - model.event(Event::Response(ServicingEvent::Completed(Ok( - ClientCompletion { - reboot: RebootDisposition::Required, - servicing_kind: Some(ServicingKind::CleanInstall), - image_hash: None, - }, - )))); - let reboot_at = model.reboot_at; - assert!(reboot_at.is_some()); - assert!(shell_result_event(Ok(Ok(()))).is_none()); - let warning = model.event(shell_result_event(Ok(Err(anyhow!("exit 1")))).unwrap()); - assert_eq!(warning.level, LogLevel::Warn); - assert!(warning.plain().contains("Recovery shell failed: exit 1")); - assert_eq!(model.screen, Screen::Success); - assert!(!model.failed); - assert_eq!(model.reboot_at, reboot_at); - } - - /// Unknown servicing kinds fail rather than triggering an automatic reboot. - #[test] - fn unknown_completion_kind_does_not_claim_success() { - let mut model = Model::new(); - model.reboot = true; - let entry = model.event(Event::Response(ServicingEvent::Completed(Ok( - ClientCompletion { - reboot: RebootDisposition::Required, - servicing_kind: Some(ServicingKind::Unknown(42)), - image_hash: None, - }, - )))); - assert!(model.failed); - assert!(model.uncertain); - assert!(model.reboot_at.is_none()); - assert!(entry.plain().contains("Unsupported daemon servicing kind")); - } - - /// Missing inputs produce a tagged failure record suitable for serial output. - #[test] - fn errors_for_missing_inputs_are_tagged_for_serial_without_a_tui() { - let mut model = Model::new(); - let entry = model.event(Event::Error { - details: "No image or Host Configuration".into(), - uncertain: false, - }); - assert!(model.failed); - assert_eq!( - entry.plain(), - "00:00 [INST:ERROR] FAILURE: No image or Host Configuration" - ); - } - - /// A success result remains highlighted even when earlier logs are filtered out. - #[test] - fn success_keeps_prior_logs_and_highlights_the_final_record() { - let mut model = Model::new(); - model.log( - LogSource::Trident, - LogLevel::Debug, - "Earlier disk action".into(), - ); - let result = model.event(Event::Response(ServicingEvent::Completed(Ok( - ClientCompletion { - reboot: RebootDisposition::NotRequired, - servicing_kind: None, - image_hash: None, - }, - )))); - assert_eq!(result.result_color, Some(Color::Rgb(93, 200, 148))); - assert_eq!( - result.plain(), - "00:00 [INST:INFO] SUCCESS: Installation completed successfully.\n\ - 00:00 [INST:INFO] Remove the installation media before booting the installed OS." - ); - let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal.draw(|frame| render(frame, &model)).unwrap(); - let rows = terminal - .backend() - .buffer() - .content - .chunks(80) - .collect::>(); - let rendered = rows - .iter() - .map(|row| row.iter().map(|cell| cell.symbol()).collect::()) - .collect::>(); - let title = rendered - .iter() - .position(|row| row.contains("SUCCESS - Installation complete")) - .unwrap(); - assert_eq!( - rows[title][rendered[title].find("SUCCESS").unwrap()].fg, - Color::Rgb(93, 200, 148) - ); - let result_row = rendered - .iter() - .position(|row| row.contains("SUCCESS: Installation")) - .unwrap(); - assert_eq!( - rows[result_row][rendered[result_row].find("SUCCESS:").unwrap()].fg, - Color::Rgb(93, 200, 148) - ); - model.key(KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), false); - terminal.draw(|frame| render(frame, &model)).unwrap(); - let earlier = terminal - .backend() - .buffer() - .content - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(earlier.contains("Earlier disk action")); - model.scroll = 0; - model.verbosity = LevelFilter::Off; - terminal.draw(|frame| render(frame, &model)).unwrap(); - let text = terminal - .backend() - .buffer() - .content - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(text.contains("SUCCESS: Installation")); - assert!(!text.contains("Earlier disk action")); - } - - /// Wrapped diagnostics and activity cannot hide the latest result at 80x24. - #[test] - fn wrapped_log_rows_keep_the_result_visible_and_scrollable() { - let mut model = Model::new(); - model.log( - LogSource::Trident, - LogLevel::Info, - "Long preceding diagnostic ".repeat(40), - ); - model.event(Event::Response(ServicingEvent::Completed(Ok( - ClientCompletion { - reboot: RebootDisposition::NotRequired, - servicing_kind: Some(ServicingKind::CleanInstall), - image_hash: None, - }, - )))); - let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - let visible_text = |terminal: &Terminal| { - terminal - .backend() - .buffer() - .content - .iter() - .map(|cell| cell.symbol()) - .collect::() - }; - terminal.draw(|frame| render(frame, &model)).unwrap(); - assert!( - visible_text(&terminal).contains("SUCCESS: Installation"), - "{}", - visible_text(&terminal) - ); - model.key(KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), false); - terminal.draw(|frame| render(frame, &model)).unwrap(); - assert!(!visible_text(&terminal).contains("SUCCESS: Installation")); - model.key(KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE), false); - terminal.draw(|frame| render(frame, &model)).unwrap(); - assert!(visible_text(&terminal).contains("SUCCESS: Installation")); - - model.screen = Screen::Progress; - model.activity = "Scanning media ".repeat(60); - model.log(LogSource::Inst, LogLevel::Info, "Latest activity".into()); - terminal.draw(|frame| render(frame, &model)).unwrap(); - assert!(visible_text(&terminal).contains("Latest activity")); - } - - /// Failures show prior logs first while still allowing full diagnostic details. - #[test] - fn failure_shows_logs_by_default_and_keeps_full_details_accessible() { - let mut model = Model::new(); - model.log(LogSource::Trident, LogLevel::Warn, "Earlier warning".into()); - let result = model.event(Event::Error { - details: "Network timeout\nUnderlying transport failure".into(), - uncertain: false, - }); - assert_eq!(result.result_color, Some(Color::Rgb(255, 107, 115))); - let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal.draw(|frame| render(frame, &model)).unwrap(); - let text = terminal - .backend() - .buffer() - .content - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(text.contains("FAILURE - Installation stopped")); - assert!(text.contains("Earlier warning")); - assert!(text.contains("FAILURE: Network timeout")); - for marker in ["FAILURE - Installation stopped", "FAILURE: Network timeout"] { - let row = terminal - .backend() - .buffer() - .content - .chunks(80) - .find(|row| { - row.iter() - .map(|cell| cell.symbol()) - .collect::() - .contains(marker) - }) - .unwrap(); - let content = row.iter().map(|cell| cell.symbol()).collect::(); - assert_eq!( - row[content.find(marker).unwrap()].fg, - Color::Rgb(255, 107, 115) - ); - } - model.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE), false); - assert!(model.error_details_open); - terminal.draw(|frame| render(frame, &model)).unwrap(); - let text = terminal - .backend() - .buffer() - .content - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(text.contains("Underlying transport failure")); - model.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), false); - assert_eq!(model.screen, Screen::Error); - assert!(!model.error_details_open); - } - - /// Compact layouts retain the application heading and available actions. - #[test] - fn small_terminals_use_a_readable_compact_header() { - assert_eq!( - header_height(Rect::new(0, 0, 40, 24)), - COMPACT_HEADER_HEIGHT - ); - assert_eq!( - header_height(Rect::new(0, 0, 80, 16)), - COMPACT_HEADER_HEIGHT - ); - let mut model = Model::new(); - model.screen = Screen::Success; - let mut terminal = Terminal::new(TestBackend::new(40, 16)).unwrap(); - terminal.draw(|frame| render(frame, &model)).unwrap(); - let text = terminal - .backend() - .buffer() - .content - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(text.contains(APPLICATION_NAME), "{text}"); - assert!(text.contains("Reboot"), "{text}"); - assert!(text.contains("Shell"), "{text}"); - } - - /// Recovery reports invalid reloaded configuration instead of using defaults. - #[tokio::test] - async fn recovery_reloads_configuration_without_using_defaults_on_error() { - let root = TempDir::new().unwrap(); - let path = root.path().join("installer.toml"); - for (text, diagnostic) in [("", "mode"), ("mode = 'not-a-mode'", "not-a-mode")] { - fs::write(&path, text).unwrap(); - let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); - let worker = spawn_operation(path.clone(), Request::Autorun, false, tx); - let Event::Error { details, uncertain } = rx.recv().await.unwrap() else { - panic!("Invalid settings must fail before preparing a source"); - }; - assert!(!uncertain); - assert!(details.contains(diagnostic), "{details}"); - worker.await.unwrap().unwrap(); - rx.try_recv().unwrap_err(); - } - } - - /// Continuing from an error opens recovery; shell actions leave their screen unchanged. - #[test] - fn error_continue_and_shell_keep_the_originating_screen() { - let mut model = Model::new(); - model.fail("Complete diagnostic information".into(), false); - assert!(model.key(enter(), false).is_none()); - assert_eq!(model.screen, Screen::Recovery); - assert_eq!(model.key(enter(), false), Some(Action::Shell)); - assert_eq!(model.screen, Screen::Recovery); - assert_eq!(model.details, "Complete diagnostic information"); - model.screen = Screen::Success; - model.selected = 1; - assert_eq!(model.key(enter(), false), Some(Action::Shell)); - assert_eq!(model.screen, Screen::Success); - model.selected = 0; - assert_eq!(model.key(enter(), false), Some(Action::Reboot)); - } - - /// Repeated interrupts and power choices cannot shut down an active installation. - #[test] - fn active_worker_cannot_reach_a_power_action() { - let mut model = Model::new(); - let interrupt = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL); - model.key(interrupt, true); - assert_eq!(model.screen, Screen::Menu); - model.key(interrupt, true); - assert_eq!(model.screen, Screen::Menu); - - model.screen = Screen::Recovery; - model.selected = 3; - assert_eq!(model.key(enter(), true), Some(Action::BlockedPower)); - assert_eq!(model.key(enter(), false), Some(Action::Poweroff)); - model.screen = Screen::Success; - model.selected = 0; - assert_eq!(model.key(enter(), true), Some(Action::BlockedPower)); - } - - /// Losing the client stream must not make shutdown safe until Trident confirms idleness. - #[test] - fn uncertain_outcome_blocks_power_after_worker_exits() { - let mut model = Model::new(); - model.event(Event::Error { - details: "gRPC stream disconnected; servicing may continue".into(), - uncertain: true, - }); - model.screen = Screen::Recovery; - model.selected = 3; - assert!(!model.choices().contains(&"Shutdown")); - assert_eq!(model.key(enter(), false), Some(Action::BlockedPower)); - - model.event(Event::IdleConfirmed); - assert!(!model.uncertain); - assert!(model.choices().contains(&"Shutdown")); - assert_eq!(model.key(enter(), false), Some(Action::Poweroff)); - - model.event(Event::Error { - details: "Another stream disconnected".into(), - uncertain: true, - }); - model.event(Event::Response(ServicingEvent::Completed(Ok( - ClientCompletion { - reboot: RebootDisposition::NotRequired, - servicing_kind: Some(ServicingKind::CleanInstall), - image_hash: None, - }, - )))); - assert!(!model.uncertain); - assert_eq!(model.key(enter(), false), Some(Action::Reboot)); - } - - /// An enqueued completion must be handled before the worker stops blocking power actions. - #[tokio::test] - async fn pending_completion_keeps_worker_active() { - let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); - let worker = task::spawn(async move { - tx.send(Event::Response(ServicingEvent::Completed(Ok( - ClientCompletion { - reboot: RebootDisposition::NotRequired, - servicing_kind: Some(ServicingKind::CleanInstall), - image_hash: None, - }, - )))) - .await - .unwrap(); - Ok::<(), Error>(()) - }); - time::timeout(Duration::from_secs(1), async { - while !worker.is_finished() { - task::yield_now().await; - } - }) - .await - .unwrap(); - - let mut model = Model::new(); - model.screen = Screen::Recovery; - model.selected = 3; - assert!(!rx.is_empty()); - assert_eq!( - model.key(enter(), !(rx.is_empty() && worker.is_finished())), - Some(Action::BlockedPower) - ); - model.event(rx.recv().await.unwrap()); - assert!(rx.is_empty()); - assert_eq!(model.screen, Screen::Success); - worker.await.unwrap().unwrap(); - } - - /// An uncertain terminal event queued behind a full batch must block shutdown. - #[tokio::test] - async fn pending_disconnect_cannot_release_power_guard() { - let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); - let worker = task::spawn(async move { - for _ in 0..EVENT_CAPACITY { - tx.send(Event::Preparing("Still writing".into())) - .await - .unwrap(); - } - tx.send(Event::Error { - details: "gRPC stream disconnected".into(), - uncertain: true, - }) - .await - .unwrap(); - Ok::<(), Error>(()) - }); - for _ in 0..EVENT_CAPACITY { - rx.recv().await.unwrap(); - } - time::timeout(Duration::from_secs(1), async { - while !worker.is_finished() { - task::yield_now().await; - } - }) - .await - .unwrap(); - - let mut model = Model::new(); - model.screen = Screen::Recovery; - model.selected = 3; - assert!(!rx.is_empty()); - assert_eq!( - model.key(enter(), !(rx.is_empty() && worker.is_finished())), - Some(Action::BlockedPower) - ); - model.event(rx.recv().await.unwrap()); - assert!(model.uncertain); - model.screen = Screen::Recovery; - model.selected = 3; - assert_eq!(model.key(enter(), false), Some(Action::BlockedPower)); - worker.await.unwrap().unwrap(); - } - - /// Rechecking an empty queue before worker completion misses a late disconnect. - #[tokio::test] - async fn disconnect_between_worker_and_queue_checks_stays_guarded() { - let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); - let (release, ready) = oneshot::channel::<()>(); - let worker = task::spawn(async move { - ready.await.unwrap(); - tx.send(Event::Error { - details: "gRPC stream disconnected".into(), - uncertain: true, - }) - .await - .unwrap(); - Ok::<(), Error>(()) - }); - let previously_empty = rx.is_empty(); - release.send(()).unwrap(); - time::timeout(Duration::from_secs(1), async { - while !worker.is_finished() { - task::yield_now().await; - } - }) - .await - .unwrap(); - assert!(previously_empty && worker.is_finished()); - assert!(!rx.is_empty()); - - let mut model = Model::new(); - model.screen = Screen::Recovery; - model.selected = 3; - assert_eq!( - model.key(enter(), !(worker.is_finished() && rx.is_empty())), - Some(Action::BlockedPower) - ); - model.event(rx.recv().await.unwrap()); - assert!(model.uncertain); - model.screen = Screen::Recovery; - model.selected = 3; - assert_eq!(model.key(enter(), false), Some(Action::BlockedPower)); - worker.await.unwrap().unwrap(); - } - - /// Standard-size result and recovery screens expose their expected actions. - #[test] - fn screens_render_at_80_by_24_with_visible_actions() { - for screen in [Screen::Recovery, Screen::Success, Screen::Error] { - let mut model = Model::new(); - model.screen = screen; - model.details = "Diagnostic or result details".into(); - let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); - terminal.draw(|frame| render(frame, &model)).unwrap(); - let text = terminal - .backend() - .buffer() - .content - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(text.contains("Shell"), "{text}"); - if screen == Screen::Success { - assert!(text.contains("Reboot"), "{text}"); - } - if screen == Screen::Recovery { - assert!(text.contains("Shutdown"), "{text}"); - } - } - } -} diff --git a/crates/installer/src/ui/mod.rs b/crates/installer/src/ui/mod.rs new file mode 100644 index 0000000000..b74c655105 --- /dev/null +++ b/crates/installer/src/ui/mod.rs @@ -0,0 +1,678 @@ +//! Runs the live installer console, coordinating operations, TTY ownership and power actions. + +use std::{ + fs::OpenOptions, + io::{self, IsTerminal}, + path::PathBuf, + time::{Duration, Instant}, +}; + +use anyhow::{anyhow, Error}; +use crossterm::{ + cursor::MoveTo, + event::{self, Event as TerminalEvent}, + execute, + terminal::{ + self, Clear as ClearTerminal, ClearType, EnterAlternateScreen, LeaveAlternateScreen, + }, +}; +use log::{error, Level}; +use ratatui::{backend::CrosstermBackend, Terminal}; +use tokio::{ + signal::unix::{self, SignalKind}, + sync::mpsc, + task::{self, JoinError, JoinHandle}, + time, +}; + +use osutils::{systemd, terminal as os_terminal}; +use trident_client::{LogLevel, ServicingEvent}; + +use crate::{ + client::{self, Event}, + config::Config, + source::{self, Request}, + INSTALLER_LOG_TX, +}; + +mod model; +mod output; +mod render; +mod theme; + +use model::{display_text, log_visible, Action, LogSource, Model, Screen}; +pub(super) use output::Output; +use render::{render, title}; +use theme::{vt_rgb_palette, VERBOSITY_LEVELS}; + +const FRAME_INTERVAL: Duration = Duration::from_millis(100); +const INPUT_EVENTS_PER_FRAME: usize = 256; +const EVENT_CAPACITY: usize = 64; + +struct Display { + terminal: Terminal>, + active: bool, + linux_vt: bool, +} + +impl Display { + /// Creates a terminal display and enters its alternate screen. + fn new(linux_vt: bool) -> Result { + let mut display = Self { + terminal: Terminal::new(CrosstermBackend::new(io::stdout()))?, + active: false, + linux_vt, + }; + display.resume()?; + Ok(display) + } + + /// Restores raw-mode drawing and the VT palette after a shell exits. + fn resume(&mut self) -> Result<(), Error> { + terminal::enable_raw_mode()?; + self.active = true; + if self.linux_vt { + os_terminal::set_vt_palette(&mut io::stdout(), &vt_rgb_palette()?)?; + } + execute!(self.terminal.backend_mut(), EnterAlternateScreen)?; + self.terminal.clear()?; + Ok(()) + } + + /// Leaves raw mode and the alternate screen so an interactive shell can use the terminal. + /// On Linux VTs, also clears the old UI and restores the original palette. + fn suspend(&mut self) -> Result<(), Error> { + let restore = (|| -> Result<(), Error> { + terminal::disable_raw_mode()?; + execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; + if self.linux_vt { + // Linux VT leaves the old TUI visible when exiting its alternate screen. + execute!( + self.terminal.backend_mut(), + ClearTerminal(ClearType::All), + MoveTo(0, 0) + )?; + } + self.terminal.show_cursor()?; + Ok(()) + })(); + let palette = if self.linux_vt { + os_terminal::reset_vt_palette(&mut io::stdout()) + } else { + Ok(()) + }; + self.active = false; + restore?; + palette?; + Ok(()) + } +} + +impl Drop for Display { + /// Attempts terminal restoration if the display was not explicitly suspended. + fn drop(&mut self) { + if self.active { + if let Err(error) = self.suspend() { + error!("Failed to restore terminal: {error:#}"); + } + } + } +} + +/// Reloads live configuration for each installation attempt. +/// Configuration read errors are reported as events rather than worker failures. +fn spawn_operation( + config_path: PathBuf, + request: Request, + uncertain: bool, + tx: mpsc::Sender, +) -> JoinHandle> { + task::spawn(async move { + match Config::read(config_path) { + Ok(settings) => client::execute(settings, request, uncertain, tx).await, + Err(error) => { + client::send( + &tx, + Event::Error { + details: format!("{error:#}"), + uncertain, + }, + ) + .await + } + } + }) +} + +/// Reports shell failures without changing the result of an independent installation. +fn shell_result_event(result: Result, JoinError>) -> Option { + match result { + Ok(Ok(())) => None, + Ok(Err(error)) => Some(Event::InstallerLog { + level: Level::Warn, + message: format!("Recovery shell failed: {error:#}"), + }), + Err(error) => Some(Event::InstallerLog { + level: Level::Warn, + message: format!("Recovery shell task terminated: {error}"), + }), + } +} + +/// Sanitizes received diagnostics before persistence, display, or serial mirroring. +fn redact_event(mut event: Event) -> Event { + match &mut event { + Event::InstallerLog { message, .. } | Event::Preparing(message) => { + *message = source::redact_message_urls(message); + } + Event::Prepared { description, .. } => { + *description = source::redact_message_urls(description); + } + Event::Error { details, .. } => { + *details = source::redact_message_urls(details); + } + Event::Response(ServicingEvent::Log(record)) => { + record.message = source::redact_message_urls(&record.message); + } + Event::Response(ServicingEvent::Completed(Err(error))) => { + error.message = source::redact_message_urls(&error.message); + error.error_message = source::redact_message_urls(&error.error_message); + } + _ => {} + } + event +} + +/// Runs the installer event loop, starting autorun and offering recovery on a terminal. +/// Noninteractive runs return after work completes and fail if recovery needs user input; +/// `plain` selects text output instead of the full-screen display. +pub(super) async fn run( + config_path: PathBuf, + plain: bool, + linux_vt: bool, + mut output: Output, +) -> Result<(), Error> { + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let (log_tx, mut log_rx) = mpsc::unbounded_channel(); + INSTALLER_LOG_TX + .set(log_tx) + .map_err(|_| anyhow!("Installer log receiver already initialized"))?; + let mut interrupts = unix::signal(SignalKind::interrupt())?; + let mut termination = unix::signal(SignalKind::terminate())?; + let mut model = Model::new(); + model.linux_vt = linux_vt; + let mut worker = Some(spawn_operation( + config_path.clone(), + Request::Autorun, + false, + tx.clone(), + )); + let interactive = io::stdin().is_terminal() && io::stdout().is_terminal(); + let mut display = if interactive && !plain { + Some(Display::new(linux_vt)?) + } else { + None + }; + let mut shell: Option>> = None; + let mut printed_screen = None; + loop { + for _ in 0..EVENT_CAPACITY { + let event = match rx.try_recv() { + Ok(event) => event, + Err(_) => match log_rx.try_recv() { + Ok(event) => event, + Err(_) => break, + }, + }; + let event = redact_event(event); + output.record(&event)?; + let entry = model.event(event); + output.announce(&entry, model.serial_verbosity); + if display.is_none() && shell.is_none() && log_visible(entry.level, model.verbosity) { + println!("{}", entry.plain()); + } + } + // A finished worker cannot enqueue a terminal event after the queue check. + if worker.as_ref().is_some_and(JoinHandle::is_finished) && rx.is_empty() { + if let Some(finished) = worker.take() { + let failure = match finished.await { + Ok(Ok(())) => None, + Ok(Err(error)) => { + Some(model.fail(format!("Installer worker failed: {error:#}"), true)) + } + Err(error) => { + Some(model.fail(format!("Installer worker terminated: {error}"), true)) + } + }; + if let Some(failure) = failure { + output.announce(&failure, model.serial_verbosity); + } + } + } + if shell.as_ref().is_some_and(JoinHandle::is_finished) { + if let Some(finished) = shell.take() { + if let Some(event) = shell_result_event(finished.await) { + let event = redact_event(event); + output.record(&event)?; + let warning = model.event(event); + output.announce(&warning, model.serial_verbosity); + } + } + if let Some(display) = &mut display { + display.resume()?; + } + printed_screen = None; + } + if shell.is_some() { + tokio::select! { + _ = time::sleep(FRAME_INTERVAL) => {} + _ = termination.recv() => return Err(anyhow!("Installer stopped; servicing may continue in Trident")), + } + continue; + } + if model + .reboot_at + .is_some_and(|deadline| Instant::now() >= deadline) + { + model.reboot_at = None; + let reboot = model.log( + LogSource::Inst, + LogLevel::Info, + "Rebooting after successful installation".into(), + ); + output.announce(&reboot, model.serial_verbosity); + match systemd::reboot() { + Ok(()) => return Ok(()), + Err(error) => { + let failure = model.fail(format!("Failed to reboot: {error:#}"), false); + output.announce(&failure, model.serial_verbosity); + } + } + } + if !interactive { + if worker.is_none() && rx.is_empty() && log_rx.is_empty() && model.reboot_at.is_none() { + if model.failed { + return Err(anyhow!( + "{}\nRecovery requires an interactive terminal.", + model.details + )); + } + return Ok(()); + } + } else { + if let Some(display) = &mut display { + display.terminal.draw(|frame| render(frame, &model))?; + } else if printed_screen != Some((model.screen, model.verbosity_open)) { + if model.verbosity_open { + println!("\nLOG VERBOSITY (display only)"); + for (index, level) in VERBOSITY_LEVELS.iter().enumerate() { + println!("{}: {level}", index + 1); + } + println!("Number + Enter: apply; Esc: cancel"); + } else { + println!( + "\n{}\n{}", + title(model.screen), + display_text(&model.details) + ); + for (index, choice) in model.choices().iter().enumerate() { + println!("{}: {choice}", index + 1); + } + if matches!(model.screen, Screen::StreamUrl | Screen::HostConfigUrl) { + println!("Enter an http:// or https:// URL, then press Enter"); + } + println!("Number + Enter: select; S + Enter: shell; Esc: menu; V: verbosity"); + } + printed_screen = Some((model.screen, model.verbosity_open)); + } + for _ in 0..INPUT_EVENTS_PER_FRAME { + if !event::poll(Duration::ZERO)? { + break; + } + let event = event::read()?; + if let TerminalEvent::Key(key) = event { + if let Some(action) = model.key(key, worker.is_some()) { + match action { + Action::Shell => { + if let Some(display) = &mut display { + display.suspend()?; + } + println!("\nShell: type exit to return. Installation continues; automatic reboot waits."); + let tty = + OpenOptions::new().read(true).write(true).open("/dev/tty")?; + shell = + Some(task::spawn_blocking(move || os_terminal::shell(&tty))); + break; + } + Action::Start(request) => { + if worker.is_some() { + let failure = model.fail("An installer operation is still active. Wait for it to finish.".into(), true); + output.announce(&failure, model.serial_verbosity); + } else { + model.screen = Screen::Progress; + model.activity = "Preparing installation".into(); + model.started = Instant::now(); + model.reboot_at = None; + model.scroll = 0; + model.selected = 0; + worker = Some(spawn_operation( + config_path.clone(), + request, + model.uncertain, + tx.clone(), + )); + } + } + Action::BlockedPower => { + model.screen = if worker.is_some() { + Screen::Menu + } else { + Screen::Recovery + }; + model.selected = 0; + model.activity = "Servicing may still be active; power actions are unavailable until Trident confirms it is idle".into(); + let warning = model.log( + LogSource::Inst, + LogLevel::Warn, + model.activity.clone(), + ); + output.announce(&warning, model.serial_verbosity); + } + Action::Reboot => { + let action = model.log( + LogSource::Inst, + LogLevel::Info, + "Reboot requested".into(), + ); + output.announce(&action, model.serial_verbosity); + if let Err(error) = systemd::reboot() { + let failure = + model.fail(format!("Failed to reboot: {error:#}"), false); + output.announce(&failure, model.serial_verbosity); + } else { + return Ok(()); + } + } + Action::Poweroff => { + let action = model.log( + LogSource::Inst, + LogLevel::Info, + "Shutdown requested".into(), + ); + output.announce(&action, model.serial_verbosity); + if let Err(error) = systemd::poweroff() { + let failure = model + .fail(format!("Failed to shut down: {error:#}"), false); + output.announce(&failure, model.serial_verbosity); + } else { + return Ok(()); + } + } + } + } + } + } + } + tokio::select! { + _ = time::sleep(FRAME_INTERVAL) => {} + signal = interrupts.recv() => { + if signal.is_none() { + return Err(anyhow!("Installer interrupt handler closed")); + } + model.screen = if worker.is_some() { Screen::Menu } else { Screen::Recovery }; + model.selected = 0; + } + _ = termination.recv() => return Err(anyhow!("Installer stopped; servicing may continue in Trident")), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::fs::{self, File}; + + use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; + use tempfile::TempDir; + use tokio::sync::oneshot; + + use trident_client::{ + Completion as ClientCompletion, LogRecord, RebootDisposition, RemoteError, + RemoteErrorCategory, ServicingKind, + }; + + /// Creates an unmodified Enter press for navigation tests. + fn enter() -> KeyEvent { + KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) + } + + /// Trident's COSI-loading messages must not expose signed URLs on any installer output. + #[test] + fn daemon_image_urls_are_redacted_before_recording_or_mirroring() { + let root = TempDir::new().unwrap(); + let diagnostic = root.path().join("installer.log"); + let serial = root.path().join("serial.log"); + let mut output = Output { + log: Some(File::create(&diagnostic).unwrap()), + mirrors: vec![(serial.clone(), File::create(&serial).unwrap())], + }; + let mut model = Model::new(); + for message in [ + "Attempting to load COSI file from 'https://user:password@example.test/image.cosi?sig=secret#fragment'", + "Loaded COSI file from 'https://user:password@example.test/image.cosi?sig=secret#fragment' with hash 'abc'", + ] { + let event = redact_event(Event::Response(ServicingEvent::Log(LogRecord { + timestamp: None, + level: LogLevel::Info, + message: message.into(), + module: String::new(), + target: String::new(), + location: None, + }))); + output.record(&event).unwrap(); + let entry = model.event(event); + output.announce(&entry, model.serial_verbosity); + assert!(entry.message.contains("https://example.test/image.cosi")); + for secret in ["user", "password", "sig=secret", "fragment"] { + assert!(!entry.message.contains(secret), "{entry:?}"); + } + } + for text in [ + fs::read_to_string(diagnostic).unwrap(), + fs::read_to_string(serial).unwrap(), + ] { + assert!(text.contains("https://example.test/image.cosi"), "{text}"); + for secret in ["user", "password", "sig=secret", "fragment"] { + assert!(!text.contains(secret), "{text}"); + } + } + } + + /// Daemon failures cannot reintroduce a signed URL through structured error details. + #[test] + fn daemon_image_errors_redact_signed_urls() { + let event = redact_event(Event::Response(ServicingEvent::Completed(Err( + RemoteError { + category: RemoteErrorCategory::Servicing, + message: "Failed to load 'https://user:password@example.test/image.cosi?sig=secret#fragment'".into(), + error_message: "HTTP failure at https://user:password@example.test/image.cosi?sig=secret#fragment".into(), + subkind: "image-load-failed".into(), + location: None, + }, + )))); + let mut model = Model::new(); + let entry = model.event(event); + assert!(model.details.contains("https://example.test/image.cosi")); + for secret in ["user", "password", "sig=secret", "fragment"] { + assert!(!model.details.contains(secret), "{}", model.details); + assert!(!entry.plain().contains(secret)); + } + } + + /// A failed recovery shell cannot erase a successful servicing result or its reboot. + #[test] + fn shell_exit_failure_is_only_a_warning() { + let mut model = Model::new(); + model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::Required, + servicing_kind: Some(ServicingKind::CleanInstall), + image_hash: None, + }, + )))); + let reboot_at = model.reboot_at; + assert!(reboot_at.is_some()); + assert!(shell_result_event(Ok(Ok(()))).is_none()); + let warning = model.event(shell_result_event(Ok(Err(anyhow!("exit 1")))).unwrap()); + assert_eq!(warning.level, LogLevel::Warn); + assert!(warning.plain().contains("Recovery shell failed: exit 1")); + assert_eq!(model.screen, Screen::Success); + assert!(!model.failed); + assert_eq!(model.reboot_at, reboot_at); + } + + /// Recovery reports invalid reloaded configuration instead of using defaults. + #[tokio::test] + async fn recovery_reloads_configuration_without_using_defaults_on_error() { + let root = TempDir::new().unwrap(); + let path = root.path().join("installer.toml"); + for (text, diagnostic) in [("", "mode"), ("mode = 'not-a-mode'", "not-a-mode")] { + fs::write(&path, text).unwrap(); + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let worker = spawn_operation(path.clone(), Request::Autorun, false, tx); + let Event::Error { details, uncertain } = rx.recv().await.unwrap() else { + panic!("Invalid settings must fail before preparing a source"); + }; + assert!(!uncertain); + assert!(details.contains(diagnostic), "{details}"); + worker.await.unwrap().unwrap(); + rx.try_recv().unwrap_err(); + } + } + + /// An enqueued completion must be handled before the worker stops blocking power actions. + #[tokio::test] + async fn pending_completion_keeps_worker_active() { + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let worker = task::spawn(async move { + tx.send(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::NotRequired, + servicing_kind: Some(ServicingKind::CleanInstall), + image_hash: None, + }, + )))) + .await + .unwrap(); + Ok::<(), Error>(()) + }); + time::timeout(Duration::from_secs(1), async { + while !worker.is_finished() { + task::yield_now().await; + } + }) + .await + .unwrap(); + + let mut model = Model::new(); + model.screen = Screen::Recovery; + model.selected = 3; + assert!(!rx.is_empty()); + assert_eq!( + model.key(enter(), !(rx.is_empty() && worker.is_finished())), + Some(Action::BlockedPower) + ); + model.event(rx.recv().await.unwrap()); + assert!(rx.is_empty()); + assert_eq!(model.screen, Screen::Success); + worker.await.unwrap().unwrap(); + } + + /// An uncertain terminal event queued behind a full batch must block shutdown. + #[tokio::test] + async fn pending_disconnect_cannot_release_power_guard() { + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let worker = task::spawn(async move { + for _ in 0..EVENT_CAPACITY { + tx.send(Event::Preparing("Still writing".into())) + .await + .unwrap(); + } + tx.send(Event::Error { + details: "gRPC stream disconnected".into(), + uncertain: true, + }) + .await + .unwrap(); + Ok::<(), Error>(()) + }); + for _ in 0..EVENT_CAPACITY { + rx.recv().await.unwrap(); + } + time::timeout(Duration::from_secs(1), async { + while !worker.is_finished() { + task::yield_now().await; + } + }) + .await + .unwrap(); + + let mut model = Model::new(); + model.screen = Screen::Recovery; + model.selected = 3; + assert!(!rx.is_empty()); + assert_eq!( + model.key(enter(), !(rx.is_empty() && worker.is_finished())), + Some(Action::BlockedPower) + ); + model.event(rx.recv().await.unwrap()); + assert!(model.uncertain); + model.screen = Screen::Recovery; + model.selected = 3; + assert_eq!(model.key(enter(), false), Some(Action::BlockedPower)); + worker.await.unwrap().unwrap(); + } + + /// Rechecking an empty queue before worker completion misses a late disconnect. + #[tokio::test] + async fn disconnect_between_worker_and_queue_checks_stays_guarded() { + let (tx, mut rx) = mpsc::channel(EVENT_CAPACITY); + let (release, ready) = oneshot::channel::<()>(); + let worker = task::spawn(async move { + ready.await.unwrap(); + tx.send(Event::Error { + details: "gRPC stream disconnected".into(), + uncertain: true, + }) + .await + .unwrap(); + Ok::<(), Error>(()) + }); + let previously_empty = rx.is_empty(); + release.send(()).unwrap(); + time::timeout(Duration::from_secs(1), async { + while !worker.is_finished() { + task::yield_now().await; + } + }) + .await + .unwrap(); + assert!(previously_empty && worker.is_finished()); + assert!(!rx.is_empty()); + + let mut model = Model::new(); + model.screen = Screen::Recovery; + model.selected = 3; + assert_eq!( + model.key(enter(), !(worker.is_finished() && rx.is_empty())), + Some(Action::BlockedPower) + ); + model.event(rx.recv().await.unwrap()); + assert!(model.uncertain); + model.screen = Screen::Recovery; + model.selected = 3; + assert_eq!(model.key(enter(), false), Some(Action::BlockedPower)); + worker.await.unwrap().unwrap(); + } +} diff --git a/crates/installer/src/ui/model.rs b/crates/installer/src/ui/model.rs new file mode 100644 index 0000000000..6048f80e11 --- /dev/null +++ b/crates/installer/src/ui/model.rs @@ -0,0 +1,804 @@ +//! Installer screen state, actions and bounded log history. + +use std::{ + borrow::Cow, + collections::VecDeque, + time::{Duration, Instant}, +}; + +use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; +use log::{Level, LevelFilter}; +use ratatui::{ + style::{Color, Modifier, Style}, + text::{Line, Span}, +}; +use trident_client::{LogLevel, ServicingEvent}; + +use crate::{ + client::{self, Completion, Event}, + config::SerialVerbosity, + source::{self, Request}, +}; + +use super::theme::{ + theme_color, COLOR_DEBUG, COLOR_ERROR, COLOR_INFO, COLOR_INST, COLOR_SUCCESS, COLOR_TRACE, + COLOR_TRIDENT, COLOR_WARN, VERBOSITY_LEVELS, +}; + +const REBOOT_DELAY: Duration = Duration::from_secs(5); +const LOG_CAPACITY: usize = 500; +const DISPLAY_LOG_CHARACTERS: usize = 4096; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(super) enum Screen { + Progress, + Error, + Recovery, + Success, + StreamUrl, + HostConfigUrl, + Menu, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(super) enum LogSource { + Inst, + Trident, +} + +impl LogSource { + /// Returns the source tag used in both displayed and mirrored diagnostics. + fn label(self) -> &'static str { + match self { + Self::Inst => "INST", + Self::Trident => "TRIDENT", + } + } + + /// Uses the source's theme color or its Linux VT equivalent. + fn color(self, linux_vt: bool) -> Color { + theme_color( + match self { + Self::Inst => COLOR_INST, + Self::Trident => COLOR_TRIDENT, + }, + linux_vt, + ) + } +} + +#[derive(Debug, Clone)] +pub(super) struct LogEntry { + pub(super) elapsed: Duration, + pub(super) source: LogSource, + pub(super) level: LogLevel, + pub(super) message: String, + pub(super) result_color: Option, +} + +impl LogEntry { + /// Formats elapsed time as a minutes-and-seconds prefix without wrapping at an hour. + fn prefix(&self) -> String { + let seconds = self.elapsed.as_secs(); + format!("{:02}:{:02} [", seconds / 60, seconds % 60) + } + + /// Prefixes each line of a diagnostic for plain output and serial mirrors. + pub(super) fn plain(&self) -> String { + let prefix = format!( + "{}{}:{}] ", + self.prefix(), + self.source.label(), + level_label(self.level) + ); + display_text(&self.message) + .split('\n') + .map(|line| format!("{prefix}{line}")) + .collect::>() + .join("\n") + } + + /// Styles the source and severity separately, highlighting result records as a whole. + pub(super) fn styled(&self, linux_vt: bool) -> Line<'_> { + let level = Style::default().fg(theme_color( + self.result_color.unwrap_or(log_color(self.level)), + linux_vt, + )); + let surrounding = self + .result_color + .map(|color| Style::default().fg(theme_color(color, linux_vt))) + .unwrap_or_default(); + Line::from(vec![ + Span::styled(self.prefix(), surrounding), + Span::styled( + self.source.label(), + Style::default().fg(self.source.color(linux_vt)), + ), + Span::styled(":", surrounding), + Span::styled(level_label(self.level), level.add_modifier(Modifier::BOLD)), + Span::styled("] ", surrounding), + Span::styled(self.message.as_str(), level), + ]) + } +} + +#[derive(Debug)] +pub(super) struct Model { + pub(super) screen: Screen, + pub(super) source: String, + pub(super) activity: String, + pub(super) details: String, + pub(super) logs: VecDeque, + pub(super) verbosity: LevelFilter, + pub(super) serial_verbosity: LevelFilter, + pub(super) verbosity_open: bool, + pub(super) verbosity_selected: usize, + pub(super) selected: usize, + pub(super) scroll: u16, + pub(super) url: String, + pub(super) uncertain: bool, + reboot: bool, + pub(super) reboot_at: Option, + pub(super) started: Instant, + pub(super) failed: bool, + error_details_open: bool, + pub(super) linux_vt: bool, +} + +impl Model { + /// Starts on the progress screen with default display and serial log levels. + pub(super) fn new() -> Self { + Self { + screen: Screen::Progress, + source: String::new(), + activity: "Preparing installer".into(), + details: String::new(), + logs: VecDeque::new(), + verbosity: LevelFilter::Debug, + serial_verbosity: SerialVerbosity::Debug.filter(), + verbosity_open: false, + verbosity_selected: 4, + selected: 0, + scroll: 0, + url: String::new(), + uncertain: false, + reboot: true, + reboot_at: None, + started: Instant::now(), + failed: false, + error_details_open: false, + linux_vt: false, + } + } + + /// Records a failure, preserving any earlier uncertainty and cancelling automatic reboot. + pub(super) fn fail(&mut self, details: String, uncertain: bool) -> LogEntry { + self.details = details.clone(); + self.uncertain |= uncertain; + self.reboot_at = None; + self.failed = true; + self.scroll = 0; + self.selected = 0; + self.screen = Screen::Error; + self.error_details_open = false; + self.result_log(LogLevel::Error, format!("FAILURE: {details}"), COLOR_ERROR) + } + + /// Retains a bounded, single-line display copy while returning the full diagnostic. + pub(super) fn log(&mut self, source: LogSource, level: LogLevel, message: String) -> LogEntry { + let entry = LogEntry { + elapsed: self.started.elapsed(), + source, + level, + message: display_text(&message), + result_color: None, + }; + let mut characters = entry.message.chars(); + let mut displayed = characters + .by_ref() + .take(DISPLAY_LOG_CHARACTERS) + .collect::() + .replace('\n', " "); + if characters.next().is_some() { + displayed.push_str(" [Display shortened; full record preserved in diagnostic log]"); + } + if self.logs.len() == LOG_CAPACITY { + self.logs.pop_front(); + } + self.logs.push_back(LogEntry { + message: displayed, + ..entry.clone() + }); + entry + } + + /// Marks an installer result record with a color even when logs are filtered. + fn result_log(&mut self, level: LogLevel, message: String, color: Color) -> LogEntry { + let mut entry = self.log(LogSource::Inst, level, message); + entry.result_color = Some(color); + if let Some(last) = self.logs.back_mut() { + last.result_color = Some(color); + } + entry + } + + /// Applies a worker event to the current screen and returns its diagnostic record. + pub(super) fn event(&mut self, event: Event) -> LogEntry { + match event { + Event::InstallerLog { level, message } => self.log( + LogSource::Inst, + match level { + Level::Error => LogLevel::Error, + Level::Warn => LogLevel::Warn, + Level::Info => LogLevel::Info, + Level::Debug => LogLevel::Debug, + Level::Trace => LogLevel::Trace, + }, + message, + ), + Event::Preparing(activity) => { + self.activity = activity.clone(); + self.log(LogSource::Inst, LogLevel::Info, activity) + } + Event::Prepared { + description, + reboot, + serial_verbosity, + } => { + self.source = description.clone(); + self.reboot = reboot; + self.serial_verbosity = serial_verbosity.filter(); + self.log(LogSource::Inst, LogLevel::Info, description) + } + Event::IdleConfirmed => { + self.uncertain = false; + self.log( + LogSource::Inst, + LogLevel::Info, + "Trident confirmed that no servicing is active".into(), + ) + } + Event::Error { details, uncertain } => self.fail(details, uncertain), + Event::Response(response) => match response { + ServicingEvent::Started { .. } => { + self.activity = "Servicing started".into(); + self.log(LogSource::Inst, LogLevel::Info, self.activity.clone()) + } + ServicingEvent::Log(log) => { + let level = log.level; + if level == LogLevel::Info { + self.activity = display_text(&log.message); + } + self.log(LogSource::Trident, level, log.message) + } + ServicingEvent::Completed(completed) => match client::completion(completed) { + Ok(Completion::Success { + reboot_required, + performed, + }) => { + self.uncertain = false; + self.failed = false; + self.screen = Screen::Success; + self.selected = 0; + self.details = if performed { + "Installation completed successfully.\nRemove the installation media before booting the installed OS.".into() + } else { + "Trident reported that no servicing was performed.".into() + }; + self.reboot_at = (self.reboot && reboot_required && performed) + .then(|| Instant::now() + REBOOT_DELAY); + self.result_log( + LogLevel::Info, + format!("SUCCESS: {}", self.details), + COLOR_SUCCESS, + ) + } + Ok(Completion::Failure(details)) => { + self.uncertain = false; + self.fail(details, false) + } + Err(error) => { + let details = format!("{error:#}"); + self.fail(details, true) + } + }, + _ => self.fail( + "Unsupported Trident servicing event; installation outcome is unknown".into(), + true, + ), + }, + } + } + + /// Lists actions in keyboard-selection order for the current screen. + pub(super) fn choices(&self) -> Vec<&'static str> { + match self.screen { + Screen::Recovery => { + let mut actions = vec![ + "Shell", + "Stream COSI from URL", + "Install from Host Configuration URL", + ]; + if !self.uncertain { + actions.push("Shutdown"); + } + actions + } + Screen::Success => vec!["Reboot", "Shell"], + Screen::Menu => vec!["Return to installation", "Shell"], + Screen::Error => vec!["Continue to recovery", "Shell"], + _ => Vec::new(), + } + } + + /// Shows the log panel on result screens unless full error details are open. + pub(super) fn showing_logs(&self) -> bool { + matches!( + self.screen, + Screen::Progress | Screen::Menu | Screen::Success + ) || self.screen == Screen::Error && !self.error_details_open + } + + /// Updates navigation for a key press and returns an action only when one is requested. + /// URL entry and the verbosity picker consume keys before screen-level shortcuts. + pub(super) fn key(&mut self, key: KeyEvent, worker_active: bool) -> Option { + if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { + return None; + } + if self.verbosity_open { + match key.code { + KeyCode::Esc => self.verbosity_open = false, + KeyCode::Up | KeyCode::BackTab => { + self.verbosity_selected = (self.verbosity_selected + VERBOSITY_LEVELS.len() + - 1) + % VERBOSITY_LEVELS.len(); + } + KeyCode::Down | KeyCode::Tab => { + self.verbosity_selected = + (self.verbosity_selected + 1) % VERBOSITY_LEVELS.len(); + } + KeyCode::Char(digit) if digit.is_ascii_digit() => { + if let Some(index) = digit.to_digit(10).and_then(|value| value.checked_sub(1)) { + if (index as usize) < VERBOSITY_LEVELS.len() { + self.verbosity_selected = index as usize; + } + } + } + KeyCode::Enter => { + self.verbosity = VERBOSITY_LEVELS[self.verbosity_selected]; + self.verbosity_open = false; + self.scroll = 0; + } + _ => {} + } + return None; + } + if matches!(self.screen, Screen::StreamUrl | Screen::HostConfigUrl) { + return match key.code { + KeyCode::Esc => { + self.screen = Screen::Recovery; + None + } + KeyCode::Backspace => { + self.url.pop(); + None + } + KeyCode::Char(c) if !c.is_control() => { + self.url.push(c); + None + } + KeyCode::Enter => match source::remote_url(self.url.trim()) { + Ok(url) => Some(Action::Start(if self.screen == Screen::StreamUrl { + Request::Stream(url) + } else { + Request::HostConfiguration(url.to_string()) + })), + Err(error) => { + self.fail(format!("{error:#}"), false); + None + } + }, + _ => None, + }; + } + if self.screen == Screen::Error && matches!(key.code, KeyCode::Char('d' | 'D')) { + self.error_details_open = !self.error_details_open; + self.scroll = 0; + return None; + } + if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') { + self.screen = if worker_active || self.screen == Screen::Progress { + Screen::Menu + } else { + Screen::Recovery + }; + self.selected = 0; + return None; + } + let count = self.choices().len(); + match key.code { + KeyCode::Char(digit) if digit.is_ascii_digit() => { + if let Some(index) = digit.to_digit(10).and_then(|value| value.checked_sub(1)) { + if (index as usize) < count { + self.selected = index as usize; + } + } + } + KeyCode::Char('s') => return Some(Action::Shell), + KeyCode::Char('v' | 'V') => { + self.verbosity_selected = VERBOSITY_LEVELS + .iter() + .position(|level| *level == self.verbosity) + .expect("invariant: all log levels are present in the verbosity picker"); + self.verbosity_open = true; + return None; + } + KeyCode::PageUp => { + self.scroll = if self.showing_logs() { + self.scroll.saturating_add(5) + } else { + self.scroll.saturating_sub(5) + }; + return None; + } + KeyCode::PageDown => { + self.scroll = if self.showing_logs() { + self.scroll.saturating_sub(5) + } else { + self.scroll.saturating_add(5) + }; + return None; + } + KeyCode::Esc => { + if self.screen == Screen::Error && self.error_details_open { + self.error_details_open = false; + self.scroll = 0; + return None; + } + self.screen = match self.screen { + Screen::Progress => Screen::Menu, + Screen::Menu => Screen::Progress, + Screen::Error => Screen::Recovery, + other => other, + }; + self.selected = 0; + return None; + } + _ => {} + } + if count == 0 { + return None; + } + match key.code { + KeyCode::Up | KeyCode::BackTab => self.selected = (self.selected + count - 1) % count, + KeyCode::Down | KeyCode::Tab => self.selected = (self.selected + 1) % count, + KeyCode::Enter => { + let action = match (self.screen, self.selected) { + (Screen::Recovery, 0) + | (Screen::Success, 1) + | (Screen::Menu, 1) + | (Screen::Error, 1) => Some(Action::Shell), + (Screen::Recovery, 1) => { + self.url.clear(); + self.screen = Screen::StreamUrl; + None + } + (Screen::Recovery, 2) => { + self.url.clear(); + self.screen = Screen::HostConfigUrl; + None + } + (Screen::Recovery, 3) => Some(Action::Poweroff), + (Screen::Success, 0) => Some(Action::Reboot), + (Screen::Menu, 0) => { + self.screen = Screen::Progress; + None + } + (Screen::Error, 0) => { + self.screen = Screen::Recovery; + self.selected = 0; + None + } + _ => None, + }; + return if (worker_active || self.uncertain) + && matches!(action, Some(Action::Reboot | Action::Poweroff)) + { + Some(Action::BlockedPower) + } else { + action + }; + } + _ => {} + } + None + } +} + +#[derive(Debug, PartialEq, Eq)] +pub(super) enum Action { + Start(Request), + Shell, + BlockedPower, + Reboot, + Poweroff, +} + +/// Removes terminal control characters while retaining newlines and expanding tabs. +pub(super) fn display_text(text: &str) -> String { + text.chars() + .filter(|c| !c.is_control() || *c == '\n' || *c == '\t') + .collect::() + .replace('\t', " ") +} + +/// Compares a Trident severity against the chosen display filter. +pub(super) fn log_visible(level: LogLevel, verbosity: LevelFilter) -> bool { + let level = match level { + LogLevel::Error => Level::Error, + LogLevel::Warn | LogLevel::Unspecified | LogLevel::Unknown(_) => Level::Warn, + LogLevel::Info => Level::Info, + LogLevel::Debug => Level::Debug, + LogLevel::Trace => Level::Trace, + _ => Level::Warn, + }; + level.to_level_filter() <= verbosity +} + +/// Preserves the uppercase severity names used by TUI and serial records. +fn level_label(level: LogLevel) -> Cow<'static, str> { + match level { + LogLevel::Error => Cow::Borrowed("ERROR"), + LogLevel::Warn => Cow::Borrowed("WARN"), + LogLevel::Info => Cow::Borrowed("INFO"), + LogLevel::Debug => Cow::Borrowed("DEBUG"), + LogLevel::Trace => Cow::Borrowed("TRACE"), + LogLevel::Unspecified => Cow::Borrowed("UNSPECIFIED"), + LogLevel::Unknown(value) => Cow::Owned(format!("UNKNOWN({value})")), + _ => Cow::Borrowed("UNSUPPORTED"), + } +} + +/// Selects the severity color for Trident and installer records. +fn log_color(level: LogLevel) -> Color { + match level { + LogLevel::Error => COLOR_ERROR, + LogLevel::Warn | LogLevel::Unspecified | LogLevel::Unknown(_) => COLOR_WARN, + LogLevel::Info => COLOR_INFO, + LogLevel::Debug => COLOR_DEBUG, + LogLevel::Trace => COLOR_TRACE, + _ => COLOR_WARN, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use trident_client::{ + Completion as ClientCompletion, LogRecord, RebootDisposition, ServicingKind, + }; + + use crate::config::Config; + + /// Creates an unmodified Enter press for navigation tests. + fn enter() -> KeyEvent { + KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE) + } + + /// Changing display verbosity hides entries without deleting retained logs. + #[test] + fn live_verbosity_picker_filters_without_discarding_logs() { + let mut model = Model::new(); + model.log(LogSource::Inst, LogLevel::Info, "ordinary event".into()); + model.log( + LogSource::Trident, + LogLevel::Trace, + "diagnostic detail".into(), + ); + assert_eq!(model.verbosity, LevelFilter::Debug); + assert!(!log_visible(LogLevel::Trace, model.verbosity)); + model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), false); + assert!(model.verbosity_open); + model.key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE), false); + model.key(enter(), false); + assert_eq!(model.verbosity, LevelFilter::Trace); + assert!(!model.verbosity_open); + assert!(log_visible(LogLevel::Trace, model.verbosity)); + assert_eq!(model.logs.len(), 2); + model.key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE), false); + model.key(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE), false); + model.key(enter(), false); + assert_eq!(model.verbosity, LevelFilter::Off); + assert!(!log_visible(LogLevel::Error, model.verbosity)); + assert_eq!(model.logs.len(), 2); + } + + /// Preparation events replace the initial idle activity with the current step. + #[test] + fn preparation_steps_replace_the_idle_status() { + let mut model = Model::new(); + let step = "Looking for installer media by filesystem label"; + let entry = model.event(Event::Preparing(step.into())); + assert_eq!(entry.source, LogSource::Inst); + assert_eq!(entry.message, step); + assert_eq!(model.activity, step); + } + + /// Installer logger events retain their own source tag and severity styling. + #[test] + fn installer_logger_records_use_inst_source_and_colour() { + let mut model = Model::new(); + let entry = model.event(Event::InstallerLog { + level: Level::Debug, + message: "Loaded installer settings".into(), + }); + assert_eq!(entry.source, LogSource::Inst); + assert_eq!( + entry.plain(), + "00:00 [INST:DEBUG] Loaded installer settings" + ); + assert_eq!( + entry.styled(false).spans[1].style.fg, + Some(Color::Rgb(171, 186, 196)) + ); + assert_eq!( + entry.styled(false).spans[3].style.fg, + Some(Color::Rgb(167, 180, 189)) + ); + assert_eq!(model.logs.len(), 1); + } + + /// Domain logs retain Trident's source tag and update the displayed activity. + #[test] + fn trident_domain_log_event_updates_activity_and_serial_record() { + let mut model = Model::new(); + let entry = model.event(Event::Response(ServicingEvent::Log(LogRecord { + timestamp: None, + level: LogLevel::Info, + message: "Preparing storage".into(), + module: String::new(), + target: String::new(), + location: None, + }))); + assert_eq!(entry.source, LogSource::Trident); + assert_eq!(model.activity, "Preparing storage"); + assert_eq!(entry.plain(), "00:00 [TRIDENT:INFO] Preparing storage"); + } + + /// Unknown daemon severities remain identifiable in serial output and visible as warnings. + #[test] + fn unknown_log_level_is_not_silently_discarded() { + let mut model = Model::new(); + let entry = model.event(Event::Response(ServicingEvent::Log(LogRecord { + timestamp: None, + level: LogLevel::Unknown(42), + message: "New severity".into(), + module: String::new(), + target: String::new(), + location: None, + }))); + assert_eq!(entry.plain(), "00:00 [TRIDENT:UNKNOWN(42)] New severity"); + assert!(log_visible(entry.level, LevelFilter::Warn)); + assert_eq!(log_color(entry.level), COLOR_WARN); + } + + /// The autorun default schedules a reboot when servicing requires one. + #[test] + fn headless_autorun_keeps_automatic_reboot_enabled_by_default() { + let config = Config::parse("mode = 'autorun'\nserialMode = 'logs'").unwrap(); + let mut model = Model::new(); + model.reboot = config.autorun.reboot; + model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::Required, + servicing_kind: None, + image_hash: None, + }, + )))); + assert!(model.reboot_at.is_some()); + } + + /// Unknown servicing kinds fail rather than triggering an automatic reboot. + #[test] + fn unknown_completion_kind_does_not_claim_success() { + let mut model = Model::new(); + model.reboot = true; + let entry = model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::Required, + servicing_kind: Some(ServicingKind::Unknown(42)), + image_hash: None, + }, + )))); + assert!(model.failed); + assert!(model.uncertain); + assert!(model.reboot_at.is_none()); + assert!(entry.plain().contains("Unsupported daemon servicing kind")); + } + + /// Missing inputs produce a tagged failure record suitable for serial output. + #[test] + fn errors_for_missing_inputs_are_tagged_for_serial_without_a_tui() { + let mut model = Model::new(); + let entry = model.event(Event::Error { + details: "No image or Host Configuration".into(), + uncertain: false, + }); + assert!(model.failed); + assert_eq!( + entry.plain(), + "00:00 [INST:ERROR] FAILURE: No image or Host Configuration" + ); + } + + /// Continuing from an error opens recovery; shell actions leave their screen unchanged. + #[test] + fn error_continue_and_shell_keep_the_originating_screen() { + let mut model = Model::new(); + model.fail("Complete diagnostic information".into(), false); + assert!(model.key(enter(), false).is_none()); + assert_eq!(model.screen, Screen::Recovery); + assert_eq!(model.key(enter(), false), Some(Action::Shell)); + assert_eq!(model.screen, Screen::Recovery); + assert_eq!(model.details, "Complete diagnostic information"); + model.screen = Screen::Success; + model.selected = 1; + assert_eq!(model.key(enter(), false), Some(Action::Shell)); + assert_eq!(model.screen, Screen::Success); + model.selected = 0; + assert_eq!(model.key(enter(), false), Some(Action::Reboot)); + } + + /// Repeated interrupts and power choices cannot shut down an active installation. + #[test] + fn active_worker_cannot_reach_a_power_action() { + let mut model = Model::new(); + let interrupt = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL); + model.key(interrupt, true); + assert_eq!(model.screen, Screen::Menu); + model.key(interrupt, true); + assert_eq!(model.screen, Screen::Menu); + + model.screen = Screen::Recovery; + model.selected = 3; + assert_eq!(model.key(enter(), true), Some(Action::BlockedPower)); + assert_eq!(model.key(enter(), false), Some(Action::Poweroff)); + model.screen = Screen::Success; + model.selected = 0; + assert_eq!(model.key(enter(), true), Some(Action::BlockedPower)); + } + + /// Losing the client stream must not make shutdown safe until Trident confirms idleness. + #[test] + fn uncertain_outcome_blocks_power_after_worker_exits() { + let mut model = Model::new(); + model.event(Event::Error { + details: "gRPC stream disconnected; servicing may continue".into(), + uncertain: true, + }); + model.screen = Screen::Recovery; + model.selected = 3; + assert!(!model.choices().contains(&"Shutdown")); + assert_eq!(model.key(enter(), false), Some(Action::BlockedPower)); + + model.event(Event::IdleConfirmed); + assert!(!model.uncertain); + assert!(model.choices().contains(&"Shutdown")); + assert_eq!(model.key(enter(), false), Some(Action::Poweroff)); + + model.event(Event::Error { + details: "Another stream disconnected".into(), + uncertain: true, + }); + model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::NotRequired, + servicing_kind: Some(ServicingKind::CleanInstall), + image_hash: None, + }, + )))); + assert!(!model.uncertain); + assert_eq!(model.key(enter(), false), Some(Action::Reboot)); + } +} diff --git a/crates/installer/src/ui/output.rs b/crates/installer/src/ui/output.rs new file mode 100644 index 0000000000..3c107541ea --- /dev/null +++ b/crates/installer/src/ui/output.rs @@ -0,0 +1,144 @@ +//! Persists installer diagnostics and mirrors eligible records to serial consoles. + +use std::{fs::File, io::Write, path::PathBuf}; + +use anyhow::{Context, Error}; +use log::{warn, LevelFilter}; + +use crate::client::Event; + +use super::model::{log_visible, LogEntry}; + +pub(crate) struct Output { + pub(crate) log: Option, + pub(crate) mirrors: Vec<(PathBuf, File)>, +} + +impl Output { + /// Persists the complete event in the diagnostic file, if one is configured. + pub(super) fn record(&mut self, event: &Event) -> Result<(), Error> { + if let Some(log) = &mut self.log { + writeln!(log, "{event:#?}").context("Failed to preserve installer diagnostics")?; + log.flush()?; + } + Ok(()) + } + + /// Sends visible records to serial mirrors, dropping mirrors that fail to write. + pub(super) fn announce(&mut self, entry: &LogEntry, verbosity: LevelFilter) { + if !log_visible(entry.level, verbosity) { + return; + } + self.mirrors.retain_mut(|(path, mirror)| { + if let Err(error) = writeln!(mirror, "{}", entry.plain()) { + warn!( + "Stopped mirroring installer status to '{}': {error}", + path.display() + ); + return false; + } + true + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::{ + fs::{self, OpenOptions}, + time::Duration, + }; + + use tempfile::TempDir; + use trident_client::LogLevel; + + use crate::config::SerialVerbosity; + + use super::super::model::{LogSource, Model}; + + /// A failed serial destination is removed without blocking healthy mirrors. + #[test] + fn unavailable_serial_mirror_does_not_stop_the_installer() { + let root = TempDir::new().unwrap(); + let transcript = root.path().join("serial.log"); + let mut output = Output { + log: None, + mirrors: vec![ + ( + PathBuf::from("/dev/full"), + OpenOptions::new().write(true).open("/dev/full").unwrap(), + ), + ( + transcript.clone(), + OpenOptions::new() + .create(true) + .truncate(true) + .write(true) + .open(&transcript) + .unwrap(), + ), + ], + }; + let entry = LogEntry { + elapsed: Duration::from_secs(125), + source: LogSource::Inst, + level: LogLevel::Info, + message: "Preparing installer".into(), + result_color: None, + }; + output.announce(&entry, LevelFilter::Debug); + assert_eq!(output.mirrors.len(), 1); + output.announce( + &LogEntry { + message: "Still running".into(), + ..entry + }, + LevelFilter::Debug, + ); + assert_eq!( + fs::read_to_string(transcript).unwrap(), + "02:05 [INST:INFO] Preparing installer\n02:05 [INST:INFO] Still running\n" + ); + } + + /// Serial filtering preserves line prefixes and does not change display filtering. + #[test] + fn serial_verbosity_filters_independently_of_display() { + let mut model = Model::new(); + let root = TempDir::new().unwrap(); + let serial = root.path().join("serial.log"); + let mut output = Output { + log: None, + mirrors: vec![(serial.clone(), File::create(&serial).unwrap())], + }; + for level in [ + LogLevel::Error, + LogLevel::Warn, + LogLevel::Info, + LogLevel::Debug, + LogLevel::Trace, + ] { + let entry = model.log(LogSource::Trident, level, "first\nsecond".into()); + output.announce(&entry, SerialVerbosity::Debug.filter()); + } + let text = fs::read_to_string(&serial).unwrap(); + let lines = text.lines().collect::>(); + assert_eq!(lines.len(), 8); + assert!(lines.iter().all(|line| line.starts_with("00:00 [TRIDENT:"))); + assert_eq!(lines[0], "00:00 [TRIDENT:ERROR] first"); + assert_eq!(lines[1], "00:00 [TRIDENT:ERROR] second"); + assert!(!text.contains("\\n")); + let trace = model.log(LogSource::Trident, LogLevel::Trace, "trace detail".into()); + output.announce(&trace, SerialVerbosity::Trace.filter()); + let text = fs::read_to_string(serial).unwrap(); + assert!(text + .lines() + .last() + .unwrap() + .ends_with("[TRIDENT:TRACE] trace detail")); + assert!(!log_visible(LogLevel::Trace, model.verbosity)); + assert_eq!(model.logs.len(), 6); + } +} diff --git a/crates/installer/src/ui/render.rs b/crates/installer/src/ui/render.rs new file mode 100644 index 0000000000..59a9c76bee --- /dev/null +++ b/crates/installer/src/ui/render.rs @@ -0,0 +1,715 @@ +//! Draws the Ratatui screens, their action lists and the scrollable log panel. + +use std::time::Instant; + +use ratatui::{ + layout::{Alignment, Constraint, Layout, Rect}, + style::{Color, Modifier, Style}, + symbols::border::Set as BorderSet, + text::{Line, Span}, + widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Wrap}, + Frame, +}; + +use crate::APPLICATION_NAME; + +use super::{ + model::{display_text, log_visible, Model, Screen}, + theme::{ + theme_color, COLOR_BACKGROUND, COLOR_BRAND, COLOR_ERROR, COLOR_FRAME, COLOR_SUCCESS, + VERBOSITY_LEVELS, + }, +}; + +const WORDMARK_HEADER_HEIGHT: u16 = 12; +const COMPACT_HEADER_HEIGHT: u16 = 2; +const MIN_WORDMARK_TERMINAL_HEIGHT: u16 = 24; +const TRIDENT_WORDMARK: [&str; 8] = [ + r#"88888888888 d8b 888 888"#, + r#" 888 Y8P 888 888"#, + r#" 888 888 888"#, + r#" 888 888d888 888 .d88888 .d88b. 88888b. 888888"#, + r#" 888 888P" 888 d88" 888 d8P Y8b 888 "88b 888"#, + r#" 888 888 888 888 888 88888888 888 888 888"#, + r#" 888 888 888 Y88b 888 Y8b. 888 888 Y88b."#, + r#" 888 888 888 "Y88888 "Y8888 888 888 "Y888"#, +]; +const ASCII_BORDER: BorderSet = BorderSet { + top_left: "+", + top_right: "+", + bottom_left: "+", + bottom_right: "+", + vertical_left: "|", + vertical_right: "|", + horizontal_top: "-", + horizontal_bottom: "-", +}; + +/// Provides the short status heading for each installer screen. +pub(super) fn title(screen: Screen) -> &'static str { + match screen { + Screen::Progress => "INSTALLING", + Screen::Error => "INSTALLATION ERROR", + Screen::Recovery => "RECOVERY", + Screen::Success => "INSTALLATION COMPLETE", + Screen::StreamUrl => "STREAM COSI FROM URL", + Screen::HostConfigUrl => "INSTALL FROM HOST CONFIGURATION URL", + Screen::Menu => "INSTALLER MENU", + } +} + +/// Gives panels a consistent ASCII border and VT-aware background. +fn block(title: &str, linux_vt: bool) -> Block<'_> { + Block::default() + .borders(Borders::ALL) + .border_set(ASCII_BORDER) + .style(Style::default().bg(theme_color(COLOR_BACKGROUND, linux_vt))) + .border_style(Style::default().fg(theme_color(COLOR_FRAME, linux_vt))) + .title(title) +} + +/// Finds the widest wordmark row for layout and centering. +fn wordmark_width() -> usize { + TRIDENT_WORDMARK + .iter() + .fold(0, |width, row| width.max(row.len())) +} + +/// Reserves wordmark space only when both the terminal width and height fit it. +fn header_height(viewport: Rect) -> u16 { + if usize::from(viewport.width) >= wordmark_width() + && viewport.height >= MIN_WORDMARK_TERMINAL_HEIGHT + { + WORDMARK_HEADER_HEIGHT + } else { + COMPACT_HEADER_HEIGHT + } +} + +/// Draws the wordmark when space permits, plus the installer status. +fn render_header(frame: &mut Frame, area: Rect, state: &str, state_color: Color, linux_vt: bool) { + let brand = Style::default() + .fg(theme_color(COLOR_BRAND, linux_vt)) + .add_modifier(Modifier::BOLD); + let mut lines = Vec::new(); + if area.height >= WORDMARK_HEADER_HEIGHT { + lines.push(Line::default()); + let width = wordmark_width(); + lines.extend( + TRIDENT_WORDMARK + .iter() + .map(|row| Line::from(Span::styled(format!("{row: Paragraph<'_> { + let (title, color) = match model.screen { + Screen::Success => ("SUCCESS - Installation complete", COLOR_SUCCESS), + Screen::Error => ("FAILURE - Installation stopped", COLOR_ERROR), + _ => ("Activity / logs", COLOR_FRAME), + }; + let mut lines = Vec::new(); + let header_lines = if matches!(model.screen, Screen::Progress | Screen::Menu) { + 2 + } else { + 0 + }; + if header_lines > 0 { + lines.push(Line::from(Span::styled( + model.activity.clone(), + Style::default().fg(theme_color(COLOR_BRAND, model.linux_vt)), + ))); + lines.push(Line::default()); + } + let mut logs = model + .logs + .iter() + .filter(|entry| entry.result_color.is_some() || log_visible(entry.level, model.verbosity)) + .collect::>(); + if matches!(model.screen, Screen::Success | Screen::Error) { + if let Some(index) = logs.iter().rposition(|entry| entry.result_color.is_some()) { + let result = logs.remove(index); + logs.push(result); + } + } + lines.extend(logs.into_iter().map(|entry| entry.styled(model.linux_vt))); + let block = Block::default() + .borders(Borders::ALL) + .border_set(ASCII_BORDER) + .style(Style::default().bg(theme_color(COLOR_BACKGROUND, model.linux_vt))) + .border_style(Style::default().fg(theme_color(color, model.linux_vt))) + .title(Span::styled( + title, + Style::default().fg(theme_color(color, model.linux_vt)), + )); + let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false }); + let visible_rows = usize::from(area.height.saturating_sub(2)); + let total_rows = paragraph.line_count(area.width.saturating_sub(2).max(1)); + let offset = total_rows + .saturating_sub(visible_rows) + .saturating_sub(usize::from(model.scroll)); + paragraph + .block(block) + .scroll((u16::try_from(offset).unwrap_or(u16::MAX), 0)) +} + +/// Draws the current screen, actions, log controls, and any pending reboot countdown. +pub(super) fn render(frame: &mut Frame, model: &Model) { + frame.render_widget( + Block::default().style(Style::default().bg(theme_color(COLOR_BACKGROUND, model.linux_vt))), + frame.area(), + ); + let choices = model.choices(); + let action_height = if choices.is_empty() { + 0 + } else { + u16::try_from(choices.len()) + .expect("invariant: static action lists have at most four items") + + 2 + }; + let areas = Layout::vertical([ + Constraint::Length(header_height(frame.area())), + Constraint::Length(match model.screen { + Screen::Recovery => 0, + _ => 2, + }), + Constraint::Min(3), + Constraint::Length(action_height), + Constraint::Length(2), + ]) + .split(frame.area()); + render_header( + frame, + areas[0], + title(model.screen), + match model.screen { + Screen::Error => COLOR_ERROR, + Screen::Success => COLOR_SUCCESS, + _ => COLOR_BRAND, + }, + model.linux_vt, + ); + if areas[1].width > 22 { + let operation = + Layout::horizontal([Constraint::Min(0), Constraint::Length(21)]).split(areas[1]); + frame.render_widget( + Paragraph::new(display_text(&model.source)).wrap(Wrap { trim: false }), + operation[0], + ); + frame.render_widget( + Paragraph::new(format!("Logs: {} [V]", model.verbosity)) + .alignment(Alignment::Right) + .style(Style::default().fg(theme_color(COLOR_BRAND, model.linux_vt))), + operation[1], + ); + } else { + frame.render_widget( + Paragraph::new(display_text(&model.source)).wrap(Wrap { trim: false }), + areas[1], + ); + } + let content = match model.screen { + _ if model.showing_logs() => log_panel(model, areas[2]), + Screen::StreamUrl | Screen::HostConfigUrl => Paragraph::new(format!( + "Enter an http:// or https:// URL:\n\n{}\n\nEnter: start Esc: back", + display_text(&model.url) + )) + .block(block("Remote source", model.linux_vt)) + .wrap(Wrap { trim: false }), + Screen::Error => Paragraph::new(display_text(&model.details)) + .block( + block("Full error details / Esc to return", model.linux_vt) + .border_style(Style::default().fg(theme_color(COLOR_ERROR, model.linux_vt))), + ) + .wrap(Wrap { trim: false }) + .scroll((model.scroll, 0)), + _ => Paragraph::new(display_text(&model.details)) + .block(block("Result", model.linux_vt)) + .wrap(Wrap { trim: false }), + }; + frame.render_widget(content, areas[2]); + let choices = choices + .iter() + .enumerate() + .map(|(index, choice)| { + ListItem::new(format!( + "{} {choice}", + if index == model.selected { ">" } else { " " } + )) + .style(if index == model.selected { + Style::default() + .fg(theme_color(COLOR_BRAND, model.linux_vt)) + .add_modifier(Modifier::BOLD) + } else { + Style::default() + }) + }) + .collect::>(); + frame.render_widget( + List::new(choices).block(block("Actions", model.linux_vt)), + areas[3], + ); + let countdown = model + .reboot_at + .map(|deadline| { + format!( + "Automatic reboot in {}s; Shell holds reboot", + deadline.saturating_duration_since(Instant::now()).as_secs() + ) + }) + .unwrap_or_else(|| format!("Elapsed {}s", model.started.elapsed().as_secs())); + let controls = if matches!(model.screen, Screen::StreamUrl | Screen::HostConfigUrl) { + "Enter: start Esc: return to recovery".to_owned() + } else { + format!( + "Arrows: choose Enter: select S: shell V: logs{} Esc: back", + if model.screen == Screen::Error { + " D: details" + } else { + "" + }, + ) + }; + frame.render_widget( + Paragraph::new(format!("{controls}\n{countdown}")) + .style(Style::default().fg(theme_color(COLOR_BRAND, model.linux_vt))), + areas[4], + ); + if model.verbosity_open { + render_verbosity_picker(frame, model); + } +} + +/// Overlays the display-only log filter without modifying the stored diagnostics. +fn render_verbosity_picker(frame: &mut Frame, model: &Model) { + let viewport = frame.area(); + let width = viewport.width.min(44); + let height = viewport.height.min(10); + let area = Rect::new( + viewport.x + (viewport.width - width) / 2, + viewport.y + (viewport.height - height) / 2, + width, + height, + ); + frame.render_widget(Clear, area); + let rows = VERBOSITY_LEVELS + .iter() + .enumerate() + .map(|(index, level)| { + Line::from(Span::styled( + format!( + "{} {level}", + if index == model.verbosity_selected { + ">" + } else { + " " + } + ), + if index == model.verbosity_selected { + Style::default() + .fg(theme_color(COLOR_BRAND, model.linux_vt)) + .add_modifier(Modifier::BOLD) + } else { + Style::default() + }, + )) + }) + .chain([Line::default(), Line::from("Enter: apply Esc: cancel")]) + .collect::>(); + frame.render_widget( + Paragraph::new(rows).block(block("Log verbosity / display only", model.linux_vt)), + area, + ); +} + +#[cfg(test)] +mod tests { + use super::*; + + use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; + use log::LevelFilter; + use ratatui::{backend::TestBackend, style::Color, Terminal}; + + use trident_client::{ + Completion as ClientCompletion, LogLevel, RebootDisposition, ServicingEvent, ServicingKind, + }; + + use crate::{client::Event, APPLICATION_NAME}; + + use super::super::{ + model::LogSource, + theme::{vt_rgb_palette, COLOR_DEBUG, COLOR_INFO, COLOR_TRACE}, + }; + + /// The full-size header and background keep their intended colors and spacing. + #[test] + fn wordmark_and_background_use_clear_neutrals_palette_at_80_by_24() { + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal.draw(|frame| render(frame, &Model::new())).unwrap(); + let buffer = terminal.backend().buffer(); + assert_eq!(buffer.content[0].bg, Color::Rgb(11, 17, 24)); + assert_eq!(buffer.content[14 * 80].fg, Color::Rgb(53, 69, 83)); + let padding = (80 - wordmark_width()) / 2; + assert!(buffer.content[..80].iter().all(|cell| cell.symbol() == " ")); + for (row_index, expected) in TRIDENT_WORDMARK.iter().enumerate() { + let row_index = row_index + 1; + let row = buffer.content[row_index * 80..(row_index + 1) * 80] + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert_eq!(&row[padding..padding + expected.len()], *expected); + for column in padding..padding + expected.len() { + assert_eq!( + buffer.content[row_index * 80 + column].fg, + Color::Rgb(75, 216, 220) + ); + } + assert!(buffer.content[9 * 80..10 * 80] + .iter() + .all(|cell| cell.symbol() == " ")); + } + let text = buffer + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains(APPLICATION_NAME), "{text}"); + assert!(text.contains("INSTALLING"), "{text}"); + } + + /// RPC log severities and the Trident source receive independent colors. + #[test] + fn rpc_logs_are_coloured_by_severity() { + let mut model = Model::new(); + model.verbosity = LevelFilter::Trace; + let records = [ + (LogLevel::Error, "error-line", Color::Rgb(255, 107, 115)), + (LogLevel::Warn, "warn-line", Color::Rgb(245, 182, 93)), + (LogLevel::Info, "info-line", Color::Rgb(229, 234, 236)), + (LogLevel::Debug, "debug-line", Color::Rgb(167, 180, 189)), + (LogLevel::Trace, "trace-line", Color::Rgb(99, 115, 129)), + ]; + for (level, text, _) in records { + model.log(LogSource::Trident, level, text.into()); + } + let mut terminal = Terminal::new(TestBackend::new(80, 32)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); + let buffer = terminal.backend().buffer(); + for (_, message, expected) in records { + let row = buffer + .content + .chunks(80) + .find(|row| { + row.iter() + .map(|cell| cell.symbol()) + .collect::() + .contains(message) + }) + .unwrap(); + let text = row.iter().map(|cell| cell.symbol()).collect::(); + assert_eq!(row[text.find(message).unwrap()].fg, expected); + let source = text.find("TRIDENT").unwrap(); + assert_eq!(row[source].fg, Color::Rgb(114, 185, 165)); + } + } + + /// VT palette slots distinguish log severities without changing RGB-mode colors. + #[test] + fn linux_vt_palette_uses_distinct_slots_for_a1_log_levels() { + let entries = vt_rgb_palette().unwrap(); + assert!(entries.contains(&(15, 229, 234, 236))); + assert!(entries.contains(&(7, 167, 180, 189))); + assert!(entries.contains(&(8, 99, 115, 129))); + assert_eq!(theme_color(COLOR_INFO, true), Color::White); + assert_eq!(theme_color(COLOR_DEBUG, true), Color::Gray); + assert_eq!(theme_color(COLOR_TRACE, true), Color::DarkGray); + assert_eq!(theme_color(COLOR_SUCCESS, true), Color::LightGreen); + assert_eq!(theme_color(COLOR_DEBUG, false), Color::Rgb(167, 180, 189)); + + let mut model = Model::new(); + model.linux_vt = true; + model.verbosity = LevelFilter::Trace; + for (level, message) in [ + (LogLevel::Info, "info-vt"), + (LogLevel::Debug, "debug-vt"), + (LogLevel::Trace, "trace-vt"), + ] { + model.log(LogSource::Trident, level, message.into()); + } + let mut terminal = Terminal::new(TestBackend::new(80, 32)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); + let buffer = terminal.backend().buffer(); + for (message, expected) in [ + ("info-vt", Color::White), + ("debug-vt", Color::Gray), + ("trace-vt", Color::DarkGray), + ] { + let row = buffer + .content + .chunks(80) + .find(|row| { + row.iter() + .map(|cell| cell.symbol()) + .collect::() + .contains(message) + }) + .unwrap(); + let text = row.iter().map(|cell| cell.symbol()).collect::(); + assert_eq!(row[text.find(message).unwrap()].fg, expected); + } + } + + /// The source and verbosity indicator both fit on the standard-width status line. + #[test] + fn operation_and_verbosity_share_one_line() { + let mut model = Model::new(); + model.source = "StreamDisk from file:///media/cosi/payload.cosi".into(); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); + let row = terminal.backend().buffer().content[12 * 80..13 * 80] + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(row.contains("StreamDisk from file://"), "{row}"); + assert!(row.contains("Logs: DEBUG [V]"), "{row}"); + } + + /// A success result remains highlighted even when earlier logs are filtered out. + #[test] + fn success_keeps_prior_logs_and_highlights_the_final_record() { + let mut model = Model::new(); + model.log( + LogSource::Trident, + LogLevel::Debug, + "Earlier disk action".into(), + ); + let result = model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::NotRequired, + servicing_kind: None, + image_hash: None, + }, + )))); + assert_eq!(result.result_color, Some(Color::Rgb(93, 200, 148))); + assert_eq!( + result.plain(), + "00:00 [INST:INFO] SUCCESS: Installation completed successfully.\n\ + 00:00 [INST:INFO] Remove the installation media before booting the installed OS." + ); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); + let rows = terminal + .backend() + .buffer() + .content + .chunks(80) + .collect::>(); + let rendered = rows + .iter() + .map(|row| row.iter().map(|cell| cell.symbol()).collect::()) + .collect::>(); + let title = rendered + .iter() + .position(|row| row.contains("SUCCESS - Installation complete")) + .unwrap(); + assert_eq!( + rows[title][rendered[title].find("SUCCESS").unwrap()].fg, + Color::Rgb(93, 200, 148) + ); + let result_row = rendered + .iter() + .position(|row| row.contains("SUCCESS: Installation")) + .unwrap(); + assert_eq!( + rows[result_row][rendered[result_row].find("SUCCESS:").unwrap()].fg, + Color::Rgb(93, 200, 148) + ); + model.key(KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), false); + terminal.draw(|frame| render(frame, &model)).unwrap(); + let earlier = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(earlier.contains("Earlier disk action")); + model.scroll = 0; + model.verbosity = LevelFilter::Off; + terminal.draw(|frame| render(frame, &model)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains("SUCCESS: Installation")); + assert!(!text.contains("Earlier disk action")); + } + + /// Wrapped diagnostics and activity cannot hide the latest result at 80x24. + #[test] + fn wrapped_log_rows_keep_the_result_visible_and_scrollable() { + let mut model = Model::new(); + model.log( + LogSource::Trident, + LogLevel::Info, + "Long preceding diagnostic ".repeat(40), + ); + model.event(Event::Response(ServicingEvent::Completed(Ok( + ClientCompletion { + reboot: RebootDisposition::NotRequired, + servicing_kind: Some(ServicingKind::CleanInstall), + image_hash: None, + }, + )))); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + let visible_text = |terminal: &Terminal| { + terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::() + }; + terminal.draw(|frame| render(frame, &model)).unwrap(); + assert!( + visible_text(&terminal).contains("SUCCESS: Installation"), + "{}", + visible_text(&terminal) + ); + model.key(KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE), false); + terminal.draw(|frame| render(frame, &model)).unwrap(); + assert!(!visible_text(&terminal).contains("SUCCESS: Installation")); + model.key(KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE), false); + terminal.draw(|frame| render(frame, &model)).unwrap(); + assert!(visible_text(&terminal).contains("SUCCESS: Installation")); + + model.screen = Screen::Progress; + model.activity = "Scanning media ".repeat(60); + model.log(LogSource::Inst, LogLevel::Info, "Latest activity".into()); + terminal.draw(|frame| render(frame, &model)).unwrap(); + assert!(visible_text(&terminal).contains("Latest activity")); + } + + /// Failures show prior logs first while still allowing full diagnostic details. + #[test] + fn failure_shows_logs_by_default_and_keeps_full_details_accessible() { + let mut model = Model::new(); + model.log(LogSource::Trident, LogLevel::Warn, "Earlier warning".into()); + let result = model.event(Event::Error { + details: "Network timeout\nUnderlying transport failure".into(), + uncertain: false, + }); + assert_eq!(result.result_color, Some(Color::Rgb(255, 107, 115))); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains("FAILURE - Installation stopped")); + assert!(text.contains("Earlier warning")); + assert!(text.contains("FAILURE: Network timeout")); + for marker in ["FAILURE - Installation stopped", "FAILURE: Network timeout"] { + let row = terminal + .backend() + .buffer() + .content + .chunks(80) + .find(|row| { + row.iter() + .map(|cell| cell.symbol()) + .collect::() + .contains(marker) + }) + .unwrap(); + let content = row.iter().map(|cell| cell.symbol()).collect::(); + assert_eq!( + row[content.find(marker).unwrap()].fg, + Color::Rgb(255, 107, 115) + ); + } + model.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::NONE), false); + assert!(!model.showing_logs()); + terminal.draw(|frame| render(frame, &model)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains("Underlying transport failure")); + model.key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), false); + assert_eq!(model.screen, Screen::Error); + assert!(model.showing_logs()); + } + + /// Compact layouts retain the application heading and available actions. + #[test] + fn small_terminals_use_a_readable_compact_header() { + assert_eq!( + header_height(Rect::new(0, 0, 40, 24)), + COMPACT_HEADER_HEIGHT + ); + assert_eq!( + header_height(Rect::new(0, 0, 80, 16)), + COMPACT_HEADER_HEIGHT + ); + let mut model = Model::new(); + model.screen = Screen::Success; + let mut terminal = Terminal::new(TestBackend::new(40, 16)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains(APPLICATION_NAME), "{text}"); + assert!(text.contains("Reboot"), "{text}"); + assert!(text.contains("Shell"), "{text}"); + } + + /// Standard-size result and recovery screens expose their expected actions. + #[test] + fn screens_render_at_80_by_24_with_visible_actions() { + for screen in [Screen::Recovery, Screen::Success, Screen::Error] { + let mut model = Model::new(); + model.screen = screen; + model.details = "Diagnostic or result details".into(); + let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); + terminal.draw(|frame| render(frame, &model)).unwrap(); + let text = terminal + .backend() + .buffer() + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(text.contains("Shell"), "{text}"); + if screen == Screen::Success { + assert!(text.contains("Reboot"), "{text}"); + } + if screen == Screen::Recovery { + assert!(text.contains("Shutdown"), "{text}"); + } + } + } +} diff --git a/crates/installer/src/ui/theme.rs b/crates/installer/src/ui/theme.rs new file mode 100644 index 0000000000..52b88cadc4 --- /dev/null +++ b/crates/installer/src/ui/theme.rs @@ -0,0 +1,67 @@ +//! Shares the terminal colour palette between logs, rendering and Linux VT setup. + +use std::io; + +use log::LevelFilter; +use ratatui::style::Color; + +pub(super) const COLOR_BACKGROUND: Color = Color::Rgb(11, 17, 24); +pub(super) const COLOR_FRAME: Color = Color::Rgb(53, 69, 83); +pub(super) const COLOR_BRAND: Color = Color::Rgb(75, 216, 220); +pub(super) const COLOR_INST: Color = Color::Rgb(171, 186, 196); +pub(super) const COLOR_TRIDENT: Color = Color::Rgb(114, 185, 165); +pub(super) const COLOR_ERROR: Color = Color::Rgb(255, 107, 115); +pub(super) const COLOR_WARN: Color = Color::Rgb(245, 182, 93); +pub(super) const COLOR_INFO: Color = Color::Rgb(229, 234, 236); +pub(super) const COLOR_DEBUG: Color = Color::Rgb(167, 180, 189); +pub(super) const COLOR_TRACE: Color = Color::Rgb(99, 115, 129); +pub(super) const COLOR_SUCCESS: Color = Color::Rgb(93, 200, 148); +// Linux VT maps RGB SGR values into 16 colours, so give each UI role its own ANSI slot. +const VT_PALETTE: &[(u8, Color, Color)] = &[ + (0, COLOR_BACKGROUND, Color::Black), + (1, COLOR_ERROR, Color::Red), + (2, COLOR_TRIDENT, Color::Green), + (3, COLOR_WARN, Color::Yellow), + (4, COLOR_FRAME, Color::Blue), + (5, COLOR_INST, Color::Magenta), + (6, COLOR_BRAND, Color::Cyan), + (7, COLOR_DEBUG, Color::Gray), + (8, COLOR_TRACE, Color::DarkGray), + (10, COLOR_SUCCESS, Color::LightGreen), + (15, COLOR_INFO, Color::White), +]; + +/// Maps known theme RGB colors to distinct ANSI roles on Linux VTs. +/// Other colors and non-VT terminals retain the original value. +pub(super) fn theme_color(color: Color, linux_vt: bool) -> Color { + if !linux_vt { + return color; + } + VT_PALETTE + .iter() + .find(|(_, rgb, _)| *rgb == color) + .map(|(_, _, ansi)| *ansi) + .unwrap_or(color) +} + +/// Builds the Linux VT palette entries, rejecting any theme slot without an RGB value. +pub(super) fn vt_rgb_palette() -> Result, io::Error> { + VT_PALETTE + .iter() + .map(|(slot, color, _)| match color { + Color::Rgb(red, green, blue) => Ok((*slot, *red, *green, *blue)), + other => Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("Linux VT palette requires RGB values, got {other:?}"), + )), + }) + .collect() +} +pub(super) const VERBOSITY_LEVELS: [LevelFilter; 6] = [ + LevelFilter::Off, + LevelFilter::Error, + LevelFilter::Warn, + LevelFilter::Info, + LevelFilter::Debug, + LevelFilter::Trace, +]; From f1126332f5711fe8803e37adfa4c0d4a59464d5b Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 13 Nov 2026 17:50:03 -0800 Subject: [PATCH 54/62] fix: Wait for media only when installer sources depend on it Use complete live-OS and remote Host Configurations without media discovery or overlays. Keep the wait for media-relative inputs, media-backed images, and dangling symlinks into installer media. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 9 +- crates/installer/src/source.rs | 230 ++++++++++++++++++++-- docs/Reference/Installer-Configuration.md | 30 +-- 3 files changed, 244 insertions(+), 25 deletions(-) diff --git a/crates/installer/README.md b/crates/installer/README.md index c33b87ae14..f1378e7e58 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -45,9 +45,12 @@ an optical ISO alongside a separate installer USB. The older `cdromLabel` key is accepted as an alias; setting both is an error. Media is mounted read-only with filesystem auto-detection at `/run/trident/installer-media`. Media discovery waits up to 60 seconds for -USB/BMC block-device enumeration during autorun before reporting a missing -source. Explicit remote Stream COSI recovery bypasses local media discovery, -configuration overlays, and COSI scanning, including when media is broken. +USB/BMC block-device enumeration when the Host Configuration or COSI depends +on media. A live-OS or HTTP(S) Host Configuration without a media-backed image +skips discovery and media-local overrides; a symlink to media or an +`installer://image` reference still waits. Explicit remote Stream COSI recovery +bypasses local media discovery, configuration overlays, and COSI scanning, +including when media is broken. Other filesystem types require corresponding kernel mount support on the live OS. Duplicate labels are rejected. Optional `installer/installer.toml` on the mounted media can override mode, image directory, and autorun options, but not diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 729366306d..b9d5ea5723 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -121,10 +121,97 @@ pub(super) fn prepare_with_progress( if matches!(request, Request::Stream(_)) { return prepare_from_media_with_progress(settings, request, None, &mut progress); } + if request == &Request::Autorun { + settings.require_autorun()?; + if let Some(plan) = self_contained_install(settings, &mut progress)? { + return Ok((settings.clone(), plan)); + } + } let root = media_root(settings, request == &Request::Autorun, &mut progress)?; prepare_from_media_with_progress(settings, request, root.as_deref(), &mut progress) } +/// Treats a complete live-OS or remote Host Configuration as authoritative, without media overlays. +fn self_contained_install( + settings: &Config, + progress: &mut impl FnMut(&str) -> Result<(), Error>, +) -> Result, Error> { + let Some(source) = configured_host_configuration_source(settings)? else { + return Ok(None); + }; + if !source.starts_with('/') && !source.contains("://") { + return Ok(None); + } + let url = source_url(&source, None)?; + if file_source_may_need_media(&url, settings)? { + return Ok(None); + } + progress("Checking Host Configuration")?; + let text = read_text(&url)?; + let config: HostConfiguration = + serde_yaml::from_str(&text).context("Invalid Host Configuration YAML")?; + if let Some(image) = &config.image { + if image.url.as_str() == IMAGE_MARKER || file_source_may_need_media(&image.url, settings)? { + return Ok(None); + } + } + let config = resolve_parsed_host_configuration(config, None)?; + Ok(Some(Plan::Install { + config: Box::new(config), + source, + })) +} + +/// A missing path behind a symlink may become readable only after installer media is mounted. +fn file_source_may_need_media(url: &Url, settings: &Config) -> Result { + if url.scheme() != "file" { + return Ok(false); + } + let path = url + .to_file_path() + .map_err(|()| anyhow!("Invalid local file URL"))?; + let media = settings + .media + .mount_path + .as_deref() + .unwrap_or(Path::new(config::MEDIA_MOUNT)); + if path.starts_with(media) { + return Ok(true); + } + match path.canonicalize() { + Ok(resolved) => Ok(resolved.starts_with(media)), + Err(error) if error.kind() == ErrorKind::NotFound => { + let mut ancestor = PathBuf::new(); + for component in path.components() { + ancestor.push(component); + match fs::symlink_metadata(&ancestor) { + Ok(metadata) if metadata.file_type().is_symlink() => return Ok(true), + Ok(_) => {} + Err(error) if error.kind() == ErrorKind::NotFound => break, + Err(error) => { + return Err(error) + .with_context(|| format!("Failed to inspect '{}'", ancestor.display())) + } + } + } + Ok(false) + } + Err(error) => Err(error).with_context(|| format!("Failed to resolve '{}'", path.display())), + } +} + +/// Keeps dangling default Host Configuration symlinks eligible for media-dependent installs. +fn configured_host_configuration_source(settings: &Config) -> Result, Error> { + if let Some(source) = &settings.autorun.host_configuration { + return Ok(Some(source.clone())); + } + match fs::symlink_metadata(config::DEFAULT_HOST_CONFIGURATION) { + Ok(_) => Ok(Some(config::DEFAULT_HOST_CONFIGURATION.into())), + Err(error) if error.kind() == ErrorKind::NotFound => Ok(None), + Err(error) => Err(error).context("Failed to inspect default Host Configuration"), + } +} + #[cfg(test)] /// Prepares a plan from an already-mounted media root for tests. fn prepare_from_media( @@ -167,16 +254,7 @@ fn prepare_from_media_with_progress( remote_url(source)?; Some(source.clone()) } else { - match &settings.autorun.host_configuration { - Some(source) => Some(source.clone()), - None => match fs::metadata(config::DEFAULT_HOST_CONFIGURATION) { - Ok(_) => Some(config::DEFAULT_HOST_CONFIGURATION.into()), - Err(error) if error.kind() == ErrorKind::NotFound => None, - Err(error) => { - return Err(error).context("Failed to inspect default Host Configuration") - } - }, - } + configured_host_configuration_source(&settings)? }; let plan = match source { Some(source) => { @@ -405,8 +483,15 @@ pub(super) fn resolve_host_configuration( text: &str, selected: Option<&Path>, ) -> Result { - let mut config: HostConfiguration = + let config: HostConfiguration = serde_yaml::from_str(text).context("Invalid Host Configuration YAML")?; + resolve_parsed_host_configuration(config, selected) +} + +fn resolve_parsed_host_configuration( + mut config: HostConfiguration, + selected: Option<&Path>, +) -> Result { if let Some(image) = &mut config.image { if image.url.as_str() == IMAGE_MARKER { image.url = file_url(selected.context( @@ -535,6 +620,7 @@ mod tests { use std::{ io::{Cursor, Write}, net::TcpListener, + os::unix::fs as unix_fs, }; use indoc::indoc; @@ -646,6 +732,128 @@ mod tests { assert!(matches!(plan, Plan::Stream { image } if image == url)); } + #[test] + /// A complete live-OS Host Configuration never waits for media or applies its overlay. + fn self_contained_autorun_skips_media() { + let root = TempDir::new().unwrap(); + let path = root.path().join("host.yaml"); + fs::write( + &path, + valid_host_configuration().replace(IMAGE_MARKER, "https://example.com/image.cosi"), + ) + .unwrap(); + let mut settings = Config::default(); + settings.autorun.host_configuration = Some(path.display().to_string()); + settings.media.mount_path = Some(root.path().join("not-mounted")); + let (_, plan) = prepare_with_progress(&settings, &Request::Autorun, |_| Ok(())).unwrap(); + assert!(matches!(plan, Plan::Install { source, .. } if source == path.to_string_lossy())); + + settings.mode = config::Mode::Interactive; + let error = prepare_with_progress(&settings, &Request::Autorun, |_| Ok(())).unwrap_err(); + assert!( + error.to_string().contains("Interactive installation"), + "{error:#}" + ); + } + + #[test] + /// An HTTP Host Configuration with its own image does not need a local media mount. + fn self_contained_remote_autorun_skips_media() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("http://{}/host.yaml", listener.local_addr().unwrap()); + let server = thread::spawn(move || { + let (mut socket, _) = listener.accept().unwrap(); + let mut request = [0; 512]; + assert_ne!(socket.read(&mut request).unwrap(), 0); + let body = + valid_host_configuration().replace(IMAGE_MARKER, "https://example.com/image.cosi"); + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + socket.write_all(response.as_bytes()).unwrap(); + }); + let mut settings = Config::default(); + settings.autorun.host_configuration = Some(url.clone()); + settings.media.mount_path = Some("/nonexistent-installer-media".into()); + let (_, plan) = prepare_with_progress(&settings, &Request::Autorun, |_| Ok(())).unwrap(); + assert!(matches!(plan, Plan::Install { source, .. } if source == url)); + server.join().unwrap(); + } + + #[test] + /// Media-relative Host Configurations and media-backed images still need discovery. + fn media_dependent_host_configuration_is_not_self_contained() { + let root = TempDir::new().unwrap(); + let path = root.path().join("host.yaml"); + fs::write(&path, valid_host_configuration()).unwrap(); + let mut settings = Config::default(); + settings.autorun.host_configuration = Some(path.display().to_string()); + assert!(self_contained_install(&settings, &mut |_| Ok(())) + .unwrap() + .is_none()); + settings.autorun.host_configuration = Some("installer/host.yaml".into()); + assert!(self_contained_install(&settings, &mut |_| Ok(())) + .unwrap() + .is_none()); + fs::write( + &path, + valid_host_configuration().replace( + IMAGE_MARKER, + &format!("file://{}/cosi/image.cosi", config::MEDIA_MOUNT), + ), + ) + .unwrap(); + settings.autorun.host_configuration = Some(path.display().to_string()); + assert!(self_contained_install(&settings, &mut |_| Ok(())) + .unwrap() + .is_none()); + } + + #[test] + /// Dangling links into installer media must not be mistaken for an absent Host Configuration. + fn symlinked_host_configuration_waits_for_media() { + let root = TempDir::new().unwrap(); + let link = root.path().join("host.yaml"); + unix_fs::symlink( + Path::new(config::MEDIA_MOUNT).join("installer/host.yaml"), + &link, + ) + .unwrap(); + assert!(fs::metadata(&link).is_err()); + assert!(fs::symlink_metadata(&link).is_ok()); + let mut settings = Config::default(); + settings.autorun.host_configuration = Some(link.display().to_string()); + assert!(self_contained_install(&settings, &mut |_| Ok(())) + .unwrap() + .is_none()); + + let directory_link = root.path().join("media"); + unix_fs::symlink(config::MEDIA_MOUNT, &directory_link).unwrap(); + settings.autorun.host_configuration = Some( + directory_link + .join("installer/host.yaml") + .display() + .to_string(), + ); + assert!(self_contained_install(&settings, &mut |_| Ok(())) + .unwrap() + .is_none()); + + let local = root.path().join("local.yaml"); + fs::write( + &local, + valid_host_configuration().replace(IMAGE_MARKER, "https://example.com/image.cosi"), + ) + .unwrap(); + let local_link = root.path().join("local-link.yaml"); + unix_fs::symlink(local, &local_link).unwrap(); + settings.autorun.host_configuration = Some(local_link.display().to_string()); + assert!(self_contained_install(&settings, &mut |_| Ok(())) + .unwrap() + .is_some()); + } + #[test] /// Ensures the image marker needs a COSI and leaves other image URLs untouched. fn marker_requires_an_image_and_does_not_touch_other_urls() { diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index 8e808f5652..dcfa6dbc2d 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -33,14 +33,17 @@ may be omitted; unknown fields and invalid values are errors. `mode = "autorun"` means **start without an interactive source picker**. It does not choose the Trident operation; `mode = "install"` and `mode = "stream"` -are not valid TOML settings. After looking for media and applying any -[media-local overrides](#optional-configuration-on-the-media), autorun makes -this choice: +are not valid TOML settings. If the bootstrap configuration names a complete +live-OS or HTTP(S) Host Configuration whose image does not use +`installer://image` or a path on installer media, autorun uses it without +looking for media or applying [media-local +overrides](#optional-configuration-on-the-media). Otherwise it discovers media, +applies any overrides, and makes this choice: | Priority | Condition | Operation | | --- | --- | --- | | 1 | `autorun.hostConfiguration` is set | **Install** using that Host Configuration, even if a COSI is present. | -| 2 | Otherwise, `/etc/trident/config.yaml` exists in the live OS | **Install** using that file, even if a COSI is present. | +| 2 | Otherwise, `/etc/trident/config.yaml` exists or is a symlink in the live OS | **Install** using that file, even if a COSI is present. | | 3 | Otherwise, media contains a `.cosi` file | **StreamDisk** using the first regular COSI in `media.cosiDirectory`, sorted by filename. | | 4 | Neither source is available | Show recovery actions on the graphical console, or report an error without one. | @@ -88,8 +91,9 @@ necessarily the value written in the packaged file. | `autorun.reboot` | `true` | Reboot after successful servicing **only if** Trident reports that work was performed and a reboot is required. | | `autorun.force` | `false` | Legacy key accepted for compatibility but ignored. Autorun never compares installed filesystem UUIDs or asks for repeat-install confirmation. | -Media discovery waits up to 60 seconds during autorun for late USB/BMC device -enumeration. It fails if more than one device matches the selected/default +Media-dependent autorun waits up to 60 seconds for late USB/BMC device +enumeration; a self-contained Host Configuration skips discovery entirely. +Discovery fails if more than one device matches the selected/default labels. The selected filesystem is mounted read-only at `/run/trident/installer-media`; the live OS must support its filesystem. A manually selected remote recovery @@ -110,9 +114,13 @@ Remote connection and timeout errors are retried for up to 60 seconds while networking starts; HTTP response errors fail immediately. Host Configuration input must be valid YAML and is limited to 2 MiB. A Host Configuration image URL of `installer://image` fails if no COSI is available. -Authored disk paths and image checksums remain unchanged. Even with an -explicit Host Configuration source, autorun checks for media first (up to -the 60-second label-discovery wait) so it can resolve `installer://image`. +Authored disk paths and image checksums remain unchanged. Media-relative Host +Configuration paths and images on installer media (including +`installer://image`) require discovery; a default +`/etc/trident/config.yaml` symlink pointing into the media also waits for its +target to become available. A Host Configuration entirely on the live OS or +HTTP(S), with no media-backed image, takes precedence over media overlays even +when media is already present. Explicit remote Stream COSI recovery bypasses local media discovery and overlays, so it remains available when the local media cannot be read. @@ -126,8 +134,8 @@ could select as its smallest fitting target, such as a large installer USB. ## Optional configuration on the media -After finding or mounting media, the installer merges -`installer/installer.toml` from its root over the bootstrap configuration. +For media-dependent installs, after finding or mounting media the installer +merges `installer/installer.toml` from its root over the bootstrap configuration. For example, this media-local file selects **Install** without changing the bootstrap file baked into the live OS: From 851cdc83aea769b8370f9131b62a16ee9bdc7d53 Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 13 Nov 2026 22:23:30 -0800 Subject: [PATCH 55/62] fix: Address installer review feedback and CI fixture Keep terminal logging inside Ratatui, detect media-backed Host Configuration inputs beyond the image, lock workspace tests, and drop an invalid full-parser assertion from the label-discovery fixture. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Makefile | 2 +- crates/installer/README.md | 10 +- crates/installer/src/main.rs | 101 ++++++++++++++---- crates/installer/src/source.rs | 120 +++++++++++++++++++++- crates/osutils/src/lsblk.rs | 5 - docs/Reference/Installer-Configuration.md | 26 ++--- 6 files changed, 217 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index a0f643de89..6f4e551ed9 100644 --- a/Makefile +++ b/Makefile @@ -141,7 +141,7 @@ format: .PHONY: test test: .cargo/config - cargo test --all --no-fail-fast + cargo test --locked --all --no-fail-fast cargo test --locked -p trident-client --all-features --no-fail-fast COVERAGE_EXCLUDED_FILES_REGEX='crates/docbuilder|crates/pytest|crates/setsail|target|/mnt/vss' diff --git a/crates/installer/README.md b/crates/installer/README.md index f1378e7e58..55daf98d17 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -46,11 +46,11 @@ The older `cdromLabel` key is accepted as an alias; setting both is an error. Media is mounted read-only with filesystem auto-detection at `/run/trident/installer-media`. Media discovery waits up to 60 seconds for USB/BMC block-device enumeration when the Host Configuration or COSI depends -on media. A live-OS or HTTP(S) Host Configuration without a media-backed image -skips discovery and media-local overrides; a symlink to media or an -`installer://image` reference still waits. Explicit remote Stream COSI recovery -bypasses local media discovery, configuration overlays, and COSI scanning, -including when media is broken. +on media. A live-OS or HTTP(S) Host Configuration without media-backed inputs +skips discovery and media-local overrides; a symlink to media, +`installer://image`, or a file-backed HC input on media still waits. Explicit +remote Stream COSI recovery bypasses local media discovery, configuration +overlays, and COSI scanning, including when media is broken. Other filesystem types require corresponding kernel mount support on the live OS. Duplicate labels are rejected. Optional `installer/installer.toml` on the mounted media can override mode, image directory, and autorun options, but not diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index 43ffe30e84..02f95b27ce 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -1,6 +1,6 @@ use std::{ fs::{self, OpenOptions, Permissions}, - io::Write, + io::{self, IsTerminal, Write}, os::unix::fs::{OpenOptionsExt, PermissionsExt}, path::PathBuf, process::ExitCode, @@ -9,7 +9,7 @@ use std::{ use anyhow::{Context, Error}; use clap::Parser; -use log::{warn, LevelFilter}; +use log::{warn, LevelFilter, Record}; use tokio::runtime::Builder; use tokio::sync::mpsc::UnboundedSender; @@ -45,27 +45,13 @@ struct Args { /// Starts logging and the installer runtime, returning a failing exit code on startup errors. fn main() -> ExitCode { + let stderr_is_terminal = io::stderr().is_terminal(); env_logger::Builder::from_default_env() .filter_level(LevelFilter::Info) .filter_module("installer", LevelFilter::Trace) .filter_module("osutils", LevelFilter::Trace) - .format(|buf, record| { - let message = record.args().to_string(); - if let Some(tx) = INSTALLER_LOG_TX.get() { - if tx - .send(Event::InstallerLog { - level: record.level(), - message: message.clone(), - }) - .is_err() - { - writeln!( - buf, - "Installer log receiver unavailable; entry retained in journal" - )?; - } - } - writeln!(buf, "[{} {}] {}", record.level(), record.target(), message) + .format(move |buf, record| { + format_log_record(buf, record, INSTALLER_LOG_TX.get(), stderr_is_terminal) }) .init(); let args = Args::parse(); @@ -84,6 +70,41 @@ fn main() -> ExitCode { } } +/// Keeps terminal-bound logs in Ratatui while retaining startup and journal diagnostics. +fn format_log_record( + output: &mut impl Write, + record: &Record<'_>, + log_tx: Option<&UnboundedSender>, + stderr_is_terminal: bool, +) -> io::Result<()> { + let message = record.args().to_string(); + if let Some(tx) = log_tx { + if tx + .send(Event::InstallerLog { + level: record.level(), + message: message.clone(), + }) + .is_ok() + { + if stderr_is_terminal { + return Ok(()); + } + } else { + writeln!( + output, + "Installer log receiver unavailable; entry retained in journal" + )?; + } + } + writeln!( + output, + "[{} {}] {}", + record.level(), + record.target(), + message + ) +} + /// Sets up private diagnostics and optional console ownership before starting the UI. async fn run(args: Args) -> Result<(), Error> { let mut linux_vt = false; @@ -118,3 +139,45 @@ async fn run(args: Args) -> Result<(), Error> { } ui::run(args.config, args.plain, linux_vt, output).await } + +#[cfg(test)] +mod tests { + use super::*; + + use log::Level; + use tokio::sync::mpsc; + + #[test] + fn terminal_logs_use_ui_events_while_journal_and_startup_logs_remain_visible() { + let record = Record::builder() + .args(format_args!("Installer progress")) + .level(Level::Info) + .target("installer") + .build(); + let (tx, mut rx) = mpsc::unbounded_channel(); + let mut output = Vec::new(); + format_log_record(&mut output, &record, Some(&tx), true).unwrap(); + assert!(output.is_empty()); + assert!(matches!( + rx.try_recv().unwrap(), + Event::InstallerLog { + level: Level::Info, + message + } if message == "Installer progress" + )); + + format_log_record(&mut output, &record, Some(&tx), false).unwrap(); + assert_eq!(output, b"[INFO installer] Installer progress\n"); + output.clear(); + + format_log_record(&mut output, &record, None, true).unwrap(); + assert_eq!(output, b"[INFO installer] Installer progress\n"); + output.clear(); + + drop(rx); + format_log_record(&mut output, &record, Some(&tx), true).unwrap(); + assert!(String::from_utf8(output) + .unwrap() + .contains("Installer log receiver unavailable")); + } +} diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index b9d5ea5723..1f3d64240f 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -23,7 +23,9 @@ use osutils::{ mount, mountpoint, }; use trident_api::{ - config::HostConfiguration, constants::MOUNT_OPTION_READ_ONLY, primitives::hash::Sha384Hash, + config::{Check, HostConfiguration, Script, ScriptSource}, + constants::MOUNT_OPTION_READ_ONLY, + primitives::hash::Sha384Hash, }; use crate::config::{self, Config}; @@ -150,10 +152,8 @@ fn self_contained_install( let text = read_text(&url)?; let config: HostConfiguration = serde_yaml::from_str(&text).context("Invalid Host Configuration YAML")?; - if let Some(image) = &config.image { - if image.url.as_str() == IMAGE_MARKER || file_source_may_need_media(&image.url, settings)? { - return Ok(None); - } + if host_configuration_needs_media(&config, settings)? { + return Ok(None); } let config = resolve_parsed_host_configuration(config, None)?; Ok(Some(Plan::Install { @@ -162,6 +162,70 @@ fn self_contained_install( })) } +/// Checks file-backed HC inputs that Trident may need from the installer mount. +fn host_configuration_needs_media( + config: &HostConfiguration, + settings: &Config, +) -> Result { + if let Some(image) = &config.image { + if image.url.as_str() == IMAGE_MARKER || file_source_may_need_media(&image.url, settings)? { + return Ok(true); + } + } + if let Some(key) = config + .storage + .encryption + .as_ref() + .and_then(|encryption| encryption.recovery_key_url.as_ref()) + { + if file_source_may_need_media(key, settings)? { + return Ok(true); + } + } + for extension in config.os.sysexts.iter().chain(&config.os.confexts) { + if file_source_may_need_media(&extension.url, settings)? { + return Ok(true); + } + } + for file in &config.os.additional_files { + if let Some(path) = &file.source { + if path_may_need_media(path, settings)? { + return Ok(true); + } + } + } + let scripts = config + .scripts + .pre_servicing + .iter() + .chain(&config.scripts.post_provision) + .chain(&config.scripts.post_configure) + .chain(config.health.checks.iter().filter_map(|check| match check { + Check::Script(script) => Some(script), + Check::SystemdCheck(_) => None, + })); + for script in scripts { + if script_needs_media(script, settings)? { + return Ok(true); + } + } + Ok(false) +} + +fn script_needs_media(script: &Script, settings: &Config) -> Result { + if let ScriptSource::Path(path) = &script.source { + if path_may_need_media(path, settings)? { + return Ok(true); + } + } + if let Some(interpreter) = &script.interpreter { + if path_may_need_media(interpreter, settings)? { + return Ok(true); + } + } + Ok(false) +} + /// A missing path behind a symlink may become readable only after installer media is mounted. fn file_source_may_need_media(url: &Url, settings: &Config) -> Result { if url.scheme() != "file" { @@ -170,6 +234,10 @@ fn file_source_may_need_media(url: &Url, settings: &Config) -> Result Result { let media = settings .media .mount_path @@ -627,6 +695,8 @@ mod tests { use tar::Builder as TarBuilder; use tempfile::TempDir; + use trident_api::config::{AdditionalFile, Encryption}; + #[test] /// Ensures recovery sources cannot silently fall back to local files or other schemes. fn remote_sources_are_explicit_and_limited() { @@ -810,6 +880,46 @@ mod tests { .is_none()); } + #[test] + /// An HTTPS image does not make an HC self-contained if other inputs live on installer media. + fn additional_file_and_scripts_keep_media_discovery() { + let root = TempDir::new().unwrap(); + let path = root.path().join("host.yaml"); + let mut settings = Config::default(); + let mount = root.path().join("not-mounted"); + settings.media.mount_path = Some(mount.clone()); + settings.autorun.host_configuration = Some(path.display().to_string()); + let mut config: HostConfiguration = serde_yaml::from_str( + &valid_host_configuration().replace(IMAGE_MARKER, "https://example.com/image.cosi"), + ) + .unwrap(); + assert!(!host_configuration_needs_media(&config, &settings).unwrap()); + + config.os.additional_files.push(AdditionalFile { + destination: "/etc/example".into(), + source: Some(mount.join("installer/example")), + ..Default::default() + }); + fs::write(&path, serde_yaml::to_string(&config).unwrap()).unwrap(); + assert!(host_configuration_needs_media(&config, &settings).unwrap()); + let error = prepare_with_progress(&settings, &Request::Autorun, |_| Ok(())).unwrap_err(); + assert!(error.to_string().contains("not a mount point"), "{error:#}"); + + config.os.additional_files.clear(); + config.scripts.post_provision.push(Script { + source: ScriptSource::Path(mount.join("installer/script.sh")), + ..Default::default() + }); + assert!(host_configuration_needs_media(&config, &settings).unwrap()); + config.scripts.post_provision.clear(); + + config.storage.encryption = Some(Encryption { + recovery_key_url: Some(file_url(&mount.join("installer/recovery.key")).unwrap()), + ..Default::default() + }); + assert!(host_configuration_needs_media(&config, &settings).unwrap()); + } + #[test] /// Dangling links into installer media must not be mistaken for an absent Host Configuration. fn symlinked_host_configuration_waits_for_media() { diff --git a/crates/osutils/src/lsblk.rs b/crates/osutils/src/lsblk.rs index 7deb406c3d..63b29026c3 100644 --- a/crates/osutils/src/lsblk.rs +++ b/crates/osutils/src/lsblk.rs @@ -424,11 +424,6 @@ mod tests { matches, [PathBuf::from("/dev/sdb1"), PathBuf::from("/dev/sr0")] ); - let devices = parse_lsblk_output(output).unwrap(); - assert_eq!( - devices[0].partition_table_type, - Some(PartitionTableType::Gpt) - ); } /// Output obtained from running `lsblk --json --bytes --output-all --paths /dev/sda` diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index dcfa6dbc2d..ba2dbc6f05 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -33,12 +33,11 @@ may be omitted; unknown fields and invalid values are errors. `mode = "autorun"` means **start without an interactive source picker**. It does not choose the Trident operation; `mode = "install"` and `mode = "stream"` -are not valid TOML settings. If the bootstrap configuration names a complete -live-OS or HTTP(S) Host Configuration whose image does not use -`installer://image` or a path on installer media, autorun uses it without -looking for media or applying [media-local -overrides](#optional-configuration-on-the-media). Otherwise it discovers media, -applies any overrides, and makes this choice: +are not valid TOML settings. If the bootstrap configuration names a live-OS +or HTTP(S) Host Configuration whose file-backed inputs do not need installer +media, autorun uses it without looking for media or applying +[media-local overrides](#optional-configuration-on-the-media). Otherwise it +discovers media, applies any overrides, and makes this choice: | Priority | Condition | Operation | | --- | --- | --- | @@ -115,12 +114,15 @@ networking starts; HTTP response errors fail immediately. Host Configuration input must be valid YAML and is limited to 2 MiB. A Host Configuration image URL of `installer://image` fails if no COSI is available. Authored disk paths and image checksums remain unchanged. Media-relative Host -Configuration paths and images on installer media (including -`installer://image`) require discovery; a default -`/etc/trident/config.yaml` symlink pointing into the media also waits for its -target to become available. A Host Configuration entirely on the live OS or -HTTP(S), with no media-backed image, takes precedence over media overlays even -when media is already present. +Configuration paths and file-backed inputs on installer media (including +`installer://image`, additional files, script paths, extension images and +recovery keys) require discovery; a default `/etc/trident/config.yaml` symlink +pointing into the media also waits for its target to become available. A Host +Configuration with no media-backed inputs takes precedence over media overlays +even when media is already present. Script content and arguments are not +inspected for embedded paths; use a media-relative Host Configuration when +those depend on the installer media. + Explicit remote Stream COSI recovery bypasses local media discovery and overlays, so it remains available when the local media cannot be read. From 4c387ec473dd789b2ec0935dafab32455e78dcf1 Mon Sep 17 00:00:00 2001 From: Paco Date: Mon, 16 Nov 2026 15:19:51 -0800 Subject: [PATCH 56/62] refactor: Keep installer terminal ownership local and gate interactive execution Move console and recovery-shell setup into the installer; isolate process-group handoff behind the osutils run-interactive feature, enabled only by installer. Document the sparse lsblk label projection and shell job control. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Cargo.lock | 1 + crates/installer/Cargo.toml | 3 +- crates/installer/README.md | 8 ++ crates/installer/src/main.rs | 3 +- crates/{osutils => installer}/src/terminal.rs | 57 +++------- crates/installer/src/ui/mod.rs | 13 ++- crates/osutils/Cargo.toml | 1 + crates/osutils/src/dependencies.rs | 65 +---------- .../osutils/src/dependencies/interactive.rs | 105 ++++++++++++++++++ crates/osutils/src/lib.rs | 1 - crates/osutils/src/lsblk.rs | 1 + 11 files changed, 148 insertions(+), 110 deletions(-) rename crates/{osutils => installer}/src/terminal.rs (75%) create mode 100644 crates/osutils/src/dependencies/interactive.rs diff --git a/Cargo.lock b/Cargo.lock index 1d11e8e302..772b1f151f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1728,6 +1728,7 @@ dependencies = [ "env_logger 0.11.5", "indoc", "log", + "nix", "osutils", "ratatui", "regex", diff --git a/crates/installer/Cargo.toml b/crates/installer/Cargo.toml index 0b18713d38..e1be8c6fd3 100644 --- a/crates/installer/Cargo.toml +++ b/crates/installer/Cargo.toml @@ -16,6 +16,7 @@ clap = { workspace = true } crossterm = { workspace = true } env_logger = { workspace = true } log = { workspace = true } +nix = { workspace = true, features = ["fs", "process", "term"] } ratatui = { workspace = true, features = ["unstable-rendered-line-info"] } regex = { workspace = true } reqwest = { workspace = true } @@ -29,7 +30,7 @@ toml = { workspace = true } url = { workspace = true } # Local Crate Dependencies -osutils = { path = "../osutils" } +osutils = { path = "../osutils", features = ["run-interactive"] } trident-client = { path = "../trident-client", features = ["preview"] } trident_api = { path = "../trident_api" } diff --git a/crates/installer/README.md b/crates/installer/README.md index 55daf98d17..afda872f45 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -114,6 +114,14 @@ for tty1 so terminal tools such as `clear` work in the shell. Installation continues while a shell is open; automatic reboot waits. The success screen offers Reboot and Shell. While servicing is active, Ctrl-C remains in the installation menu and power actions are blocked. + +Only the installer enables `osutils`'s `run-interactive` feature. The recovery +shell gets the controlling TTY as stdin, stdout, and stderr and runs in its own +foreground process group so keyboard signals reach the shell rather than the +installer. After the shell exits, the installer reclaims the foreground group +and resumes the UI; a shell that reads before the handoff is continued once +it owns the TTY. + Before each recovery attempt the baked configuration is reloaded, so shell edits take effect. Invalid or missing configuration never supplies implicit defaults for an installation. diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index 02f95b27ce..3f14294058 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -13,13 +13,14 @@ use log::{warn, LevelFilter, Record}; use tokio::runtime::Builder; use tokio::sync::mpsc::UnboundedSender; -use osutils::{systemd, terminal}; +use osutils::systemd; use crate::{client::Event, config::DEFAULT_CONFIG, ui::Output}; mod client; mod config; mod source; +mod terminal; mod ui; const LOG_PATH: &str = "/var/log/trident-installer.log"; diff --git a/crates/osutils/src/terminal.rs b/crates/installer/src/terminal.rs similarity index 75% rename from crates/osutils/src/terminal.rs rename to crates/installer/src/terminal.rs index dd7860c135..f43b059902 100644 --- a/crates/osutils/src/terminal.rs +++ b/crates/installer/src/terminal.rs @@ -1,3 +1,5 @@ +//! Console ownership, serial mirrors and recovery-shell setup for installer boots. + use std::{ fs::{self, File, OpenOptions}, io::{self, IsTerminal, Write}, @@ -9,20 +11,19 @@ use std::{ }; use anyhow::{ensure, Context, Error}; -use nix::{ - libc, - sys::signal::{self, SigSet, SigmaskHow, Signal}, - unistd::{self, Pid}, -}; +use nix::{libc, unistd}; -use crate::dependencies::Dependency; +use osutils::dependencies::{self, Dependency}; const ACTIVE_CONSOLES_PATH: &str = "/sys/class/tty/console/active"; const GRAPHICAL_CONSOLE: &str = "tty1"; const VT_PALETTE_SIZE: u8 = 16; /// Sets RGB values for the Linux virtual console's 16 ANSI colour slots. -pub fn set_vt_palette(output: &mut impl Write, colours: &[(u8, u8, u8, u8)]) -> io::Result<()> { +pub(super) fn set_vt_palette( + output: &mut impl Write, + colours: &[(u8, u8, u8, u8)], +) -> io::Result<()> { for &(slot, red, green, blue) in colours { if slot >= VT_PALETTE_SIZE { return Err(io::Error::new( @@ -36,13 +37,13 @@ pub fn set_vt_palette(output: &mut impl Write, colours: &[(u8, u8, u8, u8)]) -> } /// Restores the Linux virtual console's built-in ANSI colour palette. -pub fn reset_vt_palette(output: &mut impl Write) -> io::Result<()> { +pub(super) fn reset_vt_palette(output: &mut impl Write) -> io::Result<()> { output.write_all(b"\x1b]R")?; output.flush() } /// Reads the active kernel consoles, resolving `tty0` to the graphical `tty1`. -pub fn active_consoles() -> Result, Error> { +pub(super) fn active_consoles() -> Result, Error> { let active = fs::read_to_string(ACTIVE_CONSOLES_PATH) .context("Failed to read active kernel consoles")?; console_paths(&active) @@ -70,21 +71,8 @@ fn console_paths(active: &str) -> Result, Error> { Ok(consoles) } -/// Prefers `tty1` for the TUI and otherwise uses the first active console. -pub fn preferred_console(consoles: &[PathBuf]) -> Result<&Path, Error> { - consoles - .iter() - .find(|path| { - path.file_name() - .is_some_and(|name| name == GRAPHICAL_CONSOLE) - }) - .or_else(|| consoles.first()) - .map(PathBuf::as_path) - .context("No console available") -} - /// Opens a serial console without allowing flow control to block a caller's event loop. -pub fn open_nonblocking_console(path: impl AsRef) -> Result { +pub(super) fn open_nonblocking_console(path: impl AsRef) -> Result { let path = path.as_ref(); OpenOptions::new() .write(true) @@ -96,7 +84,7 @@ pub fn open_nonblocking_console(path: impl AsRef) -> Result { /// Claims a character terminal as the controlling TTY and redirects stdin/stdout to it. /// /// The caller must stop competing getty units before attaching. -pub fn attach(path: impl AsRef) -> Result { +pub(super) fn attach(path: impl AsRef) -> Result { let path = path.as_ref(); let terminal = OpenOptions::new() .read(true) @@ -115,7 +103,8 @@ pub fn attach(path: impl AsRef) -> Result { if unsafe { libc::ioctl(terminal.as_raw_fd(), libc::TIOCSCTTY, 1) } < 0 { return Err(io::Error::last_os_error()).context("Failed to claim controlling terminal"); } - set_foreground_group(&terminal, unistd::getpgrp()) + // The recovery shell later takes this foreground group and returns it on exit. + dependencies::set_foreground_group(&terminal, unistd::getpgrp()) .context("Failed to make installer the terminal's foreground process group")?; for target in [libc::STDIN_FILENO, libc::STDOUT_FILENO] { // dup2 redirects the inherited stdio descriptors without transferring ownership. @@ -126,20 +115,8 @@ pub fn attach(path: impl AsRef) -> Result { Ok(terminal) } -/// Transfers foreground terminal ownership while blocking `SIGTTOU` during the handoff. -pub(crate) fn set_foreground_group(terminal: &File, group: Pid) -> io::Result<()> { - let mut blocked = SigSet::empty(); - blocked.add(Signal::SIGTTOU); - let mut previous = SigSet::empty(); - signal::pthread_sigmask(SigmaskHow::SIG_BLOCK, Some(&blocked), Some(&mut previous))?; - let result = unistd::tcsetpgrp(terminal, group); - signal::pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&previous), None)?; - result?; - Ok(()) -} - /// Runs an interactive login Bash on the supplied terminal, loading the root profile. -pub fn shell(terminal: &File) -> Result<(), Error> { +pub(super) fn shell(terminal: &File) -> Result<(), Error> { Dependency::Bash .cmd() .args(["--login", "-i"]) @@ -159,10 +136,6 @@ mod tests { consoles, [PathBuf::from("/dev/ttyS0"), PathBuf::from("/dev/tty1")] ); - assert_eq!( - preferred_console(&consoles).unwrap(), - Path::new("/dev/tty1") - ); console_paths("../tty1").unwrap_err(); console_paths("").unwrap_err(); } diff --git a/crates/installer/src/ui/mod.rs b/crates/installer/src/ui/mod.rs index b74c655105..6c571b3fd2 100644 --- a/crates/installer/src/ui/mod.rs +++ b/crates/installer/src/ui/mod.rs @@ -25,14 +25,14 @@ use tokio::{ time, }; -use osutils::{systemd, terminal as os_terminal}; +use osutils::systemd; use trident_client::{LogLevel, ServicingEvent}; use crate::{ client::{self, Event}, config::Config, source::{self, Request}, - INSTALLER_LOG_TX, + terminal as installer_terminal, INSTALLER_LOG_TX, }; mod model; @@ -72,7 +72,7 @@ impl Display { terminal::enable_raw_mode()?; self.active = true; if self.linux_vt { - os_terminal::set_vt_palette(&mut io::stdout(), &vt_rgb_palette()?)?; + installer_terminal::set_vt_palette(&mut io::stdout(), &vt_rgb_palette()?)?; } execute!(self.terminal.backend_mut(), EnterAlternateScreen)?; self.terminal.clear()?; @@ -97,7 +97,7 @@ impl Display { Ok(()) })(); let palette = if self.linux_vt { - os_terminal::reset_vt_palette(&mut io::stdout()) + installer_terminal::reset_vt_palette(&mut io::stdout()) } else { Ok(()) }; @@ -340,8 +340,9 @@ pub(super) async fn run( println!("\nShell: type exit to return. Installation continues; automatic reboot waits."); let tty = OpenOptions::new().read(true).write(true).open("/dev/tty")?; - shell = - Some(task::spawn_blocking(move || os_terminal::shell(&tty))); + shell = Some(task::spawn_blocking(move || { + installer_terminal::shell(&tty) + })); break; } Action::Start(request) => { diff --git a/crates/osutils/Cargo.toml b/crates/osutils/Cargo.toml index d59ea7898f..9efb364438 100644 --- a/crates/osutils/Cargo.toml +++ b/crates/osutils/Cargo.toml @@ -51,4 +51,5 @@ rand = {workspace = true} [features] functional-test = ["pytest", "test-utilities"] pytest-generator = ["pytest"] +run-interactive = [] test-utilities = ["rand"] diff --git a/crates/osutils/src/dependencies.rs b/crates/osutils/src/dependencies.rs index 766948b9d6..932a588c5a 100644 --- a/crates/osutils/src/dependencies.rs +++ b/crates/osutils/src/dependencies.rs @@ -1,25 +1,24 @@ use std::{ borrow::Cow, ffi::{OsStr, OsString}, - fs::File, io::{self, Write}, - os::unix::process::{CommandExt, ExitStatusExt}, + os::unix::process::ExitStatusExt, path::PathBuf, process::{Command as StdCommand, Output, Stdio}, }; use log::trace; -use nix::{ - sys::signal::{self, Signal}, - unistd::{self, Pid}, -}; use strum_macros::IntoStaticStr; use trident_api::error::{ ExecutionEnvironmentMisconfigurationError, ServicingError, TridentError, TridentResultExt, }; -use crate::terminal; +#[cfg(feature = "run-interactive")] +mod interactive; + +#[cfg(feature = "run-interactive")] +pub use interactive::set_foreground_group; #[derive(Debug, thiserror::Error)] pub enum DependencyError { @@ -231,58 +230,6 @@ pub struct Command { } impl Command { - /// Runs a command in the terminal's foreground process group with attached stdio. - /// - /// Restores the parent's foreground group after the child exits and rejects piped stdin. - /// Nonzero exits are returned as [`DependencyError`] values. - pub fn run_interactive(&self, terminal: &File) -> Result<(), Box> { - let binary = self.dependency.path()?; - let execute = || -> Result { - if self.stdin_data.is_some() { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Interactive commands cannot pipe stdin", - )); - } - let parent_group = unistd::tcgetpgrp(terminal)?; - let mut command = StdCommand::new(&binary); - command - .args(&self.args) - .envs(self.envs.clone()) - .stdin(terminal.try_clone()?) - .stdout(terminal.try_clone()?) - .stderr(terminal.try_clone()?) - .process_group(0); - let mut child = command.spawn()?; - let child_group = Pid::from_raw(i32::try_from(child.id()).map_err(io::Error::other)?); - let foreground = terminal::set_foreground_group(terminal, child_group); - if let Err(error) = foreground { - child.kill()?; - child.wait()?; - return Err(error); - } - let continued = signal::killpg(child_group, Signal::SIGCONT); - let status = child.wait(); - let restored = terminal::set_foreground_group(terminal, parent_group); - restored?; - continued?; - Ok(Output { - status: status?, - stdout: Vec::new(), - stderr: Vec::new(), - }) - }; - CommandOutput { - rendered_command: self.render_command(), - dependency: self.dependency, - inner: execute().map_err(|inner| DependencyError::CouldNotExecute { - dependency: self.dependency, - inner, - })?, - } - .check() - } - pub fn arg>(&mut self, arg: S) -> &mut Self { self.args.push(arg.as_ref().to_os_string()); self diff --git a/crates/osutils/src/dependencies/interactive.rs b/crates/osutils/src/dependencies/interactive.rs new file mode 100644 index 0000000000..de82c05066 --- /dev/null +++ b/crates/osutils/src/dependencies/interactive.rs @@ -0,0 +1,105 @@ +use std::{ + fs::File, + io, + os::unix::process::CommandExt, + process::{Command as StdCommand, Output}, +}; + +use nix::{ + sys::signal::{self, SigSet, SigmaskHow, Signal}, + unistd::{self, Pid}, +}; + +use super::{Command, CommandOutput, DependencyError}; + +impl Command { + /// Runs a command on its controlling TTY in a separate foreground process group. + /// + /// Unlike `output()`, a shell needs the real TTY for job control, and keyboard + /// signals must reach the shell rather than its caller. The former foreground + /// group is restored after exit; nonzero exits become [`DependencyError`]. + pub fn run_interactive(&self, terminal: &File) -> Result<(), Box> { + let binary = self.dependency.path()?; + let execute = || -> Result { + if self.stdin_data.is_some() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Interactive commands cannot pipe stdin", + )); + } + let parent_group = unistd::tcgetpgrp(terminal)?; + let mut command = StdCommand::new(&binary); + // A separate group makes keyboard signals target the shell, not the installer. + command + .args(&self.args) + .envs(self.envs.clone()) + .stdin(terminal.try_clone()?) + .stdout(terminal.try_clone()?) + .stderr(terminal.try_clone()?) + .process_group(0); + let mut child = command.spawn()?; + let child_group = Pid::from_raw(i32::try_from(child.id()).map_err(io::Error::other)?); + let foreground = set_foreground_group(terminal, child_group); + if let Err(error) = foreground { + child.kill()?; + child.wait()?; + return Err(error); + } + // The child can stop on SIGTTIN if it reads before the parent hands over the TTY. + let continued = signal::killpg(child_group, Signal::SIGCONT); + let status = child.wait(); + let restored = set_foreground_group(terminal, parent_group); + restored?; + continued?; + // Output went directly to the TTY, not to pipes we could collect here. + Ok(Output { + status: status?, + stdout: Vec::new(), + stderr: Vec::new(), + }) + }; + CommandOutput { + rendered_command: self.render_command(), + dependency: self.dependency, + inner: execute().map_err(|inner| DependencyError::CouldNotExecute { + dependency: self.dependency, + inner, + })?, + } + .check() + } +} + +/// A background process must block SIGTTOU to reclaim the TTY from its child. +pub fn set_foreground_group(terminal: &File, group: Pid) -> io::Result<()> { + let mut blocked = SigSet::empty(); + blocked.add(Signal::SIGTTOU); + let mut previous = SigSet::empty(); + signal::pthread_sigmask(SigmaskHow::SIG_BLOCK, Some(&blocked), Some(&mut previous))?; + let result = unistd::tcsetpgrp(terminal, group); + signal::pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(&previous), None)?; + result?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + use crate::dependencies::Dependency; + + #[test] + fn interactive_command_rejects_piped_stdin() { + let terminal = File::open("/dev/null").unwrap(); + let error = Dependency::Echo + .cmd() + .with_stdin(b"not a terminal".to_vec()) + .run_interactive(&terminal) + .unwrap_err(); + assert!(matches!( + *error, + DependencyError::CouldNotExecute { inner, .. } + if inner.kind() == io::ErrorKind::InvalidInput + )); + } +} diff --git a/crates/osutils/src/lib.rs b/crates/osutils/src/lib.rs index 3a39e4c404..6efde3b846 100644 --- a/crates/osutils/src/lib.rs +++ b/crates/osutils/src/lib.rs @@ -40,7 +40,6 @@ pub mod sfdisk; pub mod swap; pub mod systemd; pub mod tabfile; -pub mod terminal; pub mod tune2fs; pub mod udevadm; pub mod uki; diff --git a/crates/osutils/src/lsblk.rs b/crates/osutils/src/lsblk.rs index 63b29026c3..47b88f8a04 100644 --- a/crates/osutils/src/lsblk.rs +++ b/crates/osutils/src/lsblk.rs @@ -174,6 +174,7 @@ fn lsblk_output() -> Result { .context("Failed to execute lsblk") } +/// BlockDevice has no label and requires fields that label discovery does not need. #[derive(Deserialize, Default)] struct LabelledDevice { name: String, From 24aff13836fbc61dc33a1e5aff002d583bc807a8 Mon Sep 17 00:00:00 2001 From: Paco Date: Mon, 16 Nov 2026 16:20:24 -0800 Subject: [PATCH 57/62] refactor: Split Trident file URL fix into dedicated PR Restore the Trident reader to main on the installer branch; the encoded local file URL fix will ship independently. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/trident/src/io_utils/file_reader.rs | 31 ++++------------------ 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/crates/trident/src/io_utils/file_reader.rs b/crates/trident/src/io_utils/file_reader.rs index 5967bdfc26..d3e54ddd62 100644 --- a/crates/trident/src/io_utils/file_reader.rs +++ b/crates/trident/src/io_utils/file_reader.rs @@ -8,7 +8,7 @@ use std::{ #[cfg(test)] use std::io::Cursor; -use anyhow::{anyhow, bail, ensure, Error}; +use anyhow::{bail, ensure, Error}; use log::debug; use url::Url; @@ -45,10 +45,9 @@ impl FileReader { pub(crate) fn new(source: &Url, timeout: Duration) -> Result { Ok(match source.scheme() { "file" => { - let path = source - .to_file_path() - .map_err(|()| anyhow!("Invalid local file URL"))?; - debug!("Loading file: '{}'", path.display()); + // Load from local file + debug!("Loading file: '{}'", source.path()); + let path = PathBuf::from(source.path()); ensure!( path.exists(), format!("Path '{}' does not exist", path.display()), @@ -141,7 +140,7 @@ mod tests { use std::io::{SeekFrom, Write}; use log::trace; - use tempfile::{NamedTempFile, TempDir}; + use tempfile::NamedTempFile; #[test] fn test_file_reader_factory_file() { @@ -201,26 +200,6 @@ mod tests { check_section(0, 13); // Hello, World! } - #[test] - fn file_url_decodes_escaped_local_path() { - let directory = TempDir::new().unwrap(); - let path = directory.path().join("installer image #1.cosi"); - std::fs::write(&path, b"local COSI").unwrap(); - let url = Url::from_file_path(&path).unwrap(); - assert!(url.path().contains("%20")); - assert!(url.path().contains("%23")); - - let reader = FileReader::new(&url, Duration::from_secs(5)).unwrap(); - let mut bytes = Vec::new(); - reader.reader().unwrap().read_to_end(&mut bytes).unwrap(); - assert_eq!(bytes, b"local COSI"); - FileReader::new( - &Url::parse("file://remote.example/installer%20image.cosi").unwrap(), - Duration::from_secs(5), - ) - .unwrap_err(); - } - #[test] fn test_file_reader_http_file() { env_logger::builder() From 5ae831c5cce6f4710be1eba976437a3020983b32 Mon Sep 17 00:00:00 2001 From: Paco Date: Mon, 16 Nov 2026 16:24:46 -0800 Subject: [PATCH 58/62] docs: Explain installer state and media invariants Document crate-visible types, settings, constants and static state; add short comments at terminal ownership and wrapped-log decisions without annotating obvious operations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/client.rs | 21 +++++++++++++---- crates/installer/src/config.rs | 28 +++++++++++++++++++--- crates/installer/src/main.rs | 9 +++++++ crates/installer/src/source.rs | 35 +++++++++++++++++++++++++-- crates/installer/src/terminal.rs | 6 ++++- crates/installer/src/ui/mod.rs | 8 +++++++ crates/installer/src/ui/model.rs | 39 ++++++++++++++++++++++++++++++- crates/installer/src/ui/output.rs | 5 +++- crates/installer/src/ui/render.rs | 10 ++++++-- crates/installer/src/ui/theme.rs | 31 +++++++++++++++++------- 10 files changed, 169 insertions(+), 23 deletions(-) diff --git a/crates/installer/src/client.rs b/crates/installer/src/client.rs index a7180f355f..b2e8d4376d 100644 --- a/crates/installer/src/client.rs +++ b/crates/installer/src/client.rs @@ -15,32 +15,45 @@ use crate::{ source::{self, Plan, Request}, }; +/// Messages passed from source preparation and the gRPC worker to the UI. #[derive(Debug)] pub(super) enum Event { - InstallerLog { - level: Level, - message: String, - }, + /// Installer or dependency log, before display and serial filtering. + InstallerLog { level: Level, message: String }, + /// A named preparation step while no daemon operation is active. Preparing(String), + /// Current source and policy; sent again if media settings change them. Prepared { + /// Display-safe source description; never include signed URL parameters. description: String, + /// Caller-managed automatic reboot policy, not a daemon action. reboot: bool, + /// Media settings may change which records reach serial. serial_verbosity: SerialVerbosity, }, + /// A domain event received from Trident, including its terminal result. Response(ServicingEvent), + /// A status call established that an earlier uncertain write is no longer active. IdleConfirmed, + /// `uncertain` keeps power actions blocked when Trident may still be writing. Error { details: String, + /// An ambiguous RPC or disconnected stream cannot prove Trident is idle. uncertain: bool, }, } +/// Interpreted daemon completion, separate from a broken or invalid response. #[derive(Debug, PartialEq, Eq)] pub(super) enum Completion { + /// `performed` prevents a no-op from triggering the installer's reboot countdown. Success { + /// Reported by Trident; used only when the caller allows automatic reboot. reboot_required: bool, + /// No-op completions must not claim an installation or trigger reboot. performed: bool, }, + /// Trident completed with a known failure, so the active-write guard can clear. Failure(String), } diff --git a/crates/installer/src/config.rs b/crates/installer/src/config.rs index fefe7cc5ab..03ba4d970f 100644 --- a/crates/installer/src/config.rs +++ b/crates/installer/src/config.rs @@ -9,43 +9,60 @@ use log::LevelFilter; use serde::{Deserialize, Serialize}; use toml::Value as TomlValue; +/// Bootstrap TOML packaged with the installer RPM. pub(super) const DEFAULT_CONFIG: &str = "/etc/trident/installer.toml"; +/// Fallback Host Configuration when no explicit source is configured. pub(super) const DEFAULT_HOST_CONFIGURATION: &str = "/etc/trident/config.yaml"; +/// Optional settings layered from the selected installation media. pub(super) const MEDIA_CONFIG: &str = "installer/installer.toml"; +/// Read-only mount for media selected by filesystem label. pub(super) const MEDIA_MOUNT: &str = "/run/trident/installer-media"; +/// FAT-compatible label for installer USB partitions. pub(super) const DEFAULT_MEDIA_LABEL: &str = "TRD_INSTALL"; +/// Also recognize the ISO label emitted by Image Customizer. pub(super) const DEFAULT_MEDIA_LABELS: &[&str] = &[DEFAULT_MEDIA_LABEL, "CDROM"]; +/// Bootstrap settings merged with media-local overrides when the install needs media. #[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub(super) struct Config { + /// Startup mode; only autorun is implemented. pub mode: Mode, + /// Serial consoles mirror logs, not the interactive UI. #[serde(default)] pub serial_mode: SerialMode, + /// Filters serial output independently of the on-screen log filter. #[serde(default)] pub serial_verbosity: SerialVerbosity, + /// How to find and scan installation media. #[serde(default)] pub media: Media, + /// Source and reboot policy for unattended startup. #[serde(default)] pub autorun: Autorun, } +/// Interactive source selection is reserved; recovery remains available after autorun. #[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)] #[serde(rename_all = "kebab-case", deny_unknown_fields)] pub(super) enum Mode { #[default] Autorun, + /// Reserved; `require_autorun` rejects it until source selection is implemented. Interactive, } +/// The serial UI variant is reserved so it cannot silently enable an unfinished feature. #[derive(Serialize, Deserialize, Debug, Default, Clone, Copy, PartialEq, Eq)] #[serde(rename_all = "kebab-case", deny_unknown_fields)] pub(super) enum SerialMode { #[default] Logs, + /// Reserved; a serial TUI is not implemented. Ui, } +/// Limits serial records without dropping them from the private diagnostic log. #[derive(Serialize, Deserialize, Debug, Default, Clone, Copy, PartialEq, Eq)] #[serde(rename_all = "kebab-case", deny_unknown_fields)] pub(super) enum SerialVerbosity { @@ -59,7 +76,7 @@ pub(super) enum SerialVerbosity { } impl SerialVerbosity { - /// Converts the configured serial level into the log filter used when mirroring records. + /// Maps the serialized level to a serial filter without changing retained records. pub(super) fn filter(self) -> LevelFilter { match self { Self::Off => LevelFilter::Off, @@ -72,13 +89,17 @@ impl SerialVerbosity { } } +/// Locate media by label or use an existing mount; only one selector may be set. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub(super) struct Media { + /// Overrides automatic discovery of the USB and ISO labels. #[serde(default, alias = "cdromLabel", skip_serializing_if = "Option::is_none")] pub filesystem_label: Option, + /// Already-mounted media, never mounted again by label discovery. #[serde(default, skip_serializing_if = "Option::is_none")] pub mount_path: Option, + /// Relative directory containing directly selectable COSI files. #[serde(default = "default_cosi_directory")] pub cosi_directory: PathBuf, } @@ -94,16 +115,18 @@ impl Default for Media { } } -/// Returns the relative COSI directory used when the media config omits it. fn default_cosi_directory() -> PathBuf { "cosi".into() } +/// Picks a Host Configuration before falling back to a COSI, then handles reboot. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub(super) struct Autorun { + /// Local, remote, or media-relative YAML source; absent uses the live-OS fallback. #[serde(default, skip_serializing_if = "Option::is_none")] pub host_configuration: Option, + /// Reboot only if Trident reports completed work that requires it. #[serde(default = "default_reboot")] pub reboot: bool, /// Accepted for compatibility with older installer media; autorun does not check UUIDs. @@ -122,7 +145,6 @@ impl Default for Autorun { } } -/// Enables reboot when the autorun configuration does not specify a preference. fn default_reboot() -> bool { true } diff --git a/crates/installer/src/main.rs b/crates/installer/src/main.rs index 3f14294058..72c3fc6bdf 100644 --- a/crates/installer/src/main.rs +++ b/crates/installer/src/main.rs @@ -23,12 +23,18 @@ mod source; mod terminal; mod ui; +/// Append-only diagnostic log; unlike the display, it keeps full event details. const LOG_PATH: &str = "/var/log/trident-installer.log"; +/// Restricts diagnostic records that may contain configuration details. const PRIVATE_FILE_MODE: u32 = 0o600; +/// Leaves a runtime worker available while another drives a servicing request. const WORKER_THREADS: usize = 2; +/// One name for the command help and console header. const APPLICATION_NAME: &str = "Trident Linux Installer"; +/// The logger forwards records here after the UI starts; earlier records use stderr. static INSTALLER_LOG_TX: OnceLock> = OnceLock::new(); +/// Bootstrap options; installation details come from installer TOML and the Host Configuration. #[derive(Debug, Parser)] #[command( name = "trident-installer", @@ -36,10 +42,13 @@ static INSTALLER_LOG_TX: OnceLock> = OnceLock::new(); about = APPLICATION_NAME )] struct Args { + /// Path to installer TOML, not the Host Configuration YAML. #[arg(long, default_value = DEFAULT_CONFIG)] config: PathBuf, + /// Show line-oriented output instead of the full-screen UI. #[arg(long)] plain: bool, + /// Claim the ISO console and mirror log records to active serial consoles. #[arg(long)] system_console: bool, } diff --git a/crates/installer/src/source.rs b/crates/installer/src/source.rs index 1f3d64240f..97748b24b0 100644 --- a/crates/installer/src/source.rs +++ b/crates/installer/src/source.rs @@ -30,31 +30,51 @@ use trident_api::{ use crate::config::{self, Config}; +/// Host Configuration image reference replaced with the first selected media COSI. pub(super) const IMAGE_MARKER: &str = "installer://image"; +/// Maximum time for one HTTP source request. const SOURCE_TIMEOUT: Duration = Duration::from_secs(30); +/// Overall window for transient connection and request-timeout retries. const REMOTE_RETRY_TIMEOUT: Duration = Duration::from_secs(60); +/// Pause between remote source retries while networking starts. const REMOTE_RETRY_INTERVAL: Duration = Duration::from_secs(2); +/// Allow time for late USB or BMC virtual-media enumeration. const MEDIA_DISCOVERY_TIMEOUT: Duration = Duration::from_secs(60); +/// Recheck labelled block devices while awaiting media. const MEDIA_POLL_INTERVAL: Duration = Duration::from_secs(2); +/// Reject oversized YAML before deserializing a Host Configuration. const MAX_HOST_CONFIGURATION_BYTES: u64 = 2 * 1024 * 1024; +/// Bound the archive scan and the metadata entry read during preflight. const MAX_METADATA_BYTES: u64 = 16 * 1024 * 1024; +/// Trident's COSI metadata member name. const METADATA_PATH: &str = "metadata.json"; +/// Match the disk-name families StreamDisk may choose as a target. const STREAM_DISK_PREFIXES: &[&str] = &["sd", "nvme", "vd", "hd", "mmcblk"]; +/// Initial autorun or a manually chosen recovery source. #[derive(Debug, Clone, PartialEq, Eq)] pub(super) enum Request { + /// Select a Host Configuration or COSI from the configured sources. Autorun, + /// Remote Host Configuration URL entered on the recovery screen. HostConfiguration(String), + /// Remote COSI URL entered on the recovery screen. Stream(Url), } +/// Fully selected servicing operation passed to the gRPC worker. #[derive(Debug, Clone)] pub(super) enum Plan { + /// Validated Host Configuration and its original diagnostic source. Install { + /// `installer://image`, if present, has already been resolved. config: Box, + /// Original source reference; redact URL credentials before display. source: String, }, + /// Image URL retained for preflight and the daemon's subsequent fetch. Stream { + /// May contain signed query parameters, so never log it raw. image: Url, }, } @@ -93,6 +113,7 @@ pub(super) fn display_source(source: &str) -> String { /// Removes credentials and signed query data from URLs embedded in diagnostics. pub(super) fn redact_message_urls(message: &str) -> String { + /// Share the URL pattern across diagnostic records instead of recompiling it. static URLS: OnceLock = OnceLock::new(); let urls = URLS.get_or_init(|| { Regex::new(r#"(?i)https?://[^\s"<>]+"#).expect("invariant: diagnostic URL pattern is valid") @@ -133,7 +154,9 @@ pub(super) fn prepare_with_progress( prepare_from_media_with_progress(settings, request, root.as_deref(), &mut progress) } -/// Treats a complete live-OS or remote Host Configuration as authoritative, without media overlays. +/// Treats a complete live-OS or remote Host Configuration as authoritative. +/// +/// `None` means it or another file-backed input may need media and its overlay. fn self_contained_install( settings: &Config, progress: &mut impl FnMut(&str) -> Result<(), Error>, @@ -212,6 +235,7 @@ fn host_configuration_needs_media( Ok(false) } +/// Checks typed script paths; shell contents and arbitrary arguments cannot be inspected. fn script_needs_media(script: &Script, settings: &Config) -> Result { if let ScriptSource::Path(path) = &script.source { if path_may_need_media(path, settings)? { @@ -237,6 +261,7 @@ fn file_source_may_need_media(url: &Url, settings: &Config) -> Result Result { let media = settings .media @@ -556,6 +581,7 @@ pub(super) fn resolve_host_configuration( resolve_parsed_host_configuration(config, selected) } +/// Validate only after replacing the installer marker with the chosen local image. fn resolve_parsed_host_configuration( mut config: HostConfiguration, selected: Option<&Path>, @@ -573,17 +599,22 @@ fn resolve_parsed_host_configuration( Ok(config) } -// Only disk sizing and the metadata hash are needed here; Trident validates the full COSI. +/// Only the fields needed for disk safety and hash binding; Trident parses the full COSI. #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] struct Metadata { + /// StreamDisk needs COSI version 1.2 or newer. version: String, + /// Disk size decides which attached device Trident could select. disk: Option, + /// An image-less COSI is invalid even when it declares disk metadata. images: Vec, } +/// The disk size is in bytes, matching `lsblk` capacity. #[derive(Debug, Deserialize)] struct DiskMetadata { + /// Trident chooses the smallest disk fitting this byte size. size: u64, } diff --git a/crates/installer/src/terminal.rs b/crates/installer/src/terminal.rs index f43b059902..d1a2e2416f 100644 --- a/crates/installer/src/terminal.rs +++ b/crates/installer/src/terminal.rs @@ -15,8 +15,11 @@ use nix::{libc, unistd}; use osutils::dependencies::{self, Dependency}; +/// Kernel list of consoles active for this boot. const ACTIVE_CONSOLES_PATH: &str = "/sys/class/tty/console/active"; +/// The installer service owns tty1 and stops its getty before attaching. const GRAPHICAL_CONSOLE: &str = "tty1"; +/// Linux virtual terminals expose 16 programmable ANSI color slots. const VT_PALETTE_SIZE: u8 = 16; /// Sets RGB values for the Linux virtual console's 16 ANSI colour slots. @@ -97,6 +100,7 @@ pub(super) fn attach(path: impl AsRef) -> Result { path.display() ); if unistd::getsid(None)? != unistd::getpid() { + // A session leader can claim tty1 as its controlling terminal. unistd::setsid()?; } // The caller has stopped competing getty units before claiming the terminal. @@ -107,7 +111,7 @@ pub(super) fn attach(path: impl AsRef) -> Result { dependencies::set_foreground_group(&terminal, unistd::getpgrp()) .context("Failed to make installer the terminal's foreground process group")?; for target in [libc::STDIN_FILENO, libc::STDOUT_FILENO] { - // dup2 redirects the inherited stdio descriptors without transferring ownership. + // Leave stderr on its inherited sink rather than drawing it over tty1. if unsafe { libc::dup2(terminal.as_raw_fd(), target) } < 0 { return Err(io::Error::last_os_error()).context("Failed to attach terminal"); } diff --git a/crates/installer/src/ui/mod.rs b/crates/installer/src/ui/mod.rs index 6c571b3fd2..b0f4e0422b 100644 --- a/crates/installer/src/ui/mod.rs +++ b/crates/installer/src/ui/mod.rs @@ -45,13 +45,20 @@ pub(super) use output::Output; use render::{render, title}; use theme::{vt_rgb_palette, VERBOSITY_LEVELS}; +/// Refresh often enough to show progress without busy-polling the terminal. const FRAME_INTERVAL: Duration = Duration::from_millis(100); +/// Drain pasted input promptly without starving worker events. const INPUT_EVENTS_PER_FRAME: usize = 256; +/// Bound the worker event queue and the amount processed in one loop iteration. const EVENT_CAPACITY: usize = 64; +/// Owns the alternate screen and restores terminal state even on early exit. struct Display { + /// Ratatui draws to stdout, which may be an attached graphical console. terminal: Terminal>, + /// Only restore raw mode and the alternate screen when the UI owns them. active: bool, + /// Linux VTs need a programmed ANSI palette; other terminals use RGB directly. linux_vt: bool, } @@ -264,6 +271,7 @@ pub(super) async fn run( printed_screen = None; } if shell.is_some() { + // Keep draining servicing events while the shell owns the TTY, but hold reboot. tokio::select! { _ = time::sleep(FRAME_INTERVAL) => {} _ = termination.recv() => return Err(anyhow!("Installer stopped; servicing may continue in Trident")), diff --git a/crates/installer/src/ui/model.rs b/crates/installer/src/ui/model.rs index 6048f80e11..2a82193fe7 100644 --- a/crates/installer/src/ui/model.rs +++ b/crates/installer/src/ui/model.rs @@ -25,21 +25,28 @@ use super::theme::{ COLOR_TRIDENT, COLOR_WARN, VERBOSITY_LEVELS, }; +/// Give operators time to remove installer media before the automatic reboot. const REBOOT_DELAY: Duration = Duration::from_secs(5); +/// Limit in-memory display history; the private diagnostic file keeps complete records. const LOG_CAPACITY: usize = 500; +/// Bound work spent rendering one record without shortening saved diagnostics. const DISPLAY_LOG_CHARACTERS: usize = 4096; +/// Screen state used by both Ratatui and the plain-text fallback. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(super) enum Screen { Progress, Error, + /// Actions after a failed attempt when no worker is active. Recovery, Success, StreamUrl, HostConfigUrl, + /// Limited actions while an operation may still be writing. Menu, } +/// Distinguishes local installer messages from records sent by Trident. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(super) enum LogSource { Inst, @@ -67,12 +74,18 @@ impl LogSource { } } +/// One diagnostic, with a shortened copy retained in UI history. #[derive(Debug, Clone)] pub(super) struct LogEntry { + /// Time since this installation attempt began. pub(super) elapsed: Duration, + /// Controls the source tag and its color in both output modes. pub(super) source: LogSource, + /// Used by display and serial filters independently. pub(super) level: LogLevel, + /// Full message for output; the copy in `Model::logs` may be shortened. pub(super) message: String, + /// Keeps a success or failure record visible even under a restrictive filter. pub(super) result_color: Option, } @@ -122,26 +135,46 @@ impl LogEntry { } } +/// Mutable UI state; events and keys change it without directly performing I/O. #[derive(Debug)] pub(super) struct Model { + /// Current page, including recovery menus shown while work may still run. pub(super) screen: Screen, + /// Sanitized description of the current Host Configuration or COSI source. pub(super) source: String, + /// Short, most recent progress message above the log panel. pub(super) activity: String, + /// Current result or failure, retained for the detailed error view. pub(super) details: String, + /// Bounded, single-line copies of diagnostics; complete entries go to the log file. pub(super) logs: VecDeque, + /// On-screen filter; changing it does not discard stored records. pub(super) verbosity: LevelFilter, + /// Separate serial filter loaded from installer settings. pub(super) serial_verbosity: LevelFilter, + /// Whether the display-only log filter is being edited. pub(super) verbosity_open: bool, + /// Highlighted option in the log filter picker. pub(super) verbosity_selected: usize, + /// Highlighted action on the current screen. pub(super) selected: usize, + /// Wrapped log rows to scroll back, or detail rows when details are open. pub(super) scroll: u16, + /// Recovery URL under edit; it is validated before starting a request. pub(super) url: String, + /// The daemon may still be writing; block power actions until idleness is confirmed. pub(super) uncertain: bool, + /// Media settings can disable the otherwise automatic reboot. reboot: bool, + /// Countdown starts only after Trident reports completed work needing reboot. pub(super) reboot_at: Option, + /// Reset at each attempt so elapsed times refer to the current operation. pub(super) started: Instant, + /// Headless runs return an error if recovery requires a terminal. pub(super) failed: bool, + /// Toggle between retained logs and the full failure details. error_details_open: bool, + /// Selects ANSI palette roles instead of raw RGB for graphical Linux VTs. pub(super) linux_vt: bool, } @@ -340,7 +373,8 @@ impl Model { } /// Updates navigation for a key press and returns an action only when one is requested. - /// URL entry and the verbosity picker consume keys before screen-level shortcuts. + /// URL entry and the verbosity picker consume keys before screen-level shortcuts; + /// a running worker or uncertain outcome blocks power even on a stale screen. pub(super) fn key(&mut self, key: KeyEvent, worker_active: bool) -> Option { if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) { return None; @@ -515,10 +549,13 @@ impl Model { } } +/// A request for the runtime to act; key handling itself never starts servicing. #[derive(Debug, PartialEq, Eq)] pub(super) enum Action { + /// Launch the selected recovery operation after releasing any previous worker. Start(Request), Shell, + /// Reject power actions while a worker runs or the prior outcome is uncertain. BlockedPower, Reboot, Poweroff, diff --git a/crates/installer/src/ui/output.rs b/crates/installer/src/ui/output.rs index 3c107541ea..343088b8af 100644 --- a/crates/installer/src/ui/output.rs +++ b/crates/installer/src/ui/output.rs @@ -9,13 +9,16 @@ use crate::client::Event; use super::model::{log_visible, LogEntry}; +/// Writes complete diagnostics privately and mirrors filtered status to serial ports. pub(crate) struct Output { + /// Opened by startup with restrictive permissions; optional in tests. pub(crate) log: Option, + /// Device paths identify failing mirrors so only those ports are disabled. pub(crate) mirrors: Vec<(PathBuf, File)>, } impl Output { - /// Persists the complete event in the diagnostic file, if one is configured. + /// Preserve the event before the UI shortens its display copy. pub(super) fn record(&mut self, event: &Event) -> Result<(), Error> { if let Some(log) = &mut self.log { writeln!(log, "{event:#?}").context("Failed to preserve installer diagnostics")?; diff --git a/crates/installer/src/ui/render.rs b/crates/installer/src/ui/render.rs index 59a9c76bee..1aaed363a8 100644 --- a/crates/installer/src/ui/render.rs +++ b/crates/installer/src/ui/render.rs @@ -21,9 +21,13 @@ use super::{ }, }; +/// Reserve space for the wordmark, name and state on a standard terminal. const WORDMARK_HEADER_HEIGHT: u16 = 12; +/// Keep a readable two-line heading when the wordmark does not fit. const COMPACT_HEADER_HEIGHT: u16 = 2; +/// Leave enough height for logs and actions beneath the full wordmark. const MIN_WORDMARK_TERMINAL_HEIGHT: u16 = 24; +/// Trident name drawn without relying on terminal-specific fonts or graphics. const TRIDENT_WORDMARK: [&str; 8] = [ r#"88888888888 d8b 888 888"#, r#" 888 Y8P 888 888"#, @@ -34,6 +38,7 @@ const TRIDENT_WORDMARK: [&str; 8] = [ r#" 888 888 888 Y88b 888 Y8b. 888 888 Y88b."#, r#" 888 888 888 "Y88888 "Y8888 888 888 "Y888"#, ]; +/// ASCII borders remain legible on Linux VTs and serial-like terminals. const ASCII_BORDER: BorderSet = BorderSet { top_left: "+", top_right: "+", @@ -45,7 +50,7 @@ const ASCII_BORDER: BorderSet = BorderSet { horizontal_bottom: "-", }; -/// Provides the short status heading for each installer screen. +/// Use the same screen headings in Ratatui and plain terminal output. pub(super) fn title(screen: Screen) -> &'static str { match screen { Screen::Progress => "INSTALLING", @@ -68,7 +73,7 @@ fn block(title: &str, linux_vt: bool) -> Block<'_> { .title(title) } -/// Finds the widest wordmark row for layout and centering. +/// Center against the widest row; the ASCII rows do not all have equal width. fn wordmark_width() -> usize { TRIDENT_WORDMARK .iter() @@ -153,6 +158,7 @@ fn log_panel(model: &Model, area: Rect) -> Paragraph<'_> { )); let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false }); let visible_rows = usize::from(area.height.saturating_sub(2)); + // Wrapped rows, not entry counts, determine whether the final result remains visible. let total_rows = paragraph.line_count(area.width.saturating_sub(2).max(1)); let offset = total_rows .saturating_sub(visible_rows) diff --git a/crates/installer/src/ui/theme.rs b/crates/installer/src/ui/theme.rs index 52b88cadc4..fcf274d1bf 100644 --- a/crates/installer/src/ui/theme.rs +++ b/crates/installer/src/ui/theme.rs @@ -5,18 +5,29 @@ use std::io; use log::LevelFilter; use ratatui::style::Color; +/// Keep the same dark surface across Ratatui panels and the Linux VT palette. pub(super) const COLOR_BACKGROUND: Color = Color::Rgb(11, 17, 24); +/// Neutral borders distinguish ordinary panels from success and failure. pub(super) const COLOR_FRAME: Color = Color::Rgb(53, 69, 83); +/// Highlight the wordmark, selected controls and current activity. pub(super) const COLOR_BRAND: Color = Color::Rgb(75, 216, 220); +/// Source tag for messages produced by the installer itself. pub(super) const COLOR_INST: Color = Color::Rgb(171, 186, 196); +/// Source tag for logs received from the Trident daemon. pub(super) const COLOR_TRIDENT: Color = Color::Rgb(114, 185, 165); +/// Failure frame and error severity share one palette role. pub(super) const COLOR_ERROR: Color = Color::Rgb(255, 107, 115); +/// Unknown daemon levels receive the same attention as warnings. pub(super) const COLOR_WARN: Color = Color::Rgb(245, 182, 93); +/// Informational records stay readable against the dark background. pub(super) const COLOR_INFO: Color = Color::Rgb(229, 234, 236); +/// Debug records remain legible without competing with results. pub(super) const COLOR_DEBUG: Color = Color::Rgb(167, 180, 189); +/// Trace records are the least prominent diagnostic level. pub(super) const COLOR_TRACE: Color = Color::Rgb(99, 115, 129); +/// Successful completion uses a distinct result color. pub(super) const COLOR_SUCCESS: Color = Color::Rgb(93, 200, 148); -// Linux VT maps RGB SGR values into 16 colours, so give each UI role its own ANSI slot. +/// Linux VT maps RGB requests into 16 ANSI colors, so each role needs its own slot. const VT_PALETTE: &[(u8, Color, Color)] = &[ (0, COLOR_BACKGROUND, Color::Black), (1, COLOR_ERROR, Color::Red), @@ -31,6 +42,16 @@ const VT_PALETTE: &[(u8, Color, Color)] = &[ (15, COLOR_INFO, Color::White), ]; +/// One stable order for the picker and its number-key shortcuts. +pub(super) const VERBOSITY_LEVELS: [LevelFilter; 6] = [ + LevelFilter::Off, + LevelFilter::Error, + LevelFilter::Warn, + LevelFilter::Info, + LevelFilter::Debug, + LevelFilter::Trace, +]; + /// Maps known theme RGB colors to distinct ANSI roles on Linux VTs. /// Other colors and non-VT terminals retain the original value. pub(super) fn theme_color(color: Color, linux_vt: bool) -> Color { @@ -57,11 +78,3 @@ pub(super) fn vt_rgb_palette() -> Result, io::Error> { }) .collect() } -pub(super) const VERBOSITY_LEVELS: [LevelFilter; 6] = [ - LevelFilter::Off, - LevelFilter::Error, - LevelFilter::Warn, - LevelFilter::Info, - LevelFilter::Debug, - LevelFilter::Trace, -]; From 713d774d2ad0987c398ef75a4590ef4af3b6c7b9 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 18 Nov 2026 11:18:30 -0800 Subject: [PATCH 59/62] docs: Add installer operation and recovery reference Document autorun source selection, colored screen flows, recovery actions and safety controls; link the new page from installer configuration and crate README. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/README.md | 3 + docs/Reference/Installer-Configuration.md | 5 +- docs/Reference/Installer.md | 138 ++++++++++++++++++++++ 3 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 docs/Reference/Installer.md diff --git a/crates/installer/README.md b/crates/installer/README.md index afda872f45..258ad31dd7 100644 --- a/crates/installer/README.md +++ b/crates/installer/README.md @@ -4,6 +4,9 @@ autorun wrapper around Trident's gRPC API through the shared `trident-client` crate, not a partitioning engine. +See the [Installer reference](../../docs/Reference/Installer.md) for the +source-selection and screen flows. + ## Configuration Read `/etc/trident/installer.toml` or the path supplied with `--config`. diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index ba2dbc6f05..cc1d0e2831 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -1,5 +1,5 @@ --- -sidebar_position: 7 +sidebar_position: 8 title: Installer Configuration --- @@ -9,7 +9,8 @@ title: Installer Configuration `--config PATH` to use another file. This TOML file controls the **installer**; the [Host Configuration](./Host-Configuration/Host-Configuration.md) is a separate YAML document describing what Trident installs. Installation requires -a readable TOML file. +a readable TOML file. See [Trident Installer](./Installer.md) for the +source-selection and recovery-screen flows. The packaged `/etc/trident/installer.toml` explicitly sets: diff --git a/docs/Reference/Installer.md b/docs/Reference/Installer.md new file mode 100644 index 0000000000..d9467f931c --- /dev/null +++ b/docs/Reference/Installer.md @@ -0,0 +1,138 @@ +--- +sidebar_position: 7 +title: Trident Installer +--- + +# Trident Installer + +`trident-installer` starts an installation immediately on a marked installer +boot. It reads [installer TOML](./Installer-Configuration.md), then either sends +a Host Configuration to Trident's **Install** operation or sends a COSI to +**StreamDisk**. The terminal UI shows progress and offers recovery after a +failure or interruption; it is **not** an initial source picker. +`mode = "interactive"` is reserved and currently rejected. + +## Choosing the installation source + +```mermaid +flowchart TD + START(("START: installer boots")) --> CONFIG["Read installer TOML; require autorun"] + CONFIG -->|Invalid| ERROR["ERROR"] + CONFIG --> SELF{"Complete live-OS or HTTP(S) Host Configuration without media-backed inputs?"} + SELF -->|Yes| INSTALL["Install with that HC; skip media overrides"] + SELF -->|No| MEDIA["Find installer media; wait up to 60 seconds during autorun"] + MEDIA --> OVERLAY["Apply media-local installer.toml, if present"] + OVERLAY --> HC{"Configured HC or default config.yaml file or symlink?"} + HC -->|Yes| READ["Read HC; resolve installer://image to first media COSI if used"] + READ --> INSTALL + HC -->|No| COSI{"Media contains a COSI?"} + COSI -->|Yes| STREAM["StreamDisk: first regular COSI by filename"] + COSI -->|No| ERROR + STREAM --> PREFLIGHT["Check metadata hash and eligible disk safety"] + PREFLIGHT -->|Failed| ERROR + INSTALL --> TRIDENT["Send operation to Trident over gRPC"] + PREFLIGHT --> TRIDENT + TRIDENT -->|Completed successfully| SUCCESS["SUCCESS"] + TRIDENT -->|Failure or unknown outcome| ERROR + classDef startPoint fill:#07545b,stroke:#77e6db,color:#f0fffc,stroke-width:3px + classDef success fill:#123c2a,stroke:#3fb950,color:#e7ffed,stroke-width:2px + classDef error fill:#52212a,stroke:#f85149,color:#ffe8e5,stroke-width:2px + class START startPoint + class SUCCESS success + class ERROR error +``` + +Media is needed for a media-relative HC, a dangling HC symlink into the media, +`installer://image`, or another file-backed HC input on the media (such as an +additional file, script, extension, or recovery key). A self-contained HC takes +precedence over media settings even if the media is already present. An invalid +HC fails; it does not fall back to StreamDisk. Refer to +[Installer Configuration](./Installer-Configuration.md) for accepted paths, +filesystem labels, overrides, and ISO setup. + +:::warning Reinstall on every installer boot + +Autorun does not ask for confirmation or check whether the target OS is already +installed. Remove or deprioritize the installer media after installation; +leaving it first in boot order can reimage the machine on every boot. + +::: + +## Screens + +The same screen has the same color in both diagrams: blue **PROGRESS**, amber +**MENU**, red **ERROR**, purple **RECOVERY**, and green **SUCCESS**. Teal circles +mark entry points; teal rectangles are URL-entry screens. + +```mermaid +flowchart TD + START(("START: autorun")) --> P["PROGRESS: activity and logs"] + P -->|Ctrl-C or Esc| M["MENU: Return to installation; Shell"] + M -->|Return or Esc| P + P -->|Completed successfully| S["SUCCESS: Reboot; Shell"] + M -->|Completed successfully| S + P -->|Failure or unknown outcome| E["ERROR: logs; Continue; Shell"] + M -->|Failure or unknown outcome| E + E -->|D| ED["Full error details"] + ED -->|D or Esc| E + E -->|Continue, Esc, or Ctrl-C| R["RECOVERY"] + S -->|Ctrl-C| R + classDef startPoint fill:#07545b,stroke:#77e6db,color:#f0fffc,stroke-width:3px + classDef progress fill:#153858,stroke:#58a6ff,color:#f0f6fc,stroke-width:2px + classDef menu fill:#493416,stroke:#d29922,color:#fff3dc,stroke-width:2px + classDef success fill:#123c2a,stroke:#3fb950,color:#e7ffed,stroke-width:2px + classDef error fill:#52212a,stroke:#f85149,color:#ffe8e5,stroke-width:2px + classDef recovery fill:#342450,stroke:#b392f0,color:#f4ebff,stroke-width:2px + class START startPoint + class P progress + class M menu + class S success + class E,ED error + class R recovery +``` + +```mermaid +flowchart TD + START(("START: enter recovery")) --> R["RECOVERY: Shell; Stream COSI URL; Install HC URL; optional Shutdown"] + R -->|Stream COSI URL| U1["STREAM URL entry"] + R -->|Install HC URL| U2["HOST CONFIG URL entry"] + U1 -->|Esc| R + U2 -->|Esc| R + U1 -->|Invalid URL| E["ERROR"] + U2 -->|Invalid URL| E + U1 -->|Enter valid URL| P["PROGRESS: new attempt"] + U2 -->|Enter valid URL| P + P -->|Success| S["SUCCESS"] + P -->|Failure or unknown outcome| E + R -->|Shutdown when safe| OFF["Power off"] + S -->|Reboot when safe| REBOOT["Reboot"] + S -->|Automatic reboot eligible| TIMER["5-second countdown"] + TIMER --> REBOOT + classDef startPoint fill:#07545b,stroke:#77e6db,color:#f0fffc,stroke-width:3px + classDef progress fill:#153858,stroke:#58a6ff,color:#f0f6fc,stroke-width:2px + classDef success fill:#123c2a,stroke:#3fb950,color:#e7ffed,stroke-width:2px + classDef error fill:#52212a,stroke:#f85149,color:#ffe8e5,stroke-width:2px + classDef recovery fill:#342450,stroke:#b392f0,color:#f4ebff,stroke-width:2px + classDef urlEntry fill:#123c42,stroke:#56d4ca,color:#e5fffc,stroke-width:2px + class START startPoint + class R recovery + class U1,U2 urlEntry + class P progress + class E error + class S success +``` + +| Action | Behavior | +| --- | --- | +| Arrows, Tab, number keys, Enter | Select and activate an action. `V` changes only the display log filter; Page Up/Down scrolls logs or error details. | +| Shell or `S` | Opens a root recovery shell from the current screen. Servicing continues and automatic reboot waits while the shell owns the terminal; `exit` returns to the latest screen. A nonzero shell exit is only a warning. | +| Stream COSI URL / Install HC URL | Recovery accepts HTTP(S) URLs without embedded username or password. A new attempt reloads installer TOML. Explicit StreamDisk recovery skips local media; recovery HC checks it once without waiting. | +| Reboot / Shutdown | A running worker or uncertain outcome blocks power actions. Shutdown is not offered while the outcome is uncertain; a later attempt first checks Trident's servicing state before another write. | +| Automatic reboot | Starts five seconds after success **only** when enabled, work was performed, and Trident says a reboot is required. | + +With `--system-console`, the installer uses tty1 for the UI and mirrors logs +to active serial consoles; serial consoles do not receive a TUI. `--plain` +uses line-oriented output instead of the full-screen display. Without an +interactive terminal, autorun still runs, but a failure exits with an error +instead of opening recovery. All received events are retained in the private +`/var/log/trident-installer.log`, regardless of display or serial filtering. From 57c7e3bc27997ae1f871080d200e6bebd98d068d Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 18 Nov 2026 15:22:41 -0800 Subject: [PATCH 60/62] docs: Clarify installer startup and recovery guide Name the ISO kernel boot argument directly, simplify mode wording, and introduce prerequisites and source concepts before diagrams and controls. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/Reference/Installer-Configuration.md | 14 ++++++--- docs/Reference/Installer.md | 37 ++++++++++++++++------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index cc1d0e2831..728f9e32c3 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -30,13 +30,17 @@ There is **no implicit default for `mode`** in a TOML file: omitting it is an error, even though the packaged file sets it to `"autorun"`. All other fields may be omitted; unknown fields and invalid values are errors. +The packaged file (or the file passed with `--config`) is the **bootstrap +configuration**: it supplies media-discovery settings before an optional +[media-local override](#optional-configuration-on-the-media) can be read. + ## Selecting Install or StreamDisk -`mode = "autorun"` means **start without an interactive source picker**. It -does not choose the Trident operation; `mode = "install"` and `mode = "stream"` -are not valid TOML settings. If the bootstrap configuration names a live-OS -or HTTP(S) Host Configuration whose file-backed inputs do not need installer -media, autorun uses it without looking for media or applying +`mode = "autorun"` means **start without an interactive source picker**. The +selected source, not `mode`, determines whether Trident performs Install or +StreamDisk. If the bootstrap configuration names a live-OS or HTTP(S) Host +Configuration whose file-backed inputs do not need installer media, autorun +uses it without looking for media or applying [media-local overrides](#optional-configuration-on-the-media). Otherwise it discovers media, applies any overrides, and makes this choice: diff --git a/docs/Reference/Installer.md b/docs/Reference/Installer.md index d9467f931c..93dcbc26b1 100644 --- a/docs/Reference/Installer.md +++ b/docs/Reference/Installer.md @@ -5,12 +5,21 @@ title: Trident Installer # Trident Installer -`trident-installer` starts an installation immediately on a marked installer -boot. It reads [installer TOML](./Installer-Configuration.md), then either sends -a Host Configuration to Trident's **Install** operation or sends a COSI to -**StreamDisk**. The terminal UI shows progress and offers recovery after a -failure or interruption; it is **not** an initial source picker. -`mode = "interactive"` is reserved and currently rejected. +Install the optional `trident-installer` RPM on a full-OS installer ISO and add +`trident.installer=1` to the **ISO boot kernel command line**. The packaged +systemd service starts only when that argument is present; installing the RPM +alone does not start an installation. See +[Installer Configuration](./Installer-Configuration.md#building-an-installer-iso) +for an ISO recipe. + +A [Host Configuration](./Host-Configuration/Host-Configuration.md) (HC) is YAML +describing the OS image, disk layout, and settings for Trident's **Install** +operation. A [Composable OS Image](./Composable-OS-Image.md) (COSI) contains +the image data that **StreamDisk** writes to a disk. **Autorun** selects one +of these operations from the available sources and starts without an initial +confirmation or source picker. The terminal UI shows progress and offers +recovery after a failure or interruption. `mode = "interactive"` is reserved +and currently rejected. ## Choosing the installation source @@ -18,7 +27,7 @@ failure or interruption; it is **not** an initial source picker. flowchart TD START(("START: installer boots")) --> CONFIG["Read installer TOML; require autorun"] CONFIG -->|Invalid| ERROR["ERROR"] - CONFIG --> SELF{"Complete live-OS or HTTP(S) Host Configuration without media-backed inputs?"} + CONFIG --> SELF{"Complete live-OS or HTTP(S) HC without media-backed inputs?"} SELF -->|Yes| INSTALL["Install with that HC; skip media overrides"] SELF -->|No| MEDIA["Find installer media; wait up to 60 seconds during autorun"] MEDIA --> OVERLAY["Apply media-local installer.toml, if present"] @@ -46,9 +55,13 @@ Media is needed for a media-relative HC, a dangling HC symlink into the media, `installer://image`, or another file-backed HC input on the media (such as an additional file, script, extension, or recovery key). A self-contained HC takes precedence over media settings even if the media is already present. An invalid -HC fails; it does not fall back to StreamDisk. Refer to -[Installer Configuration](./Installer-Configuration.md) for accepted paths, -filesystem labels, overrides, and ISO setup. +HC fails; it does not fall back to StreamDisk. + +StreamDisk preflight reads the COSI's required disk size and metadata hash. It +refuses a mounted or read-only disk that Trident could select as the smallest +one that fits, then passes the hash to Trident so its fetch uses the checked +metadata. See [Installer Configuration](./Installer-Configuration.md) for +accepted paths, filesystem labels, and overrides. :::warning Reinstall on every installer boot @@ -125,7 +138,9 @@ flowchart TD | Action | Behavior | | --- | --- | | Arrows, Tab, number keys, Enter | Select and activate an action. `V` changes only the display log filter; Page Up/Down scrolls logs or error details. | -| Shell or `S` | Opens a root recovery shell from the current screen. Servicing continues and automatic reboot waits while the shell owns the terminal; `exit` returns to the latest screen. A nonzero shell exit is only a warning. | +| Ctrl-C / Esc | On PROGRESS, open MENU without stopping work; MENU's Esc or Return returns to PROGRESS. On ERROR, Esc or Continue opens RECOVERY; Esc first closes full details if open. On a URL-entry screen, Esc returns to RECOVERY. On SUCCESS, Ctrl-C opens RECOVERY. | +| `D` on ERROR | Toggle between the log panel and full error details. | +| Shell or `S` | Opens a root recovery shell from PROGRESS, MENU, ERROR, RECOVERY, or SUCCESS. Servicing continues and automatic reboot waits while the shell owns the terminal; `exit` returns to the latest screen. A nonzero shell exit is only a warning. | | Stream COSI URL / Install HC URL | Recovery accepts HTTP(S) URLs without embedded username or password. A new attempt reloads installer TOML. Explicit StreamDisk recovery skips local media; recovery HC checks it once without waiting. | | Reboot / Shutdown | A running worker or uncertain outcome blocks power actions. Shutdown is not offered while the outcome is uncertain; a later attempt first checks Trident's servicing state before another write. | | Automatic reboot | Starts five seconds after success **only** when enabled, work was performed, and Trident says a reboot is required. | From 620158587b76085dc5c1a82ea6b57ecbf2dd5021 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 18 Nov 2026 15:24:04 -0800 Subject: [PATCH 61/62] docs: Explain installer image placeholder precisely Clarify that installer://image is the only supported reserved image URL and selects the first COSI, not a file named in the URL. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/Reference/Installer-Configuration.md | 10 ++++++---- docs/Reference/Installer.md | 8 +++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/docs/Reference/Installer-Configuration.md b/docs/Reference/Installer-Configuration.md index 728f9e32c3..a3281e54df 100644 --- a/docs/Reference/Installer-Configuration.md +++ b/docs/Reference/Installer-Configuration.md @@ -67,10 +67,12 @@ reboot = false ``` Put `installer/host.yaml` on the ISO filesystem (not only in the live OS). -If its `image.url` is `installer://image`, also put a COSI in `cosi/`; the -marker resolves to the first discovered COSI, but the operation remains -**Install**, with disk layout taken from the Host Configuration. An explicit -image URL in the Host Configuration does not require a media COSI. See the +If its `image.url` is `installer://image`, also put a COSI in `cosi/`. +`installer://image` is a reserved installer-only URL meaning "use the first +regular COSI in `media.cosiDirectory`, sorted by filename"; it does not name +an image file. The operation remains **Install**, with disk layout taken from +the Host Configuration. An explicit image URL in the Host Configuration does +not require a media COSI. See the [Host Configuration reference](./Host-Configuration/Host-Configuration.md) for its YAML schema. To **StreamDisk** instead, leave `autorun.hostConfiguration` unset *and* ensure diff --git a/docs/Reference/Installer.md b/docs/Reference/Installer.md index 93dcbc26b1..944def4bb4 100644 --- a/docs/Reference/Installer.md +++ b/docs/Reference/Installer.md @@ -51,9 +51,11 @@ flowchart TD class ERROR error ``` -Media is needed for a media-relative HC, a dangling HC symlink into the media, -`installer://image`, or another file-backed HC input on the media (such as an -additional file, script, extension, or recovery key). A self-contained HC takes +`installer://image` is a reserved HC image URL meaning "use the first COSI +found on installer media," not a filename. Media is also needed for a +media-relative HC, a dangling HC symlink into the media, or another file-backed +HC input on the media (such as an additional file, script, extension, or +recovery key). A self-contained HC takes precedence over media settings even if the media is already present. An invalid HC fails; it does not fall back to StreamDisk. From 48db52623b557f6ea9322b83ae8d20e0e93fdf87 Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 18 Nov 2026 17:01:31 -0800 Subject: [PATCH 62/62] refactor: Extract installer screen cleanup helper Replace the immediately invoked closure with leave_screen while preserving palette cleanup and error ordering. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- crates/installer/src/ui/mod.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/crates/installer/src/ui/mod.rs b/crates/installer/src/ui/mod.rs index b0f4e0422b..86e79e2a05 100644 --- a/crates/installer/src/ui/mod.rs +++ b/crates/installer/src/ui/mod.rs @@ -89,20 +89,7 @@ impl Display { /// Leaves raw mode and the alternate screen so an interactive shell can use the terminal. /// On Linux VTs, also clears the old UI and restores the original palette. fn suspend(&mut self) -> Result<(), Error> { - let restore = (|| -> Result<(), Error> { - terminal::disable_raw_mode()?; - execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; - if self.linux_vt { - // Linux VT leaves the old TUI visible when exiting its alternate screen. - execute!( - self.terminal.backend_mut(), - ClearTerminal(ClearType::All), - MoveTo(0, 0) - )?; - } - self.terminal.show_cursor()?; - Ok(()) - })(); + let restore = self.leave_screen(); let palette = if self.linux_vt { installer_terminal::reset_vt_palette(&mut io::stdout()) } else { @@ -113,6 +100,22 @@ impl Display { palette?; Ok(()) } + + /// Releases the screen before palette cleanup, which must run even if this fails. + fn leave_screen(&mut self) -> Result<(), Error> { + terminal::disable_raw_mode()?; + execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; + if self.linux_vt { + // Linux VT leaves the old TUI visible when exiting its alternate screen. + execute!( + self.terminal.backend_mut(), + ClearTerminal(ClearType::All), + MoveTo(0, 0) + )?; + } + self.terminal.show_cursor()?; + Ok(()) + } } impl Drop for Display {