Mercurial > hg > Members > anatofuz > ie-virsh
changeset 42:2787ada7650b
fix rental mode
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Sun, 22 Nov 2020 17:42:50 +0900 |
parents | 63e77a9de3ab |
children | 98138c3f00f0 |
files | src/command.rs src/virsh.rs src/xml.rs |
diffstat | 3 files changed, 22 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/src/command.rs Sun Nov 22 16:48:05 2020 +0900 +++ b/src/command.rs Sun Nov 22 17:42:50 2020 +0900 @@ -4,6 +4,8 @@ use std::io; +const RENTAL_DIR: &str = "/ie-ryukyu/kvm/images/rental/"; + pub fn list(user_name: &str) { let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name); println!("{}", ldump_msg.info_msg); @@ -43,6 +45,7 @@ }; let builder = xml::Builder::new(&user_detail, &vm_name) .backing(&backing_path) + .qcow2_dir(RENTAL_DIR) .finalize(); let xml_path = match builder.generate() { Ok(xml_path) => xml_path, @@ -52,7 +55,8 @@ } }; virsh::command_require_vm_name(&xml_path, "define"); - virsh::generate_qemu_from_template(&vm_name, "hoge"); + let vm_path = format!("{}{}.qcow2", RENTAL_DIR, &vm_name); + virsh::generate_qemu_from_template(&vm_path, &backing_path); } pub fn start(user_name: &str, vm_name: &str) {
--- a/src/virsh.rs Sun Nov 22 16:48:05 2020 +0900 +++ b/src/virsh.rs Sun Nov 22 17:42:50 2020 +0900 @@ -5,7 +5,6 @@ use std::path::Path; const TEMPLATE_DIR: &str = "/ie-ryukyu/kvm/images/templates/"; -const RENTAL_DIR: &str = "/ie-ryukyu/kvm/images/rental/"; const TEMPLATE_SUFFIX: &str = "template-"; const TEMPLATE_FILE_EXTENSION: &str = ".qcow2"; const QCOW2: &str = "qcow2"; @@ -69,8 +68,8 @@ io::stderr().write_all(&output.stderr).unwrap(); } -pub fn generate_qemu_from_template(vm_name: &str, template_path: &str) { - let vm_path = format!("{}{}{}", RENTAL_DIR, &vm_name, TEMPLATE_FILE_EXTENSION); +pub fn generate_qemu_from_template(vm_path: &str, template_path: &str) { + //let vm_path = format!("{}{}{}", rental_dir, &vm_name, TEMPLATE_FILE_EXTENSION); //$qemu-img create -F qcow2 -b ibm-master.qcow2 -f qcow2 ibm-02.qcow2 let output = Command::new("qemu-img") .arg("create") @@ -82,7 +81,7 @@ .arg(QCOW2) .arg(vm_path) .output() - .unwrap_or_else(|_| panic!("failed to generate {}", &vm_name)); + .unwrap_or_else(|_| panic!("failed to generate {}", &vm_path)); io::stdout().write_all(&output.stdout).unwrap(); io::stderr().write_all(&output.stderr).unwrap(); }
--- a/src/xml.rs Sun Nov 22 16:48:05 2020 +0900 +++ b/src/xml.rs Sun Nov 22 17:42:50 2020 +0900 @@ -51,7 +51,6 @@ } pub struct Builder { - user_name: String, vm_name: String, debug_tcp_port: Option<u64>, backing_file: String, @@ -74,20 +73,15 @@ pub fn new(user_detail: &user::UserDetail, vm_name: &str) -> Builder { let xml_dir = format!( "{}/{}/{}", - LIBVIRT_XML_DIR, user_detail.affilication, &user_detail.user.name + LIBVIRT_XML_DIR, &user_detail.affilication, &user_detail.user.name ); - if !Path::new(&xml_dir).exists() { - fs::create_dir_all(xml_dir).ok(); - } - let qcow2_dir = format!( "{}/{}/{}", - QCOW2_PATH, user_detail.affilication, &user_detail.user.name + QCOW2_PATH, &user_detail.affilication, &user_detail.user.name ); Builder { - user_name: user_detail.user.name.clone(), vm_name: vm_name.to_string(), debug_tcp_port: None, backing_file: "".to_string(), @@ -97,6 +91,16 @@ } } + pub fn xml_dir(&mut self, xml_dir: &str) -> &mut Builder { + self.xml_dir = xml_dir.to_string(); + self + } + + pub fn qcow2_dir(&mut self, qcow2_dir: &str) -> &mut Builder { + self.qcow2_dir = qcow2_dir.to_string(); + self + } + pub fn debug_tcp_port(&mut self, port: Option<u64>) -> &mut Builder { self.debug_tcp_port = port; self @@ -108,13 +112,13 @@ } pub fn finalize(&self) -> GenerateVM { - let xml_path = format!("{}/{}.xml", &self.xml_dir, self.vm_name); + let xml_path = format!("{}/{}.xml", &self.xml_dir, &self.vm_name); if !Path::new(&self.xml_dir).exists() { fs::create_dir_all(&self.xml_dir).ok(); } - let qcow2_path = format!("{}/{}.qcow2", &self.qcow2_dir, self.vm_name); + let qcow2_path = format!("{}/{}.qcow2", &self.qcow2_dir, &self.vm_name); if !Path::new(&self.qcow2_dir).exists() { fs::create_dir_all(&self.qcow2_dir).ok();