[CRIU] [PATCH 2/2] namespaces: parametrized namespace option introduced

Kinsbursky Stanislav skinsbursky at openvz.org
Mon Jan 30 07:49:49 EST 2012


From: Stanislav Kinsbursky <skinsbursky at parallels.com>

Now '-n' option must be followed by namespaces tags, separated by commas.
Currently, only "uts" namespace is supported.

Signed-off-by: Stanislav Kinsbursky <skinsbursky at parallels.com>

---
 cr-dump.c            |    4 ++--
 cr-restore.c         |    8 +-------
 crtools.c            |   25 +++++++++++++++++++++++--
 include/crtools.h    |    2 +-
 include/namespaces.h |    2 +-
 namespaces.c         |    6 +++---
 test/zdtm.sh         |   12 ++++++------
 7 files changed, 37 insertions(+), 22 deletions(-)

diff --git a/cr-dump.c b/cr-dump.c
index dbae3d6..78d4b9b 100644
--- a/cr-dump.c
+++ b/cr-dump.c
@@ -1329,8 +1329,8 @@ int cr_dump_tasks(pid_t pid, struct cr_options *opts)
 	if (collect_pstree(pid, &pstree_list))
 		goto err;
 
-	if (opts->with_namespaces) {
-		ret = dump_namespaces(pid);
+	if (opts->namespaces_flags) {
+		ret = dump_namespaces(pid, opts->namespaces_flags);
 		if (ret < 0)
 			goto err;
 	}
diff --git a/cr-restore.c b/cr-restore.c
index 4c94936..220611a 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -1375,7 +1375,6 @@ static int restore_root_task(int fd, struct cr_options *opts)
 	struct pstree_entry e;
 	int ret, i;
 	struct sigaction act, old_act;
-	unsigned long ns_clone_flags;
 
 	ret = read(fd, &e, sizeof(e));
 	if (ret != sizeof(e)) {
@@ -1406,12 +1405,7 @@ static int restore_root_task(int fd, struct cr_options *opts)
 	 * this later.
 	 */
 
-	if (opts->with_namespaces)
-		ns_clone_flags = CLONE_NEWUTS;
-	else
-		ns_clone_flags = 0;
-
-	ret = fork_with_pid(e.pid, ns_clone_flags);
+	ret = fork_with_pid(e.pid, opts->namespaces_flags);
 	if (ret < 0)
 		return -1;
 
diff --git a/crtools.c b/crtools.c
index cde561b..2da7b4e 100644
--- a/crtools.c
+++ b/crtools.c
@@ -18,6 +18,7 @@
 #include "util.h"
 #include "log.h"
 #include "sockets.h"
+#include "syscall.h"
 
 static struct cr_options opts;
 struct page_entry zero_page_entry;
@@ -240,8 +241,11 @@ int main(int argc, char *argv[])
 	int opt, idx;
 	int action = -1;
 	int log_inited = 0;
+	char ns [8];
+	const char *ptr;
+	int n;
 
-	static const char short_opts[] = "df:p:t:hcD:o:n";
+	static const char short_opts[] = "df:p:t:hcD:o:n:";
 
 	BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
 
@@ -288,7 +292,24 @@ int main(int argc, char *argv[])
 			log_inited = 1;
 			break;
 		case 'n':
-			opts.with_namespaces = true;
+			ptr = optarg;
+			while ( sscanf(ptr, "%31[a-z]%n", ns, &n) == 1 )
+			{
+				if (!strcmp(ns, "uts"))
+					opts.namespaces_flags |= CLONE_NEWUTS;
+				else {
+					pr_err("Unknown namespace '%s'\n", ns);
+					return -1;
+				}
+				if (ptr[n] != ',') {
+					if (ptr[n] == '\0')
+						break;
+					pr_err("Unknown symbol '%c' (%d)\n",
+							ptr[n], ptr[n]);
+					return -1;
+				}
+				ptr += n + 1;
+			}
 			break;
 		case 'h':
 		default:
diff --git a/include/crtools.h b/include/crtools.h
index dd66e67..0a9f7ef 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -53,7 +53,7 @@ struct cr_options {
 	bool			leader_only;
 	bool			show_pages_content;
 	bool			restore_detach;
-	bool			with_namespaces;
+	unsigned int		namespaces_flags;
 };
 
 /* file descriptors template */
diff --git a/include/namespaces.h b/include/namespaces.h
index f7bd0fa..9e0c04f 100644
--- a/include/namespaces.h
+++ b/include/namespaces.h
@@ -1,6 +1,6 @@
 #ifndef __CR_NS_H__
 #define __CR_NS_H__
-int dump_namespaces(int pid);
+int dump_namespaces(int pid, unsigned int ns_flags);
 int prepare_namespace(int pid, unsigned long clone_flags);
 int try_show_namespaces(int pid);
 int switch_ns(int pid, int type, char *ns);
diff --git a/namespaces.c b/namespaces.c
index 7aa6f6c..ecca8b2 100644
--- a/namespaces.c
+++ b/namespaces.c
@@ -26,7 +26,7 @@ out:
 	return ret;
 }
 
-static int do_dump_namespaces(int ns_pid)
+static int do_dump_namespaces(int ns_pid, unsigned int ns_flags)
 {
 	struct cr_fdset *fdset;
 	int ret;
@@ -42,7 +42,7 @@ static int do_dump_namespaces(int ns_pid)
 
 }
 
-int dump_namespaces(int ns_pid)
+int dump_namespaces(int ns_pid, unsigned int ns_flags)
 {
 	int pid, ret, status;
 
@@ -66,7 +66,7 @@ int dump_namespaces(int ns_pid)
 	}
 
 	if (pid == 0) {
-		ret = do_dump_namespaces(ns_pid);
+		ret = do_dump_namespaces(ns_pid, ns_flags);
 		exit(ret);
 	}
 
diff --git a/test/zdtm.sh b/test/zdtm.sh
index 864b28e..8953967 100644
--- a/test/zdtm.sh
+++ b/test/zdtm.sh
@@ -31,7 +31,7 @@ $ZP/static/caps00
 $ZP/static/cmdlinenv00
 $ZP/static/socket_listen"
 
-NS_TEST_LIST="\
+UTS_TEST_LIST="\
 $ZP/static/utsname"
 
 CRTOOLS=`pwd`/`dirname $0`/../crtools
@@ -91,15 +91,15 @@ if [ $# -eq 0 ]; then
 	for t in $TEST_LIST; do
 		run_test $t "" || case_error $t
 	done
-	for t in $NS_TEST_LIST; do
-		run_test $t "-n" || case_error $t
+	for t in $UTS_TEST_LIST; do
+		run_test $t "-n UTS" || case_error $t
 	done
 elif [ "$1" == "-l" ]; then
 	echo $TEST_LIST | sed -e "s#$ZP/##g" -e 's/ /\n/g'
-	echo $NS_TEST_LIST | sed -e "s#$ZP/##g" -e 's/ /\n/g'
+	echo $UTS_TEST_LIST | sed -e "s#$ZP/##g" -e 's/ /\n/g'
 else
-	if echo "$NS_TEST_LIST" | fgrep -q "$1" ; then
-		run_test "$ZP/$1" "-n" || case_error "$ZP/$1"
+	if echo "$UTS_TEST_LIST" | fgrep -q "$1" ; then
+		run_test "$ZP/$1" "-n uts" || case_error "$ZP/$1"
 	else
 		run_test "$ZP/$1" || case_error "$ZP/$1"
 	fi



More information about the CRIU mailing list