Skip to main content

gunnar_server/
stop_record.rs

1//! **The last stop, kept where it survives the stop** (2026-10-01, the real test2 run 9:
2//! vm1 stopped cleanly, but its power-off steps were lost — the narration and the disk log
3//! die with the read-only remount of /data — and UpCloud reported `stopped` 46 s after the
4//! soft stop, so nobody could tell the guest's power-off from the provider's reporting).
5//! After the quiesce and just before the power goes, ONE line is written on the ESP (its
6//! own filesystem, flipped read-write for that write); the next boot says it at INFO and
7//! removes it.
8
9/// Where the record lives on the ESP.
10pub const RECORD: &str = "/boot/gunnar-last-stop.txt";
11
12/// The words the next boot says the record in — what the installer's kit looks for.
13pub const SAID: &str = "the previous stop (kept on the ESP):";
14
15/// **The record**: `power_off_epoch_ms=<ms> <the stop's line>` — one line, no newline in it.
16pub fn render(power_off_epoch_ms: u128, line: &str) -> String {
17    format!(
18        "power_off_epoch_ms={power_off_epoch_ms} {}\n",
19        line.replace('\n', " ")
20    )
21}
22
23/// The power-off time out of a record (or out of the line the next boot said it in).
24pub fn power_off_epoch_ms(text: &str) -> Option<u128> {
25    text.split("power_off_epoch_ms=")
26        .nth(1)?
27        .split_whitespace()
28        .next()?
29        .parse()
30        .ok()
31}
32
33/// Write the record for a stop said as `line`, stamped now — the last write before the power.
34pub fn keep(line: &str) -> bool {
35    let now = std::time::SystemTime::now()
36        .duration_since(std::time::UNIX_EPOCH)
37        .map(|d| d.as_millis())
38        .unwrap_or(0);
39    crate::narrate::write_esp_file(std::path::Path::new(RECORD), render(now, line).as_bytes())
40}
41
42/// At boot: say the previous stop's record (if any) at INFO, then remove it.
43pub fn say_previous() {
44    let path = std::path::Path::new(RECORD);
45    if let Ok(text) = std::fs::read_to_string(path) {
46        tracing::info!("{SAID} {}", text.trim());
47        crate::narrate::remove_esp_file(path);
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    /// One line, the power-off time first and readable back, the stop's own words after it.
56    #[test]
57    fn a_stop_record_is_one_line_whose_power_off_time_reads_back() {
58        let r = render(1_790_850_910_705, "front: stopped cleanly on ACPI power button — power-off: sync(2) done in 3 ms; 2 writer(s) released;\n/data remounted read-only in 41 ms");
59        assert_eq!(r.matches('\n').count(), 1, "{r}");
60        assert!(
61            r.starts_with("power_off_epoch_ms=1790850910705 front: stopped cleanly"),
62            "{r}"
63        );
64        assert_eq!(power_off_epoch_ms(&r), Some(1_790_850_910_705));
65        assert_eq!(
66            power_off_epoch_ms(&format!("INFO {SAID} {r}")),
67            Some(1_790_850_910_705),
68            "read back out of the next boot's line too"
69        );
70    }
71}