#!/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
#
#
# Resume a container using CRIU (http://criu.org).
# Useful with recent upstream (i.e. non-OpenVZ) kernels.
# Requires criu to be installed.
#
# Parameters are passed in environment variables.
# Required parameters:
#   VE_ROOT       - container root directory
#   VE_DUMP_DIR   - directory for saving dump files
#   VE_STATE_FILE - file to write CT init PID to
# Optional parameters:
#   VE_VETH_DEVS  - pair of veth names (CT=HW\n)

exec 1>&2
. /usr/libexec/vzctl/scripts/vps-functions

vzcheckvar VE_ROOT
vzcheckvar VE_STATE_FILE
vzcheckvar VE_DUMP_DIR

veth_args=""
for dev in $VE_VETH_DEVS; do
	veth_args="$veth_args --veth-pair $dev"
done

ns_script=/usr/libexec/vzctl/scripts/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			\
		--pidfile $VE_STATE_FILE \
		$veth_args

if [ $? -eq 0 ]; then
	rm -rf $VE_DUMP_DIR
else
	echo The restore log was saved in $VE_DUMP_DIR/restore.log
	exit 1
fi
