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.
This commit is contained in:
17 files changed
+433
-13
No files matched your search
@@ -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;
|
||||
@@ -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<PasswordEntry>,
|
||||
handover_deadline: Option<Duration>,
|
||||
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<impl Stream<It
|
||||
.filter(|changed| ready(*changed))
|
||||
.map(|_| ScreenInput::PasswordRequests))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{env, fs, process};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn desk_watching(resume_marker: PathBuf) -> 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);
|
||||
}
|
||||
}
|
||||
@@ -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<File> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -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<Desk: ConsoleDesk> WatchScreen<Desk> {
|
||||
Error: From<B::Error>,
|
||||
{
|
||||
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<B: Backend>(backend: &mut B, frame: &Buffer) -> Result<(), B::Error> {
|
||||
pub(super) fn overwrite_every_cell<B: Backend>(
|
||||
backend: &mut B,
|
||||
frame: &Buffer,
|
||||
) -> Result<(), B::Error> {
|
||||
backend.draw(
|
||||
frame
|
||||
.content
|
||||
|
||||
+13
-1
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Option<InstalledFile>, InstallE
|
||||
|
||||
fn unit_files(target: &InstallTarget) -> impl Iterator<Item = InstalledFile> {
|
||||
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<InstalledFile> {
|
||||
@@ -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 => {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
[Service]
|
||||
ExecStartPre=-/usr/bin/bootvine open-resume-console
|
||||
ExecStopPost=-/usr/bin/bootvine close-resume-console
|
||||
Reference in new issue
Block a user