2 Commits

Author SHA1 Message Date
de33c7d521 Check if qemu-img is present before startup
All checks were successful
continuous-integration/drone/push Build is passing
2025-05-26 19:21:40 +02:00
ff372800bd Add backend bridge support 2025-05-26 19:12:18 +02:00
8 changed files with 114 additions and 8 deletions

View File

@ -108,3 +108,6 @@ pub const API_TOKEN_DESCRIPTION_MAX_LENGTH: usize = 30;
/// API token right path max length
pub const API_TOKEN_RIGHT_PATH_MAX_LENGTH: usize = 255;
/// Qemu image program path
pub const QEMU_IMAGE_PROGRAM: &str = "/usr/bin/qemu-img";

View File

@ -80,7 +80,9 @@ pub struct NetMacAddress {
#[serde(rename = "source")]
pub struct NetIntSourceXML {
#[serde(rename = "@network")]
pub network: String,
pub network: Option<String>,
#[serde(rename = "@bridge")]
pub bridge: Option<String>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]

View File

@ -53,7 +53,8 @@ pub struct Network {
#[serde(tag = "type")]
pub enum NetworkType {
UserspaceSLIRPStack,
DefinedNetwork { network: String }, // TODO : complete network types
DefinedNetwork { network: String },
Bridge { bridge: String },
}
#[derive(serde::Serialize, serde::Deserialize)]
@ -240,7 +241,18 @@ impl VMInfo {
mac,
r#type: "network".to_string(),
source: Some(NetIntSourceXML {
network: network.to_string(),
network: Some(network.to_string()),
bridge: None,
}),
model,
filterref,
},
NetworkType::Bridge { bridge } => DomainNetInterfaceXML {
r#type: "bridge".to_string(),
mac,
source: Some(NetIntSourceXML {
network: None,
bridge: Some(bridge.to_string()),
}),
model,
filterref,
@ -468,7 +480,34 @@ impl VMInfo {
r#type: match d.r#type.as_str() {
"user" => NetworkType::UserspaceSLIRPStack,
"network" => NetworkType::DefinedNetwork {
network: d.source.as_ref().unwrap().network.to_string(),
network: d
.source
.as_ref()
.unwrap()
.network
.as_deref()
.ok_or_else(|| {
LibVirtStructError::DomainExtraction(
"Missing source network for defined network!"
.to_string(),
)
})?
.to_string(),
},
"bridge" => NetworkType::Bridge {
bridge: d
.source
.as_ref()
.unwrap()
.bridge
.as_deref()
.ok_or_else(|| {
LibVirtStructError::DomainExtraction(
"Missing bridge name for bridge connection!"
.to_string(),
)
})?
.to_string(),
},
a => {
return Err(LibVirtStructError::DomainExtraction(format!(

View File

@ -28,7 +28,7 @@ use virtweb_backend::controllers::{
use virtweb_backend::libvirt_client::LibVirtClient;
use virtweb_backend::middlewares::auth_middleware::AuthChecker;
use virtweb_backend::nat::nat_conf_mode;
use virtweb_backend::utils::files_utils;
use virtweb_backend::utils::{exec_utils, files_utils};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
@ -43,6 +43,12 @@ async fn main() -> std::io::Result<()> {
// Load additional config from file, if requested
AppConfig::parse_env_file().unwrap();
log::debug!("Checking for required programs");
exec_utils::check_program(
constants::QEMU_IMAGE_PROGRAM,
"QEMU disk image utility is required to manipulate QCow2 files!",
);
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();

View File

@ -0,0 +1,10 @@
use std::path::Path;
/// Check the existence of a required program
pub fn check_program(name: &str, description: &str) {
let path = Path::new(name);
if !path.exists() {
panic!("{name} does not exist! {description}");
}
}

View File

@ -155,7 +155,7 @@ impl FileDisk {
}
DiskFormat::QCow2 => {
let mut cmd = Command::new("/usr/bin/qemu-img");
let mut cmd = Command::new(constants::QEMU_IMAGE_PROGRAM);
cmd.arg("create")
.arg("-f")
.arg("qcow2")
@ -189,12 +189,13 @@ struct QCowInfoOutput {
/// Get QCow2 virtual size
fn qcow_virt_size(path: &str) -> anyhow::Result<usize> {
// Run qemu-img
let mut cmd = Command::new("qemu-img");
let mut cmd = Command::new(constants::QEMU_IMAGE_PROGRAM);
cmd.args(["info", path, "--output", "json", "--force-share"]);
let output = cmd.output()?;
if !output.status.success() {
anyhow::bail!(
"qemu-img info failed, status: {}, stderr: {}",
"{} info failed, status: {}, stderr: {}",
constants::QEMU_IMAGE_PROGRAM,
output.status,
String::from_utf8_lossy(&output.stderr)
);

View File

@ -1,3 +1,4 @@
pub mod exec_utils;
pub mod file_disks_utils;
pub mod files_utils;
pub mod net_utils;

44
virtweb_docs/BRIDGE.md Normal file
View File

@ -0,0 +1,44 @@
# Bridges
Bridges can be used to connect virtual machines to networks.
## Setup Bridge on Ubuntu
1. Install dependencies:
```bash
sudo apt install bridge-utils
```
2. Adapt your netplan configuration to set the following:
```yaml
network:
version: 2
renderer: networkd
ethernets:
enp2s0:
dhcp4: no
bridges:
br0: # Bridge name
dhcp4: yes
interfaces:
- enp2s0 # Set to your interface
```
3. Apply netplan configuration:
```bash
sudo netplan apply
```
4. Get the state and the list of bridges in the system:
```bash
sudo brctl show
```
## Reference
[How to Configure Network Bridge in Ubuntu](https://www.tecmint.com/create-network-bridge-in-ubuntu/)