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/mandrake-release; then
159 set -- $(cat /etc/mandrake-release)
161 elif test -e /etc/redhat-release; then
162 set -- $(cat /etc/redhat-release)
164 elif test -e /etc/debian_version; then
165 set -- $(cat /etc/debian_version)
167 elif test -e /etc/SuSE-release; then
168 set -- $(cat /etc/SuSE-release)
170 elif test -e /etc/gentoo-release; then
171 set -- $(cat /etc/gentoo-release)
172 DISTRIBUTION=gentoo$5
173 elif test -e /etc/slackware-version; then
174 set -- $(cat /etc/slackware-version)
175 DISTRIBUTION=slackware$2
176 elif test -n "$1"; then
180 ERROR: Can not determine distribution; please specify it manually with
185 test -n "$ignore_config" -o \
186 -d "$__CONFDIR/.distributions/$DISTRIBUTION" -o \
187 -d "$__DISTRIBDIR/$DISTRIBUTION" ||
189 ERROR: Can not find configuration for the distribution '$DISTRIBUTION';
190 please read http://linux-vserver.org/HowToRegisterNewDistributions
191 for information how to add support for your own distribution."
196 function base._addGeneratedFile
198 __BASE_GENERATED_FILES=( "${__BASE_GENERATED_FILES[@]}" "$@" )
201 ## Usage: initFilesystem [force]
202 function base.initFilesystem
204 test -z "$1" || _renameVserverCfg
205 { isDirectoryEmpty "$VDIR" 1 && test ! -e "$SETUP_CONFDIR"; } || colpanic $"\
206 vserver-topdirectory '$VDIR' and/or configuration at '$SETUP_CONFDIR'
207 exist already; please try to use '--force', or remove them manually."
209 mkdir -p -m755 "$VDIR"
210 $_SETATTR --~barrier "$VDIR"
211 $_SETATTR --barrier "$VDIR"/.. || colwarn $"\
212 WARNING: could not set the barrier attribute on '$VDIR/..',
213 please set it manually."
214 base._addGeneratedFile "$VDIR"
216 mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/etc
217 base._addGeneratedFile "$SETUP_CONFDIR"
219 ln -s "$VDIR" "$SETUP_CONFDIR/vdir"
220 ln -s "$CACHEDIR/$VSERVERDIRNAME" "$SETUP_CONFDIR/cache"
224 mkdir -p "$VDIR"/proc
225 findAndCopy "$VDIR"/etc/hosts "$__CONFDIR"/.defaults/files/hosts "$__CONFDIR/.distributions/$DISTRIBUTION"/files/hosts \
226 "$__DISTRIBDIR/$DISTRIBUTION"/files/hosts "$__DISTRIBDIR"/defaults/files/hosts ""
228 for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime; do
229 findAndCopy "$VDIR"/etc/$i "$__CONFDIR/.defaults/files/$i" "$__CONFDIR/.distributions/$DISTRIBUTION/files/$i" ""
231 findAndCopy "$VDIR"/etc/resolv.conf "$__CONFDIR/.defaults/files/resolv.conf" \
232 "$__CONFDIR/.distributions/$DISTRIBUTION/files/resolv.conf" /etc/resolv.conf ""
235 function base._initVariables
242 findFile _DEV_FILE "$__CONFDIR/.distributions/$DISTRIBUTION/devs" "$__DISTRIBDIR/$DISTRIBUTION/devs" "$__DISTRIBDIR/defaults/devs"
243 findDir _EXECDIR "$__CONFDIR/.distributions/$DISTRIBUTION/execdir" "$__DISTRIBDIR/$DISTRIBUTION/execdir" /
244 findFile BUILD_INITPRE "$__CONFDIR/.distributions/$DISTRIBUTION/initpre" "$__DISTRIBDIR/$DISTRIBUTION/initpre" ""
245 findFile BUILD_INITPOST "$__CONFDIR/.distributions/$DISTRIBUTION/initpost" "$__DISTRIBDIR/$DISTRIBUTION/initpost" ""
248 declare -a __BASE_CLEANUP
249 function base.pushCleanup
251 __BASE_CLEANUP=( "${__BASE_CLEANUP[@]}" "$*" )
254 function base.popCleanup
256 unset __BASE_CLEANUP[$((${#__BASE_CLEANUP[@]} - 1))]
259 function base.__cleanup
261 for cmd in "${__BASE_CLEANUP[@]}"; do
265 test -z "$__BASE_SUCCESS" || return 0
266 test -z "$OPTION_KEEP" || return 0
267 rm -rf "${__BASE_GENERATED_FILES[@]}"
272 test -z "$SETUP_CONTEXT" || ! $_VSERVER_INFO -q "$SETUP_CONTEXT" RUNNING || \
274 Context '$SETUP_CONTEXT' is already in use. Please select another one."
276 trap "base.__cleanup" EXIT
280 function base.setSuccess
285 function startSleepingGuest
288 local timeout="${2:-15}"
289 $_VSERVER "$guest" start --rescue --rescue-init bash -c "
292 trap 'kill -s 9 -- -1; exit 0' INT
299 function stopSleepingGuest
302 $_VSERVER "$guest" stop --rescue-init