Skip to content

Commit

Permalink
test/zdtm: mount a new tmpfs to the zdtm root /dev
Browse files Browse the repository at this point in the history
The current file system can be mounted with nodev.

Fixes checkpoint-restore#2441

Signed-off-by: Andrei Vagin <[email protected]>
  • Loading branch information
avagin committed Jul 13, 2024
1 parent a3badeb commit 9e16ffc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
44 changes: 32 additions & 12 deletions test/zdtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])


Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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")
Expand Down Expand Up @@ -283,6 +292,9 @@ def init(self, l_bins, x_bins):
self.__construct_root()
os.mknod(self.root + "/.constructed", stat.S_IFREG | 0o600)

if not os.access(self.devpath + "/.constructed", os.F_OK):
self.__construct_dev()
os.mknod(self.devpath + "/.constructed", stat.S_IFREG | 0o600)
for b in l_bins:
self.__copy_libs(b)
for b in x_bins:
Expand Down Expand Up @@ -480,6 +492,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:
Expand Down Expand Up @@ -510,6 +523,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()
Expand Down Expand Up @@ -587,12 +601,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
Expand Down
11 changes: 10 additions & 1 deletion test/zdtm/lib/ns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);

Check warning on line 58 in test/zdtm/lib/ns.c

View workflow job for this annotation

GitHub Actions / build

return -1;
}
}

criu_path = getenv("ZDTM_CRIU");
if (criu_path) {
snprintf(path, sizeof(path), "%s%s", root, criu_path);
Expand Down

0 comments on commit 9e16ffc

Please sign in to comment.