From 10eb2f7b9875e236bd6703f9ad1bc9ba59f543eb Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Sun, 11 Oct 2026 20:05:52 +0200 Subject: [PATCH] feat: support luks --- demo/luks-test.nix | 52 ++++++++++++++++++++++++++++++++++++++++++ flake.nix | 3 +++ src/console.rs | 57 ++++++++++++++++++++++++++++++++++++++++++---- src/run.rs | 4 ++-- 4 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 demo/luks-test.nix diff --git a/demo/luks-test.nix b/demo/luks-test.nix new file mode 100644 index 0000000..b86e86c --- /dev/null +++ b/demo/luks-test.nix @@ -0,0 +1,52 @@ +{ bootvineModule }: +{ + name = "bootvine-luks"; + enableOCR = true; + + nodes.machine = + { lib, pkgs, ... }: + { + imports = [ bootvineModule ]; + + services.bootvine.enable = true; + + boot.loader.systemd-boot.enable = true; + boot.initrd.systemd.enable = true; + environment.systemPackages = [ pkgs.cryptsetup ]; + + virtualisation = { + emptyDiskImages = [ 64 ]; + useBootLoader = true; + useEFIBoot = true; + mountHostNixStore = true; + }; + + specialisation.unlock.configuration = { + boot.initrd.luks.devices = lib.mkVMOverride { + secret.device = "/dev/vdb"; + }; + }; + }; + + testScript = '' + passphrase = "quietly unlocked" + + machine.wait_for_unit("multi-user.target") + machine.succeed(f"echo -n '{passphrase}' | cryptsetup luksFormat -q --iter-time=1 /dev/vdb -") + machine.succeed("bootctl set-default nixos-generation-1-specialisation-unlock.conf") + machine.succeed("sync") + machine.crash() + + machine.start() + machine.wait_for_text("passphrase") + machine.screenshot("passphrase") + machine.send_chars(f"{passphrase}\n") + machine.wait_for_unit("multi-user.target") + + machine.succeed("test -e /dev/mapper/secret") + journal = machine.succeed("journalctl -b -u bootvine -o cat") + print(journal) + assert "in initrd" in journal, "never ran in the initrd" + assert "taking the console back" in journal, "never took the console back after the prompt" + ''; +} diff --git a/flake.nix b/flake.nix index 4438bc7..96b9b94 100644 --- a/flake.nix +++ b/flake.nix @@ -116,6 +116,9 @@ { package = self.packages.${system}.default; boot = pkgs.testers.runNixOSTest (import ./demo/test.nix { inherit demoHost; }); + luks = pkgs.testers.runNixOSTest ( + import ./demo/luks-test.nix { bootvineModule = self.nixosModules.default; } + ); } ); diff --git a/src/console.rs b/src/console.rs index 70f545a..851d1ff 100644 --- a/src/console.rs +++ b/src/console.rs @@ -15,20 +15,58 @@ const VT_WAITACTIVE: Opcode = 0x5607; const VIRTUAL_TERMINAL_PREFIX: &str = "/dev/tty"; const KERNEL_PRINTK: &str = "/proc/sys/kernel/printk"; const ALERTS_ONLY: &str = "1"; +const PASSWORD_REQUESTS: &str = "/run/systemd/ask-password"; +const PASSWORD_REQUEST_PREFIX: &str = "ask."; pub struct Console { quiet_kernel: Option, + foreground: Option, + yields_to_password_prompts: bool, pub glyphs: Glyphs, } impl Console { - pub fn take(quiets_kernel: bool) -> Self { + pub fn take(for_system: bool) -> Self { + let prompt_showing = for_system && password_requested(); Self { - quiet_kernel: quiets_kernel.then(QuietKernelConsole::take).flatten(), + quiet_kernel: for_system.then(QuietKernelConsole::take).flatten(), + foreground: if prompt_showing { + None + } else { + ForegroundVt::take() + }, + yields_to_password_prompts: for_system, glyphs: font_glyphs(), } } + pub fn make_way_for_password_prompts(&mut self) { + if !self.yields_to_password_prompts { + return; + } + if password_requested() { + self.step_aside(); + } else { + self.come_forward(); + } + } + + fn step_aside(&mut self) { + if self.foreground.take().is_some() { + eprintln!("bootvine: stepping aside for a password prompt"); + } + } + + fn come_forward(&mut self) { + if self.foreground.is_some() { + return; + } + self.foreground = ForegroundVt::take(); + if self.foreground.is_some() { + eprintln!("bootvine: password prompt answered, taking the console back"); + } + } + pub fn keep_up(&mut self) { if let Some(quiet_kernel) = &mut self.quiet_kernel { quiet_kernel.keep_quiet(); @@ -63,6 +101,17 @@ impl QuietKernelConsole { } } +fn password_requested() -> bool { + fs::read_dir(PASSWORD_REQUESTS).is_ok_and(|entries| { + entries.flatten().any(|entry| { + entry + .file_name() + .to_string_lossy() + .starts_with(PASSWORD_REQUEST_PREFIX) + }) + }) +} + fn font_glyphs() -> Glyphs { font_characters().map_or_else(Glyphs::default, |characters| { Glyphs::fitting(|symbol| symbol.is_ascii() || characters.contains(&symbol)) @@ -123,12 +172,12 @@ struct VtState { _state: u16, } -pub struct ForegroundVt { +struct ForegroundVt { previous: u16, } impl ForegroundVt { - pub fn take() -> Option { + fn take() -> Option { let name = ttyname(stdin(), Vec::new()).ok()?; let own: u16 = name .to_str() diff --git a/src/run.rs b/src/run.rs index 45ad404..60b02a7 100644 --- a/src/run.rs +++ b/src/run.rs @@ -24,7 +24,7 @@ use signal_hook::{consts::SIGTERM, flag}; use crate::{ clock::monotonic_now, - console::{Console, ForegroundVt}, + console::Console, error::Error, feed::Feed, sysroot::INITRD_RELEASE, @@ -63,7 +63,6 @@ pub fn run(options: &Options) -> Result<(), Error> { let mut state = BootState::new(phase); let mut feed = Feed::new(options.scope, options.target.clone()); let mut console = Console::take(options.scope == Scope::System); - let _foreground = ForegroundVt::take(); let mut terminal = ratatui::try_init()?; let watched = watch( &mut state, @@ -119,6 +118,7 @@ fn watch( let mut last_full_repaint = Instant::now(); loop { feed.pump(state, FRAME_INTERVAL)?; + console.make_way_for_password_prompts(); let repaint_due = feed.connections != painted_connections || last_full_repaint.elapsed() >= FULL_REPAINT_INTERVAL; if repaint_due {