Skip to content

Commit

Permalink
Fix mount /proc, /dev issue.
Browse files Browse the repository at this point in the history
Signed-off-by: Klaus Ma <klausm@nvidia.com>
  • Loading branch information
k82cn committed Oct 17, 2024
1 parent fa646c5 commit 879c06e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion busybox.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name: busybox
image: busybox
entrypoint: ["/bin/ls", "/"]
entrypoint: ["/bin/ls", "/proc"]
25 changes: 11 additions & 14 deletions src/core/cmd/runc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use std::ffi::{CString};
use std::ffi::CString;
use std::fs;
use std::io::{Read, Write};
use std::io::Write;
use std::{thread, time};

use nix::{
Expand All @@ -25,7 +25,7 @@ use nix::{
stat::Mode,
wait::{wait, WaitStatus},
},
unistd::{chdir, dup2, execve, getgid, getpid, getuid, pivot_root, Pid},
unistd::{chdir, dup2, execve, getgid, getpid, getuid, pivot_root, Gid, Pid, Uid},
};

use flate2::read::GzDecoder;
Expand Down Expand Up @@ -123,7 +123,8 @@ fn setup_user_mapping(pid: Pid) -> ChariotResult<()> {
let mut file = fs::File::create(uid_map)?;
file.write_all(mapping.as_bytes())?;

// Disable setgroups for unpriviledge user.
// Disable setgroups for unpriviledge user;
// otherwise, we can not setup group mapping.
let setgroups = format!("/proc/{}/setgroups", pid);
let mut file = fs::File::create(setgroups)?;
file.write_all(b"deny")?;
Expand All @@ -142,20 +143,16 @@ fn setup_user_mapping(pid: Pid) -> ChariotResult<()> {
fn run_container(cxt: cfg::Context, container: Container) -> ChariotResult<()> {
// Waiting for user mapping ready.
let ten_millis = time::Duration::from_millis(10);
let mut gid_map = fs::File::open("/proc/self/gid_map")?;
let mut mapping = String::new();

loop {
gid_map.read_to_string(&mut mapping)?;
if mapping.trim().len() > 0 {
if getgid() == Gid::from(0) && getuid() == Uid::from(0) {
break;
}

thread::sleep(ten_millis);
}

// Start to run container
tracing::debug!(
"Run sandbox <{}> in <{}> as <{}:{}>.",
"Run container <{}> in <{}> as <{}:{}>.",
container.name,
getpid(),
getuid(),
Expand All @@ -173,9 +170,6 @@ fn run_container(cxt: cfg::Context, container: Container) -> ChariotResult<()> {
// Change the root of container by pivot_root.
change_root(cxt.clone(), container.clone())?;

// Setup fstab, e.g. /proc, /dev.
// setup_fstab(cxt.clone(), container.clone())?;

let cmd = CString::new(container.entrypoint[0].as_bytes())?;
let args = container
.entrypoint
Expand Down Expand Up @@ -248,6 +242,9 @@ fn change_root(cxt: cfg::Context, container: Container) -> ChariotResult<()> {

pivot_root(rootfs.as_str(), rootfs.as_str())?;

// Setup fstab, e.g. /proc, /dev, before unmount parent FS.
setup_fstab(cxt.clone(), container.clone())?;

tracing::debug!("Detach the rootfs from parent system.");
mount(
None::<&str>,
Expand Down

0 comments on commit 879c06e

Please sign in to comment.