Skip to content

Commit 40122c9

Browse files
committed
composefs/state: Copy /var from image to fresh installations
Signed-off-by: Chaser Huang <[email protected]>
1 parent 0aae35a commit 40122c9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

crates/lib/src/bootc_composefs/state.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,37 @@ pub(crate) fn copy_etc_to_state(
107107
cp_ret
108108
}
109109

110+
/// Mounts an EROFS image and copies the pristine /var to the deployment's /var
111+
#[context("Copying var")]
112+
pub(crate) fn copy_var_to_state(
113+
sysroot_path: &Utf8PathBuf,
114+
erofs_id: &String,
115+
state_path: &Utf8PathBuf,
116+
) -> Result<()> {
117+
let sysroot_fd = open(
118+
sysroot_path.as_std_path(),
119+
OFlags::PATH | OFlags::DIRECTORY | OFlags::CLOEXEC,
120+
Mode::empty(),
121+
)
122+
.context("Opening sysroot")?;
123+
124+
let composefs_fd = bootc_initramfs_setup::mount_composefs_image(&sysroot_fd, &erofs_id, false)?;
125+
126+
let tempdir = TempMount::mount_fd(composefs_fd)?;
127+
128+
// TODO: Replace this with a function to cap_std_ext
129+
let cp_ret = Command::new("cp")
130+
.args([
131+
"-a",
132+
"--remove-destination",
133+
&format!("{}/var/.", tempdir.dir.path().as_str()?),
134+
&format!("{state_path}/var/."),
135+
])
136+
.run_capture_stderr();
137+
138+
cp_ret
139+
}
140+
110141
/// Adds or updates the provided key/value pairs in the .origin file of the deployment pointed to
111142
/// by the `deployment_id`
112143
fn add_update_in_origin(
@@ -237,6 +268,11 @@ pub(crate) async fn write_composefs_state(
237268
)
238269
.context("Failed to create symlink for /var")?;
239270

271+
// Copy the pristine /var to actual var path if it is a fresh deployment
272+
if !staged {
273+
copy_var_to_state(&root_path, &deployment_id.to_hex(), &state_path)?;
274+
}
275+
240276
let ImageReference {
241277
image: image_name,
242278
transport,

0 commit comments

Comments
 (0)