3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # Expected environment:
19 # $VSERVER_NAME ... name of vserver
35 __BASE_GENERATED_FILES=()
42 (c|b) mknod -m$6 "$dst" $3 $4 $5;;
43 (d) mkdir -p -m$4 "$dst";;
49 (*) echo "Unknown dev-entry mode '$3'" >&2
59 mkdir -p -m755 "$VDIR"/dev
60 mkdir -m755 "$VDIR"/dev/pts
63 makeDevEntry "$VDIR"/dev $spec
67 function populateDirectory
76 for file in "$i"/*; do
77 isRegularFile "$file" || continue
86 test -z "$ROOTDIR" || return 0
88 for item in "\"$__CONFDIR/.defaults/vdirbase\" 1" "$__DEFAULT_VSERVERDIR"; do
92 test ! -d "$ROOTDIR" || break
95 test -d "$ROOTDIR" || {
96 echo "Root-directory '$ROOTDIR' does not exist or is invalid" >&2
101 function _setCacheDir
103 test -z "$CACHEDIR" || return 0
105 for item in "\"$__CONFDIR/.defaults/cachebase\" 1" "$__PKGCACHEDIR"; do
109 test ! -d "$CACHEDIR" || break
112 test -d "$CACHEDIR" || {
113 echo "Cache-directory '$CACHEDIR' does not exist or is invalid" >&2
118 function _setVserverDirName
120 test -z "$VSERVERDIRNAME" || return 0
121 VSERVERDIRNAME="$VSERVER_NAME"
126 VDIR="$ROOTDIR/$VSERVERDIRNAME"
131 test -z "$OPTION_SILENT" || return 0
135 function _renameVserverCfg
137 local suffix=.~$(date +'%s')~
140 for i in "$VDIR" "$SETUP_CONFDIR"; do
141 test ! -e "$i" || isDirectoryEmpty "$i" || {
143 say "Renamed '$i' to '$i$suffix'"
149 ## Usage: getDistribution [<default>] [<ignore-config>]
150 function getDistribution
152 local ignore_config=$2
154 if test -z "$DISTRIBUTION"; then
155 if test -e /etc/fedora-release; then
156 set -- $(cat /etc/fedora-release)
158 elif test -e /etc/redhat-release; then
159 set -- $(cat /etc/redhat-release)
161 elif test -e /etc/debian_version; then
162 set -- $(cat /etc/debian_version)
164 elif test -e /etc/SuSE-release; then
165 set -- $(cat /etc/SuSE-release)
167 elif test -e /etc/gentoo-release; then
168 set -- $(cat /etc/gentoo-release)
169 DISTRIBUTION=gentoo$5
170 elif test -e /etc/slackware-version; then
171 set -- $(cat /etc/slackware-version)
172 DISTRIBUTION=slackware$2
173 elif test -n "$1"; then
177 ERROR: Can not determine distribution; please specify it manually with
182 test -n "$ignore_config" -o \
183 -d "$__CONFDIR/.distributions/$DISTRIBUTION" -o \
184 -d "$__DISTRIBDIR/$DISTRIBUTION" ||
186 ERROR: Can not find configuration for the distribution '$DISTRIBUTION';
187 please read http://linux-vserver.org/HowToRegisterNewDistributions
188 for information how to add support for your own distribution."
193 function base._addGeneratedFile
195 __BASE_GENERATED_FILES=( "${__BASE_GENERATED_FILES[@]}" "$@" )
198 ## Usage: initFilesystem [force]
199 function base.initFilesystem
201 test -z "$1" || _renameVserverCfg
202 { isDirectoryEmpty "$VDIR" && test ! -e "$SETUP_CONFDIR"; } || colpanic $"\
203 vserver-topdirectory '$VDIR' and/or configuration at '$SETUP_CONFDIR'
204 exist already; please try to use '--force', or remove them manually."
206 mkdir -p -m755 "$VDIR"
207 $_SETATTR --~barrier "$VDIR"
208 $_SETATTR --barrier "$VDIR"/.. || colwarn $"\
209 WARNING: could not set the barrier attribute on '$VDIR/..',
210 please set it manually."
211 base._addGeneratedFile "$VDIR"
213 mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/etc
214 base._addGeneratedFile "$SETUP_CONFDIR"
216 ln -s "$VDIR" "$SETUP_CONFDIR/vdir"
217 ln -s "$CACHEDIR/$VSERVERDIRNAME" "$SETUP_CONFDIR/cache"
221 mkdir -p "$VDIR"/proc
222 findAndCopy "$VDIR"/etc/hosts "$__CONFDIR"/.defaults/files/hosts "$__CONFDIR/.distributions/$DISTRIBUTION"/files/hosts \
223 "$__DISTRIBDIR/$DISTRIBUTION"/files/hosts "$__DISTRIBDIR"/defaults/files/hosts ""
225 for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime resolv.conf; do
226 findAndCopy "$VDIR"/etc/$i "$__CONFDIR/.defaults/files/$i" "$__CONFDIR/.distributions/$DISTRIBUTION/files/$i" ""
230 function base._initVariables
237 findFile _DEV_FILE "$__CONFDIR/.distributions/$DISTRIBUTION/devs" "$__DISTRIBDIR/$DISTRIBUTION/devs" "$__DISTRIBDIR/defaults/devs"
238 findDir _EXECDIR "$__CONFDIR/.distributions/$DISTRIBUTION/execdir" "$__DISTRIBDIR/$DISTRIBUTION/execdir" /
239 findFile BUILD_INITPRE "$__CONFDIR/.distributions/$DISTRIBUTION/initpre" "$__DISTRIBDIR/$DISTRIBUTION/initpre" ""
240 findFile BUILD_INITPOST "$__CONFDIR/.distributions/$DISTRIBUTION/initpost" "$__DISTRIBDIR/$DISTRIBUTION/initpost" ""
243 declare -a __BASE_CLEANUP
244 function base.pushCleanup
246 __BASE_CLEANUP=( "${__BASE_CLEANUP[@]}" "$*" )
249 function base.popCleanup
251 unset __BASE_CLEANUP[$((${#__BASE_CLEANUP[@]} - 1))]
254 function base.__cleanup
256 for cmd in "${__BASE_CLEANUP[@]}"; do
260 test -z "$__BASE_SUCCESS" || return 0
261 test -z "$OPTION_KEEP" || return 0
262 rm -rf "${__BASE_GENERATED_FILES[@]}"
267 test -z "$SETUP_CONTEXT" || ! $_VSERVER_INFO -q "$SETUP_CONTEXT" RUNNING || \
269 Context '$SETUP_CONTEXT' is already in use. Please select another one."
271 trap "base.__cleanup" EXIT
275 function base.setSuccess
280 function startSleepingGuest
283 local timeout="${2:-15}"
284 $_VSERVER "$guest" start --rescue --rescue-init bash -c "
287 trap 'kill -s 9 -- -1; exit 0' INT
294 function stopSleepingGuest
297 $_VSERVER "$guest" stop --rescue-init