Can set the number of VCPUs
This commit is contained in:
@ -91,6 +91,23 @@ pub async fn server_info(client: LibVirtReq) -> HttpResult {
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn number_vcpus() -> HttpResult {
|
||||
let mut system = System::new();
|
||||
system.refresh_cpu();
|
||||
let number_cpus = system.cpus().len();
|
||||
assert_ne!(number_cpus, 0, "Got invlid number of CPU!");
|
||||
|
||||
let mut possible_numbers = vec![1];
|
||||
|
||||
if number_cpus > 1 {
|
||||
for i in 0..(number_cpus / 2) {
|
||||
possible_numbers.push(2 + 2 * i);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(possible_numbers))
|
||||
}
|
||||
|
||||
pub async fn networks_list() -> HttpResult {
|
||||
let mut system = System::new();
|
||||
system.refresh_networks_list();
|
||||
|
@ -192,6 +192,13 @@ pub struct DomainCPUTopology {
|
||||
pub threads: usize,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename = "cpu")]
|
||||
pub struct DomainVCPUXML {
|
||||
#[serde(rename = "$value")]
|
||||
pub body: usize,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename = "cpu")]
|
||||
pub struct DomainCPUXML {
|
||||
@ -221,6 +228,9 @@ pub struct DomainXML {
|
||||
/// The maximum allocation of memory for the guest at boot time
|
||||
pub memory: DomainMemoryXML,
|
||||
|
||||
/// Number of vCPU
|
||||
pub vcpu: DomainVCPUXML,
|
||||
|
||||
/// CPU information
|
||||
pub cpu: DomainCPUXML,
|
||||
|
||||
|
@ -2,15 +2,17 @@ use crate::app_config::AppConfig;
|
||||
use crate::constants;
|
||||
use crate::libvirt_lib_structures::{
|
||||
DevicesXML, DiskBootXML, DiskDriverXML, DiskReadOnlyXML, DiskSourceXML, DiskTargetXML, DiskXML,
|
||||
DomainCPUTopology, DomainCPUXML, DomainMemoryXML, DomainXML, FeaturesXML, GraphicsXML,
|
||||
NetworkDHCPRangeXML, NetworkDHCPXML, NetworkDNSForwarderXML, NetworkDNSXML, NetworkDomainXML,
|
||||
NetworkForwardXML, NetworkIPXML, NetworkXML, OSLoaderXML, OSTypeXML, XMLUuid, ACPIXML, OSXML,
|
||||
DomainCPUTopology, DomainCPUXML, DomainMemoryXML, DomainVCPUXML, 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;
|
||||
use crate::utils::files_utils;
|
||||
use ipnetwork::{Ipv4Network, Ipv6Network};
|
||||
use lazy_regex::regex;
|
||||
use num::Integer;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
use std::ops::{Div, Mul};
|
||||
|
||||
@ -74,6 +76,8 @@ pub struct VMInfo {
|
||||
pub architecture: VMArchitecture,
|
||||
/// VM allocated memory, in megabytes
|
||||
pub memory: usize,
|
||||
/// Number of vCPU for the VM
|
||||
pub number_vcpu: usize,
|
||||
/// Enable VNC access through admin console
|
||||
pub vnc_access: bool,
|
||||
/// Attach an ISO file
|
||||
@ -81,7 +85,6 @@ pub struct VMInfo {
|
||||
/// Storage - https://access.redhat.com/documentation/fr-fr/red_hat_enterprise_linux/6/html/virtualization_administration_guide/sect-virtualization-virtualized_block_devices-adding_storage_devices_to_guests#sect-Virtualization-Adding_storage_devices_to_guests-Adding_file_based_storage_to_a_guest
|
||||
pub disks: Vec<Disk>,
|
||||
// TODO : network interfaces
|
||||
// TODO : number of CPUs
|
||||
}
|
||||
|
||||
impl VMInfo {
|
||||
@ -116,6 +119,10 @@ impl VMInfo {
|
||||
return Err(StructureExtraction("VM memory is invalid!").into());
|
||||
}
|
||||
|
||||
if self.number_vcpu == 0 || (self.number_vcpu != 1 && self.number_vcpu.is_odd()) {
|
||||
return Err(StructureExtraction("Invalid number of vCPU specified!").into());
|
||||
}
|
||||
|
||||
let mut disks = vec![];
|
||||
|
||||
if let Some(iso_file) = &self.iso_file {
|
||||
@ -245,12 +252,22 @@ impl VMInfo {
|
||||
memory: self.memory,
|
||||
},
|
||||
|
||||
vcpu: DomainVCPUXML {
|
||||
body: self.number_vcpu,
|
||||
},
|
||||
|
||||
cpu: DomainCPUXML {
|
||||
mode: "host-passthrough".to_string(),
|
||||
topology: Some(DomainCPUTopology {
|
||||
sockets: 1,
|
||||
cores: 1,
|
||||
threads: 1,
|
||||
cores: match self.number_vcpu {
|
||||
1 => 1,
|
||||
v => v / 2,
|
||||
},
|
||||
threads: match self.number_vcpu {
|
||||
1 => 1,
|
||||
_ => 2,
|
||||
},
|
||||
}),
|
||||
},
|
||||
|
||||
@ -285,6 +302,7 @@ impl VMInfo {
|
||||
.into());
|
||||
}
|
||||
},
|
||||
number_vcpu: domain.vcpu.body,
|
||||
memory: convert_to_mb(&domain.memory.unit, domain.memory.memory)?,
|
||||
vnc_access: domain.devices.graphics.is_some(),
|
||||
iso_file: domain
|
||||
|
@ -102,6 +102,10 @@ async fn main() -> std::io::Result<()> {
|
||||
"/api/server/info",
|
||||
web::get().to(server_controller::server_info),
|
||||
)
|
||||
.route(
|
||||
"/api/server/number_vcpus",
|
||||
web::get().to(server_controller::number_vcpus),
|
||||
)
|
||||
.route(
|
||||
"/api/server/networks",
|
||||
web::get().to(server_controller::networks_list),
|
||||
|
Reference in New Issue
Block a user