basic 'vserver ... start|stop' function should work now...
[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 function findObject
19 {
20     local mod=$1
21     local var=$2
22     local file=
23     local i=X
24     shift 2
25     
26     for i; do
27         test "$i"        || continue
28         test ! $mod "$i" || { file=$i; break; }
29     done
30
31     test -z "$i" -o "$file" || {
32         echo "Can not find file for '$var'; aborting"
33         exit 1
34     } >&2
35
36     eval "$var=\"$file\""
37 }
38
39 function findFile
40 {
41     findObject -f "$@"
42 }
43
44 function findDir
45 {
46     findObject -d "$@"
47 }
48
49 function getPhysicalDir
50 {
51     ( set -P && cd "$1" && pwd )
52 }
53
54 function _pkgMountBindDir()
55 {
56     test "$1" != "$2" || return 0
57
58     mount -n --bind "$1" "$2"
59 }
60
61 function _pkgSetVarsBase
62 {
63     case "$vserver" in
64         /*)
65             echo "not supported yet"
66             exit 1
67             ;;
68         *)
69             BASEDIR=$CONFDIR/$vserver
70             test -d "$BASEDIR" || {
71                 echo "Can not find configuration-directory"
72                 exit 1
73             }
74             
75             VDIR=$BASEDIR/vdir
76             test -d "$VDIR"   || VDIR=/vservers/$vserver
77             VDIR=$(getPhysicalDir "$VDIR")
78             
79             PKGDIR=$BASEDIR/apps/pkgmgmt
80             test -d "$PKGDIR" || {
81                 echo "Can not find configuration-directory for package-managment tools"
82                 exit 1
83             }
84
85             findDir EXECDIR      $PKGDIR/execdir     /
86
87             ;;
88     esac
89
90     if test -z "$WORKAROUND_106057"; then
91         _rpmdb_mntpoint=/dev
92     else
93         _rpmdb_mntpoint=/.rpmdb
94     fi
95 }
96
97 function _pkgSetVarsRPM
98 {
99     case "$vserver" in
100         /*)
101             echo "not supported yet"
102             exit 1
103             ;;
104             
105         *)
106             findDir RPMETCDIR    $PKGDIR/rpmetc      $PKGDIR/base/rpm/etc       /etc/rpm
107             findDir RPMSTATEDIR  $PKGDIR/rpmstate    $PKGDIR/base/rpm/state
108
109             findDir RPMLIBDIR    $PKGDIR/rpmlib  /
110
111             RPMSTATEDIR=$(getPhysicalDir "$RPMSTATEDIR")
112             RPMETCDIR=$(getPhysicalDir "$RPMETCDIR")
113             ;;
114     esac
115 }
116
117 function _pkgSetVarsApt
118 {
119     case "$vserver" in
120         /*)
121             echo "not supported yet"
122             exit 1
123             ;;
124             
125         *)
126             findDir APTETCDIR    $PKGDIR/aptetc      $PKGDIR/base/apt/etc       /etc/apt
127             findDir APTSTATEDIR  $PKGDIR/aptstate    $PKGDIR/base/apt/state
128             findDir APTCACHEDIR  $PKGDIR/aptcache    $PKGDIR/base/apt/cache
129             findDir APTARCHIVDIR $PKGDIR/aptarchives $PKGDIR/base/apt/archives  /var/cache/apt/archives
130             ;;
131     esac
132 }
133
134 function _pkgMountBase
135 {
136     :
137 }
138
139 function _pkgMountApt
140 {
141     :
142 }
143
144 function _pkgMountRPM
145 {
146     _pkgMountBindDir "$RPMETCDIR" /etc/rpm
147     test "$RPMLIBDIR" = "/" || _pkgMountBindDir "$RPMLIBDIR" /usr/lib/rpm
148
149     "$_SECURE_MOUNT" --chroot "$VDIR" -n --secure --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
150     test -z "$WORKAROUND_106057" || mount -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
151 }
152
153 function _pkgSetEnvBase
154 {
155     test "$EXECDIR"   = "/" || {
156         PATH=$EXECDIR:$PATH
157         LD_LIBRARY_PATH=$EXECDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
158     }
159
160     export PATH LD_LIBRARY_PATH
161 }
162
163 function _pkgSetEnvApt
164 {
165     :
166 }
167
168 function _pkgSetEnvRPM
169 {
170     CUR_VSERVER=$vserver
171     RPM_FAKE_NAMESPACE_MOUNTS=$_rpmdb_mntpoint
172     RPM_BINARY=$_VRPM_PRELOAD
173
174     export CUR_VSERVER RPM_FAKE_NAMESPACE_MOUNTS RPM_BINARY
175 }
176
177 function pkgInit
178 {
179     local i
180     local vserver=$1
181     shift
182     
183     _pkgSetVarsBase
184     for i; do
185         case "$i" in
186             rpm)        _pkgSetVarsRPM;;
187             apt)        _pkgSetVarsApt;;
188             *)          echo "Unknown packaging flavor"; exit 1;;
189         esac
190     done
191
192     _pkgMountBase
193     for i; do
194         case "$i" in
195             rpm)        _pkgMountRPM;;
196             apt)        _pkgMountApt;;
197         esac
198     done
199
200     _pkgSetEnvBase
201     for i; do
202         case "$i" in
203             rpm)        _pkgSetEnvRPM;;
204             apt)        _pkgSetEnvApt;;
205         esac
206     done
207
208     _PKG_FLAVORS="$@"
209     _PKG_VSERVER=$vserver
210 }
211
212 function getAllVservers
213 {
214     local i
215     local var=$1
216
217     for i in $CONFDIR/*; do
218         test   -d "$i"          || continue
219         test ! -e "$i"/disabled || continue
220         test   -d "$i"/vdir     || continue
221
222         eval "$var=\"$var ${i##$CONFDIR/}\""
223     done
224 }
225
226 ## Usage: getVserverCtx <vdir> <result-varname> [<procnumber-varname> [<do-cleanup>]]
227 function getVserverStatus
228 {
229     test -r "$1"/run || return 1
230     eval read "$2" <"$1"/run
231
232     test "$3"        || return 0
233     local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$ctx" | $_WC -l )
234     eval "$3"=\$_tmp
235
236     test "$4" -a $_tmp = 0 || return 0
237     _tmp=$(readlink "$1/run")
238     test "$_tmp"           || return 1
239     rm -f "$_tmp"
240     return 0
241 }
242
243 ## Usage: isVserverRunning <vdir> [<ctx-varname>]
244 function isVserverRunning
245 {
246     local ctx procnum
247
248     getVserverStatus "$1" ctx procnum 1 || return 1
249     test $procnum != 0                  || return 1
250     test -z "$2" || eval "$2"=\$ctx
251     return 0
252 }