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";;
48 test -z "$5" || chmod "$5" "$dst"
50 (*) echo "Unknown dev-entry mode '$3'" >&2
60 mkdir -p -m755 "$VDIR"/dev
61 mkdir -m755 "$VDIR"/dev/pts
64 makeDevEntry "$VDIR"/dev $spec
68 function populateDirectory
77 for file in "$i"/*; do
78 isRegularFile "$file" || continue
87 test -z "$ROOTDIR" || return 0
89 for item in "\"$__CONFDIR/.defaults/vdirbase\" 1" "$__DEFAULT_VSERVERDIR"; do
93 test ! -d "$ROOTDIR" || break
96 test -d "$ROOTDIR" || {
97 echo "Root-directory '$ROOTDIR' does not exist or is invalid" >&2
102 function _setCacheDir
104 test -z "$CACHEDIR" || return 0
106 for item in "\"$__CONFDIR/.defaults/cachebase\" 1" "$__PKGCACHEDIR"; do
110 test ! -d "$CACHEDIR" || break
113 test -d "$CACHEDIR" || {
114 echo "Cache-directory '$CACHEDIR' does not exist or is invalid" >&2
119 function _setVserverDirName
121 test -z "$VSERVERDIRNAME" || return 0
122 VSERVERDIRNAME="$VSERVER_NAME"
127 VDIR="$ROOTDIR/$VSERVERDIRNAME"
132 test -z "$OPTION_SILENT" || return 0
136 function _renameVserverCfg
138 local suffix=.~$(date +'%s')~
141 for i in "$VDIR" "$SETUP_CONFDIR"; do
142 test ! -e "$i" || isDirectoryEmpty "$i" || {
144 say "Renamed '$i' to '$i$suffix'"
150 ## Usage: getDistribution [<default>] [<ignore-config>]
151 function getDistribution
153 local ignore_config=$2
155 if test -z "$DISTRIBUTION"; then
156 if test -e /etc/fedora-release; then
157 set -- $(cat /etc/fedora-release)
159 elif test -e /etc/redhat-release; then
160 set -- $(cat /etc/redhat-release)
162 elif test -e /etc/debian_version; then
163 set -- $(cat /etc/debian_version)
165 elif test -e /etc/SuSE-release; then
166 set -- $(cat /etc/SuSE-release)
168 elif test -e /etc/gentoo-release; then
169 set -- $(cat /etc/gentoo-release)
170 DISTRIBUTION=gentoo$5
171 elif test -e /etc/slackware-version; then
172 set -- $(cat /etc/slackware-version)
173 DISTRIBUTION=slackware$2
174 elif test -n "$1"; then
178 ERROR: Can not determine distribution; please specify it manually with
183 test -n "$ignore_config" -o \
184 -d "$__CONFDIR/.distributions/$DISTRIBUTION" -o \
185 -d "$__DISTRIBDIR/$DISTRIBUTION" ||
187 ERROR: Can not find configuration for the distribution '$DISTRIBUTION';
188 please read http://linux-vserver.org/HowToRegisterNewDistributions
189 for information how to add support for your own distribution."
194 function base._addGeneratedFile
196 __BASE_GENERATED_FILES=( "${__BASE_GENERATED_FILES[@]}" "$@" )
199 ## Usage: initFilesystem [force]
200 function base.initFilesystem
202 test -z "$1" || _renameVserverCfg
203 { isDirectoryEmpty "$VDIR" && test ! -e "$SETUP_CONFDIR"; } || colpanic $"\
204 vserver-topdirectory '$VDIR' and/or configuration at '$SETUP_CONFDIR'
205 exist already; please try to use '--force', or remove them manually."
207 mkdir -p -m755 "$VDIR"
208 $_SETATTR --~barrier "$VDIR"
209 $_SETATTR --barrier "$VDIR"/.. || colwarn $"\
210 WARNING: could not set the barrier attribute on '$VDIR/..',
211 please set it manually."
212 base._addGeneratedFile "$VDIR"
214 mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/etc
215 base._addGeneratedFile "$SETUP_CONFDIR"
217 ln -s "$VDIR" "$SETUP_CONFDIR/vdir"
218 ln -s "$CACHEDIR/$VSERVERDIRNAME" "$SETUP_CONFDIR/cache"
222 mkdir -p "$VDIR"/proc
223 findAndCopy "$VDIR"/etc/hosts "$__CONFDIR"/.defaults/files/hosts "$__CONFDIR/.distributions/$DISTRIBUTION"/files/hosts \
224 "$__DISTRIBDIR/$DISTRIBUTION"/files/hosts "$__DISTRIBDIR"/defaults/files/hosts ""
226 for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime resolv.conf; do
227 findAndCopy "$VDIR"/etc/$i "$__CONFDIR/.defaults/files/$i" "$__CONFDIR/.distributions/$DISTRIBUTION/files/$i" ""
231 function base._initVariables
238 findFile _DEV_FILE "$__CONFDIR/.distributions/$DISTRIBUTION/devs" "$__DISTRIBDIR/$DISTRIBUTION/devs" "$__DISTRIBDIR/defaults/devs"
239 findDir _EXECDIR "$__CONFDIR/.distributions/$DISTRIBUTION/execdir" "$__DISTRIBDIR/$DISTRIBUTION/execdir" /
240 findFile BUILD_INITPRE "$__CONFDIR/.distributions/$DISTRIBUTION/initpre" "$__DISTRIBDIR/$DISTRIBUTION/initpre" ""
241 findFile BUILD_INITPOST "$__CONFDIR/.distributions/$DISTRIBUTION/initpost" "$__DISTRIBDIR/$DISTRIBUTION/initpost" ""
244 function base.__cleanup
246 test -z "$OPTION_KEEP" || return 0
247 test -z "$__BASE_SUCCESS" || return 0
249 rm -rf "${__BASE_GENERATED_FILES[@]}"
254 test -z "$SETUP_CONTEXT" || ! $_VSERVER_INFO -q "$SETUP_CONTEXT" RUNNING || \
256 Context '$SETUP_CONTEXT' is already in use. Please select another one."
258 trap "base.__cleanup" EXIT
262 function base.setSuccess
267 function startSleepingGuest
270 local timeout="${2:-15}"
271 $_VSERVER "$guest" start --rescue --rescue-init bash -c "
274 trap 'kill -s 9 -- -1; exit 0' INT
276 while [ \$i -lt $timeout ]; do
285 function stopSleepingGuest
288 $_VSERVER "$guest" stop --rescue-init