Skip to content

Commit dafb1b6

Browse files
zzzzzzzzzy9mxpv
authored andcommitted
fix(overlay): If there are a large number of overlay layers, the container cannot be started
1 parent 200f788 commit dafb1b6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

crates/shim/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ nix = { workspace = true, features = [
4545
"socket",
4646
"signal",
4747
"mount",
48+
"sched",
4849
] }
4950
oci-spec.workspace = true
5051
page_size = "0.6.0"

crates/shim/src/mount.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use log::error;
2828
#[cfg(target_os = "linux")]
2929
use nix::mount::{mount, MsFlags};
3030
#[cfg(target_os = "linux")]
31+
use nix::sched::{unshare, CloneFlags};
32+
#[cfg(target_os = "linux")]
3133
use nix::unistd::{fork, ForkResult};
3234

3335
use crate::error::{Error, Result};
@@ -530,7 +532,15 @@ pub fn mount_rootfs(
530532
target: impl AsRef<Path>,
531533
) -> Result<()> {
532534
//TODO add helper to mount fuse
533-
//TODO compactLowerdirOption for overlay
535+
let max_size = page_size::get();
536+
// NOTE: 512 id a buffer during pagesize check.
537+
let (chdir, options) =
538+
if fs_type.unwrap_or("") == "overlay" && options_size(options) >= max_size - 512 {
539+
LowerdirCompactor::new(options).compact()
540+
} else {
541+
(None, options.to_vec())
542+
};
543+
534544
let mut flags: MsFlags = MsFlags::from_bits(0).unwrap();
535545
let mut data = Vec::new();
536546
options.iter().for_each(|x| {
@@ -552,6 +562,12 @@ pub fn mount_rootfs(
552562
None
553563
};
554564

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

0 commit comments

Comments
 (0)