diff --git a/src/agent/src/image.rs b/src/agent/src/image.rs index ce1f999bec..a06e1ad60d 100644 --- a/src/agent/src/image.rs +++ b/src/agent/src/image.rs @@ -41,6 +41,15 @@ fn sl() -> slog::Logger { slog_scope::logger().new(o!("subsystem" => "image")) } +// Function to copy a file if it does not exist at the destination +fn copy_if_not_exists(src: &Path, dst: &Path) -> Result<()> { + if let Some(dst_dir) = dst.parent() { + fs::create_dir_all(dst_dir)?; + } + fs::copy(src, dst)?; + Ok(()) +} + pub struct ImageService { image_client: ImageClient, images: HashMap, @@ -66,28 +75,47 @@ impl ImageService { if !guest_pause_bundle.exists() { bail!("Pause image not present in rootfs"); } - + let guest_pause_config = scoped_join(guest_pause_bundle, CONFIG_JSON)?; info!(sl(), "use guest pause image cid {:?}", cid); - let pause_bundle = Path::new(CONTAINER_BASE).join(cid).join(target_subpath); - let pause_rootfs = pause_bundle.join("rootfs"); + + let image_oci = oci::Spec::load(guest_pause_config.to_str().ok_or_else(|| { + anyhow!( + "Failed to load the guest pause image config from {:?}", + guest_pause_config + ) + })?) + .context("load image config file")?; + + let image_oci_process = image_oci.process.ok_or_else(|| { + anyhow!("The guest pause image config does not contain a process specification. Please check the pause image.") + })?; + info!( + sl(), + "pause image oci process {:?}", + image_oci_process.clone() + ); + + // Ensure that the args vector is not empty before accessing its elements. + let args = image_oci_process.args; + // Check the number of arguments. + if args.is_empty() { + bail!("The number of args should be greater than or equal to one! Please check the pause image."); + } + + let container_bundle = scoped_join(CONTAINER_BASE, cid)?; + fs::create_dir_all(&container_bundle)?; + let pause_bundle = scoped_join(&container_bundle, target_subpath)?; + fs::create_dir_all(&pause_bundle)?; + let pause_rootfs = scoped_join(&pause_bundle, "rootfs")?; fs::create_dir_all(&pause_rootfs)?; + info!(sl(), "pause_rootfs {:?}", pause_rootfs); - let copy_if_not_exists = |src: &Path, dst: &Path| -> Result<()> { - if !dst.exists() { - info!(sl(), "copying file {src:?} to {dst:?}"); - fs::copy(src, dst)?; - } - Ok(()) - }; + copy_if_not_exists(&guest_pause_config, &pause_bundle.join(CONFIG_JSON))?; + let arg_path = Path::new(&args[0]).strip_prefix("/")?; copy_if_not_exists( - &guest_pause_bundle.join(CONFIG_JSON), - &pause_bundle.join(CONFIG_JSON), + &guest_pause_bundle.join("rootfs").join(arg_path), + &pause_rootfs.join(arg_path), )?; - copy_if_not_exists( - &guest_pause_bundle.join("rootfs/pause"), - &pause_rootfs.join("pause"), - )?; - Ok(pause_rootfs.display().to_string()) }