Can restore disk image when adding disks to virtual machine
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is failing
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	continuous-integration/drone/push Build is failing
				
			This commit is contained in:
		@@ -255,6 +255,11 @@ impl AppConfig {
 | 
			
		||||
        self.storage_path().join("disk_images")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get the path of a disk image file
 | 
			
		||||
    pub fn disk_images_file_path(&self, name: &str) -> PathBuf {
 | 
			
		||||
        self.disk_images_storage_path().join(name)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Get VM vnc sockets directory
 | 
			
		||||
    pub fn vnc_sockets_path(&self) -> PathBuf {
 | 
			
		||||
        self.storage_path().join("vnc")
 | 
			
		||||
 
 | 
			
		||||
@@ -49,7 +49,7 @@ pub async fn upload(MultipartForm(mut form): MultipartForm<UploadDiskImageForm>)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Check if a file with the same name already exists
 | 
			
		||||
    let dest_path = AppConfig::get().disk_images_storage_path().join(file_name);
 | 
			
		||||
    let dest_path = AppConfig::get().disk_images_file_path(&file_name);
 | 
			
		||||
    if dest_path.is_file() {
 | 
			
		||||
        return Ok(HttpResponse::Conflict().json("A file with the same name already exists!"));
 | 
			
		||||
    }
 | 
			
		||||
@@ -82,9 +82,7 @@ pub async fn download(p: web::Path<DiskFilePath>, req: HttpRequest) -> HttpResul
 | 
			
		||||
        return Ok(HttpResponse::BadRequest().json("Invalid file name!"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let file_path = AppConfig::get()
 | 
			
		||||
        .disk_images_storage_path()
 | 
			
		||||
        .join(&p.filename);
 | 
			
		||||
    let file_path = AppConfig::get().disk_images_file_path(&p.filename);
 | 
			
		||||
 | 
			
		||||
    if !file_path.exists() {
 | 
			
		||||
        return Ok(HttpResponse::NotFound().json("Disk image does not exists!"));
 | 
			
		||||
@@ -109,9 +107,7 @@ pub async fn convert(
 | 
			
		||||
        return Ok(HttpResponse::BadRequest().json("Invalid src file name!"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let src_file_path = AppConfig::get()
 | 
			
		||||
        .disk_images_storage_path()
 | 
			
		||||
        .join(&p.filename);
 | 
			
		||||
    let src_file_path = AppConfig::get().disk_images_file_path(&p.filename);
 | 
			
		||||
 | 
			
		||||
    let src = DiskFileInfo::load_file(&src_file_path)?;
 | 
			
		||||
 | 
			
		||||
@@ -176,9 +172,7 @@ pub async fn handle_convert_request(
 | 
			
		||||
        return Ok(HttpResponse::BadRequest().json("Invalid destination file extension!"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let dst_file_path = AppConfig::get()
 | 
			
		||||
        .disk_images_storage_path()
 | 
			
		||||
        .join(&req.dest_file_name);
 | 
			
		||||
    let dst_file_path = AppConfig::get().disk_images_file_path(&req.dest_file_name);
 | 
			
		||||
 | 
			
		||||
    if dst_file_path.exists() {
 | 
			
		||||
        return Ok(HttpResponse::Conflict().json("Specified destination file already exists!"));
 | 
			
		||||
@@ -201,9 +195,7 @@ pub async fn delete(p: web::Path<DiskFilePath>) -> HttpResult {
 | 
			
		||||
        return Ok(HttpResponse::BadRequest().json("Invalid file name!"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let file_path = AppConfig::get()
 | 
			
		||||
        .disk_images_storage_path()
 | 
			
		||||
        .join(&p.filename);
 | 
			
		||||
    let file_path = AppConfig::get().disk_images_file_path(&p.filename);
 | 
			
		||||
 | 
			
		||||
    if !file_path.exists() {
 | 
			
		||||
        return Ok(HttpResponse::NotFound().json("Disk image does not exists!"));
 | 
			
		||||
 
 | 
			
		||||
@@ -33,6 +33,9 @@ pub struct VMFileDisk {
 | 
			
		||||
    /// Disk format
 | 
			
		||||
    #[serde(flatten)]
 | 
			
		||||
    pub format: VMDiskFormat,
 | 
			
		||||
    /// When creating a new disk, specify the disk image template to use
 | 
			
		||||
    #[serde(skip_serializing_if = "Option::is_none")]
 | 
			
		||||
    pub from_image: Option<String>,
 | 
			
		||||
    /// Set this variable to true to delete the disk
 | 
			
		||||
    pub delete: bool,
 | 
			
		||||
}
 | 
			
		||||
@@ -59,6 +62,7 @@ impl VMFileDisk {
 | 
			
		||||
                _ => anyhow::bail!("Unsupported image format: {:?}", info.format),
 | 
			
		||||
            },
 | 
			
		||||
            delete: false,
 | 
			
		||||
            from_image: None,
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -78,6 +82,19 @@ impl VMFileDisk {
 | 
			
		||||
            return Err(VMDisksError::Config("Disk size is invalid!").into());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Check specified disk image template
 | 
			
		||||
        if let Some(disk_image) = &self.from_image {
 | 
			
		||||
            if !files_utils::check_file_name(disk_image) {
 | 
			
		||||
                return Err(VMDisksError::Config("Disk image template name is not valid!").into());
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if !AppConfig::get().disk_images_file_path(disk_image).is_file() {
 | 
			
		||||
                return Err(
 | 
			
		||||
                    VMDisksError::Config("Specified disk image file does not exist!").into(),
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -115,17 +132,27 @@ impl VMFileDisk {
 | 
			
		||||
            return Ok(());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Create disk file
 | 
			
		||||
        DiskFileInfo::create(
 | 
			
		||||
            &file,
 | 
			
		||||
            match self.format {
 | 
			
		||||
                VMDiskFormat::Raw { is_sparse } => DiskFileFormat::Raw { is_sparse },
 | 
			
		||||
                VMDiskFormat::QCow2 => DiskFileFormat::QCow2 {
 | 
			
		||||
                    virtual_size: self.size,
 | 
			
		||||
                },
 | 
			
		||||
        let format = match self.format {
 | 
			
		||||
            VMDiskFormat::Raw { is_sparse } => DiskFileFormat::Raw { is_sparse },
 | 
			
		||||
            VMDiskFormat::QCow2 => DiskFileFormat::QCow2 {
 | 
			
		||||
                virtual_size: self.size,
 | 
			
		||||
            },
 | 
			
		||||
            self.size,
 | 
			
		||||
        )?;
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        // Create / Restore disk file
 | 
			
		||||
        match &self.from_image {
 | 
			
		||||
            // Create disk file
 | 
			
		||||
            None => {
 | 
			
		||||
                DiskFileInfo::create(&file, format, self.size)?;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Restore disk image template
 | 
			
		||||
            Some(disk_img) => {
 | 
			
		||||
                let src_file =
 | 
			
		||||
                    DiskFileInfo::load_file(&AppConfig::get().disk_images_file_path(disk_img))?;
 | 
			
		||||
                src_file.convert(&file, format)?;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user