[CRIU] [PATCH 8/8] vzctl: checkpoint/restoer of upstream CTs

Andrey Vagin avagin at openvz.org
Wed May 15 12:10:53 EDT 2013


It can be done by any third party tools.
sripts/ct-cpt and script/ct_rst are executed to dump and restore CT
This patch adds scripts for CRIU.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 scripts/ct-cpt     | 19 ++++++++++++
 scripts/ct-rst     | 17 ++++++++++
 src/lib/hooks_ct.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 126 insertions(+), 1 deletion(-)
 create mode 100755 scripts/ct-cpt
 create mode 100755 scripts/ct-rst

diff --git a/scripts/ct-cpt b/scripts/ct-cpt
new file mode 100755
index 0000000..87c09dc
--- /dev/null
+++ b/scripts/ct-cpt
@@ -0,0 +1,19 @@
+#!/bin/sh
+mkdir $CT_CR_DIR &&
+crtools dump	--file-locks		\
+		--tcp-established	\
+		--evasive-devices	\
+		--link-remap		\
+		--root $CT_ROOT		\
+		-t $CT_PID		\
+		-D $CT_CR_DIR		\
+		-o dump.log		\
+		-vvvv
+if [ $? -ne 0 ]; then
+	[ -d $CT_CR_DIR.fail ] && rm -rf $CT_CR_DIR.fail
+	mv -f $CT_CR_DIR $CT_CR_DIR.fail
+	echo All dump files and logs were saved in $CT_CR_DIR.fail
+	exit 1
+else
+	echo Checkpointing finished successfully
+fi
diff --git a/scripts/ct-rst b/scripts/ct-rst
new file mode 100755
index 0000000..8953ec6
--- /dev/null
+++ b/scripts/ct-rst
@@ -0,0 +1,17 @@
+#!/bin/sh
+crtools restore	--file-locks		\
+		--tcp-established	\
+		--evasive-devices	\
+		--link-remap		\
+		--root $CT_ROOT		\
+		--restore-detached	\
+		-D $CT_CR_DIR		\
+		-o restore.log		\
+		-vvvv			\
+		--pidfile $CT_STATE
+if [ $? -eq 0 ]; then
+	rm -rf $CT_CR_DIR
+else
+	echo The restore log was saved in $CT_CR_DIR/restore.log
+	exit 1
+fi
diff --git a/src/lib/hooks_ct.c b/src/lib/hooks_ct.c
index 3c82823..97c8695 100644
--- a/src/lib/hooks_ct.c
+++ b/src/lib/hooks_ct.c
@@ -17,6 +17,7 @@
 #include "logger.h"
 #include "script.h"
 #include "cgroup.h"
+#include "cpt.h"
 
 #define NETNS_RUN_DIR "/var/run/netns"
 
@@ -307,7 +308,10 @@ int ct_env_create(struct arg_start *arg)
 		return VZ_RESOURCE_ERROR;
 	}
 
-	ret = ct_env_create_real(arg);
+	if (arg->fn)
+		ret = arg->fn(arg->h, arg->veid, arg->res, arg->wait_p, arg->old_wait_p, arg->err_p, arg->data);
+	else
+		ret = ct_env_create_real(arg);
 
 	snprintf(procpath, STR_SIZE, "/proc/%d/ns/net", ret);
 	ret = symlink(procpath, ctpath);
@@ -597,6 +601,89 @@ static int ct_setcontext(envid_t veid)
 	return 0;
 }
 
+static int ct_chkpnt(vps_handler *h, envid_t veid, const vps_res *res,
+		int cmd, cpt_param *param)
+{
+	const char *dumpfile = NULL;
+	char pidstr[STR_SIZE], buf[STR_SIZE];
+	int ret, fd;
+	char *arg[2];
+	char *env[4];
+
+	ret = VZ_CHKPNT_ERROR;
+
+	get_dump_file(veid, param->dumpdir, buf, sizeof(buf));
+	dumpfile = strdup(buf);
+
+	arg[0] = SCRIPTDIR "/ct-cpt";
+	arg[1] = NULL;
+
+	get_state_file(veid, res->env.statedir, pidstr, sizeof(pidstr));
+	fd = open(pidstr, O_RDONLY);
+	if (fd == -1) {
+		logger(-1, 0, "Unable to open the pid file");
+		return VZ_CHKPNT_ERROR;
+	}
+
+	ret = read(fd, pidstr, sizeof(pidstr) - 1);
+	if (ret == -1) {
+		logger(-1, 0, "Unable to read pid");
+		return VZ_CHKPNT_ERROR;
+	}
+	pidstr[ret] = '\0';
+
+	snprintf(buf, sizeof(buf), "CT_ROOT=%s", res->fs.root);
+	env[0] = strdup(buf);
+	snprintf(buf, sizeof(buf), "CT_PID=%s", pidstr);
+	env[1] = strdup(buf);
+	snprintf(buf, sizeof(buf), "CT_CR_DIR=%s", dumpfile);
+	env[2] = strdup(buf);
+	env[3] = NULL;
+
+	if (run_script(arg[0], arg, env, 0))
+		return ret;
+
+	return 0;
+}
+
+static int ct_env_restore(vps_handler *h, envid_t veid, vps_res *res,
+			  int wait_p, int old_wait_p, int err_p, void *data)
+{
+	char *env[4];
+	char *argv[2];
+	char buf[STR_SIZE];
+	const char *dumpfile = NULL;
+	const char *statefile = NULL;
+	cpt_param *param = data;
+
+	get_dump_file(veid, param->dumpdir, buf, sizeof(buf));
+	dumpfile = strdup(buf);
+
+	get_state_file(veid, res->env.statedir, buf, sizeof(buf));
+	statefile = strdup(buf);
+
+	argv[0] = SCRIPTDIR "/ct-rst";
+	argv[1] = NULL;
+
+	snprintf(buf, sizeof(buf), "CT_ROOT=%s", res->fs.root);
+	env[0] = strdup(buf);
+	snprintf(buf, sizeof(buf), "CT_CR_DIR=%s", dumpfile);
+	env[1] = strdup(buf);
+	snprintf(buf, sizeof(buf), "CT_STATE=%s", statefile);
+	env[2] = strdup(buf);
+	env[3] = NULL;
+	if (run_script(argv[0], argv, env, 0))
+		return VZ_RESTORE_ERROR;
+
+	return 0;
+}
+
+static int ct_restore(vps_handler *h, envid_t veid, vps_param *vps_p, int cmd,
+	cpt_param *param, skipFlags skip)
+{
+	return vps_start_custom(h, veid, vps_p, SKIP_CONFIGURE | skip, NULL, ct_env_restore, param);
+}
+
 int ct_do_open(vps_handler *h)
 {
 	int ret;
@@ -630,6 +717,8 @@ int ct_do_open(vps_handler *h)
 	h->enter = ct_enter;
 	h->destroy = ct_destroy;
 	h->env_create = ct_env_create;
+	h->env_chkpnt = ct_chkpnt;
+	h->env_restore = ct_restore;
 	h->setlimits = ct_setlimits;
 	h->setcpus = ct_setcpus;
 	h->setcontext = ct_setcontext;
-- 
1.8.2



More information about the CRIU mailing list