From 795819d5c8effccce01c6a7a06f5b08f09e1cc10 Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Mon, 2 Nov 2026 17:28:04 +0100 Subject: [PATCH] feat: show the kernel's image loading progress while resuming from hibernation The kernel freezes every process before it loads a hibernated image, so the screen looked hung for as long as the load took. A drop-in on systemd-hibernate-resume.service now opens the kernel console just before resume, draws a band across the middle of tty12 and confines the kernel's progress lines to it. The running bootvine stops drawing and stops taking the console back meanwhile, and repaints if no image was restored. --- bootvine/src/console_session.rs | 1 + bootvine/src/console_session/live_desk.rs | 56 +++++- .../src/console_session/resume_console.rs | 165 ++++++++++++++++++ bootvine/src/console_session/watch_screen.rs | 12 +- bootvine/src/main.rs | 14 +- bootvine/src/system_install/install_files.rs | 27 ++- .../dracut/90bootvine/module-setup.sh | 1 + .../mkinitcpio/install/sd-bootvine | 1 + .../bootvine.conf | 3 + docs/architecture.md | 21 +++ docs/guide.md | 30 ++++ nix/crane-builds.nix | 4 + vm-tests/default.nix | 1 + vm-tests/hibernate-resume/check.nix | 38 ++++ vm-tests/hibernate-resume/check.py | 56 ++++++ vm-tests/hibernate-resume/machine.nix | 13 ++ vm-tests/shared/screen_recording.py | 3 +- 17 files changed, 433 insertions(+), 13 deletions(-) create mode 100644 bootvine/src/console_session/resume_console.rs create mode 100644 bootvine/system-files/systemd/systemd-hibernate-resume.service.d/bootvine.conf create mode 100644 vm-tests/hibernate-resume/check.nix create mode 100644 vm-tests/hibernate-resume/check.py create mode 100644 vm-tests/hibernate-resume/machine.nix diff --git a/bootvine/src/console_session.rs b/bootvine/src/console_session.rs index 767e834..20e9f9e 100644 --- a/bootvine/src/console_session.rs +++ b/bootvine/src/console_session.rs @@ -3,6 +3,7 @@ pub mod console_palette; pub mod console_takeover; pub mod live_desk; pub mod passphrase_input; +pub mod resume_console; pub mod stream_sources; pub mod watch_loop; pub mod watch_screen; diff --git a/bootvine/src/console_session/live_desk.rs b/bootvine/src/console_session/live_desk.rs index 4efa494..3de3bce 100644 --- a/bootvine/src/console_session/live_desk.rs +++ b/bootvine/src/console_session/live_desk.rs @@ -1,4 +1,4 @@ -use std::{future::ready, io, ops::ControlFlow, time::Duration}; +use std::{future::ready, io, mem, ops::ControlFlow, path::PathBuf, time::Duration}; use crossterm::event::Event; use futures::{Stream, StreamExt}; @@ -11,6 +11,7 @@ use crate::{ console_palette::ConsoleLook, console_takeover::{ForegroundVt, QuietKernelConsole, console_look, vt_of_stdin}, passphrase_input::asks_to_quit, + resume_console::{RESUME_CONSOLE_MARKER, resume_console_is_open}, stream_sources::items_when_readable, watch_screen::{ConsoleDesk, Refresh, ScreenInput, WatchEnding}, }, @@ -24,6 +25,8 @@ pub struct LiveDesk { look: ConsoleLook, entry: Option, handover_deadline: Option, + resume_marker: PathBuf, + console_was_lent: bool, } impl LiveDesk { @@ -44,6 +47,8 @@ impl LiveDesk { look: console_look(own_vt), entry: None, handover_deadline: None, + resume_marker: PathBuf::from(RESUME_CONSOLE_MARKER), + console_was_lent: false, } } @@ -134,7 +139,17 @@ impl ConsoleDesk for LiveDesk { self.follow_requests(false); } + fn lends_console(&mut self) -> bool { + let lent = resume_console_is_open(&self.resume_marker); + self.console_was_lent |= lent; + lent + } + fn keep_console_up(&mut self) -> Refresh { + if self.lends_console() { + return Refresh::Steady; + } + let console_returned = mem::take(&mut self.console_was_lent); let kernel_got_loud = self .quiet_kernel .as_mut() @@ -144,7 +159,7 @@ impl ConsoleDesk for LiveDesk { let look = console_look(self.own_vt); let restyled = look != self.look; self.look = look; - if kernel_got_loud || terminal_was_taken || restyled { + if console_returned || kernel_got_loud || terminal_was_taken || restyled { Refresh::Repaint } else { Refresh::Steady @@ -157,3 +172,40 @@ pub fn password_request_inputs(watch: RequestWatch) -> io::Result LiveDesk { + LiveDesk { + quiet_kernel: None, + foreground: None, + for_system: true, + own_vt: None, + look: console_look(None), + entry: None, + handover_deadline: None, + resume_marker, + console_was_lent: false, + } + } + + #[test] + fn the_console_is_repainted_only_after_it_was_lent_for_resume() { + let marker = env::temp_dir().join(format!("bootvine-resume-marker-{}", process::id())); + let mut desk = desk_watching(marker.clone()); + assert!(!desk.lends_console()); + + fs::write(&marker, "1").expect("create the marker"); + assert!(desk.lends_console()); + assert_eq!(desk.keep_console_up(), Refresh::Steady); + + fs::remove_file(&marker).expect("remove the marker"); + assert!(!desk.lends_console()); + assert_eq!(desk.keep_console_up(), Refresh::Repaint); + assert_eq!(desk.keep_console_up(), Refresh::Steady); + } +} diff --git a/bootvine/src/console_session/resume_console.rs b/bootvine/src/console_session/resume_console.rs new file mode 100644 index 0000000..b0bb5ff --- /dev/null +++ b/bootvine/src/console_session/resume_console.rs @@ -0,0 +1,165 @@ +use std::{ + fmt, + fs::{self, File, OpenOptions}, + io::{self, Write}, + path::Path, + thread, +}; + +use crossterm::{ + Command, + cursor::MoveTo, + queue, + style::{Attribute, Color as ConsoleColor, SetAttribute, SetForegroundColor}, +}; +use ratatui::{ + backend::CrosstermBackend, + buffer::Buffer, + layout::{Constraint, Flex, Layout, Rect}, + style::{Color, Style}, + widgets::{Block, Borders, Widget}, +}; +use rustix::termios::tcgetwinsize; + +use crate::console_session::{stream_sources::FRAME_INTERVAL, watch_screen::overwrite_every_cell}; + +pub const RESUME_CONSOLE_MARKER: &str = "/run/bootvine/resume-console-open"; +const BOOTVINE_TERMINAL: &str = "/dev/tty12"; +const KERNEL_PRINTK: &str = "/proc/sys/kernel/printk"; +const KERNEL_TIMESTAMPS: &str = "/sys/module/printk/parameters/time"; +const KERNEL_INFO_VISIBLE: &str = "7"; +const TIMESTAMPS_HIDDEN: &str = "N"; +const FRAMES_TO_SETTLE: u32 = 2; +const BAND_HEIGHT: u16 = 4; +const BAND_TITLE: &str = " Looking for a hibernated session "; + +struct ConfineScrolling(Rect); + +impl Command for ConfineScrolling { + fn write_ansi(&self, sequence: &mut impl fmt::Write) -> fmt::Result { + write!(sequence, "\x1b[{};{}r", self.0.top() + 1, self.0.bottom()) + } +} + +struct ReleaseScrolling; + +impl Command for ReleaseScrolling { + fn write_ansi(&self, sequence: &mut impl fmt::Write) -> fmt::Result { + sequence.write_str("\x1b[r") + } +} + +#[must_use] +pub fn resume_console_is_open(marker: &Path) -> bool { + marker.exists() +} + +fn band_area(screen: Rect) -> Rect { + let [band] = Layout::vertical([Constraint::Length(BAND_HEIGHT)]) + .flex(Flex::Center) + .areas(screen); + band +} + +fn band_frame() -> Block<'static> { + Block::new() + .borders(Borders::TOP | Borders::BOTTOM) + .border_style(Style::new().fg(Color::Yellow)) + .title(BAND_TITLE) +} + +fn open_bootvine_terminal() -> io::Result { + OpenOptions::new().write(true).open(BOOTVINE_TERMINAL) +} + +fn draw_progress_band() -> io::Result<()> { + let terminal = open_bootvine_terminal()?; + let window = tcgetwinsize(&terminal)?; + let band = band_area(Rect::new(0, 0, window.ws_col, window.ws_row)); + let progress_rows = band_frame().inner(band); + if progress_rows.is_empty() { + return Ok(()); + } + let mut painted = Buffer::empty(band); + band_frame().render(band, &mut painted); + let mut backend = CrosstermBackend::new(terminal); + overwrite_every_cell(&mut backend, &painted)?; + queue!( + backend, + SetAttribute(Attribute::Bold), + SetForegroundColor(ConsoleColor::DarkYellow), + ConfineScrolling(progress_rows), + MoveTo(progress_rows.x, progress_rows.y), + )?; + Write::flush(&mut backend) +} + +fn restore_bootvine_terminal() -> io::Result<()> { + let mut terminal = open_bootvine_terminal()?; + queue!(terminal, SetAttribute(Attribute::Reset), ReleaseScrolling)?; + terminal.flush() +} + +pub fn open_resume_console() -> io::Result<()> { + let printk_levels = fs::read_to_string(KERNEL_PRINTK)?; + let previous_level = printk_levels.split_whitespace().next().unwrap_or_default(); + let previous_timestamps = fs::read_to_string(KERNEL_TIMESTAMPS).unwrap_or_default(); + let marker = Path::new(RESUME_CONSOLE_MARKER); + if let Some(directory) = marker.parent() { + fs::create_dir_all(directory)?; + } + fs::write( + marker, + format!("{previous_level}\n{}\n", previous_timestamps.trim()), + )?; + thread::sleep(FRAME_INTERVAL * FRAMES_TO_SETTLE); + let _ = fs::write(KERNEL_TIMESTAMPS, TIMESTAMPS_HIDDEN); + fs::write(KERNEL_PRINTK, KERNEL_INFO_VISIBLE)?; + draw_progress_band() +} + +pub fn close_resume_console() -> io::Result<()> { + let marker = Path::new(RESUME_CONSOLE_MARKER); + let recorded = fs::read_to_string(marker)?; + let mut recorded_lines = recorded.lines(); + let previous_level = recorded_lines.next().unwrap_or_default(); + let previous_timestamps = recorded_lines.next().unwrap_or_default(); + let terminal_restored = restore_bootvine_terminal(); + if !previous_timestamps.is_empty() { + let _ = fs::write(KERNEL_TIMESTAMPS, previous_timestamps); + } + let level_restored = fs::write(KERNEL_PRINTK, previous_level); + fs::remove_file(marker)?; + terminal_restored.and(level_restored) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn screen_with_rows(rows: u16) -> Rect { + Rect::new(0, 0, 80, rows) + } + + #[test] + fn the_band_sits_in_the_middle_with_two_rows_for_the_kernel() { + let band = band_area(screen_with_rows(50)); + assert_eq!(band, Rect::new(0, 23, 80, BAND_HEIGHT)); + assert_eq!(band_frame().inner(band), Rect::new(0, 24, 80, 2)); + } + + #[test] + fn the_scroll_region_counts_rows_from_one() { + let mut sequence = String::new(); + ConfineScrolling(Rect::new(0, 24, 80, 2)) + .write_ansi(&mut sequence) + .expect("writing to a string"); + assert_eq!(sequence, "\x1b[25;26r"); + } + + #[test] + fn a_screen_without_room_for_the_band_leaves_the_kernel_no_rows() { + let band = band_area(screen_with_rows(2)); + assert!(band_frame().inner(band).is_empty()); + } +} diff --git a/bootvine/src/console_session/watch_screen.rs b/bootvine/src/console_session/watch_screen.rs index cb1c59e..4205cf9 100644 --- a/bootvine/src/console_session/watch_screen.rs +++ b/bootvine/src/console_session/watch_screen.rs @@ -64,6 +64,10 @@ pub trait ConsoleDesk { } fn begin_handover(&mut self, _reached: &str, _deadline: Duration) {} + + fn lends_console(&mut self) -> bool { + false + } } struct CachedProjection { @@ -183,6 +187,9 @@ impl WatchScreen { Error: From, { batch.into_iter().try_for_each(|input| self.absorb(input))?; + if self.desk.lends_console() { + return ControlFlow::Continue(()); + } match self.draw(terminal) { Ok(()) => ControlFlow::Continue(()), Err(error) => ControlFlow::Break(WatchEnding::Failed(error)), @@ -253,7 +260,10 @@ where .await } -fn overwrite_every_cell(backend: &mut B, frame: &Buffer) -> Result<(), B::Error> { +pub(super) fn overwrite_every_cell( + backend: &mut B, + frame: &Buffer, +) -> Result<(), B::Error> { backend.draw( frame .content diff --git a/bootvine/src/main.rs b/bootvine/src/main.rs index 0ba8af1..281bb5f 100644 --- a/bootvine/src/main.rs +++ b/bootvine/src/main.rs @@ -3,7 +3,10 @@ use std::{ }; use bootvine::{ - console_session::watch_loop::{Settings, run}, + console_session::{ + resume_console::{close_resume_console, open_resume_console}, + watch_loop::{Settings, run}, + }, screen_settings::config_file::{ Config, ConfigError, Timing, default_config_table, read_config_file, }, @@ -69,6 +72,13 @@ enum Command { Install(InstallOptions), #[command(about = "Disable and remove what install put in place")] Uninstall(UninstallOptions), + #[command( + about = "Let the kernel's image loading progress reach the console; started before \ + systemd-hibernate-resume.service" + )] + OpenResumeConsole, + #[command(about = "Undo open-resume-console when no hibernated image was restored")] + CloseResumeConsole, } #[derive(Args)] @@ -102,6 +112,8 @@ fn main() -> ExitCode { ), Some(Command::Install(options)) => report_outcome(install_files(&options)), Some(Command::Uninstall(options)) => report_outcome(uninstall_files(&options)), + Some(Command::OpenResumeConsole) => report_outcome(open_resume_console()), + Some(Command::CloseResumeConsole) => report_outcome(close_resume_console()), None => watch_boot(cli.watch), } } diff --git a/bootvine/src/system_install/install_files.rs b/bootvine/src/system_install/install_files.rs index e5573a9..189ba1d 100644 --- a/bootvine/src/system_install/install_files.rs +++ b/bootvine/src/system_install/install_files.rs @@ -34,6 +34,13 @@ const PACKAGED_UNITS: [PackagedUnit; 2] = [ contents: include_str!("../../system-files/systemd/bootvine-shutdown.service"), }, ]; +const RESUME_DROP_IN_DIR: &str = "systemd-hibernate-resume.service.d"; +const RESUME_DROP_IN: PackagedUnit = PackagedUnit { + name: "systemd-hibernate-resume.service.d/bootvine.conf", + contents: include_str!( + "../../system-files/systemd/systemd-hibernate-resume.service.d/bootvine.conf" + ), +}; const DRACUT_MODULE_SETUP: &str = include_str!("../../system-files/dracut/90bootvine/module-setup.sh"); const MKINITCPIO_INSTALL_HOOK: &str = @@ -177,14 +184,17 @@ fn binary_file(target: &InstallTarget) -> Result, InstallE fn unit_files(target: &InstallTarget) -> impl Iterator { let binary_path = target.binary_path(); - PACKAGED_UNITS.iter().map(move |unit| InstalledFile { - destination: target.unit_path(unit.name), - contents: unit - .contents - .replace(PACKAGED_BINARY_PATH, &binary_path.to_string_lossy()) - .into_bytes(), - mode: 0o644, - }) + PACKAGED_UNITS + .iter() + .chain(once(&RESUME_DROP_IN)) + .map(move |unit| InstalledFile { + destination: target.unit_path(unit.name), + contents: unit + .contents + .replace(PACKAGED_BINARY_PATH, &binary_path.to_string_lossy()) + .into_bytes(), + mode: 0o644, + }) } fn initrd_hook_files(initrd: InitrdGenerator) -> Vec { @@ -318,6 +328,7 @@ pub fn uninstall_files(options: &UninstallOptions) -> Result<(), InstallError> { .every_installed_path() .into_iter() .try_for_each(|path| remove_if_present(target.staged(&path)))?; + let _ = fs::remove_dir(target.staged(&target.unit_path(RESUME_DROP_IN_DIR))); let dracut_module_dir = target.staged(Path::new(DRACUT_MODULE_DIR)); match fs::remove_dir(&dracut_module_dir) { Err(error) if error.kind() == ErrorKind::DirectoryNotEmpty => { diff --git a/bootvine/system-files/dracut/90bootvine/module-setup.sh b/bootvine/system-files/dracut/90bootvine/module-setup.sh index fa91a27..83a427b 100755 --- a/bootvine/system-files/dracut/90bootvine/module-setup.sh +++ b/bootvine/system-files/dracut/90bootvine/module-setup.sh @@ -12,6 +12,7 @@ depends() { install() { inst_binary bootvine inst_simple "$systemdsystemunitdir/bootvine.service" + inst_simple "$systemdsystemunitdir/systemd-hibernate-resume.service.d/bootvine.conf" if [[ -f /etc/bootvine.toml ]]; then inst_simple /etc/bootvine.toml fi diff --git a/bootvine/system-files/mkinitcpio/install/sd-bootvine b/bootvine/system-files/mkinitcpio/install/sd-bootvine index 84df0a3..ea62e30 100755 --- a/bootvine/system-files/mkinitcpio/install/sd-bootvine +++ b/bootvine/system-files/mkinitcpio/install/sd-bootvine @@ -3,6 +3,7 @@ build() { add_binary bootvine add_systemd_unit bootvine.service + add_file /usr/lib/systemd/system/systemd-hibernate-resume.service.d/bootvine.conf if [[ -f /etc/bootvine.toml ]]; then add_file /etc/bootvine.toml fi diff --git a/bootvine/system-files/systemd/systemd-hibernate-resume.service.d/bootvine.conf b/bootvine/system-files/systemd/systemd-hibernate-resume.service.d/bootvine.conf new file mode 100644 index 0000000..66cec5d --- /dev/null +++ b/bootvine/system-files/systemd/systemd-hibernate-resume.service.d/bootvine.conf @@ -0,0 +1,3 @@ +[Service] +ExecStartPre=-/usr/bin/bootvine open-resume-console +ExecStopPost=-/usr/bin/bootvine close-resume-console diff --git a/docs/architecture.md b/docs/architecture.md index c25d18e..564a158 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -276,6 +276,27 @@ It restores the log level and the status output when it hands the console back. Kernel messages from before it starts are already on screen; see [early kernel messages](configuration.md#early-kernel-messages). +### Resuming from hibernation + +The kernel freezes all processes before it loads a hibernated image, so +bootvine cannot draw during the load. A drop-in on +`systemd-hibernate-resume.service` runs `bootvine open-resume-console` first, +which: + +- creates `/run/bootvine/resume-console-open`, so the running bootvine stops + drawing, stops resetting the log level and stops taking `tty12` back; +- raises the console log level to 7, so the kernel's `pr_info` progress lines + reach the screen, and hides their timestamps; +- draws a band across the middle of `tty12`, limits scrolling to the two rows + inside it, and sets the colour and cursor there. The kernel console writes at + the cursor in the terminal's current colour, always from column 0, so it + stays inside the band without knowing about it. + +If an image is restored, none of this needs undoing: the boot kernel's memory +is replaced, and the restored system keeps its own log level and terminals. If +not, `bootvine close-resume-console` runs as `ExecStopPost`, restoring what +the marker recorded, the colour and the scrolling, and bootvine repaints. + ### Passphrase prompts bootvine acts as a systemd diff --git a/docs/guide.md b/docs/guide.md index 816fa0f..9680448 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -83,6 +83,35 @@ The `scripted-initrd` machine uses NixOS's older scripted initrd, which has no systemd. The initrd's lines scroll past as usual, and bootvine starts in stage 2, from `sysinit.target` on. +## Resuming from hibernation + +Resuming from hibernation looks different from a boot, because the kernel +freezes every process before it loads the image, bootvine included. The tree +stops moving, and a band across the middle of the screen shows the kernel's own +progress: + +``` +─── Looking for a hibernated session ───────────────────────────── +PM: Image loading progress: 60% + +────────────────────────────────────────────────────────────────── +``` + +The text comes straight from the kernel, which is why it is a line of text and +not a bar. The screen stays like this until the restored system takes over. +Bootvine does not draw again, because the restore replaces it along with the +rest of the boot kernel's memory. + +The last lines show how the time was spent. `Read ... in N seconds` is the +image load. `Suspending console(s)` means the load has finished and the kernel +is about to jump into the restored system. If the screen stays on that line for +a long time, the wait is the restored kernel bringing devices back up, not the +disk. + +With no image to restore, `systemd-hibernate-resume` finishes at once and the +band disappears. Some kernel console setups +switch to a blank terminal for the load; the same lines then appear there. + ## Shutdown A poweroff: units stopping under the targets waiting on them @@ -174,6 +203,7 @@ shows the shutdown. | `nix run .#sample-vm-text-console` | the same units without a display manager, handing over at the default target | [without a display manager](#without-a-display-manager) | | `nix run .#sample-vm-scripted-initrd` | the same units with NixOS's scripted initrd, so bootvine starts in stage 2 only | [without a systemd initrd](#without-a-systemd-initrd) | | `nix run .#sample-vm-encrypted-volume` | a LUKS volume unlocked in the initrd; the passphrase is `sample passphrase` | [passphrase prompts](#passphrase-prompts) | +| `nix run .#sample-vm-hibernate-resume` | a swap disk to hibernate to; run `systemctl hibernate`, then start the machine again | [resuming from hibernation](#resuming-from-hibernation) | | `nix run .#sample-vm-failures` | a firmware failure in the initrd, then units that exit with an error, crash, time out, cannot be run, restart too often, report a status, die after starting, and one that fails only through its dependency | [failures](#failures) | Each machine keeps its disk in `~/.cache/bootvine-.qcow2`; delete it diff --git a/nix/crane-builds.nix b/nix/crane-builds.nix index e5e0923..1ab5128 100644 --- a/nix/crane-builds.nix +++ b/nix/crane-builds.nix @@ -51,6 +51,10 @@ in substituteInPlace $out/lib/systemd/system/$unit \ --replace-fail /usr/bin/bootvine $out/bin/bootvine done + resume_drop_in=systemd-hibernate-resume.service.d/bootvine.conf + install -Dm644 bootvine/system-files/systemd/$resume_drop_in $out/lib/systemd/system/$resume_drop_in + substituteInPlace $out/lib/systemd/system/$resume_drop_in \ + --replace-fail /usr/bin/bootvine $out/bin/bootvine install -Dm755 bootvine/system-files/dracut/90bootvine/module-setup.sh \ $out/lib/dracut/modules.d/90bootvine/module-setup.sh install -Dm644 bootvine/system-files/mkinitcpio/install/sd-bootvine \ diff --git a/vm-tests/default.nix b/vm-tests/default.nix index 3d687e0..829812f 100644 --- a/vm-tests/default.nix +++ b/vm-tests/default.nix @@ -15,6 +15,7 @@ let scripted-initrd = "the slow units with the scripted initrd, so bootvine starts in stage 2"; encrypted-volume = "a LUKS volume unlocked in the initrd with the passphrase 'sample passphrase'"; failures = "units that fail in every way systemd reports, each with journal messages"; + hibernate-resume = "a swap disk to hibernate to, showing the kernel's image loading progress on resume"; }; machines = lib.mapAttrs (machineName: _: machineModule machineName) machineDescriptions; diff --git a/vm-tests/hibernate-resume/check.nix b/vm-tests/hibernate-resume/check.nix new file mode 100644 index 0000000..c6b47d8 --- /dev/null +++ b/vm-tests/hibernate-resume/check.nix @@ -0,0 +1,38 @@ +{ machine, pkgs, ... }: +{ + name = "bootvine-hibernate-resume"; + + nodes.machine = + { lib, ... }: + { + imports = [ + machine + ../shared/recorded-display.nix + "${pkgs.path}/nixos/tests/common/auto-format-root-device.nix" + ]; + + boot.resumeDevice = "/dev/vdc"; + swapDevices = lib.mkOverride 0 [ + { + device = "/dev/vdc"; + options = [ "x-systemd.makefs" ]; + } + ]; + + powerManagement = { + powerDownCommands = "systemctl --no-block stop backdoor.service"; + resumeCommands = "systemctl --no-block restart backdoor.service"; + }; + + virtualisation = { + emptyDiskImages = [ 4096 ]; + useNixStoreImage = true; + useEFIBoot = true; + }; + }; + + testScript = + builtins.readFile ../shared/console.py + + builtins.readFile ../shared/screen_recording.py + + builtins.readFile ./check.py; +} diff --git a/vm-tests/hibernate-resume/check.py b/vm-tests/hibernate-resume/check.py new file mode 100644 index 0000000..0307214 --- /dev/null +++ b/vm-tests/hibernate-resume/check.py @@ -0,0 +1,56 @@ +RESUME_MARKER = "/run/bootvine/resume-console-open" +BOOT_CONSOLE_LEVEL = "3" +SENTINEL_TEXT = "held only in memory" + + +def console_level(): + return machine.succeed("cut -f1 /proc/sys/kernel/printk").strip() + + +def assert_console_returned(): + machine.wait_until_fails("systemctl is-active bootvine") + machine.fail(f"test -e {RESUME_MARKER}") + assert console_level() == BOOT_CONSOLE_LEVEL, f"kernel console level was not restored: {console_level()}" + + +recording = ScreenRecording(machine) +machine.start() +recording.start() + +machine.wait_for_unit("default.target") +assert_console_returned() +machine.succeed("swapon --show=NAME --noheadings | grep -q .") +machine.succeed( + "mkdir /run/test", + "mount -t ramfs ramfs /run/test", + f"echo '{SENTINEL_TEXT}' > /run/test/sentinel", + "dd if=/dev/urandom of=/run/test/ballast bs=1M count=500", +) + +machine.execute("systemctl hibernate >&2 &", check_return=False) +machine.wait_for_shutdown() +recording.stop() + +resume_recording = ScreenRecording(machine) +resume_recording.frames_directory = os.path.join(machine.out_dir, "resume-recording") +resume_recording.frame_seconds = 0.1 +machine.start() +resume_recording.start() +machine.wait_for_console_text("Image loading progress") +machine.wait_for_console_text("Image loading done") +machine.succeed(f"grep '{SENTINEL_TEXT}' /run/test/sentinel") +assert console_level() == BOOT_CONSOLE_LEVEL, f"the restored system kept the opened level: {console_level()}" +assert foreground_terminal() != f"tty{BOOTVINE_TERMINAL}", "the restored system came back on bootvine's terminal" +machine.fail("systemctl is-active bootvine") + +resume_recording.stop() + +machine.crash() +machine.start() +machine.wait_for_unit("default.target") +machine.fail("test -e /run/test/sentinel") +assert_console_returned() +journal = bootvine_journal() +print(journal) +assert " in Initrd " in journal, "bootvine did not run in the initrd of the boot without an image" +machine.succeed('test "$(systemctl show -p Result --value bootvine)" = success') diff --git a/vm-tests/hibernate-resume/machine.nix b/vm-tests/hibernate-resume/machine.nix new file mode 100644 index 0000000..ab25ef5 --- /dev/null +++ b/vm-tests/hibernate-resume/machine.nix @@ -0,0 +1,13 @@ +{ lib, ... }: +{ + virtualisation.vmVariant = { + boot.resumeDevice = "/dev/vdb"; + swapDevices = lib.mkOverride 0 [ + { + device = "/dev/vdb"; + options = [ "x-systemd.makefs" ]; + } + ]; + virtualisation.emptyDiskImages = [ 4096 ]; + }; +} diff --git a/vm-tests/shared/screen_recording.py b/vm-tests/shared/screen_recording.py index 9fc4a1f..630dd0b 100644 --- a/vm-tests/shared/screen_recording.py +++ b/vm-tests/shared/screen_recording.py @@ -14,6 +14,7 @@ class ScreenRecording: self.recorded_machine = recorded_machine self.frames_directory = os.path.join(recorded_machine.out_dir, "recording") self.raw_frame = os.path.join(recorded_machine.state_dir, "recording-frame.ppm") + self.frame_seconds = RECORDING_FRAME_SECONDS self.stopping = threading.Event() self.thread = threading.Thread(target=self.record_frames, daemon=True) @@ -64,7 +65,7 @@ class ScreenRecording: while not self.stopping.is_set(): self.save_frame(monitor, frame_number) frame_number += 1 - self.stopping.wait(RECORDING_FRAME_SECONDS) + self.stopping.wait(self.frame_seconds) except (OSError, ConnectionError, subprocess.CalledProcessError): pass finally: