async fn stage_image_files(
        &self,
        instance_name: &str,
        guest_config: &GuestConfig,
        device_config: &DeviceConfig,
        config: &FfxConfigWrapper,
    ) -> Result<GuestConfig> {
        let mut updated_guest = guest_config.clone();

        // Create the data directory if needed.
        let mut instance_root: PathBuf = config.file(config::EMU_INSTANCE_ROOT_DIR).await?;
        instance_root.push(instance_name);
        fs::create_dir_all(&instance_root)?;

        let kernel_name = guest_config
            .kernel_image
            .file_name()
            .ok_or(anyhow!("cannot read kernel file name '{:?}'", guest_config.kernel_image));
        let kernel_path = instance_root.join(kernel_name?);
        if kernel_path.exists() {
            log::debug!("Using existing file for {:?}", kernel_path.file_name().unwrap());
        } else {
            fs::copy(&guest_config.kernel_image, &kernel_path).expect("cannot stage kernel file");
        }

        let zbi_path = instance_root
            .join(guest_config.zbi_image.file_name().ok_or(anyhow!("cannot read zbi file name"))?);

        if zbi_path.exists() {
            log::debug!("Using existing file for {:?}", zbi_path.file_name().unwrap());
        } else {
            // Add the authorized public keys to the zbi image to enable SSH access to
            // the guest.
            Self::embed_authorized_keys(&guest_config.zbi_image, &zbi_path, config)
                .await
                .expect("cannot embed authorized keys");
        }

        let fvm_path = match &guest_config.fvm_image {
            Some(src_fvm) => {
                let fvm_path = instance_root
                    .join(src_fvm.file_name().ok_or(anyhow!("cannot read fvm file name"))?);
                if fvm_path.exists() {
                    log::debug!("Using existing file for {:?}", fvm_path.file_name().unwrap());
                } else {
                    fs::copy(src_fvm, &fvm_path).expect("cannot stage fvm file");

                    // Resize the fvm image if needed.
                    let image_size = format!(
                        "{}{}",
                        device_config.storage.quantity,
                        device_config.storage.units.abbreviate()
                    );
                    let fvm_tool = config
                        .get_host_tool(config::FVM_HOST_TOOL)
                        .await
                        .expect("cannot locate fvm tool");
                    let resize_result = Command::new(fvm_tool)
                        .arg(&fvm_path)
                        .arg("extend")
                        .arg("--length")
                        .arg(&image_size)
                        .output()?;

                    if !resize_result.status.success() {
                        bail!("Error resizing fvm: {}", str::from_utf8(&resize_result.stderr)?);
                    }
                }
                Some(fvm_path)
            }
            None => None,
        };

        updated_guest.kernel_image = kernel_path;
        updated_guest.zbi_image = zbi_path;
        updated_guest.fvm_image = fvm_path;
        Ok(updated_guest)
    }
/// Stages the source image files in an instance specific directory.
/// Also resizes the fvms to the desired size and adds the authorized
/// keys to the zbi.
/// Returns an updated GuestConfig instance with the file paths set to
/// the instance paths.
Stages the source image files in an instance specific directory.
Also resizes the fvms to the desired size and adds the authorized
keys to the zbi.
Returns an updated GuestConfig instance with the file paths set to
the instance paths.
Stages the source image files in an instance specific directory . Also resizes the fvms to the desired size and adds the authorized keys to the zbi . Returns an updated GuestConfig instance with the file paths set to the instance paths .
async fn stage_image_files(
        &self,
        instance_name: &str,
        guest_config: &GuestConfig,
        device_config: &DeviceConfig,
        config: &FfxConfigWrapper,
    ) -> Result<GuestConfig> {
        let mut updated_guest = guest_config.clone();
        // Create the data directory if needed.
        let mut instance_root: PathBuf = config.file(config::EMU_INSTANCE_ROOT_DIR).await?;
        instance_root.push(instance_name);
        fs::create_dir_all(&instance_root)?;
        let kernel_name = guest_config
            .kernel_image
            .file_name()
            .ok_or(anyhow!("cannot read kernel file name '{:?}'", guest_config.kernel_image));
        let kernel_path = instance_root.join(kernel_name?);
        if kernel_path.exists() {
            log::debug!("Using existing file for {:?}", kernel_path.file_name().unwrap());
        } else {
            fs::copy(&guest_config.kernel_image, &kernel_path).expect("cannot stage kernel file");
        }
        let zbi_path = instance_root
            .join(guest_config.zbi_image.file_name().ok_or(anyhow!("cannot read zbi file name"))?);
        if zbi_path.exists() {
            log::debug!("Using existing file for {:?}", zbi_path.file_name().unwrap());
        } else {
            // Add the authorized public keys to the zbi image to enable SSH access to
            // the guest.
            Self::embed_authorized_keys(&guest_config.zbi_image, &zbi_path, config)
                .await
                .expect("cannot embed authorized keys");
        }
        let fvm_path = match &guest_config.fvm_image {
            Some(src_fvm) => {
                let fvm_path = instance_root
                    .join(src_fvm.file_name().ok_or(anyhow!("cannot read fvm file name"))?);
                if fvm_path.exists() {
                    log::debug!("Using existing file for {:?}", fvm_path.file_name().unwrap());
                } else {
                    fs::copy(src_fvm, &fvm_path).expect("cannot stage fvm file");
                    // Resize the fvm image if needed.
                    let image_size = format!(
                        "{}{}",
                        device_config.storage.quantity,
                        device_config.storage.units.abbreviate()
                    );
                    let fvm_tool = config
                        .get_host_tool(config::FVM_HOST_TOOL)
                        .await
                        .expect("cannot locate fvm tool");
                    let resize_result = Command::new(fvm_tool)
                        .arg(&fvm_path)
                        .arg("extend")
                        .arg("--length")
                        .arg(&image_size)
                        .output()?;
                    if !resize_result.status.success() {
                        bail!("Error resizing fvm: {}", str::from_utf8(&resize_result.stderr)?);
                    }
                }
                Some(fvm_path)
            }
            None => None,
        };
        updated_guest.kernel_image = kernel_path;
        updated_guest.zbi_image = zbi_path;
        updated_guest.fvm_image = fvm_path;
        Ok(updated_guest)
    }
