Skip to content

Commit 6aa8018

Browse files
zzzzzzzzzy9mxpv
authored andcommitted
move mount to mount_linux and mount_other
Signed-off-by: zzzzzzzzzy9 <[email protected]>
1 parent 220d6d6 commit 6aa8018

File tree

3 files changed

+50
-32
lines changed

3 files changed

+50
-32
lines changed

crates/shim/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ pub mod cgroup;
5555
pub mod event;
5656
pub mod logger;
5757
pub mod monitor;
58-
pub mod mount;
58+
#[cfg(target_os = "linux")]
59+
pub mod mount_linux;
60+
#[cfg(not(target_os = "linux"))]
61+
pub mod mount_other;
62+
#[cfg(target_os = "linux")]
63+
pub use mount_linux as mount;
64+
#[cfg(not(target_os = "linux"))]
65+
pub use mount_other as mount;
5966
mod reap;
6067
#[cfg(not(feature = "async"))]
6168
pub mod synchronous;

crates/shim/src/mount.rs renamed to crates/shim/src/mount_linux.rs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![cfg(not(windows))]
21
/*
32
Copyright The containerd Authors.
43
@@ -25,30 +24,27 @@ use std::{
2524

2625
use lazy_static::lazy_static;
2726
use log::error;
28-
#[cfg(target_os = "linux")]
29-
use nix::mount::{mount, MsFlags};
30-
#[cfg(target_os = "linux")]
31-
use nix::sched::{unshare, CloneFlags};
32-
#[cfg(target_os = "linux")]
33-
use nix::unistd::{fork, ForkResult};
27+
use nix::{
28+
mount::{mount, MsFlags},
29+
sched::{unshare, CloneFlags},
30+
unistd::{fork, ForkResult},
31+
};
3432

3533
use crate::error::{Error, Result};
3634
#[cfg(not(feature = "async"))]
3735
use crate::monitor::{monitor_subscribe, wait_pid, Topic};
3836

39-
#[cfg(target_os = "linux")]
4037
struct Flag {
4138
clear: bool,
4239
flags: MsFlags,
4340
}
4441

4542
const OVERLAY_LOWERDIR_PREFIX: &str = "lowerdir=";
4643

47-
#[cfg(target_os = "linux")]
4844
lazy_static! {
4945
static ref MOUNT_FLAGS: HashMap<&'static str, Flag> = {
5046
let mut mf = HashMap::new();
51-
let zero: MsFlags = MsFlags::from_bits(0).unwrap();
47+
let zero: MsFlags = MsFlags::empty();
5248
mf.insert(
5349
"async",
5450
Flag {
@@ -267,20 +263,17 @@ fn longest_common_prefix(dirs: &[String]) -> &str {
267263
// NOTE: the snapshot id is based on digits.
268264
// in order to avoid to get snapshots/x, should be back to parent dir.
269265
// however, there is assumption that the common dir is ${root}/io.containerd.v1.overlayfs/snapshots.
270-
#[cfg(target_os = "linux")]
271266
fn trim_flawed_dir(s: &str) -> String {
272267
s[0..s.rfind('/').unwrap_or(0) + 1].to_owned()
273268
}
274269

275-
#[cfg(target_os = "linux")]
276270
#[derive(Default)]
277271
struct LowerdirCompactor {
278272
options: Vec<String>,
279273
lowerdirs: Option<Vec<String>>,
280274
lowerdir_prefix: Option<String>,
281275
}
282276

283-
#[cfg(target_os = "linux")]
284277
impl LowerdirCompactor {
285278
fn new(options: &[String]) -> Self {
286279
Self {
@@ -409,7 +402,6 @@ impl From<MountExitCode> for Result<()> {
409402
}
410403

411404
#[cfg(not(feature = "async"))]
412-
#[cfg(target_os = "linux")]
413405
pub fn mount_rootfs(
414406
fs_type: Option<&str>,
415407
source: Option<&str>,
@@ -428,7 +420,7 @@ pub fn mount_rootfs(
428420
(None, options.to_vec())
429421
};
430422

431-
let mut flags: MsFlags = MsFlags::from_bits(0).unwrap();
423+
let mut flags: MsFlags = MsFlags::empty();
432424
let mut data = Vec::new();
433425
options.iter().for_each(|x| {
434426
if let Some(f) = MOUNT_FLAGS.get(x.as_str()) {
@@ -467,7 +459,7 @@ pub fn mount_rootfs(
467459
}
468460
// mount with non-propagation first, or remount with changed data
469461
let oflags = flags.bitand(PROPAGATION_TYPES.not());
470-
let zero: MsFlags = MsFlags::from_bits(0).unwrap();
462+
let zero: MsFlags = MsFlags::empty();
471463
if flags.bitand(MsFlags::MS_REMOUNT).eq(&zero) || data.is_some() {
472464
mount(source, target.as_ref(), fs_type, oflags, data).unwrap_or_else(|err| {
473465
error!(
@@ -524,7 +516,6 @@ pub fn mount_rootfs(
524516
}
525517

526518
#[cfg(feature = "async")]
527-
#[cfg(target_os = "linux")]
528519
pub fn mount_rootfs(
529520
fs_type: Option<&str>,
530521
source: Option<&str>,
@@ -541,7 +532,7 @@ pub fn mount_rootfs(
541532
(None, options.to_vec())
542533
};
543534

544-
let mut flags: MsFlags = MsFlags::from_bits(0).unwrap();
535+
let mut flags: MsFlags = MsFlags::empty();
545536
let mut data = Vec::new();
546537
options.iter().for_each(|x| {
547538
if let Some(f) = MOUNT_FLAGS.get(x.as_str()) {
@@ -562,15 +553,15 @@ pub fn mount_rootfs(
562553
None
563554
};
564555

565-
unshare(CloneFlags::CLONE_FS).unwrap();
566556
if let Some(workdir) = chdir {
557+
unshare(CloneFlags::CLONE_FS)?;
567558
env::set_current_dir(Path::new(&workdir)).unwrap_or_else(|_| {
568559
unsafe { libc::_exit(i32::from(MountExitCode::ChdirErr)) };
569560
});
570561
}
571562
// mount with non-propagation first, or remount with changed data
572563
let oflags = flags.bitand(PROPAGATION_TYPES.not());
573-
let zero: MsFlags = MsFlags::from_bits(0).unwrap();
564+
let zero: MsFlags = MsFlags::empty();
574565
if flags.bitand(MsFlags::MS_REMOUNT).eq(&zero) || data.is_some() {
575566
mount(source, target.as_ref(), fs_type, oflags, data).map_err(mount_error!(
576567
e,
@@ -605,18 +596,7 @@ pub fn mount_rootfs(
605596
Ok(())
606597
}
607598

608-
#[cfg(not(target_os = "linux"))]
609-
pub fn mount_rootfs(
610-
fs_type: Option<&str>,
611-
source: Option<&str>,
612-
options: &[String],
613-
target: impl AsRef<Path>,
614-
) -> Result<()> {
615-
Err(Error::Unimplemented("start".to_string()))
616-
}
617-
618599
#[cfg(test)]
619-
#[cfg(target_os = "linux")]
620600
mod tests {
621601
use super::*;
622602

crates/shim/src/mount_other.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
#![allow(unused)]
17+
18+
use std::path::Path;
19+
20+
use crate::error::{Error, Result};
21+
22+
pub fn mount_rootfs(
23+
fs_type: Option<&str>,
24+
source: Option<&str>,
25+
options: &[String],
26+
target: impl AsRef<Path>,
27+
) -> Result<()> {
28+
// On on-Linux systems, we should return OK
29+
// instead of exiting with an error.
30+
Ok(())
31+
}

0 commit comments

Comments
 (0)