Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions crates/kit/src/libvirt/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

use camino::{Utf8Path, Utf8PathBuf};
use clap::{Parser, ValueEnum};
use color_eyre::eyre;
use color_eyre::eyre::{self, eyre};
use color_eyre::{eyre::Context, Result};
use std::fs;
use std::io::Write;
use std::str::FromStr;
use tracing::{debug, info};

Expand Down Expand Up @@ -633,8 +634,13 @@ fn ensure_default_pool(connect_uri: Option<&str>) -> Result<()> {
);

// Write XML to temporary file
let xml_path = "/tmp/default-pool.xml";
std::fs::write(xml_path, &pool_xml).with_context(|| "Failed to write pool XML")?;
let mut tmpf = tempfile::NamedTempFile::with_prefix("bcvk-libvirt")?;
tmpf.write_all(pool_xml.as_bytes())
.with_context(|| "Failed to write pool XML")?;
let xml_path = tmpf
.path()
.to_str()
.ok_or_else(|| eyre!("Invalid UTF-8 in tempfile"))?;

// Define the pool
let mut cmd = virsh_command(connect_uri)?;
Expand Down Expand Up @@ -662,7 +668,6 @@ fn ensure_default_pool(connect_uri: Option<&str>) -> Result<()> {

if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let _ = std::fs::remove_file(xml_path);
return Err(color_eyre::eyre::eyre!(
"Failed to start default pool: {}",
stderr
Expand All @@ -674,9 +679,6 @@ fn ensure_default_pool(connect_uri: Option<&str>) -> Result<()> {
cmd.args(&["pool-autostart", "default"]);
let _ = cmd.output(); // Not critical if this fails

// Clean up temporary XML file
let _ = std::fs::remove_file(xml_path);

info!("Default storage pool created successfully");
Ok(())
}
Expand Down Expand Up @@ -1296,8 +1298,15 @@ fn create_libvirt_domain_from_disk(
.with_context(|| "Failed to build domain XML")?;

// Write XML to temporary file
let xml_path = format!("/tmp/{}.xml", domain_name);
std::fs::write(&xml_path, domain_xml).with_context(|| "Failed to write domain XML")?;
let mut tmp_domain_file = tempfile::NamedTempFile::with_prefix("bcvk-libvirt")?;
tmp_domain_file
.as_file_mut()
.write_all(domain_xml.as_bytes())
.with_context(|| "Failed to write domain XML")?;
let xml_path = tmp_domain_file
.path()
.to_str()
.ok_or_else(|| eyre!("Invalid UTF-8 in tempfile"))?;

let connect_uri = global_opts.connect.as_deref();

Expand All @@ -1323,8 +1332,5 @@ fn create_libvirt_domain_from_disk(
)?;
}

// Clean up temporary XML file
let _ = std::fs::remove_file(&xml_path);

Ok(())
}