|
| 1 | +use std::{ |
| 2 | + fs::{create_dir_all, remove_dir_all}, |
| 3 | + process::Command, |
| 4 | +}; |
| 5 | + |
| 6 | +use anyhow::{Context, Result}; |
| 7 | +use camino::Utf8PathBuf; |
| 8 | +use fn_error_context::context; |
| 9 | + |
| 10 | +use bootc_initramfs_setup; |
| 11 | +use rustix::{ |
| 12 | + fs::{open, Mode, OFlags, CWD}, |
| 13 | + mount::{unmount, UnmountFlags}, |
| 14 | +}; |
| 15 | + |
| 16 | +/// Mounts an EROFS image and copies the pristine /etc to the deployment's /etc |
| 17 | +#[context("Copying etc")] |
| 18 | +pub(crate) fn copy_etc_to_state( |
| 19 | + sysroot_path: &Utf8PathBuf, |
| 20 | + erofs_id: &String, |
| 21 | + state_path: &Utf8PathBuf, |
| 22 | +) -> Result<()> { |
| 23 | + let sysroot_fd = open( |
| 24 | + sysroot_path.as_std_path(), |
| 25 | + OFlags::PATH | OFlags::DIRECTORY | OFlags::CLOEXEC, |
| 26 | + Mode::empty(), |
| 27 | + ) |
| 28 | + .context("Opening sysroot")?; |
| 29 | + |
| 30 | + let composefs_fd = bootc_initramfs_setup::mount_composefs_image(&sysroot_fd, &erofs_id, false)?; |
| 31 | + |
| 32 | + let uuid = uuid::Uuid::new_v4(); |
| 33 | + let dir = format!("/var/tmp/{uuid}"); |
| 34 | + |
| 35 | + create_dir_all(&dir).context("Creating temp directory")?; |
| 36 | + |
| 37 | + bootc_initramfs_setup::mount_at_wrapper(composefs_fd, CWD, &dir)?; |
| 38 | + |
| 39 | + let output = Command::new("cp") |
| 40 | + .args([ |
| 41 | + "-a", |
| 42 | + &format!("{dir}/etc/."), |
| 43 | + &format!("{state_path}/etc/."), |
| 44 | + ]) |
| 45 | + .output()?; |
| 46 | + |
| 47 | + // Unmount regardless of copy succeeding |
| 48 | + unmount(&dir, UnmountFlags::DETACH).context("Unmounting composefs")?; |
| 49 | + |
| 50 | + remove_dir_all(dir).context("Removing temp directory")?; |
| 51 | + |
| 52 | + if !output.status.success() { |
| 53 | + anyhow::bail!( |
| 54 | + "Copying /etc failed with status {}: {}", |
| 55 | + output.status, |
| 56 | + String::from_utf8_lossy(&output.stderr) |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + Ok(()) |
| 61 | +} |
0 commit comments