qemu_command_builder/
fsdev.rs1use crate::to_command::ToArg;
2use crate::to_command::ToCommand;
3use bon::Builder;
4use std::path::PathBuf;
5
6#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
7pub enum SecurityModel {
8 Passthrough,
9 MappedXAttr,
10 MappedFile,
11 None,
12}
13
14impl ToArg for SecurityModel {
15 fn to_arg(&self) -> &str {
16 match self {
17 SecurityModel::Passthrough => "passthrough",
18 SecurityModel::MappedXAttr => "mapped-xattr",
19 SecurityModel::MappedFile => "mapped-file",
20 SecurityModel::None => "none",
21 }
22 }
23}
24#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Builder)]
26pub struct FsDevLocal {
27 id: String,
29
30 path: PathBuf,
33
34 security_model: SecurityModel,
48
49 writeout: Option<()>,
55
56 readonly: Option<()>,
59
60 fmode: Option<String>,
64
65 dmode: Option<String>,
69
70 throttling_bps_total: Option<usize>,
73 throttling_bps_read: Option<usize>,
74 throttling_bps_write: Option<usize>,
75
76 throttling_bps_total_max: Option<usize>,
80 bps_read_max: Option<usize>,
81 bps_write_max: Option<usize>,
82
83 throttling_iops_total: Option<usize>,
86 throttling_iops_read: Option<usize>,
87 throttling_iops_write: Option<usize>,
88
89 throttling_iops_total_max: Option<usize>,
93 throttling_iops_read_max: Option<usize>,
94 throttling_iops_write_max: Option<usize>,
95
96 throttling_iops_size: Option<usize>,
99}
100
101#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
103pub struct FsDevSynth {
104 id: String,
106}
107
108#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
113pub enum FsDev {
114 Local(FsDevLocal),
115 Synth(FsDevSynth),
116}
117
118impl ToCommand for FsDev {
119 fn to_command(&self) -> Vec<String> {
120 let mut cmd = vec![];
121
122 cmd.push("-fsdev".to_string());
123
124 match self {
125 FsDev::Local(local) => {
126 let mut arg = vec![];
127
128 arg.push(format!("local,id={}", local.id));
129
130 arg.push(format!(",path={}", local.path.display()));
131
132 arg.push(format!(",security-model={}", local.security_model.to_arg()));
133
134 if local.writeout.is_some() {
135 arg.push("writeout=immediate".to_string());
136 }
137
138 if local.readonly.is_some() {
139 arg.push("readonly=on".to_string());
140 }
141
142 if let Some(fmode) = &local.fmode {
143 arg.push(format!("fmode={}", fmode));
144 }
145
146 if let Some(dmode) = &local.dmode {
147 arg.push(format!("dmode={}", dmode));
148 }
149
150 if let Some(throttling_bps_total) = local.throttling_bps_total {
151 arg.push(format!("throttling.bps-total={}", throttling_bps_total));
152 }
153 if let Some(throttling_bps_read) = local.throttling_bps_read {
154 arg.push(format!("throttling.bps-read={}", throttling_bps_read));
155 }
156 if let Some(throttling_bps_write) = local.throttling_bps_write {
157 arg.push(format!("throttling.bps-write={}", throttling_bps_write));
158 }
159
160 if let Some(throttling_bps_total_max) = local.throttling_bps_total_max {
161 arg.push(format!(
162 "throttling.bps-total-max={}",
163 throttling_bps_total_max
164 ));
165 }
166 if let Some(bps_read_max) = local.bps_read_max {
167 arg.push(format!("bps-read-max={}", bps_read_max));
168 }
169 if let Some(bps_write_max) = local.bps_write_max {
170 arg.push(format!("bps-write-max={}", bps_write_max));
171 }
172
173 if let Some(throttling_iops_total) = local.throttling_iops_total {
174 arg.push(format!("throttling.iops-total={}", throttling_iops_total));
175 }
176 if let Some(throttling_iops_read) = local.throttling_iops_read {
177 arg.push(format!("throttling.iops-read={}", throttling_iops_read));
178 }
179 if let Some(throttling_iops_write) = local.throttling_iops_write {
180 arg.push(format!("throttling.iops-write={}", throttling_iops_write));
181 }
182
183 if let Some(throttling_ios_total_max) = local.throttling_iops_total_max {
184 arg.push(format!(
185 "throttling.ios-total-max={}",
186 throttling_ios_total_max
187 ));
188 }
189 if let Some(throttling_iops_read_max) = local.throttling_iops_read_max {
190 arg.push(format!(
191 "throttling.iops-read-max={}",
192 throttling_iops_read_max
193 ));
194 }
195 if let Some(throttling_iops_write_max) = local.throttling_iops_write_max {
196 arg.push(format!(
197 "throttling.iops-write-max={}",
198 throttling_iops_write_max
199 ));
200 }
201
202 if let Some(throttling_iops_size) = local.throttling_iops_size {
203 arg.push(format!("throttling.iops-size={}", throttling_iops_size));
204 }
205 cmd.push(arg.join(","));
206 }
207 FsDev::Synth(synth) => {
208 let mut arg = String::new();
209 arg.push_str("synth,id=");
210 arg.push_str(synth.id.to_string().as_str());
211 cmd.push(arg);
212 }
213 }
214
215 cmd
216 }
217}