vmm: config: Port console parsing to OptionParser

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-04-02 16:52:30 +01:00
parent 143d63c88e
commit 2ae3392d32

View File

@ -45,8 +45,6 @@ pub enum Error {
ParsePmemSizeMissing, ParsePmemSizeMissing,
/// Failed parsing size parameter. /// Failed parsing size parameter.
ParseSizeParam(std::num::ParseIntError), ParseSizeParam(std::num::ParseIntError),
/// Failed parsing console parameter.
ParseConsoleParam,
/// Both console and serial are tty. /// Both console and serial are tty.
ParseTTYParam, ParseTTYParam,
/// Failed parsing vsock context ID parameter. /// Failed parsing vsock context ID parameter.
@ -71,6 +69,12 @@ pub enum Error {
ParseFileSystem(OptionParserError), ParseFileSystem(OptionParserError),
/// Error parsing persistent memorry parameters /// Error parsing persistent memorry parameters
ParsePersistentMemory(OptionParserError), ParsePersistentMemory(OptionParserError),
/// Failed parsing console
ParseConsole(OptionParserError),
/// Missing file value for console
ParseConsoleFileMissing,
/// No mode given for console
ParseConsoleInvalidModeGiven,
} }
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
@ -926,46 +930,38 @@ fn default_consoleconfig_file() -> Option<PathBuf> {
impl ConsoleConfig { impl ConsoleConfig {
pub fn parse(console: &str) -> Result<Self> { pub fn parse(console: &str) -> Result<Self> {
// Split the parameters based on the comma delimiter let mut parser = OptionParser::new();
let params_list: Vec<&str> = console.split(',').collect(); parser
.add_valueless("off")
.add_valueless("tty")
.add_valueless("null")
.add("file")
.add("iommu");
parser.parse(console).map_err(Error::ParseConsole)?;
let mut valid = false;
let mut file: Option<PathBuf> = default_consoleconfig_file(); let mut file: Option<PathBuf> = default_consoleconfig_file();
let mut mode: ConsoleOutputMode = ConsoleOutputMode::Off; let mut mode: ConsoleOutputMode = ConsoleOutputMode::Off;
let mut iommu_str: &str = "";
for param in params_list.iter() { if parser.is_set("off") {
if param.starts_with("iommu=") { } else if parser.is_set("tty") {
iommu_str = &param[6..]; mode = ConsoleOutputMode::Tty
} else { } else if parser.is_set("null") {
if *param == "off" { mode = ConsoleOutputMode::Null
mode = ConsoleOutputMode::Off; } else if parser.is_set("file") {
file = None; mode = ConsoleOutputMode::File;
} else if *param == "tty" { file = Some(PathBuf::from(
mode = ConsoleOutputMode::Tty; parser.get("file").ok_or(Error::ParseConsoleFileMissing)?,
file = None; ));
} else if param.starts_with("file=") { } else {
mode = ConsoleOutputMode::File; return Err(Error::ParseConsoleInvalidModeGiven);
file = Some(PathBuf::from(&param[5..]));
} else if param.starts_with("null") {
mode = ConsoleOutputMode::Null;
file = None;
} else {
return Err(Error::ParseConsoleParam);
}
valid = true;
}
} }
let iommu = parser
.convert::<Toggle>("iommu")
.map_err(Error::ParseConsole)?
.unwrap_or(Toggle(false))
.0;
if !valid { Ok(Self { mode, file, iommu })
return Err(Error::ParseConsoleParam);
}
Ok(Self {
mode,
file,
iommu: parse_on_off(iommu_str)?,
})
} }
pub fn default_serial() -> Self { pub fn default_serial() -> Self {