moved isAvoidNamespace() into main-functions file
[util-vserver.git] / util-vserver / scripts / functions
1 # $Id$  --*- 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 _VS_NEWLINE='
19 '
20 declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1}
21
22 function findObject
23 {
24     local mod=$1
25     local var=$2
26     local file=
27     local i=X
28     shift 2
29     
30     for i; do
31         test "$i"        || continue
32         test ! $mod "$i" || { file=$i; break; }
33     done
34
35     test -z "$i" -o "$file" || {
36         echo "Can not find file for '$var'; aborting"
37         exit 1
38     } >&2
39
40     eval "$var=\"$file\""
41 }
42
43 function findFile
44 {
45     findObject -f "$@"
46 }
47
48 function findDir
49 {
50     findObject -d "$@"
51 }
52
53 function findAndCopy
54 {
55     local dst=$1
56     test ! -s "$dst"         || return 0
57     
58     local tmp
59     shift
60     findFile tmp "$@"
61
62     test "$tmp" -a -s "$tmp" || return 0
63     cp -af "$tmp" "$dst"
64 }
65
66 function getPhysicalDir
67 {
68     ( set -P && cd "$1" && pwd )
69 }
70
71 function _pkgMountBindDir()
72 {
73     test "$1" != "$2" || return 0
74
75     mount -n --bind "$1" "$2"
76 }
77
78 function _pkgSetVarsBase
79 {
80     case "$vserver" in
81         ./*|/*)
82             if test -d "$vserver/vdir"; then
83                 BASEDIR=$vserver
84                 VDIR=$(getPhysicalDir "$vserver/vdir")
85                 
86                 PKGDIR=$BASEDIR/apps/pkgmgmt
87                 test -d "$PKGDIR" || {
88                     echo "Can not find configuration-directory for package-managment tools"
89                     exit 1
90                 }
91                 findDir EXECDIR      $PKGDIR/execdir     /
92             else
93                 VDIR=$(getPhysicalDir "$vserver")
94                 PKGDIR=
95             fi
96             ;;
97         *)
98             BASEDIR=$CONFDIR/$vserver
99             test -d "$BASEDIR" || {
100                 echo "Can not find configuration-directory"
101                 exit 1
102             }
103             
104             VDIR=$BASEDIR/vdir
105             test -d "$VDIR"   || VDIR=/vservers/$vserver
106             VDIR=$(getPhysicalDir "$VDIR")
107             
108             PKGDIR=$BASEDIR/apps/pkgmgmt
109             test -d "$PKGDIR" || {
110                 echo "Can not find configuration-directory for package-managment tools"
111                 exit 1
112             }
113
114             findDir EXECDIR      $PKGDIR/execdir     /
115
116             ;;
117     esac
118
119     if test -z "$WORKAROUND_106057"; then
120         _rpmdb_mntpoint=/dev
121     else
122         _rpmdb_mntpoint=/.rpmdb
123     fi
124 }
125
126 function _pkgSetVarsRPM
127 {
128     if test "$PKGDIR"; then
129         findDir RPMETCDIR    $PKGDIR/rpmetc      $PKGDIR/base/rpm/etc       /etc/rpm
130         findDir RPMSTATEDIR  $PKGDIR/rpmstate    $PKGDIR/base/rpm/state
131
132         findDir RPMLIBDIR    $PKGDIR/rpmlib      /
133
134     else
135         findDir RPMETCDIR    "$VDIR"/etc/rpm     /etc/rpm
136         findDir RPMSTATEDIR  "$VDIR"/var/lib/rpm
137         RPMLIBDIR=/
138     fi
139     
140     RPMSTATEDIR=$(getPhysicalDir "$RPMSTATEDIR")
141     RPMETCDIR=$(getPhysicalDir "$RPMETCDIR")
142 }
143
144 function _pkgSetVarsApt
145 {
146     if test "$PKGDIR"; then
147         findDir APTETCDIR    $PKGDIR/aptetc      $PKGDIR/base/apt/etc       /etc/apt
148         findDir APTSTATEDIR  $PKGDIR/aptstate    $PKGDIR/base/apt/state
149         findDir APTCACHEDIR  $PKGDIR/aptcache    $PKGDIR/base/apt/cache
150         findDir APTARCHIVDIR $PKGDIR/aptarchives $PKGDIR/base/apt/archives  /var/cache/apt/archives
151     else
152         findDir APTETCDIR    "$VDIR"/etc/apt            /etc/apt
153         findDir APTSTATEDIR  "$VDIR"/var/state/apt
154         findDir APTCACHEDIR  "$VDIR"/var/cache/apt
155         findDir APTARCHIVDIR "$VDIR"/var/cache/apt/archives /var/cache/apt/archives
156     fi
157
158     findFile APT_CONFIG "$APTETCDIR"/apt.conf ""
159     test -z "$APT_CONFIG" || export APT_CONFIG
160 }
161
162 function _pkgMountBase
163 {
164     :
165 }
166
167 function _pkgMountApt
168 {
169     :
170 }
171
172 function _pkgMountRPM
173 {
174     _pkgMountBindDir "$RPMETCDIR" /etc/rpm
175     test "$RPMLIBDIR" = "/" || _pkgMountBindDir "$RPMLIBDIR" /usr/lib/rpm
176
177     "$_SECURE_MOUNT" --chroot "$VDIR" -n --secure --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
178     test -z "$WORKAROUND_106057" || mount -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
179 }
180
181 function _pkgSetEnvBase
182 {
183     test "$EXECDIR"   = "/" || {
184         PATH=$EXECDIR:$PATH
185         LD_LIBRARY_PATH=$EXECDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
186     }
187
188     export PATH LD_LIBRARY_PATH
189 }
190
191 function _pkgSetEnvApt
192 {
193     :
194 }
195
196 function _pkgSetEnvRPM
197 {
198     CUR_VSERVER=$vserver
199     RPM_FAKE_NAMESPACE_MOUNTS=$_rpmdb_mntpoint
200     RPM_BINARY=$_VRPM_PRELOAD
201
202     export CUR_VSERVER RPM_FAKE_NAMESPACE_MOUNTS RPM_BINARY
203 }
204
205 function pkgInit
206 {
207     local i
208     local vserver=$1
209     shift
210     
211     _pkgSetVarsBase
212     for i; do
213         case "$i" in
214             rpm)        _pkgSetVarsRPM;;
215             apt)        _pkgSetVarsApt;;
216             *)          echo "Unknown packaging flavor"; exit 1;;
217         esac
218     done
219
220     _pkgMountBase
221     for i; do
222         case "$i" in
223             rpm)        _pkgMountRPM;;
224             apt)        _pkgMountApt;;
225         esac
226     done
227
228     _pkgSetEnvBase
229     for i; do
230         case "$i" in
231             rpm)        _pkgSetEnvRPM;;
232             apt)        _pkgSetEnvApt;;
233         esac
234     done
235
236     _PKG_FLAVORS="$@"
237     _PKG_VSERVER=$vserver
238 }
239
240 function isAvoidNamespace
241 {
242     test ! -e "$1"/namespace || return 1
243     test -e "$CONFDIR"/.defaults/nonamespace -o \
244          -e "$1"/nonamespace
245 }
246
247 function getAllVservers
248 {
249     local i
250     declare -a _tmp=()
251
252     for i in $CONFDIR/*; do
253         test   -d "$i"          || continue
254         test ! -e "$i"/disabled || continue
255         test   -d "$i"/vdir     || continue
256         case "$i" in
257             *.~*~) continue;;
258         esac
259
260         _tmp=( "${_tmp[@]}" "${i##$CONFDIR/}")
261     done
262
263     eval $1='( "${_tmp[@]}" )'
264 }
265
266 ## Usage: getVserverCtx <vdir> <result-varname> [<procnumber-varname> [<do-cleanup>]]
267 function getVserverStatus
268 {
269     test -r "$1"/run || return 1
270
271     local _ctx
272     read _ctx <"$1"/run
273     eval "$2"=\$_ctx
274
275     test "$3"        || return 0
276     local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$_ctx" | $_WC -l )
277     eval "$3"=\$_tmp
278
279     test "$4" -a $_tmp = 0 || return 0
280     _tmp=$(readlink "$1/run")
281     test "$_tmp"           || return 1
282     rm -f "$_tmp"
283     return 0
284 }
285
286 ## Usage: isCtxRunning <ctx>
287 function isCtxRunning
288 {
289     local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$1" | $_WC -l )
290     test $_tmp -gt 0
291 }
292
293 ## Usage: isVserverRunning <vdir> [<ctx-varname>]
294 function isVserverRunning
295 {
296     local ctx procnum
297
298     getVserverStatus "$1" ctx procnum 1 || return 1
299     test $procnum != 0                  || return 1
300     test -z "$2" || eval "$2"=\$ctx
301     return 0
302 }
303
304 ## Called as 'getFileValue <varname> <filename>'
305 function getFileValue
306 {
307     test -r "$2" || return 0
308     eval read "$1" <"$2"
309 }
310
311 ## Called as 'getFileArray <varname> <filename>'
312 function getFileArray
313 {
314     test -r "$2" || return 0
315     
316     local IFS=$_VS_NEWLINE
317     eval "$1"='( $(< "$2") )'
318 }
319
320 function checkComponents
321 {
322     local       i
323     local       msg=$1
324     local       x_failed=
325
326     shift
327     
328     for i; do
329         local failed=
330         case "$i" in
331             core)       test -x "$_CHBIND"           || failed=1;;
332             build)      test -x "$_VSERVER_BUILD"    || failed=1;;
333             sysv)       test -x "$INITRDDIR/vserver" || failed=1;;
334             devel)      test -d "$INCLUDEDIR/vserver.h" || failed=1;;
335             *)          echo "Unknown component '$i'"
336                         return false
337                         ;;
338         esac
339
340         test -z "$failed" || {
341             echo "$msg: $i"
342             x_failed=1
343         }
344     done
345
346     test -z "$x_failed"
347 }