feat: support luks
This commit is contained in:
1 parent
5433f23513
commit
10eb2f7b98
4 files changed
+110
-6
No files matched your search
@@ -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"
|
||||
'';
|
||||
}
|
||||
@@ -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; }
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
+53
-4
@@ -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<QuietKernelConsole>,
|
||||
foreground: Option<ForegroundVt>,
|
||||
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<Self> {
|
||||
fn take() -> Option<Self> {
|
||||
let name = ttyname(stdin(), Vec::new()).ok()?;
|
||||
let own: u16 = name
|
||||
.to_str()
|
||||
|
||||
+2
-2
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user