Move out of way
[util-vserver.git] / scripts / vserver-build.functions
1 # $Id: vserver-build.functions 2765 2008-08-22 21:41:47Z dhozac $       --*- sh -*--
2
3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 #  
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.
8 #  
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.
13 #  
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.
17
18 # Expected environment:
19 #     $VSERVER_NAME ... name of vserver
20
21 ROOTDIR=
22 ROOTDIR_REL=
23 CACHEDIR=
24 CACHEDIR_REL=
25 VSERVERDIRNAME=
26
27 VDIR=
28
29 _DEV_FILE=
30 _EXEC_DIR=
31
32 BUILD_INITPRE=
33 BUILD_INITPOST=
34
35 __BASE_GENERATED_FILES=()
36 __BASE_SUCCESS=
37
38 function makeDevEntry
39 {
40     local dst=$1/$2
41     case "$3" in
42         (c|b)   mknod -m$6 "$dst"  $3 $4 $5;;
43         (d)     mkdir -p -m$4 "$dst";;
44         (f)     touch "$dst"
45                 chmod $4 "$dst"
46                 ;;
47         (l)     ln -s "$4" "$dst"
48                 ;;
49         (*)     echo "Unknown dev-entry mode '$3'" >&2
50                 false
51                 ;;
52     esac
53 }
54
55 function populateDev
56 {
57     local spec
58
59     mkdir -p -m755 "$VDIR"/dev
60     mkdir -m755 "$VDIR"/dev/pts
61
62     while read spec; do
63         makeDevEntry "$VDIR"/dev $spec
64     done <$_DEV_FILE
65 }
66
67 function populateDirectory
68 {
69     local dst=$1
70     local i
71     
72     shift
73     for i; do
74         local file=
75         
76         for file in "$i"/*; do
77             isRegularFile "$file" || continue
78             
79             cp -a "$file" "$dst/"
80         done
81     done
82 }
83
84 function _setRootDir
85 {
86     test -z "$ROOTDIR" || return 0
87     
88     for item in "\"$__CONFDIR/.defaults/vdirbase\" 1" "$__DEFAULT_VSERVERDIR"; do
89         eval set -- "$item"
90         ROOTDIR=$1
91         ROOTDIR_REL=$2
92         test ! -d "$ROOTDIR" || break
93     done
94
95     test -d "$ROOTDIR" || {
96         echo "Root-directory '$ROOTDIR' does not exist or is invalid" >&2
97         exit 1
98     }
99 }
100
101 function _setCacheDir
102 {
103     test -z "$CACHEDIR" || return 0
104     
105     for item in "\"$__CONFDIR/.defaults/cachebase\" 1" "$__PKGCACHEDIR"; do
106         eval set -- "$item"
107         CACHEDIR=$1
108         CACHEDIR_REL=$2
109         test ! -d "$CACHEDIR" || break
110     done
111
112     test -d "$CACHEDIR" || {
113         echo "Cache-directory '$CACHEDIR' does not exist or is invalid" >&2
114         exit 1
115     }
116 }
117
118 function _setVserverDirName
119 {
120     test -z "$VSERVERDIRNAME" || return 0
121     VSERVERDIRNAME="$VSERVER_NAME"
122 }
123
124 function _setVdir
125 {
126     VDIR="$ROOTDIR/$VSERVERDIRNAME"
127 }
128
129 function say
130 {
131     test -z "$OPTION_SILENT" || return 0
132     echo "$@"
133 }
134
135 function _renameVserverCfg
136 {
137     local suffix=.~$(date +'%s')~
138     local i
139     
140     for i in "$VDIR" "$SETUP_CONFDIR"; do
141         test ! -e "$i" || isDirectoryEmpty "$i" || {
142             mv "$i" "$i$suffix"
143             say "Renamed '$i' to '$i$suffix'"
144         }
145     done
146 }
147
148
149 ## Usage: getDistribution [<default>] [<ignore-config>]
150 function getDistribution
151 {
152     local ignore_config=$2
153     
154     if test -z "$DISTRIBUTION"; then
155         if test -e /etc/fedora-release; then
156             set -- $(cat /etc/fedora-release)
157             DISTRIBUTION=fdr$4
158         elif test -e /etc/redhat-release; then
159             set -- $(cat /etc/redhat-release)
160             DISTRIBUTION=rh$5
161         elif test -e /etc/debian_version; then
162             set -- $(cat /etc/debian_version)
163             DISTRIBUTION=deb$1
164         elif test -e /etc/SuSE-release; then
165             set -- $(cat /etc/SuSE-release)
166             DISTRIBUTION=suse$3
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
174             DISTRIBUTION=$1
175         else
176             colpanic $"\
177 ERROR: Can not determine distribution; please specify it manually with
178   the '-d' option."
179         fi
180     fi
181
182     test -n "$ignore_config" -o \
183          -d "$__CONFDIR/.distributions/$DISTRIBUTION" -o \
184          -d "$__DISTRIBDIR/$DISTRIBUTION" ||
185             colpanic $"\
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."
189
190     export DISTRIBUTION
191 }
192
193 function base._addGeneratedFile
194 {
195     __BASE_GENERATED_FILES=( "${__BASE_GENERATED_FILES[@]}" "$@" )
196 }
197
198 ## Usage: initFilesystem [force]
199 function base.initFilesystem
200 {
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."
205
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"
212     
213     mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/etc
214     base._addGeneratedFile "$SETUP_CONFDIR"
215     
216     ln -s "$VDIR"                     "$SETUP_CONFDIR/vdir"
217     ln -s "$CACHEDIR/$VSERVERDIRNAME" "$SETUP_CONFDIR/cache"
218
219     populateDev
220
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 ""
224
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" ""
227     done
228 }
229
230 function base._initVariables
231 {
232     _setRootDir
233     _setCacheDir
234     _setVserverDirName
235     _setVdir
236
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" ""
241 }
242
243 declare -a __BASE_CLEANUP
244 function base.pushCleanup
245 {
246     __BASE_CLEANUP=( "${__BASE_CLEANUP[@]}" "$*" )
247 }
248
249 function base.popCleanup
250 {
251     unset __BASE_CLEANUP[$((${#__BASE_CLEANUP[@]} - 1))]
252 }
253
254 function base.__cleanup
255 {
256     for cmd in "${__BASE_CLEANUP[@]}"; do
257         $cmd
258     done
259     
260     test -z "$__BASE_SUCCESS" || return 0
261     test -z "$OPTION_KEEP"    || return 0
262     rm -rf "${__BASE_GENERATED_FILES[@]}"
263 }
264
265 function base.init
266 {
267     test -z "$SETUP_CONTEXT" || ! $_VSERVER_INFO -q "$SETUP_CONTEXT" RUNNING || \
268         panic $"\
269 Context '$SETUP_CONTEXT' is already in use. Please select another one."
270
271     trap "base.__cleanup" EXIT
272     base._initVariables
273 }
274
275 function base.setSuccess
276 {
277     __BASE_SUCCESS=1
278 }
279
280 function startSleepingGuest
281 {
282     local guest="$1"
283     local timeout="${2:-15}"
284     $_VSERVER "$guest" start --rescue --rescue-init bash -c "
285         exec  > /dev/null
286         exec 2> /dev/null
287         trap 'kill -s 9 -- -1; exit 0' INT
288         sleep $timeout
289         kill -s 15 -- -1
290         sleep 1
291         kill -s 9 -- -1"
292 }
293
294 function stopSleepingGuest
295 {
296     local guest="$1"
297     $_VSERVER "$guest" stop --rescue-init
298 }