|
| 1 | +use crate::helpers::{ |
| 2 | + CheckGroup, CheckGroupCategory, CheckGroupResult, CheckResult, |
| 3 | + CheckResultValue::{Errored, Failed, Passed}, |
| 4 | + host_executor::HostNamespaceExecutor, |
| 5 | + kernel as khelper, |
| 6 | +}; |
| 7 | + |
| 8 | +use async_trait::async_trait; |
| 9 | +use futures::{FutureExt, future::join_all}; |
| 10 | +use procfs::sys::kernel; |
| 11 | + |
| 12 | +const GROUP_IDENTIFIER: &str = "kernel"; |
| 13 | +const NAME: &str = "Postinstall Kernel Checks"; |
| 14 | + |
| 15 | +// Stuff we expect to be available under an Edera host kernel, |
| 16 | +// or a supported BYO kernel. |
| 17 | +const REQUIRED_MODULES: &[&str] = &[ |
| 18 | + "nf_tables", |
| 19 | + "xen_evtchn", |
| 20 | + "xen-privcmd", |
| 21 | + "xen-netback", |
| 22 | + "xen-pciback", |
| 23 | + "xen-blkback", |
| 24 | + "xen-gntdev", |
| 25 | + "xen-gntalloc", |
| 26 | +]; |
| 27 | + |
| 28 | +const KVER_FLOOR_PATCH: u16 = 0; |
| 29 | +const KVER_FLOOR_MINOR: u8 = 15; |
| 30 | +const KVER_FLOOR_MAJOR: u8 = 5; |
| 31 | + |
| 32 | +pub struct PostinstallKernelChecks { |
| 33 | + host_executor: HostNamespaceExecutor, |
| 34 | +} |
| 35 | + |
| 36 | +impl PostinstallKernelChecks { |
| 37 | + pub fn new(host_executor: HostNamespaceExecutor) -> Self { |
| 38 | + PostinstallKernelChecks { host_executor } |
| 39 | + } |
| 40 | + |
| 41 | + /// Run all the checkers asynchronously, then |
| 42 | + /// join and collect the results. |
| 43 | + pub async fn run_all(&self) -> CheckGroupResult { |
| 44 | + let results = join_all([self.has_modules().boxed(), self.version_is_good().boxed()]).await; |
| 45 | + |
| 46 | + let mut group_result = Passed; |
| 47 | + for res in results.iter() { |
| 48 | + // Set group result to Failed if we failed and aren't already in an Errored state |
| 49 | + if !matches!(group_result, Errored(_)) && matches!(res.result, Failed(_)) { |
| 50 | + group_result = Failed("".into()); |
| 51 | + } |
| 52 | + |
| 53 | + if matches!(res.result, Errored(_)) { |
| 54 | + group_result = Errored("".into()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + CheckGroupResult { |
| 59 | + name: NAME.to_string(), |
| 60 | + result: group_result, |
| 61 | + results, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + async fn version_is_good(&self) -> CheckResult { |
| 66 | + let name = String::from("Host Kernel Version Is Good"); |
| 67 | + let floor = kernel::Version::new(KVER_FLOOR_MAJOR, KVER_FLOOR_MINOR, KVER_FLOOR_PATCH); |
| 68 | + |
| 69 | + let passed = khelper::host_kver_above_floor(&self.host_executor, floor).await; |
| 70 | + |
| 71 | + match passed { |
| 72 | + Err(e) => CheckResult::new(&name, Errored(e.to_string())), |
| 73 | + Ok(true) => CheckResult::new(&name, Passed), |
| 74 | + Ok(false) => CheckResult::new( |
| 75 | + &name, |
| 76 | + Failed(String::from("current kernel version is unsupported")), |
| 77 | + ), |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async fn has_modules(&self) -> CheckResult { |
| 82 | + let name = String::from("Host Has Necessary Modules"); |
| 83 | + |
| 84 | + let required_modules: Vec<String> = |
| 85 | + REQUIRED_MODULES.iter().map(|s| s.to_string()).collect(); |
| 86 | + |
| 87 | + // Search builtin modules |
| 88 | + let remaining = match khelper::find_builtins(&self.host_executor, &required_modules).await { |
| 89 | + Ok(r) => r, |
| 90 | + Err(e) => { |
| 91 | + return CheckResult::new(&name, Errored(format!("getting kernel builtins {e}"))); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + // Search loaded modules |
| 96 | + let remaining = match khelper::find_loaded(&self.host_executor, &remaining).await { |
| 97 | + Ok(r) => r, |
| 98 | + Err(e) => { |
| 99 | + return CheckResult::new(&name, Errored(format!("getting kernel modules {e}"))); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + // Search loadable modules |
| 104 | + let remaining = match khelper::find_loadable(&self.host_executor, &remaining).await { |
| 105 | + Ok(r) => r, |
| 106 | + Err(e) => { |
| 107 | + return CheckResult::new(&name, Errored(format!("getting kernel modules {e}"))); |
| 108 | + } |
| 109 | + }; |
| 110 | + if !remaining.is_empty() { |
| 111 | + return CheckResult::new(&name, Failed(format!("missing {:?}", remaining))); |
| 112 | + } |
| 113 | + |
| 114 | + CheckResult::new(&name, Passed) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[async_trait] |
| 119 | +impl CheckGroup for PostinstallKernelChecks { |
| 120 | + fn id(&self) -> &str { |
| 121 | + GROUP_IDENTIFIER |
| 122 | + } |
| 123 | + |
| 124 | + fn name(&self) -> &str { |
| 125 | + NAME |
| 126 | + } |
| 127 | + |
| 128 | + fn description(&self) -> &str { |
| 129 | + "Postinstall kernel validation checks" |
| 130 | + } |
| 131 | + |
| 132 | + async fn run(&self) -> CheckGroupResult { |
| 133 | + self.run_all().await |
| 134 | + } |
| 135 | + |
| 136 | + fn category(&self) -> CheckGroupCategory { |
| 137 | + CheckGroupCategory::Required |
| 138 | + } |
| 139 | +} |
0 commit comments