qemu_command_builder/
virtfs.rs1use crate::fsdev::SecurityModel;
2use crate::to_command::{ToArg, ToCommand};
3use bon::Builder;
4use std::path::PathBuf;
5
6#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
7pub enum RemapForbidWarn {
8 Remap,
9 Forbid,
10 Warn,
11}
12
13impl ToArg for RemapForbidWarn {
14 fn to_arg(&self) -> &str {
15 match self {
16 RemapForbidWarn::Remap => "remap",
17 RemapForbidWarn::Forbid => "forbid",
18 RemapForbidWarn::Warn => "warn",
19 }
20 }
21}
22
23#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Builder)]
24pub struct Local {
25 path: PathBuf,
26 mount_tag: String,
27 security_mode: SecurityModel,
28 id: Option<String>,
29 writeout: Option<()>,
30 readonly: Option<bool>,
31 fmode: Option<String>,
32 dmode: Option<String>,
33 multidevs: Option<RemapForbidWarn>,
34}
35
36#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Builder)]
37pub struct Synth {
38 mount_tag: String,
39}
40
41#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
42pub enum Virtfs {
43 Local(Local),
44 Synth(Synth),
45}
46impl ToCommand for Virtfs {
47 fn to_command(&self) -> Vec<String> {
48 let mut cmd = vec!["-add-fd".to_string()];
49
50 let mut args = vec![];
51
52 match self {
53 Virtfs::Local(local) => {
54 args.push("local".to_string());
55 args.push(format!("path={}", local.path.display()));
56 args.push(format!("mount_tag={}", local.mount_tag));
57 args.push(format!("security_mode={}", local.security_mode.to_arg()));
58 if let Some(id) = &local.id {
59 args.push(format!("id={}", id));
60 }
61 if local.writeout.is_some() {
62 args.push("writeout=immediate".to_string());
63 }
64 if let Some(readonly) = &local.readonly
65 && *readonly
66 {
67 args.push("readonly=on".to_string());
68 }
69 if let Some(fmode) = &local.fmode {
70 args.push(format!("fmode={}", fmode));
71 }
72 if let Some(dmode) = &local.dmode {
73 args.push(format!("dmode={}", dmode));
74 }
75 if let Some(multidevs) = &local.multidevs {
76 args.push(format!("multidevs={}", multidevs.to_arg()));
77 }
78 }
79 Virtfs::Synth(synth) => {
80 args.push("synth".to_string());
81 args.push(format!("mount_tag={}", synth.mount_tag));
82 }
83 }
84
85 cmd.push(args.join(","));
86 cmd
87 }
88}