File: vserver-build.functions

package info (click to toggle)
util-vserver 0.30.216-pre2864-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,576 kB
  • ctags: 4,462
  • sloc: ansic: 23,615; sh: 15,550; xml: 1,893; makefile: 402; python: 301; perl: 85; awk: 4
file content (298 lines) | stat: -rw-r--r-- 7,353 bytes parent folder | download | duplicates (2)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# $Id: vserver-build.functions 2765 2008-08-22 21:41:47Z dhozac $	--*- sh -*--

# Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#  
# 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; version 2 of the License.
#  
# 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.

# Expected environment:
#     $VSERVER_NAME ... name of vserver

ROOTDIR=
ROOTDIR_REL=
CACHEDIR=
CACHEDIR_REL=
VSERVERDIRNAME=

VDIR=

_DEV_FILE=
_EXEC_DIR=

BUILD_INITPRE=
BUILD_INITPOST=

__BASE_GENERATED_FILES=()
__BASE_SUCCESS=

function makeDevEntry
{
    local dst=$1/$2
    case "$3" in
	(c|b)	mknod -m$6 "$dst"  $3 $4 $5;;
	(d)	mkdir -p -m$4 "$dst";;
	(f)	touch "$dst"
		chmod $4 "$dst"
		;;
	(l)	ln -s "$4" "$dst"
		;;
	(*)	echo "Unknown dev-entry mode '$3'" >&2
		false
		;;
    esac
}

function populateDev
{
    local spec

    mkdir -p -m755 "$VDIR"/dev
    mkdir -m755 "$VDIR"/dev/pts

    while read spec; do
	makeDevEntry "$VDIR"/dev $spec
    done <$_DEV_FILE
}

