-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathmount.rs
1053 lines (946 loc) · 34.9 KB
/
mount.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::fs::{canonicalize, create_dir_all, OpenOptions};
use std::mem;
use std::os::unix::io::AsRawFd;
use std::path::{Path, PathBuf};
#[cfg(feature = "v1")]
use std::{borrow::Cow, collections::HashMap};
use libcgroups::common::CgroupSetup::{Hybrid, Legacy, Unified};
#[cfg(feature = "v1")]
use libcgroups::common::DEFAULT_CGROUP_ROOT;
use nix::dir::Dir;
use nix::errno::Errno;
use nix::fcntl::OFlag;
use nix::mount::MsFlags;
use nix::sys::stat::Mode;
use nix::NixPath;
use oci_spec::runtime::{Mount as SpecMount, MountBuilder as SpecMountBuilder};
use procfs::process::{MountInfo, MountOptFields, Process};
use safe_path;
#[cfg(feature = "v1")]
use super::symlink::Symlink;
use super::symlink::SymlinkError;
use super::utils::{parse_mount, MountOptionConfig};
use crate::syscall::syscall::create_syscall;
use crate::syscall::{linux, Syscall, SyscallError};
use crate::utils::PathBufExt;
#[derive(Debug, thiserror::Error)]
pub enum MountError {
#[error("no source in mount spec")]
NoSource,
#[error("io error")]
Io(#[from] std::io::Error),
#[error("syscall")]
Syscall(#[from] crate::syscall::SyscallError),
#[error("nix error")]
Nix(#[from] nix::Error),
#[error("failed to build oci spec")]
SpecBuild(#[from] oci_spec::OciSpecError),
#[error(transparent)]
Other(Box<dyn std::error::Error + Send + Sync>),
#[error("{0}")]
Custom(String),
#[error("symlink")]
Symlink(#[from] SymlinkError),
#[error("procfs failed")]
Procfs(#[from] procfs::ProcError),
#[error("unknown mount option: {0}")]
UnsupportedMountOption(String),
}
type Result<T> = std::result::Result<T, MountError>;
#[derive(Debug)]
pub struct MountOptions<'a> {
pub root: &'a Path,
pub label: Option<&'a str>,
#[allow(dead_code)]
pub cgroup_ns: bool,
}
pub struct Mount {
syscall: Box<dyn Syscall>,
}
impl Default for Mount {
fn default() -> Self {
Self::new()
}
}
impl Mount {
pub fn new() -> Mount {
Mount {
syscall: create_syscall(),
}
}
pub fn setup_mount(&self, mount: &SpecMount, options: &MountOptions) -> Result<()> {
tracing::debug!("mounting {:?}", mount);
let mut mount_option_config = parse_mount(mount)?;
match mount.typ().as_deref() {
Some("cgroup") => {
let cgroup_setup = libcgroups::common::get_cgroup_setup().map_err(|err| {
tracing::error!("failed to determine cgroup setup: {}", err);
MountError::Other(err.into())
})?;
match cgroup_setup {
Legacy | Hybrid => {
#[cfg(not(feature = "v1"))]
panic!("libcontainer can't run in a Legacy or Hybrid cgroup setup without the v1 feature");
#[cfg(feature = "v1")]
self.mount_cgroup_v1(mount, options).map_err(|err| {
tracing::error!("failed to mount cgroup v1: {}", err);
err
})?
}
Unified => {
#[cfg(not(feature = "v2"))]
panic!("libcontainer can't run in a Unified cgroup setup without the v2 feature");
#[cfg(feature = "v2")]
self.mount_cgroup_v2(mount, options, &mount_option_config)
.map_err(|err| {
tracing::error!("failed to mount cgroup v2: {}", err);
err
})?
}
}
}
_ => {
if *mount.destination() == PathBuf::from("/dev") {
mount_option_config.flags &= !MsFlags::MS_RDONLY;
self.mount_into_container(
mount,
options.root,
&mount_option_config,
options.label,
)
.map_err(|err| {
tracing::error!("failed to mount /dev: {}", err);
err
})?;
} else {
self.mount_into_container(
mount,
options.root,
&mount_option_config,
options.label,
)
.map_err(|err| {
tracing::error!("failed to mount {:?}: {}", mount, err);
err
})?;
}
}
}
Ok(())
}
#[cfg(feature = "v1")]
fn mount_cgroup_v1(&self, cgroup_mount: &SpecMount, options: &MountOptions) -> Result<()> {
tracing::debug!("mounting cgroup v1 filesystem");
// create tmpfs into which the cgroup subsystems will be mounted
let tmpfs = SpecMountBuilder::default()
.source("tmpfs")
.typ("tmpfs")
.destination(cgroup_mount.destination())
.options(
["noexec", "nosuid", "nodev", "mode=755"]
.iter()
.map(|o| o.to_string())
.collect::<Vec<String>>(),
)
.build()
.map_err(|err| {
tracing::error!("failed to build tmpfs for cgroup: {}", err);
err
})?;
self.setup_mount(&tmpfs, options).map_err(|err| {
tracing::error!("failed to mount tmpfs for cgroup: {}", err);
err
})?;
// get all cgroup mounts on the host system
let host_mounts: Vec<PathBuf> = libcgroups::v1::util::list_subsystem_mount_points()
.map_err(|err| {
tracing::error!("failed to get subsystem mount points: {}", err);
MountError::Other(err.into())
})?
.into_iter()
.filter(|p| p.as_path().starts_with(DEFAULT_CGROUP_ROOT))
.collect();
tracing::debug!("cgroup mounts: {:?}", host_mounts);
// get process cgroups
let ppid = std::os::unix::process::parent_id();
// The non-zero ppid means that the PID Namespace is not separated.
let ppid = if ppid == 0 { std::process::id() } else { ppid };
let root_cgroups = Process::new(ppid as i32)?.cgroups()?.0;
let process_cgroups: HashMap<String, String> = Process::myself()?
.cgroups()?
.into_iter()
.map(|c| {
let hierarchy = c.hierarchy;
// When youki itself is running inside a container, the cgroup path
// will include the path of pid-1, which needs to be stripped before
// mounting.
let root_pathname = root_cgroups
.iter()
.find(|c| c.hierarchy == hierarchy)
.map(|c| c.pathname.as_ref())
.unwrap_or("");
let path = c
.pathname
.strip_prefix(root_pathname)
.unwrap_or(&c.pathname);
(c.controllers.join(","), path.to_owned())
})
.collect();
tracing::debug!("Process cgroups: {:?}", process_cgroups);
let cgroup_root = options
.root
.join_safely(cgroup_mount.destination())
.map_err(|err| {
tracing::error!(
"could not join rootfs path with cgroup mount destination: {}",
err
);
MountError::Other(err.into())
})?;
tracing::debug!("cgroup root: {:?}", cgroup_root);
let symlink = Symlink::new();
// setup cgroup mounts for container
for host_mount in &host_mounts {
if let Some(subsystem_name) = host_mount.file_name().and_then(|n| n.to_str()) {
if options.cgroup_ns {
self.setup_namespaced_subsystem(
cgroup_mount,
options,
subsystem_name,
subsystem_name == "systemd",
)?;
} else {
self.setup_emulated_subsystem(
cgroup_mount,
options,
subsystem_name,
subsystem_name == "systemd",
host_mount,
&process_cgroups,
)?;
}
symlink.setup_comount_symlinks(&cgroup_root, subsystem_name)?;
} else {
tracing::warn!("could not get subsystem name from {:?}", host_mount);
}
}
Ok(())
}
// On some distros cgroup subsystems are comounted e.g. cpu,cpuacct or net_cls,net_prio. These systems
// have to be comounted in the container as well as the kernel will reject trying to mount them separately.
#[cfg(feature = "v1")]
fn setup_namespaced_subsystem(
&self,
cgroup_mount: &SpecMount,
options: &MountOptions,
subsystem_name: &str,
named: bool,
) -> Result<()> {
tracing::debug!(
"Mounting (namespaced) {:?} cgroup subsystem",
subsystem_name
);
let subsystem_mount = SpecMountBuilder::default()
.source("cgroup")
.typ("cgroup")
.destination(cgroup_mount.destination().join(subsystem_name))
.options(
["noexec", "nosuid", "nodev"]
.iter()
.map(|o| o.to_string())
.collect::<Vec<String>>(),
)
.build()
.map_err(|err| {
tracing::error!("failed to build {subsystem_name} mount: {err}");
err
})?;
let data: Cow<str> = if named {
format!("name={subsystem_name}").into()
} else {
subsystem_name.into()
};
let mount_options_config = MountOptionConfig {
flags: MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID | MsFlags::MS_NODEV,
data: data.to_string(),
rec_attr: None,
};
self.mount_into_container(
&subsystem_mount,
options.root,
&mount_options_config,
options.label,
)
.map_err(|err| {
tracing::error!("failed to mount {subsystem_mount:?}: {err}");
err
})
}
#[cfg(feature = "v1")]
fn setup_emulated_subsystem(
&self,
cgroup_mount: &SpecMount,
options: &MountOptions,
subsystem_name: &str,
named: bool,
host_mount: &Path,
process_cgroups: &HashMap<String, String>,
) -> Result<()> {
tracing::debug!("Mounting (emulated) {:?} cgroup subsystem", subsystem_name);
let named_hierarchy: Cow<str> = if named {
format!("name={subsystem_name}").into()
} else {
subsystem_name.into()
};
if let Some(proc_path) = process_cgroups.get(named_hierarchy.as_ref()) {
let emulated = SpecMountBuilder::default()
.source(
host_mount
.join_safely(proc_path.as_str())
.map_err(|err| {
tracing::error!(
"failed to join mount source for {subsystem_name} subsystem: {}",
err
);
MountError::Other(err.into())
})?,
)
.destination(
cgroup_mount
.destination()
.join_safely(subsystem_name)
.map_err(|err| {
tracing::error!(
"failed to join mount destination for {subsystem_name} subsystem: {}",
err
);
MountError::Other(err.into())
})?,
)
.typ("bind")
.options(
["rw", "rbind"]
.iter()
.map(|o| o.to_string())
.collect::<Vec<String>>(),
)
.build()?;
tracing::debug!("Mounting emulated cgroup subsystem: {:?}", emulated);
self.setup_mount(&emulated, options).map_err(|err| {
tracing::error!("failed to mount {subsystem_name} cgroup hierarchy: {}", err);
err
})?;
} else {
tracing::warn!("Could not mount {:?} cgroup subsystem", subsystem_name);
}
Ok(())
}
#[cfg(feature = "v2")]
fn mount_cgroup_v2(
&self,
cgroup_mount: &SpecMount,
options: &MountOptions,
mount_option_config: &MountOptionConfig,
) -> Result<()> {
tracing::debug!("Mounting cgroup v2 filesystem");
let cgroup_mount = SpecMountBuilder::default()
.typ("cgroup2")
.source("cgroup")
.destination(cgroup_mount.destination())
.options(Vec::new())
.build()?;
tracing::debug!("{:?}", cgroup_mount);
if self
.mount_into_container(
&cgroup_mount,
options.root,
mount_option_config,
options.label,
)
.is_err()
{
let host_mount = libcgroups::v2::util::get_unified_mount_point().map_err(|err| {
tracing::error!("failed to get unified mount point: {}", err);
MountError::Other(err.into())
})?;
let process_cgroup = Process::myself()
.map_err(|err| {
tracing::error!("failed to get /proc/self: {}", err);
MountError::Other(err.into())
})?
.cgroups()
.map_err(|err| {
tracing::error!("failed to get process cgroups: {}", err);
MountError::Other(err.into())
})?
.into_iter()
.find(|c| c.hierarchy == 0)
.map(|c| PathBuf::from(c.pathname))
.ok_or_else(|| {
MountError::Custom("failed to find unified process cgroup".into())
})?;
let bind_mount = SpecMountBuilder::default()
.typ("bind")
.source(host_mount.join_safely(process_cgroup).map_err(|err| {
tracing::error!("failed to join host mount for cgroup hierarchy: {}", err);
MountError::Other(err.into())
})?)
.destination(cgroup_mount.destination())
.options(Vec::new())
.build()
.map_err(|err| {
tracing::error!("failed to build cgroup bind mount: {}", err);
err
})?;
tracing::debug!("{:?}", bind_mount);
let mut mount_option_config = (*mount_option_config).clone();
mount_option_config.flags |= MsFlags::MS_BIND;
self.mount_into_container(
&bind_mount,
options.root,
&mount_option_config,
options.label,
)
.map_err(|err| {
tracing::error!("failed to bind mount cgroup hierarchy: {}", err);
err
})?;
}
Ok(())
}
/// Make parent mount of rootfs private if it was shared, which is required by pivot_root.
/// It also makes sure following bind mount does not propagate in other namespaces.
pub fn make_parent_mount_private(&self, rootfs: &Path) -> Result<Option<MountInfo>> {
let mount_infos = Process::myself()
.map_err(|err| {
tracing::error!("failed to get /proc/self: {}", err);
MountError::Other(err.into())
})?
.mountinfo()
.map_err(|err| {
tracing::error!("failed to get mount info: {}", err);
MountError::Other(err.into())
})?;
let parent_mount = find_parent_mount(rootfs, mount_infos.0)?;
// check parent mount has 'shared' propagation type
if parent_mount
.opt_fields
.iter()
.any(|field| matches!(field, MountOptFields::Shared(_)))
{
self.syscall.mount(
None,
&parent_mount.mount_point,
None,
MsFlags::MS_PRIVATE,
None,
)?;
Ok(Some(parent_mount))
} else {
Ok(None)
}
}
fn mount_into_container(
&self,
m: &SpecMount,
rootfs: &Path,
mount_option_config: &MountOptionConfig,
label: Option<&str>,
) -> Result<()> {
let typ = m.typ().as_deref();
let mut d = mount_option_config.data.to_string();
if let Some(l) = label {
if typ != Some("proc") && typ != Some("sysfs") {
match mount_option_config.data.is_empty() {
true => d = format!("context=\"{l}\""),
false => d = format!("{},context=\"{}\"", mount_option_config.data, l),
}
}
}
let dest_for_host = safe_path::scoped_join(rootfs, m.destination()).map_err(|err| {
tracing::error!(
"failed to join rootfs {:?} with mount destination {:?}: {}",
rootfs,
m.destination(),
err
);
MountError::Other(err.into())
})?;
let dest = Path::new(&dest_for_host);
let source = m.source().as_ref().ok_or(MountError::NoSource)?;
let src = if typ == Some("bind") {
let src = canonicalize(source).map_err(|err| {
tracing::error!("failed to canonicalize {:?}: {}", source, err);
err
})?;
let dir = if src.is_file() {
Path::new(&dest).parent().unwrap()
} else {
Path::new(&dest)
};
create_dir_all(dir).map_err(|err| {
tracing::error!("failed to create dir for bind mount {:?}: {}", dir, err);
err
})?;
if src.is_file() && !dest.exists() {
OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(dest)
.map_err(|err| {
tracing::error!("failed to create file for bind mount {:?}: {}", src, err);
err
})?;
}
src
} else {
create_dir_all(dest).map_err(|err| {
tracing::error!("failed to create device: {:?}", dest);
err
})?;
PathBuf::from(source)
};
if let Err(err) =
self.syscall
.mount(Some(&*src), dest, typ, mount_option_config.flags, Some(&*d))
{
if let SyscallError::Nix(errno) = err {
if !matches!(errno, Errno::EINVAL) {
tracing::error!("mount of {:?} failed. {}", m.destination(), errno);
return Err(err.into());
}
}
self.syscall
.mount(
Some(&*src),
dest,
typ,
mount_option_config.flags,
Some(&mount_option_config.data),
)
.map_err(|err| {
tracing::error!("failed to mount {src:?} to {dest:?}");
err
})?;
}
if typ == Some("bind")
&& mount_option_config.flags.intersects(
!(MsFlags::MS_REC
| MsFlags::MS_REMOUNT
| MsFlags::MS_BIND
| MsFlags::MS_PRIVATE
| MsFlags::MS_SHARED
| MsFlags::MS_SLAVE),
)
{
self.syscall
.mount(
Some(dest),
dest,
None,
mount_option_config.flags | MsFlags::MS_REMOUNT,
None,
)
.map_err(|err| {
tracing::error!("failed to remount {:?}: {}", dest, err);
err
})?;
}
if let Some(mount_attr) = &mount_option_config.rec_attr {
let open_dir = Dir::open(dest, OFlag::O_DIRECTORY, Mode::empty())?;
let dir_fd_pathbuf = PathBuf::from(format!("/proc/self/fd/{}", open_dir.as_raw_fd()));
self.syscall.mount_setattr(
-1,
&dir_fd_pathbuf,
linux::AT_RECURSIVE,
mount_attr,
mem::size_of::<linux::MountAttr>(),
)?;
}
Ok(())
}
}
/// Find parent mount of rootfs in given mount infos
pub fn find_parent_mount(
rootfs: &Path,
mount_infos: Vec<MountInfo>,
) -> std::result::Result<MountInfo, MountError> {
// find the longest mount point
let parent_mount_info = mount_infos
.into_iter()
.filter(|mi| rootfs.starts_with(&mi.mount_point))
.max_by(|mi1, mi2| mi1.mount_point.len().cmp(&mi2.mount_point.len()))
.ok_or_else(|| {
MountError::Custom(format!("can't find the parent mount of {:?}", rootfs))
})?;
Ok(parent_mount_info)
}
#[cfg(test)]
mod tests {
#[cfg(feature = "v1")]
use std::fs;
use anyhow::{Context, Ok, Result};
use super::*;
use crate::syscall::test::{MountArgs, TestHelperSyscall};
#[test]
fn test_mount_to_container() -> Result<()> {
let tmp_dir = tempfile::tempdir()?;
{
let m = Mount::new();
let mount = &SpecMountBuilder::default()
.destination(PathBuf::from("/dev/pts"))
.typ("devpts")
.source(PathBuf::from("devpts"))
.options(vec![
"nosuid".to_string(),
"noexec".to_string(),
"newinstance".to_string(),
"ptmxmode=0666".to_string(),
"mode=0620".to_string(),
"gid=5".to_string(),
])
.build()?;
let mount_option_config = parse_mount(mount)?;
assert!(m
.mount_into_container(
mount,
tmp_dir.path(),
&mount_option_config,
Some("defaults")
)
.is_ok());
let want = vec![MountArgs {
source: Some(PathBuf::from("devpts")),
target: tmp_dir.path().join("dev/pts"),
fstype: Some("devpts".to_string()),
flags: MsFlags::MS_NOSUID | MsFlags::MS_NOEXEC,
data: Some(
"newinstance,ptmxmode=0666,mode=0620,gid=5,context=\"defaults\"".to_string(),
),
}];
let got = &m
.syscall
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_mount_args();
assert_eq!(want, *got);
assert_eq!(got.len(), 1);
}
{
let m = Mount::new();
let mount = &SpecMountBuilder::default()
.destination(PathBuf::from("/dev/null"))
.typ("bind")
.source(tmp_dir.path().join("null"))
.options(vec!["ro".to_string()])
.build()?;
let mount_option_config = parse_mount(mount)?;
OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(tmp_dir.path().join("null"))?;
assert!(m
.mount_into_container(mount, tmp_dir.path(), &mount_option_config, None)
.is_ok());
let want = vec![
MountArgs {
source: Some(tmp_dir.path().join("null")),
target: tmp_dir.path().join("dev/null"),
fstype: Some("bind".to_string()),
flags: MsFlags::MS_RDONLY,
data: Some("".to_string()),
},
// remount one
MountArgs {
source: Some(tmp_dir.path().join("dev/null")),
target: tmp_dir.path().join("dev/null"),
fstype: None,
flags: MsFlags::MS_RDONLY | MsFlags::MS_REMOUNT,
data: None,
},
];
let got = &m
.syscall
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_mount_args();
assert_eq!(want, *got);
assert_eq!(got.len(), 2);
}
Ok(())
}
#[test]
fn test_make_parent_mount_private() -> Result<()> {
let tmp_dir = tempfile::tempdir()?;
let m = Mount::new();
let result = m.make_parent_mount_private(tmp_dir.path())?;
assert!(result.is_some());
if result.is_some() {
let set = m
.syscall
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_mount_args();
assert_eq!(set.len(), 1);
let got = &set[0];
assert_eq!(got.source, None);
assert_eq!(got.fstype, None);
assert_eq!(got.flags, MsFlags::MS_PRIVATE);
assert_eq!(got.data, None);
// This can be either depending on the system, some systems mount tmpfs at /tmp others it's
// a plain directory. See https://github.com/containers/youki/issues/471
assert!(got.target == PathBuf::from("/") || got.target == PathBuf::from("/tmp"));
}
Ok(())
}
#[test]
#[cfg(feature = "v1")]
fn test_namespaced_subsystem_success() -> Result<()> {
let tmp = tempfile::tempdir().unwrap();
let container_cgroup = Path::new("/container_cgroup");
let mounter = Mount::new();
let spec_cgroup_mount = SpecMountBuilder::default()
.destination(container_cgroup)
.source("cgroup")
.typ("cgroup")
.build()
.context("failed to build cgroup mount")?;
let mount_opts = MountOptions {
root: tmp.path(),
label: None,
cgroup_ns: true,
};
let subsystem_name = "cpu";
mounter
.setup_namespaced_subsystem(&spec_cgroup_mount, &mount_opts, subsystem_name, false)
.context("failed to setup namespaced subsystem")?;
let expected = MountArgs {
source: Some(PathBuf::from("cgroup")),
target: tmp
.path()
.join_safely(container_cgroup)?
.join(subsystem_name),
fstype: Some("cgroup".to_owned()),
flags: MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID | MsFlags::MS_NODEV,
data: Some("cpu".to_owned()),
};
let got = mounter
.syscall
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_mount_args();
assert_eq!(got.len(), 1);
assert_eq!(expected, got[0]);
Ok(())
}
#[test]
#[cfg(feature = "v1")]
fn test_emulated_subsystem_success() -> Result<()> {
// arrange
let tmp = tempfile::tempdir().unwrap();
let host_cgroup_mount = tmp.path().join("host_cgroup");
let host_cgroup = host_cgroup_mount.join("cpu/container1");
fs::create_dir_all(&host_cgroup)?;
let container_cgroup = Path::new("/container_cgroup");
let mounter = Mount::new();
let spec_cgroup_mount = SpecMountBuilder::default()
.destination(container_cgroup)
.source("cgroup")
.typ("cgroup")
.build()
.context("failed to build cgroup mount")?;
let mount_opts = MountOptions {
root: tmp.path(),
label: None,
cgroup_ns: false,
};
let subsystem_name = "cpu";
let mut process_cgroups = HashMap::new();
process_cgroups.insert("cpu".to_owned(), "container1".to_owned());
// act
mounter
.setup_emulated_subsystem(
&spec_cgroup_mount,
&mount_opts,
subsystem_name,
false,
&host_cgroup_mount.join(subsystem_name),
&process_cgroups,
)
.context("failed to setup emulated subsystem")?;
// assert
let expected = MountArgs {
source: Some(host_cgroup),
target: tmp
.path()
.join_safely(container_cgroup)?
.join(subsystem_name),
fstype: Some("bind".to_owned()),
flags: MsFlags::MS_BIND | MsFlags::MS_REC,
data: Some("".to_owned()),
};
let got = mounter
.syscall
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_mount_args();
assert_eq!(got.len(), 1);
assert_eq!(expected, got[0]);
Ok(())
}
#[test]
#[cfg(feature = "v1")]
fn test_mount_cgroup_v1() -> Result<()> {
// arrange
let tmp = tempfile::tempdir()?;
let container_cgroup = PathBuf::from("/sys/fs/cgroup");
let spec_cgroup_mount = SpecMountBuilder::default()
.destination(&container_cgroup)
.source("cgroup")
.typ("cgroup")
.build()
.context("failed to build cgroup mount")?;
let mount_opts = MountOptions {
root: tmp.path(),
label: None,
cgroup_ns: true,
};
let mounter = Mount::new();
// act
mounter
.mount_cgroup_v1(&spec_cgroup_mount, &mount_opts)
.context("failed to mount cgroup v1")?;
// assert
let mut got = mounter
.syscall
.as_any()
.downcast_ref::<TestHelperSyscall>()
.unwrap()
.get_mount_args()
.into_iter();
let host_mounts = libcgroups::v1::util::list_subsystem_mount_points()?;
assert_eq!(got.len(), host_mounts.len() + 1);
let expected = MountArgs {
source: Some(PathBuf::from("tmpfs".to_owned())),
target: tmp.path().join_safely(&container_cgroup)?,
fstype: Some("tmpfs".to_owned()),
flags: MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID | MsFlags::MS_NODEV,
data: Some("mode=755".to_owned()),
};
assert_eq!(expected, got.next().unwrap());
for (host_mount, act) in host_mounts.iter().zip(got) {
let subsystem_name = host_mount.file_name().and_then(|f| f.to_str()).unwrap();
let expected = MountArgs {
source: Some(PathBuf::from("cgroup".to_owned())),
target: tmp
.path()
.join_safely(&container_cgroup)?
.join(subsystem_name),
fstype: Some("cgroup".to_owned()),
flags: MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID | MsFlags::MS_NODEV,
data: Some(
if subsystem_name == "systemd" {
format!("name={subsystem_name}")
} else {
subsystem_name.to_string()
}
.to_owned(),
),
};
assert_eq!(expected, act);
}
Ok(())
}
#[test]
#[cfg(feature = "v2")]
fn test_mount_cgroup_v2() -> Result<()> {
// arrange
let tmp = tempfile::tempdir().unwrap();
let container_cgroup = PathBuf::from("/sys/fs/cgroup");
let spec_cgroup_mount = SpecMountBuilder::default()
.destination(&container_cgroup)
.source("cgroup")
.typ("cgroup")
.build()
.context("failed to build cgroup mount")?;
let mount_opts = MountOptions {
root: tmp.path(),
label: None,
cgroup_ns: true,
};
let mounter = Mount::new();
let flags = MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID | MsFlags::MS_NODEV;
// act
let mount_option_config = MountOptionConfig {
flags,
data: String::new(),
rec_attr: None,
};
mounter
.mount_cgroup_v2(&spec_cgroup_mount, &mount_opts, &mount_option_config)
.context("failed to mount cgroup v2")?;
// assert
let expected = MountArgs {
source: Some(PathBuf::from("cgroup".to_owned())),
target: tmp.path().join_safely(container_cgroup)?,
fstype: Some("cgroup2".to_owned()),
flags: MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID | MsFlags::MS_NODEV,
data: Some("".to_owned()),
};
let got = mounter
.syscall