|
2 | 2 |
|
3 | 3 | use anyhow::{Context, Result, anyhow, bail}; |
4 | 4 | use serde_json::Value; |
5 | | -use std::path::Path; |
6 | 5 | use std::process::Command; |
7 | 6 | use std::str; |
8 | 7 |
|
9 | 8 | use crate::grub::get_boot_counter; |
10 | 9 |
|
11 | 10 | /// Detects if the system is managed by bootc or is a rpm-ostree system |
| 11 | +/// Inspect bootc status JSON and decide based on `status.booted.incompatible`. |
12 | 12 | fn detect_os_deployment() -> Option<&'static str> { |
13 | | - // 1. Check if this is a bootc-managed host. |
14 | | - if let Ok(output) = Command::new("bootc") |
| 13 | + let output = match Command::new("bootc") |
15 | 14 | .args(["status", "--booted", "--json"]) |
16 | 15 | .output() |
17 | | - && output.status.success() |
18 | | - && let Ok(json) = serde_json::from_slice::<Value>(&output.stdout) |
19 | | - && json.get("kind").and_then(|v| v.as_str()) == Some("BootcHost") |
20 | 16 | { |
21 | | - log::info!("System detected as bootc-managed host."); |
22 | | - return Some("bootc"); |
23 | | - } |
| 17 | + Ok(output) => output, |
| 18 | + Err(_) => return None, |
| 19 | + }; |
24 | 20 |
|
25 | | - // 2. If not bootc, check if it's an ostree-based OS by looking for /run/ostree-booted. |
26 | | - if Path::new("/run/ostree-booted").exists() { |
27 | | - log::info!("System detected as ostree-based (via /run/ostree-booted)."); |
28 | | - return Some("rpm-ostree"); |
| 21 | + if !output.status.success() { |
| 22 | + log::error!("'bootc status --booted --json' exited with non-zero status"); |
| 23 | + return None; |
29 | 24 | } |
30 | 25 |
|
31 | | - // 3. If neither check passes, the deployment type is unsupported. |
32 | | - log::warn!("System is neither bootc nor a known ostree variant."); |
33 | | - None |
| 26 | + let json: Value = match serde_json::from_slice::<Value>(&output.stdout) { |
| 27 | + Ok(json) => json, |
| 28 | + Err(_) => { |
| 29 | + log::error!("Failed to parse JSON from 'bootc status --booted --json'"); |
| 30 | + return None; |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + match json |
| 35 | + .get("status") |
| 36 | + .and_then(|s| s.get("booted")) |
| 37 | + .and_then(|b| b.get("incompatible")) |
| 38 | + .and_then(|i| i.as_bool()) |
| 39 | + { |
| 40 | + Some(true) => { |
| 41 | + log::info!("System detected as rpm-ostree (incompatible=true)"); |
| 42 | + Some("rpm-ostree") |
| 43 | + } |
| 44 | + Some(false) => { |
| 45 | + log::info!("System detected as bootc (incompatible=false)"); |
| 46 | + Some("bootc") |
| 47 | + } |
| 48 | + None => { |
| 49 | + log::error!("bootc status JSON missing boolean field status.booted.incompatible"); |
| 50 | + None |
| 51 | + } |
| 52 | + } |
34 | 53 | } |
35 | 54 |
|
36 | 55 | /// reboots the system if boot_counter is greater than 0 or can be forced too |
|
0 commit comments