Can resize disk image when adding a new disk image to a VM
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -394,6 +394,40 @@ impl DiskFileInfo {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get disk virtual size, if available
|
||||
pub fn virtual_size(&self) -> Option<FileSize> {
|
||||
match self.format {
|
||||
DiskFileFormat::Raw { .. } => Some(self.file_size),
|
||||
DiskFileFormat::QCow2 { virtual_size } => Some(virtual_size),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Resize disk
|
||||
pub fn resize(&self, new_size: FileSize) -> anyhow::Result<()> {
|
||||
let mut cmd = Command::new(constants::PROGRAM_QEMU_IMAGE);
|
||||
cmd.arg("resize")
|
||||
.arg("-f")
|
||||
.arg(match self.format {
|
||||
DiskFileFormat::QCow2 { .. } => "qcow2",
|
||||
DiskFileFormat::Raw { .. } => "raw",
|
||||
f => anyhow::bail!("Unsupported disk format for resize: {f:?}"),
|
||||
})
|
||||
.arg(&self.file_path)
|
||||
.arg(new_size.as_bytes().to_string());
|
||||
|
||||
let output = cmd.output()?;
|
||||
if !output.status.success() {
|
||||
anyhow::bail!(
|
||||
"{} info failed, status: {}, stderr: {}",
|
||||
constants::PROGRAM_QEMU_IMAGE,
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
|
Reference in New Issue
Block a user