|
| 1 | +use std::process::Command; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use bootc_mount::run_findmnt; |
| 5 | +use bootc_utils::CommandRunExt; |
| 6 | +use serde::Deserialize; |
| 7 | + |
| 8 | +#[derive(Debug, Deserialize)] |
| 9 | +pub(crate) struct Lvs { |
| 10 | + report: Vec<LvsReport>, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Debug, Deserialize)] |
| 14 | +pub(crate) struct LvsReport { |
| 15 | + lv: Vec<LogicalVolume>, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Debug, Deserialize, Clone)] |
| 19 | +pub(crate) struct LogicalVolume { |
| 20 | + lv_name: String, |
| 21 | + lv_size: String, |
| 22 | + lv_path: String, |
| 23 | + vg_name: String, |
| 24 | +} |
| 25 | + |
| 26 | +pub(crate) fn parse_volumes(group: Option<&str>) -> Result<Vec<LogicalVolume>> { |
| 27 | + if which::which("podman").is_err() { |
| 28 | + tracing::debug!("lvs binary not found. Skipping logical volume check."); |
| 29 | + return Ok(Vec::<LogicalVolume>::new()); |
| 30 | + } |
| 31 | + |
| 32 | + let mut cmd = Command::new("lvs"); |
| 33 | + cmd.args([ |
| 34 | + "--reportformat=json", |
| 35 | + "-o", |
| 36 | + "lv_name,lv_size,lv_path,vg_name", |
| 37 | + ]) |
| 38 | + .args(group); |
| 39 | + |
| 40 | + let output: Lvs = cmd.run_and_parse_json()?; |
| 41 | + |
| 42 | + Ok(output |
| 43 | + .report |
| 44 | + .iter() |
| 45 | + .flat_map(|r| r.lv.iter().cloned()) |
| 46 | + .collect()) |
| 47 | +} |
| 48 | + |
| 49 | +pub(crate) fn check_root_siblings() -> Result<Vec<String>> { |
| 50 | + let all_volumes = parse_volumes(None)?; |
| 51 | + |
| 52 | + // first look for a lv mounted to '/' |
| 53 | + // then gather all the sibling lvs in the vg along with their mount points |
| 54 | + let siblings: Vec<String> = all_volumes |
| 55 | + .iter() |
| 56 | + .filter(|lv| { |
| 57 | + let mount = run_findmnt(&["-S", &lv.lv_path], None).unwrap_or_default(); |
| 58 | + if let Some(fs) = mount.filesystems.first() { |
| 59 | + &fs.target == "/" |
| 60 | + } else { |
| 61 | + false |
| 62 | + } |
| 63 | + }) |
| 64 | + .flat_map(|root_lv| parse_volumes(Some(root_lv.vg_name.as_str())).unwrap_or_default()) |
| 65 | + .try_fold(Vec::new(), |mut acc, r| -> anyhow::Result<_> { |
| 66 | + let mount = run_findmnt(&["-S", &r.lv_path], None)?; |
| 67 | + let mount_path = if let Some(fs) = mount.filesystems.first() { |
| 68 | + &fs.target |
| 69 | + } else { |
| 70 | + "" |
| 71 | + }; |
| 72 | + |
| 73 | + if mount_path != "/" { |
| 74 | + acc.push(format!( |
| 75 | + "Type: LVM, Mount Point: {}, LV: {}, VG: {}, Size: {}", |
| 76 | + mount_path, r.lv_name, r.vg_name, r.lv_size |
| 77 | + )) |
| 78 | + }; |
| 79 | + |
| 80 | + Ok(acc) |
| 81 | + })?; |
| 82 | + |
| 83 | + Ok(siblings) |
| 84 | +} |
0 commit comments