Process size of disks in bytes instead of megabytes
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Pierre HUBERT 2025-05-27 21:29:16 +02:00
parent 19ec9992be
commit 5a5450070a
3 changed files with 30 additions and 18 deletions

View File

@ -46,11 +46,11 @@ pub const DISK_NAME_MIN_LEN: usize = 2;
/// Disk name max length
pub const DISK_NAME_MAX_LEN: usize = 10;
/// Disk size min (MB)
pub const DISK_SIZE_MIN: usize = 100;
/// Disk size min (B)
pub const DISK_SIZE_MIN: usize = 100 * 1000 * 1000;
/// Disk size max (MB)
pub const DISK_SIZE_MAX: usize = 1000 * 1000 * 2;
/// Disk size max (B)
pub const DISK_SIZE_MAX: usize = 1000 * 1000 * 1000 * 1000 * 2;
/// Net nat entry comment max size
pub const NET_NAT_COMMENT_MAX_SIZE: usize = 250;

View File

@ -40,7 +40,7 @@ pub enum DiskFormat {
pub struct FileDisk {
/// Disk name
pub name: String,
/// Disk size, in megabytes
/// Disk size, in bytes
pub size: usize,
/// Disk format
#[serde(flatten)]
@ -141,8 +141,10 @@ impl FileDisk {
.arg("bs=1M");
match alloc_type {
DiskAllocType::Fixed => cmd.arg(format!("count={}", self.size)),
DiskAllocType::Sparse => cmd.arg(format!("seek={}", self.size)).arg("count=0"),
DiskAllocType::Fixed => cmd.arg(format!("count={}", self.size_mb())),
DiskAllocType::Sparse => {
cmd.arg(format!("seek={}", self.size_mb())).arg("count=0")
}
};
cmd.output()?
@ -154,7 +156,7 @@ impl FileDisk {
.arg("-f")
.arg("qcow2")
.arg(file)
.arg(format!("{}M", self.size));
.arg(format!("{}M", self.size_mb()));
cmd.output()?
}
@ -172,6 +174,11 @@ impl FileDisk {
Ok(())
}
/// Get the size of file disk in megabytes
pub fn size_mb(&self) -> usize {
self.size / (1000 * 1000)
}
}
#[derive(Debug, serde::Serialize)]
@ -211,7 +218,7 @@ impl DiskFileInfo {
// Determine file format
let format = match ext {
"qcow2" => DiskFileFormat::QCow2 {
virtual_size: qcow_virt_size(file)? / (1000 * 1000),
virtual_size: qcow_virt_size(file)?,
},
"raw" => DiskFileFormat::Raw {
is_sparse: metadata.len() / 512 >= metadata.st_blocks(),
@ -226,7 +233,7 @@ impl DiskFileInfo {
Ok(Self {
name,
file_size: metadata.len() as usize / (1000 * 1000),
file_size: metadata.len() as usize,
format,
file_name: file
.file_name()

View File

@ -27,7 +27,7 @@ export function VMDisksList(p: {
const addNewDisk = () => {
p.vm.file_disks.push({
format: "QCow2",
size: 10000,
size: 10000 * 1000 * 1000,
delete: false,
name: `disk${p.vm.file_disks.length}`,
new: true,
@ -125,9 +125,9 @@ function DiskInfo(p: {
)}
</>
}
secondary={`${filesize(p.disk.size * 1000 * 1000)} - ${
p.disk.format
}${p.disk.format == "Raw" ? " - " + p.disk.alloc_type : ""}`}
secondary={`${filesize(p.disk.size)} - ${p.disk.format}${
p.disk.format == "Raw" ? " - " + p.disk.alloc_type : ""
}`}
/>
</ListItem>
);
@ -153,11 +153,16 @@ function DiskInfo(p: {
<TextInput
editable={true}
label="Disk size (MB)"
size={ServerApi.Config.constraints.disk_size}
value={p.disk.size.toString()}
label="Disk size (GB)"
size={{
min:
ServerApi.Config.constraints.disk_size.min / (1000 * 1000 * 1000),
max:
ServerApi.Config.constraints.disk_size.max / (1000 * 1000 * 1000),
}}
value={(p.disk.size / (1000 * 1000 * 1000)).toString()}
onValueChange={(v) => {
p.disk.size = Number(v ?? "0");
p.disk.size = Number(v ?? "0") * 1000 * 1000 * 1000;
p.onChange?.();
}}
type="number"