#!/bin/bash
# Copyright (c) 2015-2017, Parallels International GmbH. All rights reserved.
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.

. /etc/vz/vz.conf

reboot_disabled()
{
	grep -q -e '^[[:space:]]*ALLOWREBOOT=no' -e '^[[:space:]]*ALLOWREBOOT="no"' $1 2>/dev/null
	return $?
}

[ "$ALLOWREBOOT" = "no" ] && exit 0

if reboot_disabled /etc/vz/conf/$ID.conf; then
	exit 0
fi

opt="--quiet"
for ((i=0; i<20; i++)); do
	if ! vzctl --quiet status $ID | grep -wq running; then
		vzctl $opt start $ID
		[ $? -ne 9 ] && break

		# Process CT locked error
		if grep -q 'Backing' "$LOCKDIR/$ID.lck"; then
			opt="$opt --skiplock"
			continue
		fi
	fi
	sleep 5;
done

exit 0
