Skip to content

Commit 023be10

Browse files
Johan-Liebert1jeckersb
authored andcommitted
composefs/install: Copy /etc contents to state
For bind mounting /etc we copy the contents of the EROFS' /etc to the deployment's state directory Mounting the EORFS requires help from the initramfs crate, so we also turn it into a library crate. Signed-off-by: Johan-Liebert1 <[email protected]>
1 parent 24bf572 commit 023be10

File tree

6 files changed

+55
-2
lines changed

6 files changed

+55
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bootc-sysusers = { path = "../sysusers" }
2222
bootc-tmpfiles = { path = "../tmpfiles" }
2323
bootc-utils = { package = "bootc-internal-utils", path = "../utils", version = "0.0.0" }
2424
ostree-ext = { path = "../ostree-ext", features = ["bootc"] }
25+
bootc-initramfs-setup = { path = "../initramfs" }
2526

2627
# Workspace dependencies
2728
anstream = { workspace = true }

crates/lib/src/bootc_composefs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod state;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

crates/lib/src/install.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ use serde::{Deserialize, Serialize};
7777

7878
#[cfg(feature = "install-to-disk")]
7979
use self::baseline::InstallBlockDeviceOpts;
80+
use crate::bootc_composefs::state::copy_etc_to_state;
8081
use crate::boundimage::{BoundImage, ResolvedBoundImage};
8182
use crate::composefs_consts::{
8283
BOOT_LOADER_ENTRIES, COMPOSEFS_CMDLINE, COMPOSEFS_STAGED_DEPLOYMENT_FNAME,
@@ -2247,8 +2248,9 @@ pub(crate) fn write_composefs_state(
22472248
) -> Result<()> {
22482249
let state_path = root_path.join(format!("{STATE_DIR_RELATIVE}/{}", deployment_id.to_hex()));
22492250

2250-
create_dir_all(state_path.join("etc/upper"))?;
2251-
create_dir_all(state_path.join("etc/work"))?;
2251+
create_dir_all(state_path.join("etc"))?;
2252+
2253+
copy_etc_to_state(&root_path, &deployment_id.to_hex(), &state_path)?;
22522254

22532255
let actual_var_path = root_path.join(SHARED_VAR_PATH);
22542256
create_dir_all(&actual_var_path)?;

crates/lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! to provide a fully "container native" tool for using
55
//! bootable container images.
66
7+
mod bootc_composefs;
78
pub(crate) mod bootc_kargs;
89
mod bootloader;
910
mod boundimage;

0 commit comments

Comments
 (0)