capsid_protocol/
volume.rs1use std::collections::BTreeSet;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{Error, Result, path};
6
7pub const MAX_MOUNTS: usize = 8;
8
9const RESERVED: [&str; 3] = ["/dev", "/proc", "/sys"];
10
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(deny_unknown_fields)]
13pub struct Mount {
14 pub device: u8,
15
16 pub target: String,
17
18 pub read_only: bool,
19}
20
21pub fn validate_targets<'a>(targets: impl IntoIterator<Item = &'a str>) -> Result<()> {
22 let mut selected = BTreeSet::<&str>::new();
23 for target in targets {
24 if !path::is_guest_target(target, &RESERVED) {
25 return Err(Error::Invalid("invalid or reserved guest volume target"));
26 }
27
28 if selected.len() == MAX_MOUNTS || selected.iter().any(|old| path::overlap(old, target)) {
29 return Err(Error::Invalid(
30 "local block volumes require at most eight distinct, non-overlapping targets",
31 ));
32 }
33
34 selected.insert(target);
35 }
36 Ok(())
37}
38
39pub fn validate_mounts(mounts: &[Mount]) -> Result<()> {
40 validate_targets(mounts.iter().map(|mount| mount.target.as_str()))?;
41 let mut devices = BTreeSet::new();
42 for mount in mounts {
43 if usize::from(mount.device) >= MAX_MOUNTS || !devices.insert(mount.device) {
44 return Err(Error::Invalid("invalid or duplicate attached block device"));
45 }
46 }
47 Ok(())
48}