File: vps-create.in

package info (click to toggle)
vzctl 3.0.30.2-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,056 kB
  • sloc: ansic: 17,282; sh: 14,549; makefile: 528
file content (84 lines) | stat: -rwxr-xr-x 2,778 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/sh
#  Copyright (C) 2000-2009, 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

. @PKGLIBDIR@/scripts/vps-functions

vzcheckvar VE_PRVT PRIVATE_TEMPLATE

create_prvt()
{
	local TMP 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}
	TMP="$(df -P "$VE_PRVT")" ||
		vzerror "Failed to calculate available disk space on $VE_PRVT" ${VZ_FS_NEW_VE_PRVT}
	AVAIL="$(echo "$TMP" | awk 'END{print $4}')"

	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 -lk "$PRIVATE_TEMPLATE" | \
				awk 'END {print int($5*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 -lk "$PRIVATE_TEMPLATE" | \
					awk 'END {print int($5*4.7)}')"
			fi

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

	[ "$AVAIL" -ge "$NEEDED" ] ||
		vzerror "Insufficient disk space in $VE_PRVT; available: $AVAIL, needed: $NEEDED" ${VZ_FS_NO_DISK_SPACE}
	chmod 700 "$VE_PRVT"
	tar -C "$VE_PRVT" ${TAR_OPT} ${OPT} -xf "$PRIVATE_TEMPLATE" ||
		vzerror "Error in tar ${TAR_OPT} ${OPT} -xf $PRIVATE_TEMPLATE" ${VZ_FS_NEW_VE_PRVT}
	chmod 755 "$VE_PRVT"
}

create_prvt
exit 0