introduce xencl for high-level interaction with xen

This commit is contained in:
Alex Zenla
2024-01-08 23:23:26 -08:00
parent faf8027590
commit c9f61ec72f
6 changed files with 105 additions and 5 deletions

15
xencl/Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "xencl"
version = "0.0.1"
edition = "2021"
resolver = "2"
[dependencies.xenstore]
path = "../xenstore"
[lib]
path = "src/lib.rs"
[[example]]
name = "xencl-simple"
path = "examples/simple.rs"

9
xencl/examples/simple.rs Normal file
View File

@ -0,0 +1,9 @@
use std::collections::HashMap;
use xencl::{XenClient, XenClientError};
fn main() -> Result<(), XenClientError> {
let mut client = XenClient::open()?;
let entries: HashMap<String, String> = HashMap::new();
client.create(2, entries)?;
Ok(())
}

68
xencl/src/lib.rs Normal file
View File

@ -0,0 +1,68 @@
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Display, Formatter};
use xenstore::bus::XsdBusError;
use xenstore::client::{XsdClient, XsdInterface};
pub struct XenClient {
store: XsdClient,
}
#[derive(Debug)]
pub struct XenClientError {
message: String,
}
impl XenClientError {
pub fn new(msg: &str) -> XenClientError {
XenClientError {
message: msg.to_string(),
}
}
}
impl Display for XenClientError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl Error for XenClientError {
fn description(&self) -> &str {
&self.message
}
}
impl From<std::io::Error> for XenClientError {
fn from(value: std::io::Error) -> Self {
XenClientError::new(value.to_string().as_str())
}
}
impl From<XsdBusError> for XenClientError {
fn from(value: XsdBusError) -> Self {
XenClientError::new(value.to_string().as_str())
}
}
impl XenClient {
pub fn open() -> Result<XenClient, XenClientError> {
let store = XsdClient::open()?;
Ok(XenClient { store })
}
pub fn create(
&mut self,
domid: u32,
entries: HashMap<String, String>,
) -> Result<(), XenClientError> {
let domain = self.store.get_domain_path(domid)?;
let mut tx = self.store.transaction()?;
for (key, value) in entries {
let path = format!("{}/{}", domain, key);
tx.write(path.as_str(), value.into_bytes())?;
}
tx.commit()?;
Ok(())
}
}