use $_READLINK
[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     "$_SECURE_MOUNT" --chroot "$VDIR" -n --secure -t proc none /proc
181 }
182
183 function _pkgSetEnvBase
184 {
185     test "$EXECDIR"   = "/" || {
186         PATH=$EXECDIR:$PATH
187         LD_LIBRARY_PATH=$EXECDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
188     }
189
190     export PATH LD_LIBRARY_PATH
191 }
192
193 function _pkgSetEnvApt
194 {
195     :
196 }
197
198 function _pkgSetEnvRPM
199 {
200     CUR_VSERVER=$vserver
201     RPM_FAKE_NAMESPACE_MOUNTS=$_rpmdb_mntpoint
202     RPM_BINARY=$_VRPM_PRELOAD
203
204     export CUR_VSERVER RPM_FAKE_NAMESPACE_MOUNTS RPM_BINARY
205 }
206
207 function pkgInit
208 {
209     local i
210     local vserver=$1
211     shift
212     
213     _pkgSetVarsBase
214     for i; do
215         case "$i" in
216             rpm)        _pkgSetVarsRPM;;
217             apt)        _pkgSetVarsApt;;
218             *)          echo "Unknown packaging flavor"; exit 1;;
219         esac
220     done
221
222     _pkgMountBase
223     for i; do
224         case "$i" in
225             rpm)        _pkgMountRPM;;
226             apt)        _pkgMountApt;;
227         esac
228     done
229
230     _pkgSetEnvBase
231     for i; do
232         case "$i" in
233             rpm)        _pkgSetEnvRPM;;
234             apt)        _pkgSetEnvApt;;
235         esac
236     done
237
238     _PKG_FLAVORS="$@"
239     _PKG_VSERVER=$vserver
240 }
241
242 function isAvoidNamespace
243 {
244     test ! -e "$1"/namespace || return 1
245     test -e "$CONFDIR"/.defaults/nonamespace -o \
246          -e "$1"/nonamespace
247 }
248
249 function getAllVservers
250 {
251     local i
252     declare -a _tmp=()
253
254     for i in $CONFDIR/*; do
255         test   -d "$i"          || continue
256         test ! -e "$i"/disabled || continue
257         test   -d "$i"/vdir     || continue
258         case "$i" in
259             *.~*~) continue;;
260         esac
261
262         _tmp=( "${_tmp[@]}" "${i##$CONFDIR/}")
263     done
264
265     eval $1='( "${_tmp[@]}" )'
266 }
267
268 ## Usage: getVserverCtx <vdir> <result-varname> [<procnumber-varname> [<do-cleanup>]]
269 function getVserverStatus
270 {
271     test -r "$1"/run || return 1
272
273     local _ctx
274     read _ctx <"$1"/run
275     eval "$2"=\$_ctx
276
277     test "$3"        || return 0
278     local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$_ctx" | $_WC -l )
279     eval "$3"=\$_tmp
280
281     test "$4" -a $_tmp = 0 || return 0
282     _tmp=$($_READLINK "$1/run")
283     test "$_tmp"           || return 1
284     rm -f "$_tmp"
285     return 0
286 }
287
288 ## Usage: isCtxRunning <ctx>
289 function isCtxRunning
290 {
291     local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$1" | $_WC -l )
292     test $_tmp -gt 0
293 }
294
295 ## Usage: isVserverRunning <vdir> [<ctx-varname>]
296 function isVserverRunning
297 {
298     local ctx procnum
299
300     getVserverStatus "$1" ctx procnum 1 || return 1
301     test $procnum != 0                  || return 1
302     test -z "$2" || eval "$2"=\$ctx
303     return 0
304 }
305
306 ## Called as 'getFileValue <varname> <filename>'
307 function getFileValue
308 {
309     test -r "$2" || return 0
310     eval read "$1" <"$2"
311 }
312
313 ## Called as 'getFileArray <varname> <filename>'
314 function getFileArray
315 {
316     test -r "$2" || return 0
317     
318     local IFS=$_VS_NEWLINE
319     eval "$1"='( $(< "$2") )'
320 }
321
322 function checkComponents
323 {
324     local       i
325     local       msg=$1
326     local       x_failed=
327
328     shift
329     
330     for i; do
331         local failed=
332         case "$i" in
333             core)       test -x "$_CHBIND"           || failed=1;;
334             build)      test -x "$_VSERVER_BUILD"    || failed=1;;
335             sysv)       test -x "$INITRDDIR/vserver" || failed=1;;
336             devel)      test -d "$INCLUDEDIR/vserver.h" || failed=1;;
337             *)          echo "Unknown component '$i'"
338                         return false
339                         ;;
340         esac
341
342         test -z "$failed" || {
343             echo "$msg: $i"
344             x_failed=1
345         }
346     done
347
348     test -z "$x_failed"
349 }