mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-02 12:50:54 +00:00
hypha-agent: command line arguments
This commit is contained in:
parent
8af6e25edc
commit
f69ce96054
@ -10,6 +10,10 @@ path = "../xenclient"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
|
|
||||||
|
[dependencies.clap]
|
||||||
|
version = "4.4.18"
|
||||||
|
features = ["derive"]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
@ -1,8 +1,26 @@
|
|||||||
|
use clap::Parser;
|
||||||
use hypha::agent::Agent;
|
use hypha::agent::Agent;
|
||||||
use hypha::error::Result;
|
use hypha::error::Result;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(version, about)]
|
||||||
|
struct AgentArgs {
|
||||||
|
#[arg(short, long)]
|
||||||
|
kernel: String,
|
||||||
|
|
||||||
|
#[arg(short, long)]
|
||||||
|
initrd: String,
|
||||||
|
|
||||||
|
#[arg(short, long, default_value_t = 1)]
|
||||||
|
cpus: u32,
|
||||||
|
|
||||||
|
#[arg(short, long, default_value_t = 512)]
|
||||||
|
mem: u64,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut agent = Agent::new()?;
|
let args = AgentArgs::parse();
|
||||||
|
let mut agent = Agent::new(args.kernel, args.initrd, args.cpus, args.mem)?;
|
||||||
let domid = agent.launch()?;
|
let domid = agent.launch()?;
|
||||||
println!("launched domain: {}", domid);
|
println!("launched domain: {}", domid);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -1,49 +1,34 @@
|
|||||||
use crate::error::{HyphaError, Result};
|
use crate::error::Result;
|
||||||
use std::fs::{read_dir, DirEntry};
|
use xenclient::{DomainConfig, XenClient};
|
||||||
use xenclient::create::DomainConfig;
|
|
||||||
use xenclient::XenClient;
|
|
||||||
|
|
||||||
pub struct Agent {
|
pub struct Agent {
|
||||||
client: XenClient,
|
client: XenClient,
|
||||||
|
kernel_path: String,
|
||||||
|
initrd_path: String,
|
||||||
|
vcpus: u32,
|
||||||
|
mem: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Agent {
|
impl Agent {
|
||||||
pub fn new() -> Result<Agent> {
|
pub fn new(kernel_path: String, initrd_path: String, vcpus: u32, mem: u64) -> Result<Agent> {
|
||||||
let client = XenClient::open()?;
|
let client = XenClient::open()?;
|
||||||
Ok(Agent { client })
|
Ok(Agent {
|
||||||
|
client,
|
||||||
|
kernel_path,
|
||||||
|
initrd_path,
|
||||||
|
vcpus,
|
||||||
|
mem,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn launch(&mut self) -> Result<u32> {
|
pub fn launch(&mut self) -> Result<u32> {
|
||||||
let kernel_path = self.find_boot_path("vmlinuz-")?;
|
|
||||||
let initrd_path = self.find_boot_path("initrd.img-")?;
|
|
||||||
|
|
||||||
let config = DomainConfig {
|
let config = DomainConfig {
|
||||||
max_vcpus: 1,
|
max_vcpus: self.vcpus,
|
||||||
mem_mb: 512,
|
mem_mb: self.mem,
|
||||||
kernel_path,
|
kernel_path: self.kernel_path.as_str(),
|
||||||
initrd_path,
|
initrd_path: self.initrd_path.as_str(),
|
||||||
cmdline: "debug elevator=noop".to_string(),
|
cmdline: "debug elevator=noop",
|
||||||
};
|
};
|
||||||
Ok(self.client.create(config)?)
|
Ok(self.client.create(&config)?)
|
||||||
}
|
|
||||||
|
|
||||||
fn find_boot_path(&self, prefix: &str) -> Result<String> {
|
|
||||||
let vmlinuz = read_dir("/boot")?
|
|
||||||
.filter_map(|x| x.ok())
|
|
||||||
.filter(|x| {
|
|
||||||
x.file_name()
|
|
||||||
.to_str()
|
|
||||||
.ok_or(HyphaError::new("invalid direntry"))
|
|
||||||
.map(|x| x.starts_with(prefix))
|
|
||||||
.unwrap_or(false)
|
|
||||||
})
|
|
||||||
.collect::<Vec<DirEntry>>();
|
|
||||||
Ok(vmlinuz
|
|
||||||
.first()
|
|
||||||
.ok_or(HyphaError::new("unable to find suitable image"))?
|
|
||||||
.path()
|
|
||||||
.to_str()
|
|
||||||
.ok_or(HyphaError::new("invalid direntry"))?
|
|
||||||
.to_string())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use std::{env, process};
|
use std::{env, process};
|
||||||
use xenclient::create::DomainConfig;
|
use xenclient::{DomainConfig, XenClient, XenClientError};
|
||||||
use xenclient::{XenClient, XenClientError};
|
|
||||||
|
|
||||||
fn main() -> Result<(), XenClientError> {
|
fn main() -> Result<(), XenClientError> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
@ -16,11 +15,11 @@ fn main() -> Result<(), XenClientError> {
|
|||||||
let config = DomainConfig {
|
let config = DomainConfig {
|
||||||
max_vcpus: 1,
|
max_vcpus: 1,
|
||||||
mem_mb: 512,
|
mem_mb: 512,
|
||||||
kernel_path: kernel_image_path.to_string(),
|
kernel_path: kernel_image_path.as_str(),
|
||||||
initrd_path: initrd_path.to_string(),
|
initrd_path: initrd_path.as_str(),
|
||||||
cmdline: "debug elevator=noop".to_string(),
|
cmdline: "debug elevator=noop",
|
||||||
};
|
};
|
||||||
let domid = client.create(config)?;
|
let domid = client.create(&config)?;
|
||||||
println!("created domain {}", domid);
|
println!("created domain {}", domid);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,92 +0,0 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
pub struct DomainConfig {
|
|
||||||
pub max_vcpus: u32,
|
|
||||||
pub mem_mb: u64,
|
|
||||||
pub kernel_path: String,
|
|
||||||
pub initrd_path: String,
|
|
||||||
pub cmdline: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct PvDomainStore {
|
|
||||||
kernel: String,
|
|
||||||
ramdisk: Option<String>,
|
|
||||||
cmdline: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DomainStore {
|
|
||||||
vm_entries: HashMap<String, String>,
|
|
||||||
domain_entries: HashMap<String, String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DomainStore {
|
|
||||||
pub fn new() -> DomainStore {
|
|
||||||
DomainStore {
|
|
||||||
vm_entries: HashMap::new(),
|
|
||||||
domain_entries: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn put_vm(&mut self, key: &str, value: String) {
|
|
||||||
self.vm_entries.insert(key.to_string(), value);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn put_vm_str(&mut self, key: &str, value: &str) {
|
|
||||||
self.put_vm(key, value.to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn put_domain(&mut self, key: &str, value: String) {
|
|
||||||
self.vm_entries.insert(key.to_string(), value);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn put_domain_str(&mut self, key: &str, value: &str) {
|
|
||||||
self.put_domain(key, value.to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn configure_memory(&mut self, maxkb: u32, targetkb: u32, videokb: u32) {
|
|
||||||
self.put_domain("memory/static-max", maxkb.to_string());
|
|
||||||
self.put_domain("memory/target", targetkb.to_string());
|
|
||||||
self.put_domain("memory/videoram", videokb.to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn configure_cpus(&mut self, _maxvcpus: u32) {}
|
|
||||||
|
|
||||||
pub fn configure_pv(&mut self, pv: PvDomainStore) {
|
|
||||||
self.put_vm_str("image/ostype", "linux");
|
|
||||||
self.put_vm("image/kernel", pv.kernel);
|
|
||||||
|
|
||||||
match pv.ramdisk {
|
|
||||||
None => {}
|
|
||||||
Some(ramdisk) => self.put_vm("image/ramdisk", ramdisk),
|
|
||||||
}
|
|
||||||
|
|
||||||
match pv.cmdline {
|
|
||||||
None => {}
|
|
||||||
Some(cmdline) => self.put_vm("image/cmdline", cmdline),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn clone_vm_entries(&self) -> HashMap<String, String> {
|
|
||||||
self.vm_entries.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn clone_domain_entries(&self) -> HashMap<String, String> {
|
|
||||||
self.domain_entries.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for DomainStore {
|
|
||||||
fn default() -> Self {
|
|
||||||
DomainStore::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PvDomainStore {
|
|
||||||
pub fn new(kernel: String, ramdisk: Option<String>, cmdline: Option<String>) -> PvDomainStore {
|
|
||||||
PvDomainStore {
|
|
||||||
kernel,
|
|
||||||
ramdisk,
|
|
||||||
cmdline,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +1,10 @@
|
|||||||
pub mod boot;
|
pub mod boot;
|
||||||
pub mod create;
|
|
||||||
pub mod elfloader;
|
pub mod elfloader;
|
||||||
pub mod mem;
|
pub mod mem;
|
||||||
pub mod sys;
|
pub mod sys;
|
||||||
mod x86;
|
mod x86;
|
||||||
|
|
||||||
use crate::boot::BootSetup;
|
use crate::boot::BootSetup;
|
||||||
use crate::create::DomainConfig;
|
|
||||||
use crate::elfloader::ElfImageLoader;
|
use crate::elfloader::ElfImageLoader;
|
||||||
use crate::x86::X86BootSetup;
|
use crate::x86::X86BootSetup;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
@ -80,6 +78,14 @@ impl From<EventChannelError> for XenClientError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct DomainConfig<'a> {
|
||||||
|
pub max_vcpus: u32,
|
||||||
|
pub mem_mb: u64,
|
||||||
|
pub kernel_path: &'a str,
|
||||||
|
pub initrd_path: &'a str,
|
||||||
|
pub cmdline: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
impl XenClient {
|
impl XenClient {
|
||||||
pub fn open() -> Result<XenClient, XenClientError> {
|
pub fn open() -> Result<XenClient, XenClientError> {
|
||||||
let store = XsdClient::open()?;
|
let store = XsdClient::open()?;
|
||||||
@ -87,7 +93,7 @@ impl XenClient {
|
|||||||
Ok(XenClient { store, call })
|
Ok(XenClient { store, call })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create(&mut self, config: DomainConfig) -> Result<u32, XenClientError> {
|
pub fn create(&mut self, config: &DomainConfig) -> Result<u32, XenClientError> {
|
||||||
let domain = CreateDomain {
|
let domain = CreateDomain {
|
||||||
max_vcpus: config.max_vcpus,
|
max_vcpus: config.max_vcpus,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -99,9 +105,7 @@ impl XenClient {
|
|||||||
let libxl_path = format!("/libxl/{}", domid);
|
let libxl_path = format!("/libxl/{}", domid);
|
||||||
|
|
||||||
let ro_perm = XsPermissions { id: 0, perms: 0 };
|
let ro_perm = XsPermissions { id: 0, perms: 0 };
|
||||||
|
|
||||||
let rw_perm = XsPermissions { id: 0, perms: 0 };
|
let rw_perm = XsPermissions { id: 0, perms: 0 };
|
||||||
|
|
||||||
let no_perm = XsPermissions { id: 0, perms: 0 };
|
let no_perm = XsPermissions { id: 0, perms: 0 };
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -156,7 +160,7 @@ impl XenClient {
|
|||||||
|
|
||||||
self.call.set_max_vcpus(domid, config.max_vcpus)?;
|
self.call.set_max_vcpus(domid, config.max_vcpus)?;
|
||||||
self.call.set_max_mem(domid, config.mem_mb * 1024)?;
|
self.call.set_max_mem(domid, config.mem_mb * 1024)?;
|
||||||
let image_loader = ElfImageLoader::load_file_kernel(config.kernel_path.as_str())?;
|
let image_loader = ElfImageLoader::load_file_kernel(config.kernel_path)?;
|
||||||
|
|
||||||
let console_evtchn: u32;
|
let console_evtchn: u32;
|
||||||
let xenstore_evtchn: u32;
|
let xenstore_evtchn: u32;
|
||||||
@ -166,7 +170,7 @@ impl XenClient {
|
|||||||
{
|
{
|
||||||
let mut boot = BootSetup::new(&self.call, domid);
|
let mut boot = BootSetup::new(&self.call, domid);
|
||||||
let mut arch = X86BootSetup::new();
|
let mut arch = X86BootSetup::new();
|
||||||
let initrd = read(config.initrd_path.as_str())?;
|
let initrd = read(config.initrd_path)?;
|
||||||
let mut state = boot.initialize(
|
let mut state = boot.initialize(
|
||||||
&mut arch,
|
&mut arch,
|
||||||
&image_loader,
|
&image_loader,
|
||||||
@ -174,7 +178,7 @@ impl XenClient {
|
|||||||
config.max_vcpus,
|
config.max_vcpus,
|
||||||
config.mem_mb,
|
config.mem_mb,
|
||||||
)?;
|
)?;
|
||||||
boot.boot(&mut arch, &mut state, config.cmdline.as_str())?;
|
boot.boot(&mut arch, &mut state, config.cmdline)?;
|
||||||
console_evtchn = state.console_evtchn;
|
console_evtchn = state.console_evtchn;
|
||||||
xenstore_evtchn = state.store_evtchn;
|
xenstore_evtchn = state.store_evtchn;
|
||||||
console_mfn = boot.phys.p2m[state.console_segment.pfn as usize];
|
console_mfn = boot.phys.p2m[state.console_segment.pfn as usize];
|
||||||
@ -186,15 +190,15 @@ impl XenClient {
|
|||||||
tx.write_string(format!("{}/image/os_type", vm_path).as_str(), "linux")?;
|
tx.write_string(format!("{}/image/os_type", vm_path).as_str(), "linux")?;
|
||||||
tx.write_string(
|
tx.write_string(
|
||||||
format!("{}/image/kernel", vm_path).as_str(),
|
format!("{}/image/kernel", vm_path).as_str(),
|
||||||
&config.kernel_path,
|
config.kernel_path,
|
||||||
)?;
|
)?;
|
||||||
tx.write_string(
|
tx.write_string(
|
||||||
format!("{}/image/ramdisk", vm_path).as_str(),
|
format!("{}/image/ramdisk", vm_path).as_str(),
|
||||||
&config.initrd_path,
|
config.initrd_path,
|
||||||
)?;
|
)?;
|
||||||
tx.write_string(
|
tx.write_string(
|
||||||
format!("{}/image/cmdline", vm_path).as_str(),
|
format!("{}/image/cmdline", vm_path).as_str(),
|
||||||
&config.cmdline,
|
config.cmdline,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
tx.write_string(
|
tx.write_string(
|
||||||
|
Loading…
Reference in New Issue
Block a user