From 5f78bba9cf698a5d0101ad85f3e2faac2bd6273b Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Fri, 12 Jul 2024 16:35:05 -0700 Subject: [PATCH] test/zdtm: mount a new tmpfs to the zdtm root /dev The current file system can be mounted with nodev. Fixes #2441 Signed-off-by: Andrei Vagin --- test/zdtm.py | 42 ++++++++++++++++++++++++++++++------------ test/zdtm/lib/ns.c | 11 ++++++++++- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/test/zdtm.py b/test/zdtm.py index df23ea03d9..284cb04158 100755 --- a/test/zdtm.py +++ b/test/zdtm.py @@ -74,7 +74,9 @@ def traceit(f, e, a): def clean_tests_root(): global tests_root if tests_root and tests_root[0] == os.getpid(): + subprocess.call(["./umount2", os.path.join(tests_root[1], "dev")]) os.rmdir(os.path.join(tests_root[1], "root")) + os.rmdir(os.path.join(tests_root[1], "dev")) os.rmdir(tests_root[1]) @@ -85,8 +87,11 @@ def make_tests_root(): tests_root = (os.getpid(), tempfile.mkdtemp("", "criu-root-", tmpdir)) atexit.register(clean_tests_root) os.mkdir(os.path.join(tests_root[1], "root")) + devpath = os.path.join(tests_root[1], "dev") + os.mkdir(devpath) + subprocess.check_call(["mount", "-t", "tmpfs", "criu-test-dev", devpath]) os.chmod(tests_root[1], 0o777) - return os.path.join(tests_root[1], "root") + return os.path.join(tests_root[1], "root"), os.path.join(tests_root[1], "dev") # Report generation @@ -182,15 +187,16 @@ def clean(): class ns_flavor: __root_dirs = [ - "/bin", "/sbin", "/etc", "/lib", "/lib64", "/dev", "/dev/pts", - "/dev/net", "/tmp", "/usr", "/proc", "/run" + "/bin", "/sbin", "/etc", "/lib", "/lib64", "/dev", + "/tmp", "/usr", "/proc", "/run" ] + __dev_dirs = ["pts", "net"] def __init__(self, opts): self.name = "ns" self.ns = True self.uns = False - self.root = make_tests_root() + self.root, self.devpath = make_tests_root() self.root_mounted = False def __copy_one(self, fname): @@ -236,15 +242,14 @@ def __copy_libs(self, binary): self.__copy_one(lib) def __mknod(self, name, rdev=None): - name = "/dev/" + name if not rdev: - if not os.access(name, os.F_OK): + if not os.access(os.path.join("/dev", name), os.F_OK): print("Skipping %s at root" % name) return else: - rdev = os.stat(name).st_rdev + rdev = os.stat(os.path.join("/dev", name)).st_rdev - name = self.root + name + name = os.path.join(self.devpath, name) os.mknod(name, stat.S_IFCHR, rdev) os.chmod(name, 0o666) @@ -256,6 +261,10 @@ def __construct_root(self): for ldir in ["/bin", "/sbin", "/lib", "/lib64"]: os.symlink(".." + ldir, self.root + "/usr" + ldir) + def __construct_dev(self): + for dir in self.__dev_dirs: + os.mkdir(os.path.join(self.devpath, dir)) + os.chmod(os.path.join(self.devpath, dir), 0o777) self.__mknod("tty", os.makedev(5, 0)) self.__mknod("null", os.makedev(1, 3)) self.__mknod("net/tun") @@ -283,6 +292,7 @@ def init(self, l_bins, x_bins): self.__construct_root() os.mknod(self.root + "/.constructed", stat.S_IFREG | 0o600) + self.__construct_dev() for b in l_bins: self.__copy_libs(b) for b in x_bins: @@ -480,6 +490,7 @@ def start(self): if self.__flavor.ns: env['ZDTM_NEWNS'] = "1" env['ZDTM_ROOT'] = self.__flavor.root + env['ZDTM_DEV'] = self.__flavor.devpath env['PATH'] = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" if self.__flavor.uns: @@ -510,6 +521,7 @@ def start(self): # unprivileged users. The permissions here are set to test # this use case. os.chmod(os.path.dirname(self.__flavor.root), 0o700) + os.chmod(os.path.dirname(self.__flavor.devpath), 0o755) def kill(self, sig=signal.SIGKILL): self.__freezer.thaw() @@ -587,12 +599,18 @@ def __getcropts(self): return opts def getdopts(self): - return self.__getcropts() + self.__freezer.getdopts( - ) + self.__desc.get('dopts', '').split() + opts = self.__getcropts() + self.__freezer.getdopts() + \ + self.__desc.get('dopts', '').split() + if self.__flavor.ns: + opts += ["--external", "mnt[/dev]:ZDTM_DEV"] + return opts def getropts(self): - return self.__getcropts() + self.__freezer.getropts( - ) + self.__desc.get('ropts', '').split() + opts = self.__getcropts() + self.__freezer.getropts() + \ + self.__desc.get('ropts', '').split() + if self.__flavor.ns: + opts += ["--external", "mnt[ZDTM_DEV]:%s" % self.__flavor.devpath] + return opts def unlink_pidfile(self): self.__pid = 0 diff --git a/test/zdtm/lib/ns.c b/test/zdtm/lib/ns.c index 6f6cccc992..79ee892521 100644 --- a/test/zdtm/lib/ns.c +++ b/test/zdtm/lib/ns.c @@ -27,7 +27,7 @@ extern int pivot_root(const char *new_root, const char *put_old); static int prepare_mntns(void) { int dfd, ret; - char *root, *criu_path; + char *root, *criu_path, *dev_path; char path[PATH_MAX]; root = getenv("ZDTM_ROOT"); @@ -51,6 +51,15 @@ static int prepare_mntns(void) return -1; } + dev_path = getenv("ZDTM_DEV"); + if (dev_path) { + snprintf(path, sizeof(path), "%s/dev", root); + if (mount(dev_path, path, NULL, MS_BIND | MS_REC, NULL)) { + pr_perror("Unable to mount %s", path); + return -1; + } + } + criu_path = getenv("ZDTM_CRIU"); if (criu_path) { snprintf(path, sizeof(path), "%s%s", root, criu_path);