Skip to content

Commit f661aad

Browse files
authored
Add --virtiofsd CLI option and VIRTIOFSD_BIN env var support (#266)
* Add --virtiofsd CLI option and VIRTIOFSD_BIN env var support Signed-off-by: Nick Downs <nickryand@gmail.com>
1 parent 0aafd1a commit f661aad

4 files changed

Lines changed: 47 additions & 23 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
target/
1+
target/
2+
.flox

crates/bcvk-qemu/src/virtiofsd.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub struct VirtiofsConfig {
2121
pub readonly: bool,
2222
/// Optional log file path for virtiofsd output.
2323
pub log_file: Option<Utf8PathBuf>,
24+
/// Optional explicit path to virtiofsd binary (overrides auto-detection).
25+
pub virtiofsd_binary: Option<Utf8PathBuf>,
2426
}
2527

2628
impl Default for VirtiofsConfig {
@@ -32,6 +34,7 @@ impl Default for VirtiofsConfig {
3234
// We don't need to write to this, there's a transient overlay
3335
readonly: true,
3436
log_file: None,
37+
virtiofsd_binary: None,
3538
}
3639
}
3740
}
@@ -61,32 +64,42 @@ pub async fn spawn_virtiofsd_async(config: &VirtiofsConfig) -> Result<tokio::pro
6164
// Validate configuration
6265
validate_virtiofsd_config(config)?;
6366

64-
// Try common virtiofsd binary locations
65-
let virtiofsd_paths = [
66-
"/usr/libexec/virtiofsd",
67-
"/usr/bin/virtiofsd",
68-
"/usr/local/bin/virtiofsd",
69-
"/usr/lib/virtiofsd",
70-
];
71-
72-
let virtiofsd_binary = virtiofsd_paths
73-
.iter()
74-
.find(|path| std::path::Path::new(path).exists())
75-
.ok_or_else(|| {
76-
eyre!(
77-
"virtiofsd binary not found. Searched paths: {}. Please install virtiofsd.",
78-
virtiofsd_paths.join(", ")
79-
)
80-
})?;
67+
// Resolve virtiofsd binary: explicit override or path search
68+
let virtiofsd_binary: String = if let Some(ref path) = config.virtiofsd_binary {
69+
if !path.exists() {
70+
return Err(eyre!("Explicit virtiofsd binary not found at: {}", path));
71+
}
72+
camino::absolute_utf8(path)?.to_string()
73+
} else {
74+
// Try common virtiofsd binary locations
75+
let virtiofsd_paths = [
76+
"/usr/libexec/virtiofsd",
77+
"/usr/bin/virtiofsd",
78+
"/usr/local/bin/virtiofsd",
79+
"/usr/lib/virtiofsd",
80+
];
81+
82+
virtiofsd_paths
83+
.iter()
84+
.find(|path| std::path::Path::new(path).exists())
85+
.ok_or_else(|| {
86+
eyre!(
87+
"virtiofsd binary not found. Searched paths: {}. \
88+
Set --virtiofsd or VIRTIOFSD_BIN to specify the path explicitly.",
89+
virtiofsd_paths.join(", ")
90+
)
91+
})?
92+
.to_string()
93+
};
8194

8295
// Check if virtiofsd supports --readonly flag
83-
let supports_readonly = virtiofsd_supports_readonly(virtiofsd_binary).await;
96+
let supports_readonly = virtiofsd_supports_readonly(&virtiofsd_binary).await;
8497
debug!(
8598
"virtiofsd at {} supports --readonly: {}",
8699
virtiofsd_binary, supports_readonly
87100
);
88101

89-
let mut cmd = tokio::process::Command::new(virtiofsd_binary);
102+
let mut cmd = tokio::process::Command::new(&virtiofsd_binary);
90103
// SAFETY: This API is safe to call in a forked child.
91104
#[allow(unsafe_code)]
92105
unsafe {
@@ -148,13 +161,13 @@ pub async fn spawn_virtiofsd_async(config: &VirtiofsConfig) -> Result<tokio::pro
148161
let child = cmd.spawn().with_context(|| {
149162
format!(
150163
"Failed to spawn virtiofsd. Binary: {}, Socket: {}, Shared dir: {}",
151-
virtiofsd_binary, config.socket_path, config.shared_dir
164+
&virtiofsd_binary, config.socket_path, config.shared_dir
152165
)
153166
})?;
154167

155168
debug!(
156169
"Spawned virtiofsd: binary={}, socket={}, shared_dir={}, debug={}, log_file={:?}",
157-
virtiofsd_binary, config.socket_path, config.shared_dir, config.debug, config.log_file
170+
&virtiofsd_binary, config.socket_path, config.shared_dir, config.debug, config.log_file
158171
);
159172

160173
Ok(child)

crates/kit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ base64 = "0.22"
99
chrono = { version = "0.4", features = ["serde"] }
1010
const_format = { workspace = true }
1111
color-eyre = { workspace = true }
12-
clap = { version = "4.4", features = ["derive"] }
12+
clap = { version = "4.4", features = ["derive", "env"] }
1313
clap_mangen = { version = "0.2.20", optional = true }
1414
data-encoding = { version = "2.9" }
1515
dirs = "5.0"

crates/kit/src/run_ephemeral.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ pub struct CommonVmOpts {
218218
help = "Generate SSH keypair and inject via systemd credentials"
219219
)]
220220
pub ssh_keygen: bool,
221+
222+
#[clap(
223+
long = "virtiofsd",
224+
env = "VIRTIOFSD_BIN",
225+
help = "Path to virtiofsd binary (overrides auto-detection)"
226+
)]
227+
pub virtiofsd_binary: Option<String>,
221228
}
222229

223230
impl CommonVmOpts {
@@ -1171,6 +1178,7 @@ pub(crate) async fn run_impl(opts: RunEphemeralOpts) -> Result<()> {
11711178
debug: false,
11721179
readonly: is_readonly,
11731180
log_file: Some(format!("/run/virtiofsd-{}.log", mount_name_str).into()),
1181+
virtiofsd_binary: opts.common.virtiofsd_binary.as_deref().map(Into::into),
11741182
};
11751183
additional_mounts.push((virtiofsd_config, tag.clone()));
11761184

@@ -1329,6 +1337,8 @@ StandardOutput=file:/dev/virtio-ports/executestatus
13291337
main_virtiofsd_config.debug = std::env::var("DEBUG_MODE").is_ok();
13301338
// Always log virtiofsd output for debugging
13311339
main_virtiofsd_config.log_file = Some("/run/virtiofsd.log".into());
1340+
main_virtiofsd_config.virtiofsd_binary =
1341+
opts.common.virtiofsd_binary.as_deref().map(Into::into);
13321342

13331343
std::fs::create_dir_all(CONTAINER_STATEDIR)?;
13341344

0 commit comments

Comments
 (0)