-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathrootfs.rs
152 lines (131 loc) · 4.33 KB
/
rootfs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::collections::HashSet;
use std::path::Path;
use nix::mount::MsFlags;
use oci_spec::runtime::{Linux, Spec};
use super::device::Device;
use super::mount::{Mount, MountOptions};
use super::symlink::Symlink;
use super::utils::default_devices;
use super::{Result, RootfsError};
use crate::error::MissingSpecError;
use crate::syscall::syscall::create_syscall;
use crate::syscall::Syscall;
/// Holds information about rootfs
pub struct RootFS {
syscall: Box<dyn Syscall>,
}
impl Default for RootFS {
fn default() -> Self {
Self::new()
}
}
impl RootFS {
pub fn new() -> RootFS {
RootFS {
syscall: create_syscall(),
}
}
pub fn mount_to_rootfs(
&self,
linux: &Linux,
spec: &Spec,
rootfs: &Path,
cgroup_ns: bool,
) -> Result<()> {
let mut flags = MsFlags::MS_REC;
match linux.rootfs_propagation().as_deref() {
Some("shared") => flags |= MsFlags::MS_SHARED,
Some("private") => flags |= MsFlags::MS_PRIVATE,
Some("slave" | "unbindable") | None => flags |= MsFlags::MS_SLAVE,
Some(unknown) => {
return Err(RootfsError::UnknownRootfsPropagation(unknown.to_string()));
}
}
self.syscall
.mount(None, Path::new("/"), None, flags, None)
.map_err(|err| {
tracing::error!(
?err,
?flags,
"failed to change the mount propagation type of the root"
);
err
})?;
let mounter = Mount::new();
mounter.make_parent_mount_private(rootfs)?;
tracing::debug!("mount root fs {:?}", rootfs);
self.syscall
.mount(
Some(rootfs),
rootfs,
None,
MsFlags::MS_BIND | MsFlags::MS_REC,
None,
)
.map_err(|err| {
tracing::error!(?rootfs, ?err, "failed to bind mount rootfs");
err
})?;
let global_options = MountOptions {
root: rootfs,
label: linux.mount_label().as_deref(),
cgroup_ns,
};
if let Some(mounts) = spec.mounts() {
for mount in mounts {
mounter.setup_mount(mount, &global_options)?;
}
}
Ok(())
}
pub fn prepare_rootfs(
&self,
spec: &Spec,
rootfs: &Path,
bind_devices: bool,
cgroup_ns: bool,
) -> Result<()> {
tracing::debug!(?rootfs, "prepare rootfs");
let linux = spec.linux().as_ref().ok_or(MissingSpecError::Linux)?;
self.mount_to_rootfs(linux, spec, rootfs, cgroup_ns)?;
let symlinker = Symlink::new();
symlinker.setup_kcore_symlink(rootfs)?;
symlinker.setup_default_symlinks(rootfs)?;
let devicer = Device::new();
if let Some(added_devices) = linux.devices() {
let mut path_set = HashSet::new();
let devices = default_devices();
added_devices.iter().for_each(|d| {
path_set.insert(d.path());
});
let default = devices.iter().filter(|d| !path_set.contains(d.path()));
devicer.create_devices(rootfs, added_devices.iter().chain(default), bind_devices)
} else {
devicer.create_devices(rootfs, &default_devices(), bind_devices)
}?;
symlinker.setup_ptmx(rootfs)?;
Ok(())
}
/// Change propagation type of rootfs as specified in spec.
pub fn adjust_root_mount_propagation(&self, linux: &Linux) -> Result<()> {
let rootfs_propagation = linux.rootfs_propagation().as_deref();
let flags = match rootfs_propagation {
Some("shared") => Some(MsFlags::MS_SHARED),
Some("unbindable") => Some(MsFlags::MS_UNBINDABLE),
_ => None,
};
if let Some(flags) = flags {
self.syscall
.mount(None, Path::new("/"), None, flags, None)
.map_err(|err| {
tracing::error!(
?err,
?flags,
"failed to adjust the mount propagation type of the root"
);
err
})?;
}
Ok(())
}
}