#!/bin/bash
# Copyright (c) 2017, Parallels International GmbH. All rights reserved.
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
#
ctid=
SYSFSDEV=/sys/fs/cgroup/devices/machine.slice

eval $(. /etc/vz/vz.conf && echo ON_VE_FSERROR="${ON_VE_FSERROR}")

[ "$ON_VE_FSERROR" = "stop" ] || exit

read -r DEV < /sys/class/block/$ID/dev
[ -z "$DEV" ] && exit 0

for d in `ls -1 $SYSFSDEV`; do
	[ -d "$SYSFSDEV/$d" ] || continue
	if grep "b $DEV r" $SYSFSDEV/$d/devices.list; then
		ctid=$d
		break;
	fi
done

echo "`date --iso-8601=seconds` fserror : Container $ctid : fserror on $ID/$DEV" >> /var/log/vzctl.log
[ -z "$ctid" ] && exit 0

DEV="${ID%%p1}"
read -r DELTA < /sys/class/block/$DEV/pdelta/0/image
/usr/sbin/vzctl stop $ctid --fast >/dev/null 2>&1
if [ $? -ne 0 -a $? -ne 9 ]; then
	read -r DELTA_NEW < /sys/class/block/$DEV/pdelta/0/image
	[ "$DELTA" = "$DELTA_NEW" ] && ploop umount -d /dev/$DEV
fi
