From 3502476d33c2ca5eafe804da448338592715fb3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Wed, 28 Aug 2024 14:33:30 -0700 Subject: [PATCH] Move gen_sock() helper into util module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/lib.rs | 1 + src/qemu.rs | 9 +-------- src/util.rs | 11 +++++++++++ 3 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 src/util.rs diff --git a/src/lib.rs b/src/lib.rs index 46b6303..7c7de7c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,3 +16,4 @@ pub use crate::vmtest::*; mod qemu; mod qga; +mod util; diff --git a/src/qemu.rs b/src/qemu.rs index 5103a7f..c2787ee 100644 --- a/src/qemu.rs +++ b/src/qemu.rs @@ -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"); @@ -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. diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..0cba119 --- /dev/null +++ b/src/util.rs @@ -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")) +}