use std::fmt;
use bollard::models::HostConfig;
use crate::api::{Check, CheckStatus};
use crate::config;
pub trait Isolation: Send + Sync + fmt::Debug {
fn name(&self) -> &'static str;
fn is_unsafe(&self) -> bool;
fn configure(&self, host_config: &mut HostConfig);
}
#[derive(Debug)]
pub struct Sysbox;
impl Isolation for Sysbox {
fn name(&self) -> &'static str {
"sysbox"
}
fn is_unsafe(&self) -> bool {
false
}
fn configure(&self, host_config: &mut HostConfig) {
host_config.runtime = Some("sysbox-runc".to_owned());
host_config.privileged = Some(false);
}
}
#[derive(Debug)]
pub struct UnsafeDind;
impl Isolation for UnsafeDind {
fn name(&self) -> &'static str {
"unsafe-dind"
}
fn is_unsafe(&self) -> bool {
true
}
fn configure(&self, host_config: &mut HostConfig) {
host_config.runtime = Some("runc".to_owned());
host_config.privileged = Some(true);
}
}
pub fn backend(isolation: config::Isolation) -> &'static dyn Isolation {
match isolation {
config::Isolation::Sysbox => &Sysbox,
config::Isolation::UnsafeDind => &UnsafeDind,
}
}
const MODULES_STAT_REFUSED: &str = "failed to stat mount source at /lib/modules/";
fn missing_modules_release(error: &str) -> Option<&str> {
let (_, after) = error.split_once(MODULES_STAT_REFUSED)?;
if !after.contains("no such file or directory") {
return None;
}
let release = after.split(['/', ':']).next()?;
(!release.is_empty()).then_some(release)
}
pub fn start_failure(runner: &str, docker_error: &str) -> String {
match missing_modules_release(docker_error) {
Some(release) => format!(
"cannot start {runner}: the running kernel's modules are gone (/lib/modules/{release} does not exist), \
most likely removed by a package upgrade; reboot to run the installed kernel. Docker said: {docker_error}"
),
None => format!("cannot start {runner}: {docker_error}"),
}
}
pub fn modules_check(release: &str, present: bool) -> Check {
if present {
return Check {
name: "kernel modules".to_owned(),
status: CheckStatus::Pass,
detail: format!("/lib/modules/{release} exists"),
fix: None,
};
}
Check {
name: "kernel modules".to_owned(),
status: CheckStatus::Fail,
detail: format!(
"/lib/modules/{release} does not exist, which sysbox mounts into every runner, so no runner starts; a \
package upgrade removes it while that kernel runs"
),
fix: Some("reboot to run the installed kernel".to_owned()),
}
}
#[cfg(test)]
mod tests;