Can restore disk image when adding disks to virtual machine
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2025-05-30 14:41:48 +02:00
parent ec9492c933
commit 1d4af8c74e
8 changed files with 132 additions and 40 deletions

View File

@ -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(())
}