|
| 1 | +//! # bootc-managed container storage |
| 2 | +//! |
| 3 | +//! The default storage for this project uses ostree, canonically storing all of its state in |
| 4 | +//! `/sysroot/ostree`. |
| 5 | +//! |
| 6 | +//! This containers-storage: which canonically lives in `/sysroot/ostree/bootc`. |
| 7 | +
|
| 8 | +use std::io::{Read, Seek}; |
| 9 | +use std::os::unix::process::CommandExt; |
| 10 | +use std::process::Command; |
| 11 | +use std::sync::Arc; |
| 12 | + |
| 13 | +use anyhow::{Context, Result}; |
| 14 | +use camino::Utf8Path; |
| 15 | +use cap_std_ext::cap_std; |
| 16 | +use cap_std_ext::cap_std::fs::Dir; |
| 17 | +use cap_std_ext::cap_tempfile::TempDir; |
| 18 | +use cap_std_ext::dirext::CapStdExtDirExt; |
| 19 | +use fn_error_context::context; |
| 20 | +use rustix::io::DupFlags; |
| 21 | +use std::os::fd::{AsFd, FromRawFd, OwnedFd}; |
| 22 | +use tokio::process::Command as AsyncCommand; |
| 23 | + |
| 24 | +use crate::utils::{AsyncCommandRunExt, CommandRunExt}; |
| 25 | + |
| 26 | +/// Global directory path which we use for podman to point |
| 27 | +/// it at our storage. |
| 28 | +pub(crate) const STORAGE_ALIAS_DIR: &str = "/run/bootc/storage"; |
| 29 | +/// And a similar alias for the runtime state. The 3 here is hardcoded, |
| 30 | +/// and set up in a fork below too. |
| 31 | +pub(crate) const STORAGE_RUN_ALIAS_DIR: &str = "/proc/self/fd/3"; |
| 32 | +const STORAGE_RUN_FD: i32 = 3; |
| 33 | + |
| 34 | +/// The path to the storage, relative to the physical system root. |
| 35 | +pub(crate) const SUBPATH: &str = "ostree/bootc/storage"; |
| 36 | +/// The path to the "runroot" with transient runtime state; this is |
| 37 | +/// relative to the /run directory |
| 38 | +const RUNROOT: &str = "bootc/storage"; |
| 39 | +pub(crate) struct Storage { |
| 40 | + /// The root directory |
| 41 | + sysroot: Dir, |
| 42 | + /// The location of container storage |
| 43 | + storage_root: Dir, |
| 44 | + #[allow(dead_code)] |
| 45 | + /// Our runtime state |
| 46 | + run: Dir, |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Debug, PartialEq, Eq)] |
| 50 | +pub(crate) enum PullMode { |
| 51 | + /// Pull only if the image is not present |
| 52 | + IfNotExists, |
| 53 | + /// Always check for an update |
| 54 | + #[allow(dead_code)] |
| 55 | + Always, |
| 56 | +} |
| 57 | + |
| 58 | +async fn run_cmd_async(cmd: Command) -> Result<()> { |
| 59 | + let mut cmd = tokio::process::Command::from(cmd); |
| 60 | + cmd.kill_on_drop(true); |
| 61 | + let mut stderr = tempfile::tempfile()?; |
| 62 | + cmd.stderr(stderr.try_clone()?); |
| 63 | + if let Err(e) = cmd.run().await { |
| 64 | + stderr.seek(std::io::SeekFrom::Start(0))?; |
| 65 | + let mut stderr_buf = String::new(); |
| 66 | + // Ignore errors |
| 67 | + let _ = stderr.read_to_string(&mut stderr_buf); |
| 68 | + return Err(anyhow::anyhow!("{e}: {stderr_buf}")); |
| 69 | + } |
| 70 | + Ok(()) |
| 71 | +} |
| 72 | + |
| 73 | +#[allow(unsafe_code)] |
| 74 | +#[context("Binding storage roots")] |
| 75 | +fn bind_storage_roots(cmd: &mut Command, storage_root: &Dir, run_root: &Dir) -> Result<()> { |
| 76 | + // podman requires an absolute path, for two reasons right now: |
| 77 | + // - It writes the file paths into `db.sql`, a sqlite database for unknown reasons |
| 78 | + // - It forks helper binaries, so just giving it /proc/self/fd won't work as |
| 79 | + // those helpers may not get the fd passed. (which is also true of skopeo) |
| 80 | + // We create a new mount namespace, which also has the helpful side effect |
| 81 | + // of automatically cleaning up the global bind mount that the storage stack |
| 82 | + // creates. |
| 83 | + |
| 84 | + let storage_root = Arc::new(storage_root.try_clone().context("Cloning storage root")?); |
| 85 | + let run_root = Arc::new(run_root.try_clone().context("Cloning runroot")?); |
| 86 | + // SAFETY: All the APIs we call here are safe to invoke between fork and exec. |
| 87 | + unsafe { |
| 88 | + cmd.pre_exec(move || { |
| 89 | + // Set our working directory here, because this is the only way I could |
| 90 | + // get the mount() below to work. |
| 91 | + rustix::process::fchdir(&storage_root)?; |
| 92 | + rustix::thread::unshare(rustix::thread::UnshareFlags::NEWNS)?; |
| 93 | + rustix::mount::mount_bind(".", STORAGE_ALIAS_DIR)?; |
| 94 | + // Set up the runtime dir via /proc/self/fd, which works because it's |
| 95 | + // not passed to any child processes. Note that we dup it without |
| 96 | + // setting O_CLOEXEC intentionally |
| 97 | + let mut ofd = OwnedFd::from_raw_fd(STORAGE_RUN_FD); |
| 98 | + rustix::io::dup3(run_root.as_fd(), &mut ofd, DupFlags::empty())?; |
| 99 | + // We didn't actually "own" this fd to start with |
| 100 | + std::mem::forget(ofd); |
| 101 | + Ok(()) |
| 102 | + }) |
| 103 | + }; |
| 104 | + Ok(()) |
| 105 | +} |
| 106 | + |
| 107 | +fn new_podman_cmd_in(storage_root: &Dir, run_root: &Dir) -> Result<Command> { |
| 108 | + let mut cmd = Command::new("podman"); |
| 109 | + bind_storage_roots(&mut cmd, storage_root, run_root)?; |
| 110 | + cmd.args([ |
| 111 | + "--root", |
| 112 | + STORAGE_ALIAS_DIR, |
| 113 | + "--runroot", |
| 114 | + STORAGE_RUN_ALIAS_DIR, |
| 115 | + ]); |
| 116 | + Ok(cmd) |
| 117 | +} |
| 118 | + |
| 119 | +impl Storage { |
| 120 | + /// Create a `podman image` Command instance prepared to operate on our alternative |
| 121 | + /// root. |
| 122 | + pub(crate) fn new_image_cmd(&self) -> Result<Command> { |
| 123 | + let mut r = new_podman_cmd_in(&self.storage_root, &self.run)?; |
| 124 | + // We want to limit things to only manipulating images by default. |
| 125 | + r.arg("image"); |
| 126 | + Ok(r) |
| 127 | + } |
| 128 | + |
| 129 | + fn init_globals() -> Result<()> { |
| 130 | + // Ensure our global storage alias dirs exist |
| 131 | + for d in [STORAGE_ALIAS_DIR] { |
| 132 | + std::fs::create_dir_all(d).with_context(|| format!("Creating {d}"))?; |
| 133 | + } |
| 134 | + Ok(()) |
| 135 | + } |
| 136 | + |
| 137 | + #[context("Creating imgstorage")] |
| 138 | + pub(crate) fn create(sysroot: &Dir, run: &Dir) -> Result<Self> { |
| 139 | + Self::init_globals()?; |
| 140 | + let subpath = Utf8Path::new(SUBPATH); |
| 141 | + // SAFETY: We know there's a parent |
| 142 | + let parent = subpath.parent().unwrap(); |
| 143 | + if !sysroot |
| 144 | + .try_exists(subpath) |
| 145 | + .with_context(|| format!("Querying {subpath}"))? |
| 146 | + { |
| 147 | + let tmp = format!("{SUBPATH}.tmp"); |
| 148 | + sysroot.remove_all_optional(&tmp).context("Removing tmp")?; |
| 149 | + sysroot |
| 150 | + .create_dir_all(parent) |
| 151 | + .with_context(|| format!("Creating {parent}"))?; |
| 152 | + sysroot.create_dir_all(&tmp).context("Creating tmpdir")?; |
| 153 | + let storage_root = sysroot.open_dir(&tmp).context("Open tmp")?; |
| 154 | + // There's no explicit API to initialize a containers-storage: |
| 155 | + // root, simply passing a path will attempt to auto-create it. |
| 156 | + // We run "podman images" in the new root. |
| 157 | + new_podman_cmd_in(&storage_root, &run)? |
| 158 | + .arg("images") |
| 159 | + .run() |
| 160 | + .context("Initializing images")?; |
| 161 | + drop(storage_root); |
| 162 | + sysroot |
| 163 | + .rename(&tmp, sysroot, subpath) |
| 164 | + .context("Renaming tmpdir")?; |
| 165 | + } |
| 166 | + Self::open(sysroot, run) |
| 167 | + } |
| 168 | + |
| 169 | + #[context("Opening imgstorage")] |
| 170 | + pub(crate) fn open(sysroot: &Dir, run: &Dir) -> Result<Self> { |
| 171 | + Self::init_globals()?; |
| 172 | + let storage_root = sysroot |
| 173 | + .open_dir(SUBPATH) |
| 174 | + .with_context(|| format!("Opening {SUBPATH}"))?; |
| 175 | + // Always auto-create this if missing |
| 176 | + run.create_dir_all(RUNROOT) |
| 177 | + .with_context(|| format!("Creating {RUNROOT}"))?; |
| 178 | + let run = run.open_dir(RUNROOT)?; |
| 179 | + Ok(Self { |
| 180 | + sysroot: sysroot.try_clone()?, |
| 181 | + storage_root, |
| 182 | + run, |
| 183 | + }) |
| 184 | + } |
| 185 | + |
| 186 | + /// Fetch the image if it is not already present; return whether |
| 187 | + /// or not the image was fetched. |
| 188 | + pub(crate) async fn pull(&self, image: &str, mode: PullMode) -> Result<bool> { |
| 189 | + match mode { |
| 190 | + PullMode::IfNotExists => { |
| 191 | + // Sadly https://docs.rs/containers-image-proxy/latest/containers_image_proxy/struct.ImageProxy.html#method.open_image_optional |
| 192 | + // doesn't work with containers-storage yet |
| 193 | + let mut cmd = AsyncCommand::from(self.new_image_cmd()?); |
| 194 | + cmd.args(["exists", image]); |
| 195 | + let exists = cmd.status().await?.success(); |
| 196 | + if exists { |
| 197 | + return Ok(false); |
| 198 | + } |
| 199 | + } |
| 200 | + PullMode::Always => {} |
| 201 | + }; |
| 202 | + let mut cmd = self.new_image_cmd()?; |
| 203 | + cmd.args(["pull", image]); |
| 204 | + let authfile = ostree_ext::globals::get_global_authfile(&self.sysroot)? |
| 205 | + .map(|(authfile, _fd)| authfile); |
| 206 | + if let Some(authfile) = authfile { |
| 207 | + cmd.args(["--authfile", authfile.as_str()]); |
| 208 | + } |
| 209 | + run_cmd_async(cmd).await.context("Failed to pull image")?; |
| 210 | + Ok(true) |
| 211 | + } |
| 212 | + |
| 213 | + pub(crate) async fn pull_from_host_storage(&self, image: &str) -> Result<()> { |
| 214 | + let mut cmd = Command::new("podman"); |
| 215 | + // An ephemeral place for the transient state |
| 216 | + let temp_runroot = TempDir::new(cap_std::ambient_authority())?; |
| 217 | + bind_storage_roots(&mut cmd, &self.storage_root, &temp_runroot)?; |
| 218 | + |
| 219 | + // The destination (target stateroot) + container storage dest |
| 220 | + let storage_dest = |
| 221 | + &format!("containers-storage:[overlay@{STORAGE_ALIAS_DIR}+{STORAGE_RUN_ALIAS_DIR}]"); |
| 222 | + cmd.args(["image", "push", image]) |
| 223 | + .arg(format!("{storage_dest}{image}")); |
| 224 | + run_cmd_async(cmd).await?; |
| 225 | + temp_runroot.close()?; |
| 226 | + Ok(()) |
| 227 | + } |
| 228 | +} |
0 commit comments