Blob Blame History Raw
From 8f1a5dadf72c917336c06f028b58111b6c00fd50 Mon Sep 17 00:00:00 2001
From: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
Date: Tue, 18 Feb 2020 13:41:18 +0300
Subject: [PATCH 169/245] zdtm: add new mnt_ext_root test

This test simply creates a) root external mount and b) "deeper"
bindmount for it (deeper in terms of mnt_depth). Our mount restore code
tries to mount (b) first and fails (without previous patch ordering
external mounts before their binds).

Cherry-picked from Virtuozzo criu:
https://src.openvz.org/projects/OVZ/repos/criu/commits/d31954669

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
---
 test/zdtm/static/Makefile          |  1 +
 test/zdtm/static/mnt_ext_root.c    | 88 ++++++++++++++++++++++++++++++
 test/zdtm/static/mnt_ext_root.desc |  5 ++
 test/zdtm/static/mnt_ext_root.hook | 12 ++++
 4 files changed, 106 insertions(+)
 create mode 100644 test/zdtm/static/mnt_ext_root.c
 create mode 100644 test/zdtm/static/mnt_ext_root.desc
 create mode 100755 test/zdtm/static/mnt_ext_root.hook

diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
index 33e15f85a..a64c51cd0 100644
--- a/test/zdtm/static/Makefile
+++ b/test/zdtm/static/Makefile
@@ -399,6 +399,7 @@ TST_DIR		=				\
 		mnt_ext_auto			\
 		mnt_ext_master			\
 		mnt_ext_dev			\
+		mnt_ext_root			\
 		mnt_tracefs			\
 		mntns_deleted			\
 		unlink_regular00		\
diff --git a/test/zdtm/static/mnt_ext_root.c b/test/zdtm/static/mnt_ext_root.c
new file mode 100644
index 000000000..9f9554d51
--- /dev/null
+++ b/test/zdtm/static/mnt_ext_root.c
@@ -0,0 +1,88 @@
+#include <sched.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <linux/limits.h>
+
+#include "zdtmtst.h"
+
+const char *test_doc = "Check root external mount with \"deepper\" bind";
+const char *test_author = "Pavel Tikhomirov <ptikhomirov@virtuozzo.com>";
+
+char *source = "zdtm_ext_root";
+char *dirname = "mnt_ext_root.test";
+TEST_OPTION(dirname, string, "directory name", 1);
+
+#define BUF_SIZE 4096
+
+int main(int argc, char **argv)
+{
+	char *root, testdir[PATH_MAX];
+	char dst[PATH_MAX], deep_bind[PATH_MAX];
+	char *tmp = "/tmp/zdtm_ext_root.tmp";
+	char *zdtm_newns = getenv("ZDTM_NEWNS");
+
+	root = getenv("ZDTM_ROOT");
+	if (root == NULL) {
+		pr_perror("root");
+		return 1;
+	}
+
+	if (!zdtm_newns) {
+		pr_perror("ZDTM_NEWNS is not set");
+		return 1;
+	} else if (strcmp(zdtm_newns, "1")) {
+		goto test;
+	}
+
+	/* Prepare directories in test root */
+	sprintf(testdir, "%s/%s", root, dirname);
+	mkdir(testdir, 0755);
+
+	sprintf(dst, "%s/%s/dst", root, dirname);
+	mkdir(dst, 0755);
+	sprintf(deep_bind, "%s/%s/deep", root, dirname);
+	mkdir(deep_bind, 0755);
+	sprintf(deep_bind, "%s/%s/deep/bind", root, dirname);
+	mkdir(deep_bind, 0755);
+
+	/* Prepare mount in criu root */
+	mkdir(tmp, 0755);
+	if (mount(source, tmp, "tmpfs", 0, NULL)) {
+		pr_perror("mount tmpfs");
+		return 1;
+	}
+	if (mount(NULL, tmp, NULL, MS_PRIVATE, NULL)) {
+		pr_perror("make private");
+		return 1;
+	}
+
+	/*
+	 * Create temporary mntns, next mounts will not show up in criu mntns
+	 */
+	if (unshare(CLONE_NEWNS)) {
+		pr_perror("unshare");
+		return 1;
+	}
+
+	/*
+	 * Populate to the tests mntns root mounts
+	 */
+	if (mount(tmp, dst, NULL, MS_BIND, NULL)) {
+		pr_perror("bind");
+		return 1;
+	}
+
+	if (mount(tmp, deep_bind, NULL, MS_BIND, NULL)) {
+		pr_perror("bind");
+		return 1;
+	}
+
+test:
+	test_init(argc, argv);
+
+	test_daemon();
+	test_waitsig();
+
+	pass();
+	return 0;
+}
diff --git a/test/zdtm/static/mnt_ext_root.desc b/test/zdtm/static/mnt_ext_root.desc
new file mode 100644
index 000000000..121dbb0a7
--- /dev/null
+++ b/test/zdtm/static/mnt_ext_root.desc
@@ -0,0 +1,5 @@
+{   'dopts': '--external mnt[/mnt_ext_root.test/dst]:ZDTM',
+    'feature': 'mnt_id',
+    'flavor': 'ns uns',
+    'flags': 'suid',
+    'ropts': '--external mnt[ZDTM]:/tmp/zdtm_ext_root.tmp'}
diff --git a/test/zdtm/static/mnt_ext_root.hook b/test/zdtm/static/mnt_ext_root.hook
new file mode 100755
index 000000000..c022f91ad
--- /dev/null
+++ b/test/zdtm/static/mnt_ext_root.hook
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+[ "$1" == "--clean" ] || exit 0
+
+TMP="/tmp/zdtm_ext_root.tmp"
+echo "Cleanup mnt_ext_sharing"
+umount "$TMP"
+rm -rf $TMP
+
+rm -rf "mnt_ext_root.test"
+
+exit 0
-- 
2.35.1