From 60d35f17bdddf83b6201ab8e63f235e411b53c90 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Wed, 6 Dec 2023 20:27:59 +0100 Subject: [PATCH] Managed to start a VM again --- virtweb_backend/src/app_config.rs | 3 +++ .../src/controllers/iso_controller.rs | 5 ++++- virtweb_backend/src/libvirt_lib_structures.rs | 22 +++++++++++++++++++ .../src/libvirt_rest_structures.rs | 19 +++++++++++----- virtweb_backend/src/main.rs | 1 + virtweb_backend/src/utils/files_utils.rs | 9 ++++++++ 6 files changed, 53 insertions(+), 6 deletions(-) diff --git a/virtweb_backend/src/app_config.rs b/virtweb_backend/src/app_config.rs index 533bc3d..2c2218a 100644 --- a/virtweb_backend/src/app_config.rs +++ b/virtweb_backend/src/app_config.rs @@ -76,6 +76,9 @@ pub struct AppConfig { pub storage: String, /// Directory where temporary files are stored + /// + /// Warning! This directory MUST be changed if `/tmp` is not in the same disk as the storage + /// directory! #[arg(long, env, default_value = "/tmp")] pub temp_dir: String, diff --git a/virtweb_backend/src/controllers/iso_controller.rs b/virtweb_backend/src/controllers/iso_controller.rs index 3ccf24c..560f3d8 100644 --- a/virtweb_backend/src/controllers/iso_controller.rs +++ b/virtweb_backend/src/controllers/iso_controller.rs @@ -59,7 +59,10 @@ pub async fn upload_file(MultipartForm(mut form): MultipartForm) return Ok(HttpResponse::Conflict().json("The file already exists!")); } - file.file.persist(dest_file)?; + file.file.persist(&dest_file)?; + + // Set file permissions + files_utils::set_file_permission(dest_file, 0o644)?; Ok(HttpResponse::Accepted().finish()) } diff --git a/virtweb_backend/src/libvirt_lib_structures.rs b/virtweb_backend/src/libvirt_lib_structures.rs index 29ac23f..9935fdc 100644 --- a/virtweb_backend/src/libvirt_lib_structures.rs +++ b/virtweb_backend/src/libvirt_lib_structures.rs @@ -181,6 +181,25 @@ pub struct DomainMemoryXML { pub memory: usize, } +#[derive(serde::Serialize, serde::Deserialize)] +#[serde(rename = "topology")] +pub struct DomainCPUTopology { + #[serde(rename(serialize = "@sockets"))] + pub sockets: usize, + #[serde(rename(serialize = "@cores"))] + pub cores: usize, + #[serde(rename(serialize = "@threads"))] + pub threads: usize, +} + +#[derive(serde::Serialize, serde::Deserialize)] +#[serde(rename = "cpu")] +pub struct DomainCPUXML { + #[serde(rename(serialize = "@mode"))] + pub mode: String, + pub topology: Option, +} + /// Domain information, see https://libvirt.org/formatdomain.html #[derive(serde::Serialize, serde::Deserialize)] #[serde(rename = "domain")] @@ -202,6 +221,9 @@ pub struct DomainXML { /// The maximum allocation of memory for the guest at boot time pub memory: DomainMemoryXML, + /// CPU information + pub cpu: DomainCPUXML, + pub on_poweroff: String, pub on_reboot: String, pub on_crash: String, diff --git a/virtweb_backend/src/libvirt_rest_structures.rs b/virtweb_backend/src/libvirt_rest_structures.rs index 9ca1058..09267b7 100644 --- a/virtweb_backend/src/libvirt_rest_structures.rs +++ b/virtweb_backend/src/libvirt_rest_structures.rs @@ -2,9 +2,9 @@ use crate::app_config::AppConfig; use crate::constants; use crate::libvirt_lib_structures::{ DevicesXML, DiskBootXML, DiskDriverXML, DiskReadOnlyXML, DiskSourceXML, DiskTargetXML, DiskXML, - DomainMemoryXML, DomainXML, FeaturesXML, GraphicsXML, NetworkDHCPRangeXML, NetworkDHCPXML, - NetworkDNSForwarderXML, NetworkDNSXML, NetworkDomainXML, NetworkForwardXML, NetworkIPXML, - NetworkXML, OSLoaderXML, OSTypeXML, XMLUuid, ACPIXML, OSXML, + DomainCPUTopology, DomainCPUXML, DomainMemoryXML, DomainXML, FeaturesXML, GraphicsXML, + NetworkDHCPRangeXML, NetworkDHCPXML, NetworkDNSForwarderXML, NetworkDNSXML, NetworkDomainXML, + NetworkForwardXML, NetworkIPXML, NetworkXML, OSLoaderXML, OSTypeXML, XMLUuid, ACPIXML, OSXML, }; use crate::libvirt_rest_structures::LibVirtStructError::StructureExtraction; use crate::utils::disks_utils::Disk; @@ -245,9 +245,18 @@ impl VMInfo { memory: self.memory, }, - on_poweroff: "preserve".to_string(), + cpu: DomainCPUXML { + mode: "host-passthrough".to_string(), + topology: Some(DomainCPUTopology { + sockets: 1, + cores: 1, + threads: 1, + }), + }, + + on_poweroff: "destroy".to_string(), on_reboot: "restart".to_string(), - on_crash: "preserve".to_string(), + on_crash: "destroy".to_string(), }) } diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index 78e11f4..134a2d2 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -35,6 +35,7 @@ async fn main() -> std::io::Result<()> { log::debug!("Create required directory, if missing"); files_utils::create_directory_if_missing(AppConfig::get().iso_storage_path()).unwrap(); files_utils::create_directory_if_missing(AppConfig::get().vnc_sockets_path()).unwrap(); + files_utils::set_file_permission(AppConfig::get().vnc_sockets_path(), 0o644).unwrap(); files_utils::create_directory_if_missing(AppConfig::get().disks_storage_path()).unwrap(); let conn = Data::new(LibVirtClient( diff --git a/virtweb_backend/src/utils/files_utils.rs b/virtweb_backend/src/utils/files_utils.rs index f4503a3..54fc403 100644 --- a/virtweb_backend/src/utils/files_utils.rs +++ b/virtweb_backend/src/utils/files_utils.rs @@ -1,3 +1,4 @@ +use std::os::unix::fs::PermissionsExt; use std::path::Path; const INVALID_CHARS: [&str; 19] = [ @@ -19,6 +20,14 @@ pub fn create_directory_if_missing>(path: P) -> anyhow::Result<() Ok(()) } +/// Update file permission +pub fn set_file_permission>(path: P, mode: u32) -> anyhow::Result<()> { + let mut perms = std::fs::metadata(path.as_ref())?.permissions(); + perms.set_mode(mode); + std::fs::set_permissions(path.as_ref(), perms)?; + Ok(()) +} + #[cfg(test)] mod test { use crate::utils::files_utils::check_file_name;