|
| 1 | +use std::process::Command; |
| 2 | + |
| 3 | +use anyhow::{Context, Result}; |
| 4 | +use bootc_utils::CommandRunExt; |
| 5 | +use camino::Utf8PathBuf; |
| 6 | +use fn_error_context::context; |
| 7 | + |
| 8 | +use rustix::{ |
| 9 | + fs::{open, Mode, OFlags, CWD}, |
| 10 | + mount::{unmount, UnmountFlags}, |
| 11 | + path::Arg, |
| 12 | +}; |
| 13 | + |
| 14 | +/// Mounts an EROFS image and copies the pristine /etc to the deployment's /etc |
| 15 | +#[context("Copying etc")] |
| 16 | +pub(crate) fn copy_etc_to_state( |
| 17 | + sysroot_path: &Utf8PathBuf, |
| 18 | + erofs_id: &String, |
| 19 | + state_path: &Utf8PathBuf, |
| 20 | +) -> Result<()> { |
| 21 | + let sysroot_fd = open( |
| 22 | + sysroot_path.as_std_path(), |
| 23 | + OFlags::PATH | OFlags::DIRECTORY | OFlags::CLOEXEC, |
| 24 | + Mode::empty(), |
| 25 | + ) |
| 26 | + .context("Opening sysroot")?; |
| 27 | + |
| 28 | + let composefs_fd = bootc_initramfs_setup::mount_composefs_image(&sysroot_fd, &erofs_id, false)?; |
| 29 | + |
| 30 | + let tempdir = tempfile::tempdir().context("Creating tempdir")?; |
| 31 | + |
| 32 | + bootc_initramfs_setup::mount_at_wrapper(composefs_fd, CWD, tempdir.path())?; |
| 33 | + |
| 34 | + // TODO: Replace this with a function to cap_std_ext |
| 35 | + let cp_ret = Command::new("cp") |
| 36 | + .args([ |
| 37 | + "-a", |
| 38 | + &format!("{}/etc/.", tempdir.path().as_str()?), |
| 39 | + &format!("{state_path}/etc/."), |
| 40 | + ]) |
| 41 | + .run_capture_stderr(); |
| 42 | + |
| 43 | + // Unmount regardless of copy succeeding |
| 44 | + unmount(tempdir.path(), UnmountFlags::DETACH).context("Unmounting composefs")?; |
| 45 | + |
| 46 | + cp_ret |
| 47 | +} |
0 commit comments