|
| 1 | +//! # Write deployments merging image with configmap |
| 2 | +//! |
| 3 | +//! Create a merged filesystem tree with the image and mounted configmaps. |
| 4 | +
|
| 5 | +// Unfortunately needed here to work with linkme |
| 6 | +#![allow(unsafe_code)] |
| 7 | + |
| 8 | +use std::process::Command; |
| 9 | + |
| 10 | +use cap_std_ext::cap_std; |
| 11 | +use cap_std_ext::cap_std::fs::{Dir, MetadataExt as _}; |
| 12 | +use cap_std_ext::dirext::CapStdExtDirExt; |
| 13 | +use linkme::distributed_slice; |
| 14 | + |
| 15 | +use crate::store::Storage; |
| 16 | + |
| 17 | +/// A lint check has failed. |
| 18 | +#[derive(thiserror::Error, Debug)] |
| 19 | +struct FsckError(String); |
| 20 | + |
| 21 | +/// The outer error is for unexpected fatal runtime problems; the |
| 22 | +/// inner error is for the check failing in an expected way. |
| 23 | +type FsckResult = anyhow::Result<std::result::Result<(), FsckError>>; |
| 24 | + |
| 25 | +/// Everything is OK - we didn't encounter a runtime error, and |
| 26 | +/// the targeted check passed. |
| 27 | +fn fsck_ok() -> FsckResult { |
| 28 | + Ok(Ok(())) |
| 29 | +} |
| 30 | + |
| 31 | +/// We successfully found a failure. |
| 32 | +fn fsck_err(msg: impl AsRef<str>) -> FsckResult { |
| 33 | + Ok(Err(FsckError::new(msg))) |
| 34 | +} |
| 35 | + |
| 36 | +impl std::fmt::Display for FsckError { |
| 37 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 38 | + f.write_str(&self.0) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl FsckError { |
| 43 | + fn new(msg: impl AsRef<str>) -> Self { |
| 44 | + Self(msg.as_ref().to_owned()) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug)] |
| 49 | +struct FsckCheck { |
| 50 | + name: &'static str, |
| 51 | + ordering: u16, |
| 52 | + f: FsckFn, |
| 53 | +} |
| 54 | + |
| 55 | +type FsckFn = fn(&Storage) -> FsckResult; |
| 56 | +#[distributed_slice] |
| 57 | +pub(crate) static FSCK_CHECKS: [FsckCheck]; |
| 58 | + |
| 59 | +impl FsckCheck { |
| 60 | + pub(crate) const fn new(name: &'static str, ordering: u16, f: FsckFn) -> Self { |
| 61 | + FsckCheck { name, ordering, f } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#[distributed_slice(FSCK_CHECKS)] |
| 66 | +static CHECK_RESOLVCONF: FsckCheck = FsckCheck::new("etc-resolvconf", 5, check_resolvconf); |
| 67 | +/// See https://github.com/containers/bootc/pull/1096 and https://github.com/containers/bootc/pull/1167 |
| 68 | +/// Basically verify that if /usr/etc/resolv.conf exists, it is not a zero-sized file that was |
| 69 | +/// probably injected by buildah and that bootc should have removed. |
| 70 | +/// |
| 71 | +/// Note that this fsck check can fail for systems upgraded from old bootc right now, as |
| 72 | +/// we need the *new* bootc to fix it. |
| 73 | +/// |
| 74 | +/// But at the current time fsck is an experimental feature that we should only be running |
| 75 | +/// in our CI. |
| 76 | +fn check_resolvconf(storage: &Storage) -> FsckResult { |
| 77 | + // For now we only check the booted deployment. |
| 78 | + if storage.booted_deployment().is_none() { |
| 79 | + return fsck_ok(); |
| 80 | + } |
| 81 | + // Read usr/etc/resolv.conf directly. |
| 82 | + let usr = Dir::open_ambient_dir("/usr", cap_std::ambient_authority())?; |
| 83 | + let Some(meta) = usr.symlink_metadata_optional("etc/resolv.conf")? else { |
| 84 | + return fsck_ok(); |
| 85 | + }; |
| 86 | + if meta.is_file() && meta.size() == 0 { |
| 87 | + return fsck_err("Found usr/etc/resolv.conf as zero-sized file"); |
| 88 | + } |
| 89 | + fsck_ok() |
| 90 | +} |
| 91 | + |
| 92 | +pub(crate) fn fsck(storage: &Storage, mut output: impl std::io::Write) -> anyhow::Result<()> { |
| 93 | + let mut checks = FSCK_CHECKS.static_slice().iter().collect::<Vec<_>>(); |
| 94 | + checks.sort_by(|a, b| a.ordering.cmp(&b.ordering)); |
| 95 | + |
| 96 | + let mut errors = false; |
| 97 | + for check in checks.iter() { |
| 98 | + let name = check.name; |
| 99 | + // SAFETY: Shouldn't fail to join a thread |
| 100 | + match (check.f)(&storage) { |
| 101 | + Ok(Ok(())) => {} |
| 102 | + Ok(Err(e)) => { |
| 103 | + errors = true; |
| 104 | + writeln!(output, "Failed check \"{name}\": {e}")?; |
| 105 | + } |
| 106 | + Err(e) => { |
| 107 | + errors = true; |
| 108 | + writeln!(output, "Unexpected runtime error in check \"{name}\": {e}")?; |
| 109 | + } |
| 110 | + } |
| 111 | + println!("ok: {name}"); |
| 112 | + } |
| 113 | + if errors { |
| 114 | + anyhow::bail!("Encountered errors") |
| 115 | + } |
| 116 | + |
| 117 | + // Run an `ostree fsck` (yes, ostree exposes enough APIs |
| 118 | + // that we could reimplement this in Rust, but eh) |
| 119 | + let st = Command::new("ostree") |
| 120 | + .arg("fsck") |
| 121 | + .stdin(std::process::Stdio::inherit()) |
| 122 | + .status()?; |
| 123 | + if !st.success() { |
| 124 | + anyhow::bail!("ostree fsck failed"); |
| 125 | + } |
| 126 | + |
| 127 | + Ok(()) |
| 128 | +} |
0 commit comments