async fn stage_image_files ( & self , instance_name : & str , guest_config : & GuestConfig , device_config : & DeviceConfig , config : & FfxConfigWrapper , ) -> Result < GuestConfig > { let mut updated_guest = guest_config . clone ( ) ; // Create the data directory if needed. let mut instance_root : PathBuf = config . file ( config :: EMU_INSTANCE_ROOT_DIR ) . await ? ; instance_root . push ( instance_name ) ; fs :: create_dir_all ( & instance_root ) ? ; let kernel_name = guest_config . kernel_image . file_name ( ) . ok_or ( anyhow ! ( " " guest_config kernel_image ) ) ; let kernel_path = instance_root . join ( kernel_name ? ) ; if kernel_path . exists ( ) { log :: debug ! ( " " kernel_path file_name ( ) unwrap ( ) ) ; } else { fs :: copy ( & guest_config . kernel_image , & kernel_path ) . expect ( " " ) ; } let zbi_path = instance_root . join ( guest_config . zbi_image . file_name ( ) . ok_or ( anyhow ! ( " " ) ) ? ) ; if zbi_path . exists ( ) { log :: debug ! ( " " zbi_path file_name ( ) unwrap ( ) ) ; } else { // Add the authorized public keys to the zbi image to enable SSH access to // the guest. Self :: embed_authorized_keys ( & guest_config . zbi_image , & zbi_path , config ) . await . expect ( " " ) ; } let fvm_path = match & guest_config . fvm_image { Some ( src_fvm ) => { let fvm_path = instance_root . join ( src_fvm . file_name ( ) . ok_or ( anyhow ! ( " " ) ) ? ) ; if fvm_path . exists ( ) { log :: debug ! ( " " fvm_path file_name ( ) unwrap ( ) ) ; } else { fs :: copy ( src_fvm , & fvm_path ) . expect ( " " ) ; // Resize the fvm image if needed. let image_size = format ! ( " " device_config storage quantity device_config storage units abbreviate ( ) ) ; let fvm_tool = config . get_host_tool ( config :: FVM_HOST_TOOL ) . await . expect ( " " ) ; let resize_result = Command :: new ( fvm_tool ) . arg ( & fvm_path ) . arg ( " " ) . arg ( " " ) . arg ( & image_size ) . output ( ) ? ; if ! resize_result . status . success ( ) { bail ! ( " " str from_utf8 ( resize_result stderr ) ) ; } } Some ( fvm_path ) } None => None , } ; updated_guest . kernel_image = kernel_path ; updated_guest . zbi_image = zbi_path ; updated_guest . fvm_image = fvm_path ; Ok ( updated_guest ) }
Stages the source image files in an instance specific directory.
Stages the source image files in an instance specific directory .
&self
instance_name
&str
guest_config
&GuestConfig
device_config
&DeviceConfig
config
&FfxConfigWrapper
&self
instance_name
&str
guest_config
&GuestConfig
device_config
&DeviceConfig
config
&FfxConfigWrapper