diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..9741f11 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +xtask = "run --quiet --package xtask --" diff --git a/.gitignore b/.gitignore index 3dec543..6963297 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ target result* -*.qcow2 diff --git a/rustfmt.toml b/.rustfmt.toml similarity index 100% rename from rustfmt.toml rename to .rustfmt.toml diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f1e081a..02cfe8a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,30 +2,52 @@ ## Workflow -Work inside the dev shell (`direnv allow` once). Run `cargo clippy ---all-targets` before committing (the crate warns on clippy pedantic plus -`absolute_paths`) and `cargo fmt`, which needs the nightly toolchain for -`rustfmt.toml`. Version control is jj, not the `git` CLI. +Work inside the dev shell (`direnv allow` once). Run `cargo clippy --workspace +--all-targets` before committing (the workspace warns on clippy pedantic plus +`absolute_paths`) and `cargo fmt --all`, which needs the nightly toolchain for +`.rustfmt.toml`. Version control is jj, not the `git` CLI. + +The workspace has two crates: `bootvine` itself and `xtask`, the repository +tasks behind `cargo xtask` (`record-screencasts`, `install`, `uninstall`). ## Testing -- `cargo test`: model and render snapshot tests. -- `nix run .#demo-units -- start`, then - `cargo run -- --user --target bootvine-demo.target --hold 3s`: live against - your user manager. -- `nix build .#checks.x86_64-linux.`: each `tests/.nix` boots a VM - and runs `tests/console.py` followed by `tests/.py`. `boot` follows - the initrd, stage 2 and a poweroff; `luks` answers a passphrase prompt in - the initrd. They read tty12 as text from `/dev/vcsu12`, hold the boot with - the initrd backdoor and gate files under `/run` instead of sleeping. Run one - by hand with `nix run .#checks.x86_64-linux.boot.driverInteractive`, then - `test_script()`. -- `nix run .#docs`, from the repository root: renders `docs/boot.tape` and - `docs/shutdown.tape` with VHS against the scripted examples into the README - GIFs, and copies the passphrase screenshot from the `luks` check. +Everything that boots a machine or a service manager lives under `tests/`: +`tests/sample-machine/` holds the sample host, its slow and failing sample units +and the interactive VM; `tests/vm-checks/` holds the NixOS tests that boot it. -Prefix the VM commands with `env -u LD_LIBRARY_PATH` when the shell exports -libraries that clash with QEMU's. +- `cargo test --workspace`: model and render snapshot tests. +- Live against your user manager, with the sample units: + + ```sh + units=$(nix build --no-link --print-out-paths .#sample-unit-files) + systemctl --user link --runtime $units/* + systemctl --user start --no-block bootvine-demo.target + cargo run -- --user --target bootvine-demo.target --hold 3s + systemctl --user stop $(ls $units) + systemctl --user disable --runtime $(ls $units) + ``` + +- `nix run .#sample-machine-vm`: the sample host in a QEMU window. Its disk + image goes to `~/.cache/bootvine-sample-machine.qcow2`. +- `nix build .#checks.x86_64-linux.`: each `tests/vm-checks/.nix` + boots a VM and runs `console.py` followed by `.py`. + `vm-boot-and-shutdown` follows the initrd, stage 2 and a poweroff; + `vm-luks-passphrase` answers a passphrase prompt in the initrd. They read + tty12 as text from `/dev/vcsu12`, hold the boot with the initrd backdoor and + gate files under `/run` instead of sleeping. Run one by hand with + `nix run .#checks.x86_64-linux.vm-boot-and-shutdown.driverInteractive`, then + `test_script()`. +- `nix run .#record-screencasts`, from the repository root: runs + `cargo xtask record-screencasts`, which records `recordings/boot.tape` and + `recordings/shutdown.tape` with VHS against the scripted examples into the README + GIFs and copies the passphrase screenshot from the `vm-luks-passphrase` check. +- `cargo xtask install --destdir --initrd dracut`: stages an install + without touching the system; `cargo xtask uninstall --destdir ` empties + it again. + +Prefix the VM and VHS commands with `env -u LD_LIBRARY_PATH` when the shell +exports libraries that clash with QEMU's or Chromium's. ## Conventions diff --git a/Cargo.lock b/Cargo.lock index bdd1a43..a93594b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2227,6 +2227,31 @@ version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" +[[package]] +name = "xshell" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e7290c623014758632efe00737145b6867b66292c42167f2ec381eb566a373d" +dependencies = [ + "xshell-macros", +] + +[[package]] +name = "xshell-macros" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32ac00cd3f8ec9c1d33fb3e7958a82df6989c42d747bd326c822b1d625283547" + +[[package]] +name = "xtask" +version = "0.1.0" +dependencies = [ + "clap", + "rustix 1.1.5", + "thiserror 2.0.21", + "xshell", +] + [[package]] name = "zeroize" version = "1.9.1" diff --git a/Cargo.toml b/Cargo.toml index 933d70a..dcc0f38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,14 +9,20 @@ readme = "README.md" keywords = ["systemd", "boot", "initrd", "tui", "console"] categories = ["command-line-utilities", "os::linux-apis"] rust-version = "1.91" -include = ["/src", "/examples", "/packaging", "/README.md", "/LICENSE"] +include = ["/src", "/examples", "/system-files", "/README.md", "/LICENSE"] -[lints.clippy] +[workspace] +members = [".", "xtask"] + +[workspace.lints.clippy] absolute_paths = "warn" pedantic = { priority = -1, level = "warn" } needless_for_each = "allow" missing_errors_doc = "allow" +[lints] +workspace = true + [dependencies] clap = { version = "4.6.7", features = ["derive"] } crossterm = "0.29.0" diff --git a/README.md b/README.md index 0632997..7e10f03 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ nix-output-monitor. It replaces the scrolling kernel and systemd lines with the units still pending and, in the room the tree leaves, a timeline of the slowest ones that ran. -A boot through the initrd into stage 2, with one failed unit +A boot through the initrd into stage 2, with one failed unit It needs systemd ≥ 246 as PID 1, libdbus and a virtual terminal (it draws on tty12). With a systemd initrd it starts about a second after the kernel and @@ -19,25 +19,45 @@ It also follows shutdowns and reboots: the units being stopped, nested under the units that have to stop first, until systemd hands over to `systemd-shutdown`. -A poweroff: units stopping under the targets waiting on them +A poweroff: units stopping under the targets waiting on them Passphrase prompts, such as a LUKS unlock in the initrd, are answered in place over the tree. -A LUKS passphrase prompt floating over the initrd tree +A LUKS passphrase prompt floating over the initrd tree -## Try +## Try it without installing + +Neither of these touches your system's boot. + +### Scripted boots in your terminal + +From a clone, with Rust installed (see step 1 of the Ubuntu install below): ```sh +git clone https://codeberg.org/wvhulle/bootvine.git +cd bootvine cargo run --example boot cargo run --example shutdown cargo run --example luks ``` -play a scripted boot, poweroff or boot from an encrypted root without systemd; -`q` quits. The encrypted root unlocks with the passphrase `demo`; `Esc` quits -while it asks. `nix run .#vm` boots a demo -VM in a window. +These play a scripted boot, a poweroff and a boot from an encrypted root, with +no systemd involved. `q` quits. The encrypted root unlocks with the passphrase +`demo`; `Esc` quits while it asks. + +### A sample machine in a VM + +With Nix, and no clone needed: + +```sh +nix run git+https://codeberg.org/wvhulle/bootvine#sample-machine-vm +``` + +This boots a NixOS machine with bootvine and a set of slow and failing sample +units in a QEMU window, from the initrd through stage 2. It logs in as root; +`poweroff` shows the shutdown. Its disk image is kept in +`~/.cache/bootvine-sample-machine.qcow2`. ## Install @@ -98,45 +118,43 @@ VM in a window. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` -2. Build: +2. Clone, build and install the binary and the systemd units, and enable + them. `cargo xtask install` builds as you and asks `sudo` only for the + copying and `systemctl enable`: ```sh git clone https://codeberg.org/wvhulle/bootvine.git cd bootvine - cargo build --release + cargo xtask install ``` -3. Install the binary and the systemd units, and enable them: + This is enough for bootvine to show stage 2 and shutdown. It installs + `/usr/bin/bootvine` and `bootvine.service` and `bootvine-shutdown.service` + in `/usr/lib/systemd/system`. - ```sh - sudo install -Dm755 target/release/bootvine /usr/bin/bootvine - sudo install -Dm644 -t /usr/lib/systemd/system \ - packaging/systemd/bootvine.service packaging/systemd/bootvine-shutdown.service - sudo systemctl enable bootvine.service bootvine-shutdown.service - ``` - - This is enough for bootvine to show stage 2 and shutdown. - -4. Optionally, follow the boot from the initrd. This needs a dracut initramfs +3. Optionally, follow the boot from the initrd. This needs a dracut initramfs with systemd in it; initramfs-tools has no systemd, so on releases that use it bootvine starts in stage 2 only. ```sh - sudo install -Dm755 packaging/dracut/90bootvine/module-setup.sh \ - /usr/lib/dracut/modules.d/90bootvine/module-setup.sh - echo 'add_dracutmodules+=" bootvine "' | sudo tee /etc/dracut.conf.d/bootvine.conf + cargo xtask install --initrd dracut sudo dracut --force ``` -5. Reboot. + This adds the dracut module in `/usr/lib/dracut/modules.d/90bootvine` and + enables it in `/etc/dracut.conf.d/bootvine.conf`. + +4. Reboot. + +`cargo xtask uninstall` removes all of it again. ### Other distributions -Install the binary and units as in the Ubuntu steps. For the initrd, use the -dracut module as above, or on mkinitcpio install -`packaging/mkinitcpio/install/sd-bootvine` to `/usr/lib/initcpio/install/`, add -`sd-bootvine` after `systemd` in `HOOKS` in `/etc/mkinitcpio.conf`, and run -`sudo mkinitcpio -P`. +Install as in the Ubuntu steps. For the initrd, use the dracut module as above, +or on mkinitcpio run `cargo xtask install --initrd mkinitcpio`, which installs +`/usr/lib/initcpio/install/sd-bootvine`, then add `sd-bootvine` after `systemd` +in `HOOKS` in `/etc/mkinitcpio.conf` and run `sudo mkinitcpio -P`. +`--destdir ` stages the files under `` instead, for packaging. ## Configure diff --git a/crane-builds.nix b/crane-builds.nix new file mode 100644 index 0000000..81cabad --- /dev/null +++ b/crane-builds.nix @@ -0,0 +1,63 @@ +{ + lib, + pkgs, + craneLib, +}: +let + commonArgs = { + src = lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + (craneLib.fileset.commonCargoSources ./.) + ./system-files + ]; + }; + strictDeps = true; + nativeBuildInputs = [ pkgs.pkg-config ]; + buildInputs = [ pkgs.dbus ]; + }; + + cargoArtifacts = craneLib.buildDepsOnly (commonArgs // { pname = "bootvine-workspace"; }); + + buildWorkspaceMember = + args: craneLib.buildPackage (commonArgs // { inherit cargoArtifacts; } // args); +in +{ + bootvine = buildWorkspaceMember { + cargoExtraArgs = "-p bootvine"; + postInstall = '' + for unit in bootvine.service bootvine-shutdown.service; do + install -Dm644 system-files/systemd/$unit $out/lib/systemd/system/$unit + substituteInPlace $out/lib/systemd/system/$unit \ + --replace-fail /usr/bin/bootvine $out/bin/bootvine + done + install -Dm755 system-files/dracut/90bootvine/module-setup.sh \ + $out/lib/dracut/modules.d/90bootvine/module-setup.sh + install -Dm644 system-files/mkinitcpio/install/sd-bootvine \ + $out/lib/initcpio/install/sd-bootvine + ''; + meta = { + mainProgram = "bootvine"; + platforms = lib.platforms.linux; + license = lib.getLicenseFromSpdxId (lib.importTOML ./Cargo.toml).package.license; + }; + }; + + scripted-boot-examples = buildWorkspaceMember { + pname = "bootvine-scripted-boot-examples"; + cargoExtraArgs = "-p bootvine --examples"; + doCheck = false; + installPhaseCommand = '' + for example in boot shutdown; do + install -Dm755 target/release/examples/$example $out/bin/bootvine-$example-example + done + ''; + }; + + repo-tasks = buildWorkspaceMember { + pname = "xtask"; + cargoExtraArgs = "-p xtask"; + doCheck = false; + meta.mainProgram = "xtask"; + }; +} diff --git a/demo/demo-units.nu b/demo/demo-units.nu deleted file mode 100755 index de98c68..0000000 --- a/demo/demo-units.nu +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env nu - -def user-unit-dir []: nothing -> path { - $env.XDG_RUNTIME_DIR | path join systemd user -} - -def unit-names [units: path]: nothing -> list { - ls $units | get name | path basename -} - -def install-units [units: path] { - let target_dir = user-unit-dir - mkdir $target_dir - unit-names $units | each {|name| ^ln -sfn ($units | path join $name) ($target_dir | path join $name) } | ignore - ^systemctl --user daemon-reload -} - -def "main start" [--units: path, --target: string] { - install-units $units - ^systemctl --user stop ...(unit-names $units) - ^systemctl --user start --no-block $target - print $"started ($target); watch it with: bootvine --user --target ($target) --hold 3s" -} - -def "main stop" [--units: path, --target: string] { - ^systemctl --user stop --no-block ...(unit-names $units) - print $"stopping ($target); watch it with: bootvine --user --shutdown --target ($target)" -} - -def "main remove" [--units: path, --target: string] { - ^systemctl --user stop ...(unit-names $units) - unit-names $units | each {|name| rm -f (user-unit-dir | path join $name) } | ignore - ^systemctl --user daemon-reload -} - -def main [--units: path, --target: string] { - print "usage: bootvine-demo-units (start | stop | remove)" -} diff --git a/docs/passphrase.png b/docs/passphrase.png deleted file mode 100644 index cfa18da..0000000 Binary files a/docs/passphrase.png and /dev/null differ diff --git a/flake.nix b/flake.nix index d7f66e7..40fa0ac 100644 --- a/flake.nix +++ b/flake.nix @@ -24,8 +24,7 @@ f: lib.genAttrs [ "x86_64-linux" "aarch64-linux" ] ( system: - f rec { - inherit system; + let pkgs = import nixpkgs { inherit system; overlays = [ (import rust-overlay) ]; @@ -33,120 +32,64 @@ craneLib = (crane.mkLib pkgs).overrideToolchain ( p: p.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml ); + in + f { + inherit system pkgs craneLib; + builds = import ./crane-builds.nix { inherit lib pkgs craneLib; }; } ); - buildInputsFor = pkgs: { - nativeBuildInputs = [ pkgs.pkg-config ]; - buildInputs = [ pkgs.dbus ]; + sampleMachine = import ./tests/sample-machine { + inherit lib; + bootvineModule = self.nixosModules.bootvine; }; - - demoUnits = import ./demo/units.nix; - - demoHost = { - imports = [ - self.nixosModules.default - ./demo/host.nix - ]; - _module.args = { inherit demoUnits; }; - }; - - demoSystem = - system: - lib.nixosSystem { - inherit system; - modules = [ - demoHost - ./demo/interactive.nix - ]; - }; in { packages = forEachSystem ( - { pkgs, craneLib, ... }: - let - commonArgs = { - inherit (buildInputsFor pkgs) nativeBuildInputs buildInputs; - src = lib.fileset.toSource { - root = ./.; - fileset = lib.fileset.unions [ - (craneLib.fileset.commonCargoSources ./.) - ./packaging - ]; - }; - strictDeps = true; - }; - in + { pkgs, builds, ... }: { - examples = craneLib.buildPackage ( - commonArgs - // { - pname = "bootvine-examples"; - cargoExtraArgs = "--examples"; - doCheck = false; - installPhaseCommand = '' - for example in boot shutdown; do - install -Dm755 target/release/examples/$example $out/bin/bootvine-$example-example - done - ''; - } - ); - default = craneLib.buildPackage ( - commonArgs - // { - postInstall = '' - for unit in bootvine.service bootvine-shutdown.service; do - install -Dm644 packaging/systemd/$unit $out/lib/systemd/system/$unit - substituteInPlace $out/lib/systemd/system/$unit \ - --replace-fail /usr/bin/bootvine $out/bin/bootvine - done - install -Dm755 packaging/dracut/90bootvine/module-setup.sh \ - $out/lib/dracut/modules.d/90bootvine/module-setup.sh - install -Dm644 packaging/mkinitcpio/install/sd-bootvine \ - $out/lib/initcpio/install/sd-bootvine - ''; - meta.mainProgram = "bootvine"; - meta.platforms = lib.platforms.linux; - meta.license = lib.getLicenseFromSpdxId (lib.importTOML ./Cargo.toml).package.license; - } - ); - demo-units = pkgs.callPackage ./demo/user-units.nix { inherit demoUnits; }; + default = builds.bootvine; + inherit (builds) bootvine scripted-boot-examples repo-tasks; + sample-unit-files = pkgs.callPackage ./tests/sample-machine/sample-unit-files.nix { + inherit (sampleMachine) sampleUnits; + }; } ); - nixosModules.default = import ./nixos-module.nix { inherit self; }; + nixosModules = { + default = self.nixosModules.bootvine; + bootvine = import ./nixos-module.nix { inherit self; }; + }; - nixosConfigurations.demo = demoSystem "x86_64-linux"; + nixosConfigurations.sample-machine = sampleMachine.nixosSystemFor "x86_64-linux"; apps = forEachSystem ( - { pkgs, system, ... }: { - vm = { + pkgs, + system, + builds, + ... + }: + { + sample-machine-vm = { type = "app"; - program = lib.getExe (demoSystem system).config.system.build.vm; + meta.description = "Boot a sample machine with bootvine and slow sample units in QEMU"; + program = lib.getExe (sampleMachine.nixosSystemFor system).config.system.build.vm; }; - demo-units = { - type = "app"; - program = lib.getExe self.packages.${system}.demo-units; - }; - docs = { + record-screencasts = { type = "app"; + meta.description = "Re-record the README screencasts and passphrase screenshot into recordings/"; program = lib.getExe ( pkgs.writeShellApplication { - name = "bootvine-docs"; + name = "record-screencasts"; runtimeInputs = [ pkgs.vhs - self.packages.${system}.examples + builds.scripted-boot-examples + builds.repo-tasks ]; - text = '' - if [ ! -d docs ]; then - echo "no docs directory here; run from the repository root" >&2 - exit 1 - fi - vhs ${./docs/boot.tape} - vhs ${./docs/shutdown.tape} - install -m644 -t docs ${self.checks.${system}.luks}/passphrase.png - ''; + text = "exec xtask record-screencasts --passphrase-png ${ + self.checks.${system}.vm-luks-passphrase + }/passphrase.png"; } ); }; @@ -154,22 +97,23 @@ ); checks = forEachSystem ( - { pkgs, system, ... }: + { pkgs, builds, ... }: { - package = self.packages.${system}.default; - boot = pkgs.testers.runNixOSTest (import ./tests/boot.nix { inherit demoHost; }); - luks = pkgs.testers.runNixOSTest ( - import ./tests/luks.nix { bootvineModule = self.nixosModules.default; } + bootvine-build = builds.bootvine; + repo-tasks-build = builds.repo-tasks; + vm-boot-and-shutdown = pkgs.testers.runNixOSTest ( + import ./tests/vm-checks/boot.nix { sampleMachine = sampleMachine.machine; } + ); + vm-luks-passphrase = pkgs.testers.runNixOSTest ( + import ./tests/vm-checks/luks.nix { bootvineModule = self.nixosModules.bootvine; } ); } ); devShells = forEachSystem ( - { pkgs, craneLib, ... }: + { craneLib, builds, ... }: { - default = craneLib.devShell { - packages = with buildInputsFor pkgs; nativeBuildInputs ++ buildInputs; - }; + default = craneLib.devShell { inputsFrom = [ builds.bootvine ]; }; } ); }; diff --git a/docs/boot.tape b/recordings/boot.tape similarity index 91% rename from docs/boot.tape rename to recordings/boot.tape index 579e5d7..a50b3ff 100644 --- a/docs/boot.tape +++ b/recordings/boot.tape @@ -1,4 +1,4 @@ -Output docs/boot.gif +Output recordings/boot.gif Require bootvine-boot-example diff --git a/recordings/passphrase.png b/recordings/passphrase.png new file mode 100644 index 0000000..4824e57 Binary files /dev/null and b/recordings/passphrase.png differ diff --git a/docs/shutdown.tape b/recordings/shutdown.tape similarity index 90% rename from docs/shutdown.tape rename to recordings/shutdown.tape index dec1fe5..3b92cf3 100644 --- a/docs/shutdown.tape +++ b/recordings/shutdown.tape @@ -1,4 +1,4 @@ -Output docs/shutdown.gif +Output recordings/shutdown.gif Require bootvine-shutdown-example diff --git a/packaging/dracut/.shellcheckrc b/system-files/dracut/.shellcheckrc similarity index 100% rename from packaging/dracut/.shellcheckrc rename to system-files/dracut/.shellcheckrc diff --git a/packaging/dracut/90bootvine/module-setup.sh b/system-files/dracut/90bootvine/module-setup.sh similarity index 100% rename from packaging/dracut/90bootvine/module-setup.sh rename to system-files/dracut/90bootvine/module-setup.sh diff --git a/packaging/mkinitcpio/install/sd-bootvine b/system-files/mkinitcpio/install/sd-bootvine similarity index 100% rename from packaging/mkinitcpio/install/sd-bootvine rename to system-files/mkinitcpio/install/sd-bootvine diff --git a/packaging/systemd/bootvine-shutdown.service b/system-files/systemd/bootvine-shutdown.service similarity index 100% rename from packaging/systemd/bootvine-shutdown.service rename to system-files/systemd/bootvine-shutdown.service diff --git a/packaging/systemd/bootvine.service b/system-files/systemd/bootvine.service similarity index 100% rename from packaging/systemd/bootvine.service rename to system-files/systemd/bootvine.service diff --git a/tests/sample-machine/default.nix b/tests/sample-machine/default.nix new file mode 100644 index 0000000..67c8dbb --- /dev/null +++ b/tests/sample-machine/default.nix @@ -0,0 +1,25 @@ +{ lib, bootvineModule }: +let + sampleUnits = import ./sample-units.nix; + + machine = { + imports = [ + bootvineModule + ./machine.nix + ]; + _module.args = { inherit sampleUnits; }; + }; +in +{ + inherit sampleUnits machine; + + nixosSystemFor = + system: + lib.nixosSystem { + inherit system; + modules = [ + machine + ./interactive-vm.nix + ]; + }; +} diff --git a/demo/interactive.nix b/tests/sample-machine/interactive-vm.nix similarity index 68% rename from demo/interactive.nix rename to tests/sample-machine/interactive-vm.nix index f0591f1..e876556 100644 --- a/demo/interactive.nix +++ b/tests/sample-machine/interactive-vm.nix @@ -1,7 +1,8 @@ { boot.loader.grub.enable = false; - services.getty.autologinUser = "root"; fileSystems."/" = { + services.getty.autologinUser = "root"; + fileSystems."/" = { device = "/dev/disk/by-label/nixos"; fsType = "ext4"; }; @@ -10,6 +11,7 @@ memorySize = 2048; cores = 2; graphics = true; + diskImage = "\${XDG_CACHE_HOME:-$HOME/.cache}/bootvine-sample-machine.qcow2"; qemu.options = [ "-vga none" "-device virtio-vga,xres=1920,yres=1080" diff --git a/demo/host.nix b/tests/sample-machine/machine.nix similarity index 91% rename from demo/host.nix rename to tests/sample-machine/machine.nix index 295dbd2..edf7b2b 100644 --- a/demo/host.nix +++ b/tests/sample-machine/machine.nix @@ -1,11 +1,11 @@ { - demoUnits, + sampleUnits, lib, pkgs, ... }: let - demoService = spec: { + sampleService = spec: { inherit (spec) description; after = map (dependency: "${dependency}.service") (spec.after or [ ]); wantedBy = [ "multi-user.target" ]; @@ -37,7 +37,7 @@ in unitConfig.DefaultDependencies = false; serviceConfig = { Type = "oneshot"; - ExecStart = "/bin/sleep ${demoUnits.initrdProbeSeconds}"; + ExecStart = "/bin/sleep ${sampleUnits.initrdProbeSeconds}"; }; }; }; @@ -76,7 +76,7 @@ in font = "${pkgs.terminus_font}/share/consolefonts/ter-v20n.psf.gz"; }; - systemd.services = lib.mapAttrs (_: demoService) demoUnits.services // { + systemd.services = lib.mapAttrs (_: sampleService) sampleUnits.services // { greetd.after = [ "multi-user.target" ]; }; diff --git a/demo/user-units.nix b/tests/sample-machine/sample-unit-files.nix similarity index 65% rename from demo/user-units.nix rename to tests/sample-machine/sample-unit-files.nix index a71b422..9e4a860 100644 --- a/demo/user-units.nix +++ b/tests/sample-machine/sample-unit-files.nix @@ -1,10 +1,8 @@ { - demoUnits, + sampleUnits, lib, coreutils, - linkFarm, - nushell, - writeShellApplication, + runCommand, writeText, }: let @@ -30,30 +28,27 @@ let }; }); - serviceNames = map (name: "${name}.service") (lib.attrNames demoUnits.services); + serviceNames = map (name: "${name}.service") (lib.attrNames sampleUnits.services); - targetFile = writeText "${demoUnits.target}.target" (toUnitFile { + targetFile = writeText "${sampleUnits.target}.target" (toUnitFile { Unit = { Description = "bootvine demo services"; Wants = serviceNames; After = serviceNames; }; }); - - units = linkFarm "bootvine-demo-units" ( + unitFiles = lib.mapAttrsToList (name: spec: { name = "${name}.service"; path = serviceFile name spec; - }) demoUnits.services + }) sampleUnits.services ++ [ { - name = "${demoUnits.target}.target"; + name = "${sampleUnits.target}.target"; path = targetFile; } - ] - ); + ]; in -writeShellApplication { - name = "bootvine-demo-units"; - text = ''exec ${lib.getExe nushell} ${./demo-units.nu} "$@" --units ${units} --target ${demoUnits.target}.target''; -} +runCommand "bootvine-sample-unit-files" { } ( + lib.concatMapStringsSep "\n" (unit: "install -Dm644 ${unit.path} $out/${unit.name}") unitFiles +) diff --git a/demo/units.nix b/tests/sample-machine/sample-units.nix similarity index 100% rename from demo/units.nix rename to tests/sample-machine/sample-units.nix diff --git a/tests/boot.nix b/tests/vm-checks/boot.nix similarity index 89% rename from tests/boot.nix rename to tests/vm-checks/boot.nix index 56635eb..ee19522 100644 --- a/tests/boot.nix +++ b/tests/vm-checks/boot.nix @@ -1,6 +1,6 @@ -{ demoHost }: +{ sampleMachine }: { - name = "bootvine"; + name = "bootvine-boot-and-shutdown"; nodes.machine = { lib, ... }: @@ -8,7 +8,7 @@ waitFor = flag: "/bin/sh -c 'until [ -e /run/${flag} ]; do sleep 0.1; done'"; in { - imports = [ demoHost ]; + imports = [ sampleMachine ]; testing.initrdBackdoor = true; boot.initrd.systemd.services.demo-initrd-probe.serviceConfig.ExecStart = lib.mkForce ( waitFor "release-initrd-probe" diff --git a/tests/boot.py b/tests/vm-checks/boot.py similarity index 100% rename from tests/boot.py rename to tests/vm-checks/boot.py diff --git a/tests/console.py b/tests/vm-checks/console.py similarity index 100% rename from tests/console.py rename to tests/vm-checks/console.py diff --git a/tests/luks.nix b/tests/vm-checks/luks.nix similarity index 97% rename from tests/luks.nix rename to tests/vm-checks/luks.nix index 2df1291..1b29e82 100644 --- a/tests/luks.nix +++ b/tests/vm-checks/luks.nix @@ -3,7 +3,7 @@ let passphrase = "quietly unlocked"; in { - name = "bootvine-luks"; + name = "bootvine-luks-passphrase"; nodes.machine = { lib, pkgs, ... }: let diff --git a/tests/luks.py b/tests/vm-checks/luks.py similarity index 100% rename from tests/luks.py rename to tests/vm-checks/luks.py diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 0000000..74ee7cc --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,15 @@ +[package] +edition = "2024" +name = "xtask" +version = "0.1.0" +license = "GPL-3.0-or-later" +publish = false + +[dependencies] +clap = { version = "4.6.7", features = ["derive"] } +rustix = { version = "1.1.5", features = ["process"] } +thiserror = "2.0.12" +xshell = "0.2.7" + +[lints] +workspace = true diff --git a/xtask/src/install_files.rs b/xtask/src/install_files.rs new file mode 100644 index 0000000..9774025 --- /dev/null +++ b/xtask/src/install_files.rs @@ -0,0 +1,280 @@ +use std::{ + env, + ffi::OsString, + fs::{self, Permissions}, + io::ErrorKind, + iter::once, + os::unix::fs::PermissionsExt, + path::{Path, PathBuf}, +}; + +use clap::{Args, ValueEnum}; +use rustix::process::geteuid; +use xshell::{Shell, cmd}; + +use crate::task_error::TaskError; + +const UNIT_NAMES: [&str; 2] = ["bootvine.service", "bootvine-shutdown.service"]; +const DRACUT_MODULE_DIR: &str = "/usr/lib/dracut/modules.d/90bootvine"; +const DRACUT_CONF: &str = "/etc/dracut.conf.d/bootvine.conf"; +const MKINITCPIO_HOOK: &str = "/usr/lib/initcpio/install/sd-bootvine"; +const PACKAGED_BINARY_PATH: &str = "/usr/bin/bootvine"; + +#[derive(Args)] +pub struct InstallTarget { + #[arg( + long, + default_value = "/usr", + help = "Prefix for bin/bootvine and lib/systemd/system" + )] + prefix: PathBuf, + #[arg( + long, + help = "Stage the files under this directory instead of /; skips sudo and systemctl" + )] + destdir: Option, +} + +#[derive(Args)] +pub struct InstallOptions { + #[command(flatten)] + target: InstallTarget, + #[arg(long, value_enum, default_value_t = InitrdGenerator::None)] + initrd: InitrdGenerator, + #[arg(long, help = "Leave the units disabled")] + no_enable: bool, + #[arg(long, hide = true)] + built_binary: Option, +} + +#[derive(Args)] +pub struct UninstallOptions { + #[command(flatten)] + target: InstallTarget, +} + +#[derive(Clone, Copy, PartialEq, Eq, ValueEnum)] +pub enum InitrdGenerator { + None, + Dracut, + Mkinitcpio, +} + +struct InstalledFile { + destination: PathBuf, + contents: Vec, + mode: u32, +} + +impl InstallTarget { + fn staged(&self, path: &Path) -> PathBuf { + match &self.destdir { + Some(root) => root.join(path.strip_prefix("/").unwrap_or(path)), + None => path.to_owned(), + } + } + + fn changes_live_system(&self) -> bool { + self.destdir.is_none() + } + + fn needs_sudo(&self) -> bool { + self.changes_live_system() && !geteuid().is_root() + } + + fn binary_path(&self) -> PathBuf { + self.prefix.join("bin/bootvine") + } + + fn unit_path(&self, unit: &str) -> PathBuf { + self.prefix.join("lib/systemd/system").join(unit) + } + + fn every_installed_path(&self) -> Vec { + once(self.binary_path()) + .chain(UNIT_NAMES.iter().map(|unit| self.unit_path(unit))) + .chain([ + Path::new(DRACUT_MODULE_DIR).join("module-setup.sh"), + PathBuf::from(DRACUT_CONF), + PathBuf::from(MKINITCPIO_HOOK), + ]) + .collect() + } +} + +fn workspace_root() -> &'static Path { + let xtask_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + xtask_dir.parent().unwrap_or(xtask_dir) +} + +fn release_binary() -> PathBuf { + env::var_os("CARGO_TARGET_DIR") + .map_or_else(|| workspace_root().join("target"), PathBuf::from) + .join("release/bootvine") +} + +fn read_file(path: &Path) -> Result, TaskError> { + fs::read(path).map_err(|source| TaskError::File { + path: path.to_owned(), + source, + }) +} + +fn read_system_file(relative: &str) -> Result, TaskError> { + read_file(&workspace_root().join("system-files").join(relative)) +} + +fn unit_files(target: &InstallTarget) -> Result, TaskError> { + UNIT_NAMES + .iter() + .map(|unit| { + let packaged = read_system_file(&format!("systemd/{unit}"))?; + let rewritten = String::from_utf8_lossy(&packaged).replace( + PACKAGED_BINARY_PATH, + &target.binary_path().to_string_lossy(), + ); + Ok(InstalledFile { + destination: target.unit_path(unit), + contents: rewritten.into_bytes(), + mode: 0o644, + }) + }) + .collect() +} + +fn initrd_hook_files(initrd: InitrdGenerator) -> Result, TaskError> { + Ok(match initrd { + InitrdGenerator::None => Vec::new(), + InitrdGenerator::Dracut => vec![ + InstalledFile { + destination: Path::new(DRACUT_MODULE_DIR).join("module-setup.sh"), + contents: read_system_file("dracut/90bootvine/module-setup.sh")?, + mode: 0o755, + }, + InstalledFile { + destination: PathBuf::from(DRACUT_CONF), + contents: b"add_dracutmodules+=\" bootvine \"\n".to_vec(), + mode: 0o644, + }, + ], + InitrdGenerator::Mkinitcpio => vec![InstalledFile { + destination: PathBuf::from(MKINITCPIO_HOOK), + contents: read_system_file("mkinitcpio/install/sd-bootvine")?, + mode: 0o644, + }], + }) +} + +fn write_installed_file(target: &InstallTarget, file: &InstalledFile) -> Result<(), TaskError> { + let path = target.staged(&file.destination); + path.parent() + .map_or(Ok(()), fs::create_dir_all) + .and_then(|()| fs::write(&path, &file.contents)) + .and_then(|()| fs::set_permissions(&path, Permissions::from_mode(file.mode))) + .map_err(|source| TaskError::File { path, source }) +} + +fn remove_if_present(path: PathBuf) -> Result<(), TaskError> { + match fs::remove_file(&path) { + Err(error) if error.kind() != ErrorKind::NotFound => Err(TaskError::File { + path, + source: error, + }), + _ => Ok(()), + } +} + +fn rerun_with_sudo(shell: &Shell, extra_arguments: &[OsString]) -> Result<(), TaskError> { + let this_task = env::current_exe().map_err(|source| TaskError::File { + path: PathBuf::from("/proc/self/exe"), + source, + })?; + let task_arguments = env::args_os().skip(1); + cmd!( + shell, + "sudo {this_task} {task_arguments...} {extra_arguments...}" + ) + .run()?; + Ok(()) +} + +fn print_initramfs_hint(initrd: InitrdGenerator) { + match initrd { + InitrdGenerator::None => {} + InitrdGenerator::Dracut => println!("rebuild the initramfs: sudo dracut --force"), + InitrdGenerator::Mkinitcpio => println!( + "add sd-bootvine after systemd in HOOKS in /etc/mkinitcpio.conf, then run: sudo \ + mkinitcpio -P" + ), + } +} + +fn build_release_binary(shell: &Shell) -> Result { + let cargo = env::var_os("CARGO").unwrap_or_else(|| "cargo".into()); + let _in_workspace = shell.push_dir(workspace_root()); + cmd!(shell, "{cargo} build --release --package bootvine").run()?; + Ok(release_binary()) +} + +pub fn install_files(options: &InstallOptions) -> Result<(), TaskError> { + let shell = Shell::new()?; + let target = &options.target; + let binary = match &options.built_binary { + Some(built) => built.clone(), + None => build_release_binary(&shell)?, + }; + if target.needs_sudo() { + return rerun_with_sudo(&shell, &["--built-binary".into(), binary.into_os_string()]); + } + let binary_file = InstalledFile { + destination: target.binary_path(), + contents: read_file(&binary)?, + mode: 0o755, + }; + once(binary_file) + .chain(unit_files(target)?) + .chain(initrd_hook_files(options.initrd)?) + .try_for_each(|file| write_installed_file(target, &file))?; + if target.changes_live_system() { + cmd!(shell, "systemctl daemon-reload").run()?; + if !options.no_enable { + cmd!(shell, "systemctl enable {UNIT_NAMES...}").run()?; + } + print_initramfs_hint(options.initrd); + } + Ok(()) +} + +pub fn uninstall_files(options: &UninstallOptions) -> Result<(), TaskError> { + let shell = Shell::new()?; + let target = &options.target; + if target.needs_sudo() { + return rerun_with_sudo(&shell, &[]); + } + if target.changes_live_system() { + let enabled_units: Vec<&str> = UNIT_NAMES + .into_iter() + .filter(|unit| target.unit_path(unit).exists()) + .collect(); + if !enabled_units.is_empty() { + cmd!(shell, "systemctl disable {enabled_units...}").run()?; + } + } + target + .every_installed_path() + .iter() + .try_for_each(|path| remove_if_present(target.staged(path)))?; + match fs::remove_dir(target.staged(Path::new(DRACUT_MODULE_DIR))) { + Err(error) if error.kind() == ErrorKind::DirectoryNotEmpty => { + return Err(TaskError::File { + path: target.staged(Path::new(DRACUT_MODULE_DIR)), + source: error, + }); + } + _ => {} + } + if target.changes_live_system() { + cmd!(shell, "systemctl daemon-reload").run()?; + } + Ok(()) +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 0000000..730b6ad --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,49 @@ +mod install_files; +mod record_screencasts; +mod task_error; + +use std::{path::PathBuf, process::ExitCode}; + +use clap::{Parser, Subcommand}; + +use crate::install_files::{InstallOptions, UninstallOptions, install_files, uninstall_files}; + +#[derive(Parser)] +#[command(about = "Repository tasks for bootvine")] +struct RepoTasks { + #[command(subcommand)] + task: Task, +} + +#[derive(Subcommand)] +enum Task { + #[command( + about = "Record the README screencasts with vhs and copy the passphrase screenshot into \ + recordings/" + )] + RecordScreencasts { + #[arg(long, help = "passphrase.png produced by the vm-luks-passphrase check")] + passphrase_png: PathBuf, + }, + #[command(about = "Build bootvine and install the binary, units and initrd hook")] + Install(InstallOptions), + #[command(about = "Remove what install put in place")] + Uninstall(UninstallOptions), +} + +fn main() -> ExitCode { + let outcome = match RepoTasks::parse().task { + Task::RecordScreencasts { passphrase_png } => { + record_screencasts::record_screencasts(&passphrase_png) + } + Task::Install(options) => install_files(&options), + Task::Uninstall(options) => uninstall_files(&options), + }; + match outcome { + Ok(()) => ExitCode::SUCCESS, + Err(error) => { + eprintln!("xtask: {error}"); + ExitCode::FAILURE + } + } +} diff --git a/xtask/src/record_screencasts.rs b/xtask/src/record_screencasts.rs new file mode 100644 index 0000000..3c77800 --- /dev/null +++ b/xtask/src/record_screencasts.rs @@ -0,0 +1,30 @@ +use std::{ + fs::{self, Permissions}, + os::unix::fs::PermissionsExt, + path::Path, +}; + +use xshell::{Shell, cmd}; + +use crate::task_error::TaskError; + +const TAPES: [&str; 2] = ["boot.tape", "shutdown.tape"]; + +pub fn record_screencasts(passphrase_png: &Path) -> Result<(), TaskError> { + let recordings = Path::new("recordings"); + if !recordings.is_dir() { + return Err(TaskError::NotInRepositoryRoot); + } + let shell = Shell::new()?; + TAPES.iter().try_for_each(|tape| { + let tape_path = recordings.join(tape); + cmd!(shell, "vhs {tape_path}").run() + })?; + let copied_png = recordings.join("passphrase.png"); + fs::copy(passphrase_png, &copied_png) + .and_then(|_| fs::set_permissions(&copied_png, Permissions::from_mode(0o644))) + .map_err(|source| TaskError::File { + path: copied_png, + source, + }) +} diff --git a/xtask/src/task_error.rs b/xtask/src/task_error.rs new file mode 100644 index 0000000..72b3379 --- /dev/null +++ b/xtask/src/task_error.rs @@ -0,0 +1,13 @@ +use std::{io, path::PathBuf}; + +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum TaskError { + #[error(transparent)] + Shell(#[from] xshell::Error), + #[error("{}: {source}", path.display())] + File { path: PathBuf, source: io::Error }, + #[error("no recordings directory here; run from the repository root")] + NotInRepositoryRoot, +}