function populateDirectory
{
    local dst=$1
    local i
    
    shift
    for i; do
	local file=
	
	for file in "$i"/*; do
	    isRegularFile "$file" || continue
	    
	    cp -a "$file" "$dst/"
	done
    done
}

function _setRootDir
{
    test -z "$ROOTDIR" || return 0
    
    for item in "\"$__CONFDIR/.defaults/vdirbase\" 1" "$__DEFAULT_VSERVERDIR"; do
	eval set -- "$item"
	ROOTDIR=$1
	ROOTDIR_REL=$2
	test ! -d "$ROOTDIR" || break
    done

    test -d "$ROOTDIR" || {
	echo "Root-directory '$ROOTDIR' does not exist or is invalid" >&2
	exit 1
    }
}

function _setCacheDir
{
    test -z "$CACHEDIR" || return 0
    
    for item in "\"$__CONFDIR/.defaults/cachebase\" 1" "$__PKGCACHEDIR"; do
	eval set -- "$item"
	CACHEDIR=$1
	CACHEDIR_REL=$2
	test ! -d "$CACHEDIR" || break
    done

    test -d "$CACHEDIR" || {
	echo "Cache-directory '$CACHEDIR' does not exist or is invalid" >&2
	exit 1
    }
}

function _setVserverDirName
{
    test -z "$VSERVERDIRNAME" || return 0
    VSERVERDIRNAME="$VSERVER_NAME"
}

function _setVdir
{
    VDIR="$ROOTDIR/$VSERVERDIRNAME"
}

function say
{
    test -z "$OPTION_SILENT" || return 0
    echo "$@"
}

function _renameVserverCfg
{
    local suffix=.~$(date +'%s')~
    local i
    
    for i in "$VDIR" "$SETUP_CONFDIR"; do
	test ! -e "$i" || isDirectoryEmpty "$i" || {
	    mv "$i" "$i$suffix"
	    say "Renamed '$i' to '$i$suffix'"
	}
    done
}


## Usage: getDistribution [<default>] [<ignore-config>]
function getDistribution
{
    local ignore_config=$2
    
    if test -z "$DISTRIBUTION"; then
	if test -e /etc/fedora-release; then
	    set -- $(cat /etc/fedora-release)
	    DISTRIBUTION=fdr$4
	elif test -e /etc/redhat-release; then
	    set -- $(cat /etc/redhat-release)
	    DISTRIBUTION=rh$5
	elif test -e /etc/debian_version; then
	    set -- $(cat /etc/debian_version)
	    DISTRIBUTION=deb$1
	elif test -e /etc/SuSE-release; then
	    set -- $(cat /etc/SuSE-release)
	    DISTRIBUTION=suse$3
	elif test -e /etc/gentoo-release; then
	    set -- $(cat /etc/gentoo-release)
	    DISTRIBUTION=gentoo$5
	elif test -e /etc/slackware-version; then
	    set -- $(cat /etc/slackware-version)
	    DISTRIBUTION=slackware$2
	elif test -n "$1"; then
	    DISTRIBUTION=$1
	else
	    colpanic $"\
ERROR: Can not determine distribution; please specify it manually with
  the '-d' option."
	fi
    fi

    test -n "$ignore_config" -o \
         -d "$__CONFDIR/.distributions/$DISTRIBUTION" -o \
         -d "$__DISTRIBDIR/$DISTRIBUTION" ||
	    colpanic $"\
ERROR: Can not find configuration for the distribution '$DISTRIBUTION';
  please read http://linux-vserver.org/HowToRegisterNewDistributions
  for information how to add support for your own distribution."

    export DISTRIBUTION
}

function base._addGeneratedFile
{
    __BASE_GENERATED_FILES=( "${__BASE_GENERATED_FILES[@]}" "$@" )
}

## Usage: initFilesystem [force]
function base.initFilesystem
{
    test -z "$1" || _renameVserverCfg
    { isDirectoryEmpty "$VDIR" && test  ! -e "$SETUP_CONFDIR"; } || colpanic $"\
vserver-topdirectory '$VDIR' and/or configuration at '$SETUP_CONFDIR'
exist already; please try to use '--force', or remove them manually."

    mkdir -p -m755 "$VDIR"
    $_SETATTR --~barrier "$VDIR"
    $_SETATTR --barrier "$VDIR"/.. || colwarn $"\
WARNING: could not set the barrier attribute on '$VDIR/..',
         please set it manually."
    base._addGeneratedFile "$VDIR"
    
    mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/etc
    base._addGeneratedFile "$SETUP_CONFDIR"
    
    ln -s "$VDIR"                     "$SETUP_CONFDIR/vdir"
    ln -s "$CACHEDIR/$VSERVERDIRNAME" "$SETUP_CONFDIR/cache"

    populateDev

    mkdir -p "$VDIR"/proc
    findAndCopy "$VDIR"/etc/hosts         "$__CONFDIR"/.defaults/files/hosts "$__CONFDIR/.distributions/$DISTRIBUTION"/files/hosts \
	                                  "$__DISTRIBDIR/$DISTRIBUTION"/files/hosts "$__DISTRIBDIR"/defaults/files/hosts ""

    for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime resolv.conf; do
	findAndCopy "$VDIR"/etc/$i  "$__CONFDIR/.defaults/files/$i" "$__CONFDIR/.distributions/$DISTRIBUTION/files/$i" ""
    done
}

function base._initVariables
{
    _setRootDir
    _setCacheDir
    _setVserverDirName
    _setVdir

    findFile _DEV_FILE      "$__CONFDIR/.distributions/$DISTRIBUTION/devs"      "$__DISTRIBDIR/$DISTRIBUTION/devs"     "$__DISTRIBDIR/defaults/devs"
    findDir  _EXECDIR       "$__CONFDIR/.distributions/$DISTRIBUTION/execdir"   "$__DISTRIBDIR/$DISTRIBUTION/execdir"  /
    findFile BUILD_INITPRE  "$__CONFDIR/.distributions/$DISTRIBUTION/initpre"   "$__DISTRIBDIR/$DISTRIBUTION/initpre"  ""
    findFile BUILD_INITPOST "$__CONFDIR/.distributions/$DISTRIBUTION/initpost"  "$__DISTRIBDIR/$DISTRIBUTION/initpost" ""
}

declare -a __BASE_CLEANUP
function base.pushCleanup
{
    __BASE_CLEANUP=( "${__BASE_CLEANUP[@]}" "$*" )
}

function base.popCleanup
{
    unset __BASE_CLEANUP[$((${#__BASE_CLEANUP[@]} - 1))]
}

function base.__cleanup
{
    for cmd in "${__BASE_CLEANUP[@]}"; do
	$cmd
    done
    
    test -z "$__BASE_SUCCESS" || return 0
    test -z "$OPTION_KEEP"    || return 0
    rm -rf "${__BASE_GENERATED_FILES[@]}"
}

function base.init
{
    test -z "$SETUP_CONTEXT" || ! $_VSERVER_INFO -q "$SETUP_CONTEXT" RUNNING || \
	panic $"\
Context '$SETUP_CONTEXT' is already in use. Please select another one."

    trap "base.__cleanup" EXIT
    base._initVariables
}

function base.setSuccess
{
    __BASE_SUCCESS=1
}

function startSleepingGuest
{
    local guest="$1"
    local timeout="${2:-15}"
    $_VSERVER "$guest" start --rescue --rescue-init bash -c "
	exec  > /dev/null
	exec 2> /dev/null
	trap 'kill -s 9 -- -1; exit 0' INT
	sleep $timeout
	kill -s 15 -- -1
	sleep 1
	kill -s 9 -- -1"
}

function stopSleepingGuest
{
    local guest="$1"
    $_VSERVER "$guest" stop --rescue-init
}