#!/bin/sh
#  Copyright (C) 2000-2015, 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
#
#
# Create container's private area.
#
# Parameters are passed in environment variables.
# Required parameters:
#   VE_PRVT		- path to root of CT private areas
#   PRIVATE_TEMPLATE	- path to private template used as a source for copying
#
# Optional parameters:
#   UID_OFFSET		- offset to be added to all tar UIDs
#   GID_OFFSET		- offset to be added to all tar GIDs

. @SCRIPTDIR@/vps-functions

vzcheckvar VE_PRVT PRIVATE_TEMPLATE

chown_preload_if_needed()
{
	[ -z "$UID_OFFSET" -o -z "$GID_OFFSET" ] && return

	export LD_PRELOAD=libvzchown.so
}

create_prvt()
{
	local AVAIL NEEDED HEADER OPT
	local TAR_OPT="--numeric-owner -Sp"

	[ -d "$VE_PRVT" ] ||
		vzerror "Destination directory does not exist: $VE_PRVT" ${VZ_FS_NEW_VE_PRVT}
	[ -f "$PRIVATE_TEMPLATE" ] ||
		vzerror "Tarball does not exist: $PRIVATE_TEMPLATE" ${VZ_FS_NEW_VE_PRVT}
	HEADER="$(od -A n -N 2 -t x1 -- "$PRIVATE_TEMPLATE")" ||
		vzerror "Invalid tarball: $PRIVATE_TEMPLATE" ${VZ_FS_NEW_VE_PRVT}
	AVAIL=$(awk "BEGIN {print $(stat -f -c '%a*%s/1024' $VE_PRVT)}")
	test "${AVAIL}0" -gt 0 ||
		vzerror "Failed to get available disk space on $VE_PRVT" ${VZ_FS_NEW_VE_PRVT}

	case $HEADER in
		' 1f 8b') # gzip
			OPT=-z
			NEEDED="$(gzip -l "$PRIVATE_TEMPLATE" | \
				awk 'END {print int($2/1024)}')"
			;;
		' 42 5a') # bzip2
			OPT=-j
			# No way to get uncompressed size, so guess
			NEEDED="$(ls -l "$PRIVATE_TEMPLATE" | \
				awk 'END {print int($5/1024*3)}')"
			;;
		' fd 37') # xz
			OPT=-J
			NEEDED="$(xz -l --robot "$PRIVATE_TEMPLATE" \
				2>/dev/null | \
				awk 'END {print int($5/1024)}')"
			# Not all xz versions implement -l, so guess
			if [ "$NEEDED" = "0" ] ; then
				NEEDED="$(ls -l "$PRIVATE_TEMPLATE" | \
					awk 'END {print int($5/1024*4.7)}')"
			fi

			;;
	esac
	# For the uncompressed case
	if [ -z "$NEEDED" ] ; then
		NEEDED="$(ls -l "$PRIVATE_TEMPLATE" | awk 'END {print $5/1024}')"
	fi

	[ "$AVAIL" -ge "$NEEDED" ] ||
		vzerror "Insufficient disk space in $VE_PRVT; available: $AVAIL, needed: $NEEDED" ${VZ_FS_NO_DISK_SPACE}
	CAT=cat

	chown_preload_if_needed

	# Use pv to show nice progress bar if we can
	pv -V >/dev/null 2>&1 && CAT=pv
	chmod 700 "$VE_PRVT"
	$CAT "$PRIVATE_TEMPLATE" | tar -C "$VE_PRVT" ${TAR_OPT} ${OPT} -x ||
		vzerror "Unpack $PRIVATE_TEMPLATE failed" ${VZ_FS_NEW_VE_PRVT}
	chmod 755 "$VE_PRVT"
}

create_prvt
exit 0
