cli: Accept K,M,G suffixes for size parameters

For every parameter dealing with a size as option, such as memory or
virtio-pmem, the CLI can now parse sizes with the suffixes K, M or G.

Fixes #70

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2019-07-08 20:13:02 -07:00 committed by Rob Bradford
parent 2bb0b22cc1
commit a2947f9a9f
3 changed files with 34 additions and 20 deletions

View File

@ -135,7 +135,7 @@ fn main() {
"Cloud Hypervisor Guest\n\tvCPUs: {}\n\tMemory: {} MB\
\n\tKernel: {:?}\n\tKernel cmdline: {}\n\tDisk(s): {:?}",
u8::from(&vm_config.cpus),
vm_config.memory.size,
vm_config.memory.size >> 20,
vm_config.kernel.path,
vm_config.cmdline.args.as_str(),
vm_config.disks,
@ -294,7 +294,7 @@ mod tests {
let (disks, fw_path) = prepare_files();
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "1"])
.args(&["--memory", "size=512"])
.args(&["--memory", "size=512M"])
.args(&["--kernel", fw_path.as_str()])
.args(&["--disk", disks[0], disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])
@ -322,7 +322,7 @@ mod tests {
let (disks, fw_path) = prepare_files();
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "2"])
.args(&["--memory", "size=512"])
.args(&["--memory", "size=512M"])
.args(&["--kernel", fw_path.as_str()])
.args(&["--disk", disks[0], disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])
@ -347,7 +347,7 @@ mod tests {
let (disks, fw_path) = prepare_files();
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "1"])
.args(&["--memory", "size=5120"])
.args(&["--memory", "size=5120M"])
.args(&["--kernel", fw_path.as_str()])
.args(&["--disk", disks[0], disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])
@ -372,7 +372,7 @@ mod tests {
let (disks, fw_path) = prepare_files();
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "1"])
.args(&["--memory", "size=512"])
.args(&["--memory", "size=512M"])
.args(&["--kernel", fw_path.as_str()])
.args(&["--disk", disks[0], disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])
@ -410,7 +410,7 @@ mod tests {
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "1"])
.args(&["--memory", "size=512"])
.args(&["--memory", "size=512M"])
.args(&["--kernel", kernel_path.to_str().unwrap()])
.args(&["--disk", disks[0], disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])
@ -452,7 +452,7 @@ mod tests {
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "1"])
.args(&["--memory", "size=512"])
.args(&["--memory", "size=512M"])
.args(&["--kernel", kernel_path.to_str().unwrap()])
.args(&["--disk", disks[0], disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])
@ -488,7 +488,7 @@ mod tests {
let (disks, fw_path) = prepare_files();
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "1"])
.args(&["--memory", "size=512"])
.args(&["--memory", "size=512M"])
.args(&["--kernel", fw_path.as_str()])
.args(&["--disk", disks[0], disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])
@ -535,7 +535,7 @@ mod tests {
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "1"])
.args(&["--memory", "size=512,file=/dev/shm"])
.args(&["--memory", "size=512M,file=/dev/shm"])
.args(&["--kernel", kernel_path.to_str().unwrap()])
.args(&["--disk", disks[0], disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])
@ -605,7 +605,7 @@ mod tests {
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "1"])
.args(&["--memory", "size=512"])
.args(&["--memory", "size=512M"])
.args(&["--kernel", kernel_path.to_str().unwrap()])
.args(&["--disk", disks[0], disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])
@ -673,7 +673,7 @@ mod tests {
let mut child = Command::new("target/debug/cloud-hypervisor")
.args(&["--cpus", "1"])
.args(&["--memory", "size=512"])
.args(&["--memory", "size=512M"])
.args(&["--kernel", kernel_path.to_str().unwrap()])
.args(&["--disk", disks[1]])
.args(&["--net", "tap=,mac=,ip=192.168.2.1,mask=255.255.255.0"])

View File

@ -22,8 +22,6 @@ const CMDLINE_OFFSET: GuestAddress = GuestAddress(0x20000);
pub enum Error<'a> {
/// Failed parsing cpus parameters.
ParseCpusParams(std::num::ParseIntError),
/// Failed parsing memory size parameter.
ParseMemorySizeParam(std::num::ParseIntError),
/// Failed parsing memory file parameter.
ParseMemoryFileParam,
/// Failed parsing kernel parameters.
@ -50,8 +48,8 @@ pub enum Error<'a> {
ParseFsQueueSizeParam(std::num::ParseIntError),
/// Failed parsing persitent memory file parameter.
ParsePmemFileParam,
/// Failed parsing persitent memory size parameter.
ParsePmemSizeParam(std::num::ParseIntError),
/// Failed parsing size parameter.
ParseSizeParam(std::num::ParseIntError),
}
pub type Result<'a, T> = result::Result<T, Error<'a>>;
@ -67,6 +65,24 @@ pub struct VmParams<'a> {
pub pmem: Option<Vec<&'a str>>,
}
fn parse_size(size: &str) -> Result<u64> {
let s = size.trim();
let shift = if s.ends_with('K') {
10
} else if s.ends_with('M') {
20
} else if s.ends_with('G') {
30
} else {
0
};
let s = s.trim_end_matches(|c| c == 'K' || c == 'M' || c == 'G');
let res = s.parse::<u64>().map_err(Error::ParseSizeParam)?;
Ok(res << shift)
}
pub struct CpusConfig(pub u8);
impl CpusConfig {
@ -117,9 +133,7 @@ impl<'a> MemoryConfig<'a> {
};
Ok(MemoryConfig {
size: size_str
.parse::<u64>()
.map_err(Error::ParseMemorySizeParam)?,
size: parse_size(size_str)?,
file,
})
}
@ -323,7 +337,7 @@ impl<'a> PmemConfig<'a> {
Ok(PmemConfig {
file: Path::new(file_str),
size: size_str.parse::<u64>().map_err(Error::ParsePmemSizeParam)?,
size: parse_size(size_str)?,
})
}
}

View File

@ -924,7 +924,7 @@ impl<'a> Vm<'a> {
let fd = Arc::new(fd);
// Init guest memory
let arch_mem_regions = arch::arch_memory_regions(config.memory.size << 20);
let arch_mem_regions = arch::arch_memory_regions(config.memory.size);
let guest_memory = match config.memory.file {
Some(file) => {