From 0d10bc096bb4d791b6528d7ca6d450c83cfd1778 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 24 Jan 2021 15:47:24 +0200 Subject: create --- Cargo.toml | 3 ++- src/bin/tool.rs | 27 +++++++++++++++++++++++++++ src/image.rs | 1 - src/install.rs | 12 +++++++++++- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/bin/tool.rs diff --git a/Cargo.toml b/Cargo.toml index 9e49642..acda35e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1" +tempfile = "3.2" thiserror = "1" -tempfile = "3.2" \ No newline at end of file diff --git a/src/bin/tool.rs b/src/bin/tool.rs new file mode 100644 index 0000000..18a5c19 --- /dev/null +++ b/src/bin/tool.rs @@ -0,0 +1,27 @@ +use std::path::PathBuf; + +use vmadm::cloudinit::CloudInitConfig; +use vmadm::image::VirtualMachineImage; +use vmadm::install::{virt_install, VirtInstallArgs}; + +const BASE_PATH: &'static str = "/home/liw/tmp/debian-10-openstack-amd64.qcow2"; +const IMAGE_PATH: &'static str = "/home/liw/tmp/try-vm.qcow2"; + +fn main() -> anyhow::Result<()> { + let mut init = CloudInitConfig::default(); + init.set_hostname("toy-vm"); + init.set_authorized_keys("xxx liw-openpgp xxx"); + println!("init: {:#?}", init); + + let base = PathBuf::from(BASE_PATH); + let image = PathBuf::from(IMAGE_PATH); + let image = VirtualMachineImage::new_from_base(&base, &image)?; + image.resize(1024 * 1024 * 1024 * 10)?; + + let args = VirtInstallArgs::new("toy-vm", &image, &init); + println!("{:#?}", args); + virt_install(&args)?; + println!("OK"); + + Ok(()) +} diff --git a/src/image.rs b/src/image.rs index 9d30ca0..da7e559 100644 --- a/src/image.rs +++ b/src/image.rs @@ -35,7 +35,6 @@ impl VirtualMachineImage { pub fn resize(&self, new_size: usize) -> Result<(), ImageError> { let r = Command::new("qemu-img") .arg("resize") - .arg("-f") .arg(self.filename()) .arg(format!("{}", new_size)) .output()?; diff --git a/src/install.rs b/src/install.rs index 5476277..89969e0 100644 --- a/src/install.rs +++ b/src/install.rs @@ -19,6 +19,7 @@ pub enum VirtInstallError { CloudInitError(#[from] CloudInitError), } +#[derive(Debug)] pub struct VirtInstallArgs { name: String, memory: usize, @@ -83,7 +84,16 @@ pub fn virt_install(args: &VirtInstallArgs) -> Result<(), VirtInstallError> { "--disk=path={},cache=none", args.image().filename().display() )) - .arg(format!("--disk=path={},readconly=on", iso.display())) + .arg(format!("--disk=path={},readonly=on", iso.display())) + .arg("--connect=qemu::///system") + .arg("--connect") + .arg("qemu:///system") + .arg("--cpu=host-passthrough") + .arg("--os-variant=debian9") + .arg("--import") + .arg("--graphics=spice") + .arg("--noautoconsole") + .arg("--quiet") .output()?; if !r.status.success() { let stderr = String::from_utf8(r.stderr)?; -- cgit v1.2.1