Skip to content

Commit

Permalink
fix(overlay): If there are a large number of overlay layers, the cont…
Browse files Browse the repository at this point in the history
…ainer cannot be started
  • Loading branch information
zzzzzzzzzy9 committed May 27, 2024
1 parent 200f788 commit d9693b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ nix = { workspace = true, features = [
"socket",
"signal",
"mount",
"sched",
] }
oci-spec.workspace = true
page_size = "0.6.0"
Expand Down
18 changes: 17 additions & 1 deletion crates/shim/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use log::error;
#[cfg(target_os = "linux")]
use nix::mount::{mount, MsFlags};
#[cfg(target_os = "linux")]
use nix::sched::{unshare, CloneFlags};
#[cfg(target_os = "linux")]
use nix::unistd::{fork, ForkResult};

use crate::error::{Error, Result};
Expand Down Expand Up @@ -530,7 +532,15 @@ pub fn mount_rootfs(
target: impl AsRef<Path>,
) -> Result<()> {
//TODO add helper to mount fuse
//TODO compactLowerdirOption for overlay
let max_size = page_size::get();
// NOTE: 512 id a buffer during pagesize check.
let (chdir, options) =
if fs_type.unwrap_or("") == "overlay" && options_size(options) >= max_size - 512 {
LowerdirCompactor::new(options).compact()
} else {
(None, options.to_vec())
};

let mut flags: MsFlags = MsFlags::from_bits(0).unwrap();
let mut data = Vec::new();
options.iter().for_each(|x| {
Expand All @@ -552,6 +562,12 @@ pub fn mount_rootfs(
None
};

_ = unshare(CloneFlags::CLONE_FS).unwrap();
if let Some(workdir) = chdir {
env::set_current_dir(Path::new(&workdir)).unwrap_or_else(|_| {
unsafe { libc::_exit(i32::from(MountExitCode::ChdirErr)) };
});
}
// mount with non-propagation first, or remount with changed data
let oflags = flags.bitand(PROPAGATION_TYPES.not());
let zero: MsFlags = MsFlags::from_bits(0).unwrap();
Expand Down

0 comments on commit d9693b8

Please sign in to comment.