Hoặc là unshare (1) bị hỏng hoặc tôi ngu ngốc.
Tôi đã sửa đổi mã trong http://crosbymichael.com/creating-containers-part-1.html Vì vậy, nó thực sự sẽ làm việc cho tôi. Phải uể oải / Proc uể oải với umount2và bao gồm linux/sched.h thay vì sched.h.
Để biên dịch gcc foo.c -ofoo.
Bạn sẽ lưu ý rằng sau khi chạy, ví dụ: ./foo ls /proc, / Proc trên hệ thống máy chủ sẽ không bị xóa sổ.
//
// This compiles and works on Amazon Linux 2016.03 (kernel 4.4.5-15.26.amzn1.x86_64)
//
#include <stdio.h>
#include <stdlib.h>
// was <sched.h>, but wouldn't compile on Amazon Linux
#include <linux/sched.h>
// for umount2()
#include <sys/mount.h>
#include <sys/wait.h>
#include <errno.h>
#define STACKSIZE (1024*1024)
static char child_stack[STACKSIZE];
struct clone_args {
char **argv;
};
static int child_exec(void *stuff) {
struct clone_args *args = (struct clone_args *)stuff;
// the fprintf()s crash. Not sure why.
// changed from umount(), lazy umount succeeds
if (umount2("/proc", MNT_DETACH) != 0) {
fprintf(stderr, "failed to unmount /proc: %s\n", strerror(errno));
exit(-1);
}
if (mount("proc", "/proc", "proc", 0, "") != 0) {
fprintf(stderr, "failed to mount /proc: %s\n", strerror(errno));
exit(-1);
}
if (execvp(args->argv[0], args->argv) != 0) {
fprintf(stderr, "failed to execvp arguments: %s\n", strerror(errno));
exit(-1);
}
// unreachable
exit(EXIT_FAILURE);
}
int main(int argc, char **argv) {
struct clone_args args;
args.argv = &argv[1];
int clone_flags = CLONE_NEWPID | CLONE_NEWNS | SIGCHLD;
pid_t pid = clone(child_exec, child_stack + STACKSIZE, clone_flags, &args);
if (pid < 0) {
fprintf(stderr, "clone failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (waitpid(pid, NULL, 0) == -1) {
fprintf(stderr, "failed to wait pid %d\n", pid);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
write failed /proc/self/gid_map: Operation not permittedtrừ khi tôi đã chorootđặc quyền; Khi tôi làm điều này, nó hoạt động như bạn mô tả và tất cả người dùng bị mất quyền truy cập và gắn kết: chỉ khởi động lại cho phép tôi khôi phục. Việc tôi đọc hướng dẫn đồng tình với bạn, vì vậy tôi kết luận rằng cả hai chúng tôi đều hiểu lầm hoặc việc thực hiện là hoàn toàn sai. Trong cả hai trường hợp, tôi không thể thấy một cách để giải pháp, ngoài việc thử một số ví dụ mã hóa.