Skip to content

Commit

Permalink
Move gen_sock() helper into util module
Browse files Browse the repository at this point in the history
Move the gen_sock() helper function into the util module, where it will
be accessible by more than just the qemu module. Also switch over to
using std::env::temp_dir() for retrieving the path to the hosts temp
directory instead of hard coding it.

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o committed Aug 29, 2024
1 parent 7534bb2 commit 7c87449
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ pub use crate::vmtest::*;

mod qemu;
mod qga;
mod util;
9 changes: 1 addition & 8 deletions src/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ use std::time::Duration;
use anyhow::{anyhow, bail, Context, Result};
use log::{debug, log_enabled, warn, Level};
use qapi::{qga, qmp, Qmp};
use rand::Rng;
use serde_derive::Serialize;
use tempfile::{Builder, NamedTempFile};
use tinytemplate::{format_unescaped, TinyTemplate};

use crate::output::Output;
use crate::qga::QgaWrapper;
use crate::util::gen_sock;
use crate::{Mount, Target, VMConfig};

const INIT_TEMPLATE: &str = include_str!("init/init.sh.template");
Expand Down Expand Up @@ -118,13 +118,6 @@ fn host_supports_kvm(arch: &str) -> bool {
arch == ARCH && Path::new("/dev/kvm").exists()
}

// Generate a path to a randomly named socket
fn gen_sock(prefix: &str) -> PathBuf {
let id = rand::thread_rng().gen_range(100_000..1_000_000);
let sock = format!("/tmp/{prefix}-{id}.sock");
PathBuf::from(sock)
}

// Given a guest temp dir and a host init path, generate the path to the init file
// in the guest.
// This path is the one that will be passed to the guest via the kernel's `init=` parameter.
Expand Down
11 changes: 11 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::env::temp_dir;
use std::path::PathBuf;

use rand::Rng as _;


/// Generate a path to a randomly named socket
pub(crate) fn gen_sock(prefix: &str) -> PathBuf {
let id = rand::thread_rng().gen_range(100_000..1_000_000);
temp_dir().join(format!("{prefix}-{id}.sock"))
}

0 comments on commit 7c87449

Please sign in to comment.