Can resize disk image when adding a new disk image to a VM
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-06-09 17:42:36 +02:00
parent 9c374f849b
commit 940302a825
4 changed files with 105 additions and 36 deletions

View File

@ -44,6 +44,9 @@ pub struct VMFileDisk {
/// 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 resize disk image
#[serde(skip_serializing_if = "Option::is_none")]
pub resize: Option<bool>,
/// Set this variable to true to delete the disk
pub delete: bool,
}
@ -78,6 +81,7 @@ impl VMFileDisk {
delete: false,
from_image: None,
resize: None,
})
}
@ -144,28 +148,40 @@ impl VMFileDisk {
if file.exists() {
log::debug!("File {file:?} does not exists, so it was not touched");
return Ok(());
}
// Create disk if required
else {
// Determine file format
let format = match self.format {
VMDiskFormat::Raw { is_sparse } => DiskFileFormat::Raw { is_sparse },
VMDiskFormat::QCow2 => DiskFileFormat::QCow2 {
virtual_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)?;
}
}
}
let format = match self.format {
VMDiskFormat::Raw { is_sparse } => DiskFileFormat::Raw { is_sparse },
VMDiskFormat::QCow2 => DiskFileFormat::QCow2 {
virtual_size: self.size,
},
};
// Resize disk file if requested
if self.resize == Some(true) {
let disk = DiskFileInfo::load_file(&file)?;
// 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)?;
// Can only increase disk size
if let Err(e) = disk.resize(self.size) {
log::error!("Failed to resize disk file {}: {e:?}", self.name);
}
}