[CRIU] [PATCH 1/3] zdtm: Construct all root dirs and files in launcher
Pavel Emelyanov
xemul at parallels.com
Tue Dec 1 02:03:10 PST 2015
Instead of doing some dirs/files/links in launcher and some in
libzdtm, make everything in the launcher. The library is then
just to set up the namespaces.
Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
test/zdtm.py | 35 +++++++++++++++++++------
test/zdtm.sh | 11 ++++++++
test/zdtm/lib/ns.c | 75 +-----------------------------------------------------
3 files changed, 39 insertions(+), 82 deletions(-)
diff --git a/test/zdtm.py b/test/zdtm.py
index a889e0f..c892ecc 100755
--- a/test/zdtm.py
+++ b/test/zdtm.py
@@ -142,6 +142,32 @@ class ns_flavor:
for lib in libs:
self.__copy_one(lib)
+ def __mknod(self, name, rdev = None):
+ name = "/dev/" + name
+ if not rdev:
+ if not os.access(name, os.F_OK):
+ print "Skipping %s at root" % name
+ return
+ else:
+ rdev = os.stat(name).st_rdev
+
+ name = self.root + name
+ os.mknod(name, stat.S_IFCHR, rdev)
+ os.chmod(name, 0666)
+
+ def __construct_root(self):
+ for dir in ["/bin", "/sbin", "/etc", "/lib", "/lib64", "/dev", "/dev/pts", "/tmp", "/usr", "/proc"]:
+ os.mkdir(self.root + dir)
+ os.chmod(self.root + dir, 0777)
+
+ for ldir in [ "/bin", "/sbin", "/lib", "/lib64" ]:
+ os.symlink(".." + ldir, self.root + "/usr" + ldir)
+
+ self.__mknod("tty", os.makedev(5, 0))
+ self.__mknod("null", os.makedev(1, 3))
+ self.__mknod("net/tun")
+ self.__mknod("rtc")
+
def init(self, test_bin, deps):
subprocess.check_call(["mount", "--make-private", "--bind", ".", self.root])
self.root_mounted = True
@@ -151,14 +177,7 @@ class ns_flavor:
fcntl.flock(o, fcntl.LOCK_EX)
if not os.access(self.root + "/.constructed", os.F_OK):
print "Construct root for %s" % test_bin
- for dir in ["/bin", "/sbin", "/etc", "/lib", "/lib64", "/dev", "/tmp", "/usr"]:
- os.mkdir(self.root + dir)
- os.chmod(self.root + dir, 0777)
-
- os.mknod(self.root + "/dev/tty", stat.S_IFCHR, os.makedev(5, 0))
- os.chmod(self.root + "/dev/tty", 0666)
- for ldir in [ "/bin", "/sbin", "/lib", "/lib64" ]:
- os.symlink(".." + ldir, self.root + "/usr" + ldir)
+ self.__construct_root()
os.mknod(self.root + "/.constructed", stat.S_IFREG | 0600)
self.__copy_libs(test_bin)
diff --git a/test/zdtm.sh b/test/zdtm.sh
index b6fd0a8..f15bcbe 100755
--- a/test/zdtm.sh
+++ b/test/zdtm.sh
@@ -541,7 +541,18 @@ construct_root()
done
mkdir $root/dev
+ mkdir $root/dev/pts
mknod -m 0666 $root/dev/tty c 5 0
+ mknod -m 0666 $root/dev/null c 1 3
+ if [ -r "/dev/net/tun" ]; then
+ mkdir $root/dev/net/
+ mknod -m 0666 $root/dev/net/tun $(stat -c "0x%t" /dev/net/tun) $(stat -c "0x%T" /dev/net/tun)
+ fi
+ if [ -r "/dev/rtc" ]; then
+ mknod -m 0666 $root/dev/rtc $(stat -c "0x%t" /dev/rtc) $(stat -c "0x%T" /dev/rtc)
+ fi
+
+ mkdir $root/proc
# make 'tmp' dir under new root
mkdir -p $tmpdir
diff --git a/test/zdtm/lib/ns.c b/test/zdtm/lib/ns.c
index dec4344..ed3c241 100644
--- a/test/zdtm/lib/ns.c
+++ b/test/zdtm/lib/ns.c
@@ -77,11 +77,6 @@ static int prepare_mntns()
return -1;
}
- if (mkdir("proc", 0777) && errno != EEXIST) {
- fprintf(stderr, "mkdir(proc) failed: %m\n");
- return -1;
- }
-
/*
* proc and sysfs can be mounted in an unprivileged namespace,
* if they are already mounted when the user namespace is created.
@@ -97,14 +92,6 @@ static int prepare_mntns()
return -1;
}
- if (mkdir("/dev", 0755) && errno != EEXIST) {
- fprintf(stderr, "mkdir(/dev) failed: %m\n");
- return -1;
- }
- if (mkdir("/dev/pts", 0755) && errno != EEXIST) {
- fprintf(stderr, "mkdir(/dev/pts) failed: %m\n");
- return -1;
- }
if (mount("pts", "/dev/pts", "devpts", MS_MGC_VAL, "mode=666,ptmxmode=666,newinstance")) {
fprintf(stderr, "mount(/dev/pts) failed: %m\n");
return -1;
@@ -127,14 +114,7 @@ static int prepare_mntns()
return -1;
}
}
- if (access("/dev/tty", F_OK)) {
- if (mknod("/dev/tty", 0666 | S_IFCHR, makedev(5, 0)) == 0) {
- chmod("/dev/tty", 0666);
- } else if (errno != EEXIST) {
- fprintf(stderr, "mknod(/dev/tty) failed: %m\n");
- return -1;
- }
- }
+
if (fchdir(dfd)) {
fprintf(stderr, "fchdir() failed: %m\n");
return -1;
@@ -340,56 +320,6 @@ int ns_init(int argc, char **argv)
exit(1);
}
-static int construct_root()
-{
- struct stat st;
- char *root;
- int dfd;
-
- root = getenv("ZDTM_ROOT");
- if (!root) {
- fprintf(stderr, "ZDTM_ROOT isn't set\n");
- return -1;
- }
-
- dfd = open(".", O_RDONLY);
- if (dfd == -1) {
- fprintf(stderr, "open(.) failed: %m\n");
- return -1;
- }
- if (chdir(root)) {
- fprintf(stderr, "chdir(%s): %m\n", root);
- return -1;
- }
-
- mkdir("dev", 0777);
- chmod("dev", 0777);
- mknod("dev/null", 0777 | S_IFCHR, makedev(1, 3));
- chmod("dev/null", 0777);
- if (stat("/dev/net/tun", &st))
- fprintf(stderr, "Unable to stat /dev/net/tun: %m");
- else {
- mkdir("dev/net", 0777);
- mknod("dev/net/tun", 0777 | S_IFCHR, st.st_rdev);
- chmod("dev/net/tun", 0777);
- }
-
- if (stat("/dev/rtc", &st)) {
- fprintf(stderr, "Unable to stat /dev/rtc: %m");
- return -1;
- }
- mknod("dev/rtc", 0777 | S_IFCHR, st.st_rdev);
- chmod("dev/rtc", 0777);
-
- if (fchdir(dfd)) {
- fprintf(stderr, "fchdir() failed: %m\n");
- return -1;
- }
- close(dfd);
-
- return 0;
-}
-
#define UID_MAP "0 100000 100000\n100000 200000 50000"
#define GID_MAP "0 400000 50000\n50000 500000 100000"
void ns_create(int argc, char **argv)
@@ -417,9 +347,6 @@ void ns_create(int argc, char **argv)
if (val)
flags |= CLONE_NEWUSER;
- if (construct_root())
- exit(1);
-
pid = clone(ns_exec, args.stack_ptr, flags, &args);
if (pid < 0) {
fprintf(stderr, "clone() failed: %m\n");
--
1.9.3
More information about the CRIU
mailing list