[CRIU] [PATCH 1/2] vzctl: synchronize CRIU with vzctl (v2)

Andrey Vagin avagin at openvz.org
Fri May 31 16:10:35 EDT 2013


vzctl provides two descriptors signalfd and waitfd, it's used for apply
host-side configuration after creating environment.
A signal is send to signalfd after creating environment and an answer is
recieved from waitfd.

v2: fix comments from Kir

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 scripts/Makefile.am    |  3 ++-
 scripts/vps-rst-env.in | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 scripts/vps-rst.in     |  3 +++
 src/lib/hooks_ct.c     | 31 +++++++++++++++++++------------
 4 files changed, 73 insertions(+), 13 deletions(-)
 create mode 100755 scripts/vps-rst-env.in

diff --git a/scripts/Makefile.am b/scripts/Makefile.am
index 05381f8..e77f19d 100644
--- a/scripts/Makefile.am
+++ b/scripts/Makefile.am
@@ -29,7 +29,8 @@ script_SCRIPTS = \
 	vzevent-reboot \
 	vps-pci	\
 	vps-cpt \
-	vps-rst
+	vps-rst \
+	vps-rst-env
 
 EXTRA_DIST = \
 	$(script_SCRIPTS:%=%.in)
diff --git a/scripts/vps-rst-env.in b/scripts/vps-rst-env.in
new file mode 100755
index 0000000..6e48626
--- /dev/null
+++ b/scripts/vps-rst-env.in
@@ -0,0 +1,49 @@
+#!/bin/sh
+#  Copyright (C) 2013, Parallels, Inc. All rights reserved.
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software
+#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+# This script is called by CRIU (http://criu.org) after creating namespaces.
+#
+# Parameters are passed in environment variables.
+# Required parameters:
+#   VZCTL_PID	  - pid of vzctl
+#   STATUSFD	  - file descriptor for sending signal to vzctl
+#   WAITFD	  - file descriptor for receiving signal from vzctl
+#   VE_STATE_FILE - file with PID of container init process
+#   VE_NETNS_FILE - file, which used by ip netns
+#   CRTOOLS_SCRIPT_ACTION - current action
+
+exec 1>&2
+. @SCRIPTDIR@/vps-functions
+
+vzcheckvar CRTOOLS_SCRIPT_ACTION
+[ "$CRTOOLS_SCRIPT_ACTION" = "setup-namespaces" ] || exit 0
+
+vzcheckvar VZCTL_PID
+vzcheckvar STATUSFD
+vzcheckvar WAITFD
+vzcheckvar VE_NETNS_FILE
+vzcheckvar VE_STATE_FILE
+
+set -e
+
+pid=$(cat $VE_STATE_FILE)
+ln -s /proc/$pid/ns/net $VE_NETNS_FILE
+
+printf '\0\0\0\0' > /proc/$VZCTL_PID/fd/$STATUSFD
+ret=$(cat /proc/$VZCTL_PID/fd/$WAITFD | hexdump -e '"%d"' -n 4)
+
+[ "$ret" -eq "0" ]
diff --git a/scripts/vps-rst.in b/scripts/vps-rst.in
index 91080b3..e33f464 100755
--- a/scripts/vps-rst.in
+++ b/scripts/vps-rst.in
@@ -40,12 +40,15 @@ for dev in $VE_VETH_DEVS; do
 	veth_args="$veth_args --veth-pair $dev"
 done
 
+ns_script=`dirname $0`/vps-rst-env
+
 criu restore	--file-locks		\
 		--tcp-established	\
 		--evasive-devices	\
 		--link-remap		\
 		--root $VE_ROOT		\
 		--restore-detached	\
+		--action-script $ns_script \
 		-D $VE_DUMP_DIR		\
 		-o restore.log		\
 		-vvvv			\
diff --git a/src/lib/hooks_ct.c b/src/lib/hooks_ct.c
index 1472da3..c69be64 100644
--- a/src/lib/hooks_ct.c
+++ b/src/lib/hooks_ct.c
@@ -391,6 +391,7 @@ static int ct_env_create_real(struct arg_start *arg)
 	int userns_p[2];
 	int ret, fd;
 	char pidpath[STR_SIZE];
+	char ctpath[STR_SIZE];
 
 	stack_size = get_pagesize();
 	if (stack_size < 0)
@@ -479,13 +480,20 @@ static int ct_env_create_real(struct arg_start *arg)
 		close(userns_p[1]);
 	}
 
+	snprintf(ctpath, STR_SIZE, "%s/%d", NETNS_RUN_DIR, arg->veid);
+	snprintf(pidpath, STR_SIZE, "/proc/%d/ns/net", ret);
+	if (symlink(pidpath, ctpath)) {
+		logger(-1, errno, "Can't symlink into netns file %s", ctpath);
+		destroy_container(arg->veid);
+		return -VZ_RESOURCE_ERROR;
+	}
+
 	return ret;
 }
 
 int ct_env_create(struct arg_start *arg)
 {
 	int ret;
-	char procpath[STR_SIZE];
 	char ctpath[STR_SIZE];
 
 	/* non-fatal */
@@ -520,14 +528,6 @@ int ct_env_create(struct arg_start *arg)
 	if (ret < 0)
 		return -ret;
 
-	snprintf(procpath, STR_SIZE, "/proc/%d/ns/net", ret);
-	ret = symlink(procpath, ctpath);
-	if (ret) {
-		logger(-1, errno, "Can't symlink into netns file %s", ctpath);
-		destroy_container(arg->veid);
-		return VZ_RESOURCE_ERROR;
-	}
-
 	return 0;
 }
 
@@ -925,7 +925,7 @@ static int ct_chkpnt(vps_handler *h, envid_t veid,
 static int ct_restore_fn(vps_handler *h, envid_t veid, const vps_res *res,
 			  int wait_p, int old_wait_p, int err_p, void *data)
 {
-	char *argv[2], *env[5];
+	char *argv[2], *env[9];
 	const char *dumpfile = NULL;
 	const char *statefile = NULL;
 	cpt_param *param = data;
@@ -958,8 +958,15 @@ static int ct_restore_fn(vps_handler *h, envid_t veid, const vps_res *res,
 				"%s=%s\n", veth->dev_name_ve, veth->dev_name);
 	}
 	env[3] = strdup(buf);
-
-	env[4] = NULL;
+	snprintf(buf, sizeof(buf), "VZCTL_PID=%d", getpid());
+	env[4] = strdup(buf);
+	snprintf(buf, sizeof(buf), "STATUSFD=%d", STDIN_FILENO);
+	env[5] = strdup(buf);
+	snprintf(buf, sizeof(buf), "WAITFD=%d", wait_p);
+	env[6] = strdup(buf);
+	snprintf(buf, sizeof(buf), "VE_NETNS_FILE=%s/%d", NETNS_RUN_DIR, veid);
+	env[7] = strdup(buf);
+	env[8] = NULL;
 
 	ret = run_script(argv[0], argv, env, 0);
 	free_arg(env);
-- 
1.8.2



More information about the CRIU mailing list