99cc96c95641b849ecf7ca44f1a07f7b09ee9370
[util-vserver.git] / scripts / pkgmgmt
1 #!/bin/bash
2 # $Id: pkgmgmt,v 1.13 2005/07/03 17:43:34 ensc Exp $
3
4 # Copyright (C) 2004,2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 #  
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 of the License.
9 #  
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #  
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
20 test -e "$UTIL_VSERVER_VARS" || {
21     echo $"Can not find util-vserver installation (the file '$UTIL_VSERVER_VARS' would be expected); aborting..." >&2
22     exit 1
23 }
24 . "$UTIL_VSERVER_VARS"
25 . "$_LIB_FUNCTIONS"
26 . "$_LIB_VSERVER_BUILD_FUNCTIONS_PKGMGMT"
27 . "$__PKGLIBDIR/vserver.functions"
28
29 function showHelp()
30 {
31     echo \
32 $"Usage: $0 --externalize|--internalize [-y] [--] <vserver-name>
33
34 Report bugs to <$PACKAGE_BUGREPORT>."
35     exit 0
36 }
37
38 function showVersion()
39 {
40     echo $"\
41 vpkg $PACKAGE_VERSION -- shows information about packages in vservers
42 This program is part of $PACKAGE_STRING
43
44 Copyright (C) 2004,2005 Enrico Scholz
45 This program is free software; you may redistribute it under the terms of
46 the GNU General Public License.  This program has absolutely no warranty."
47     exit 0
48 }
49
50 function init()
51 {
52     if test -z "$WORKAROUND_106057"; then
53         rpmdb_mntpoint=/dev
54     else
55         rpmdb_mntpoint=/.rpmdb
56     fi
57     pkgmgmt.initVariables
58 }
59
60 function _createDirs()
61 {
62     for i; do
63         test -n "$i" || continue
64         mkdir -p -m755 "$i"
65     done
66 }
67
68 function _copySecure()
69 {
70     local chroot=$1
71     local srcdir=$2
72     local dstdir=$3
73
74     
75     ( cd "$srcdir" && tar chf - '.' ) | \
76     ( cd "$chroot" && $_EXEC_CD "$dstdir" $_TAR xf - )
77 }
78
79 function _copySecureRev()
80 {
81     local chroot=$1
82     local srcdir=$2
83     local dstdir=$3
84
85     ( cd "$chroot" && $_EXEC_CD "$srcdir" $_TAR cf - '.' ) | \
86     ( cd "$dstdir" && tar xf - )
87 }
88
89 ## Usage: _substFile <filename> <sed-expression>
90 function _substFile()
91 {
92     local file=$1
93     local expr=$2
94     
95     $_CHROOT_SH testfile "$file" || return 0
96
97     local tmp=$($_MKTEMP /tmp/pkgmgmt_subst.XXXXXX)
98     trap "$_RM -f $tmp" EXIT
99
100     $_CHROOT_SH cat "$file" | \
101         $_SED -e "$expr" >$tmp
102
103     $_CHROOT_SH cat "$file" | $_CMP -s $tmp - || \
104         $_CHROOT_SH truncate "$file" <$tmp
105
106     $_RM -f $tmp
107 }
108
109 function _hashAuto()
110 {
111     local file=$1
112     local hash=$2
113
114     $_CHROOT_SH testfile "$file" || return 0
115     
116     local tmp=$($_MKTEMP /tmp/apt.conf.XXXXXX)
117     trap "$_RM -f $tmp" EXIT
118
119     $_CHROOT_SH cat "$file" | \
120         $_SED -e "s|^\([^$hash].*@autogenerated@\)|$hash$hash\1|" >$tmp
121
122     $_CHROOT_SH cat "$file" | $_CMP -s $tmp - || \
123         $_CHROOT_SH truncate "$file" <$tmp
124
125     $_RM -f $tmp
126 }
127
128 function _unhashAuto()
129 {
130     test -e "$1" || return 0
131
132     local hash=$2
133     local tmp=$($_MKTEMP /tmp/apt.conf.XXXXXX)
134     trap "$_RM -f $tmp" EXIT
135
136     $_SED -e "s|^$hash$hash\(.*@autogenerated@\)|\1|" "$1" >$tmp
137     $_CMP -s "$tmp" "$1" || \
138         $_CAT "$tmp" >"$1"
139
140     $_RM -f $tmp
141 }
142
143 function _mountFilesystemsInternal()
144 {
145     local fstab="$1"
146     test -e "$fstab" || return 0
147     shift
148
149     pushd "$vdir" >/dev/null
150     "$@" $_SECURE_MOUNT -n -a --chroot --fstab "$fstab"
151     popd >/dev/null
152 }
153
154 function _mountFilesystems()
155 {
156     local cfgdir
157     cfgdir=$($_VSERVER_INFO "$1" CFGDIR) || {
158         echo "Can not determine configuration directory for '$1'; ..." >&2
159         return 1
160     }
161     test -n "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$1"
162     
163     _mountFilesystemsInternal "$cfgdir"/fstab       $_CHBIND "${CHBIND_OPTS[@]}" || return 1
164     _mountFilesystemsInternal "$cfgdir"/fstab.local $_CHBIND "${CHBIND_OPTS[@]}" || return 1
165 }
166
167 function _umountFilesystems()
168 {
169     local cfgdir
170     cfgdir=$($_VSERVER_INFO "$1" CFGDIR) || {
171         echo "Can not determine configuration directory for '$1'; ..." >&2
172         return 1
173     }
174     local vdir=$cfgdir/vdir
175     local is_ok=1
176     test -n "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$1"
177
178     pushd "$vdir/" >/dev/null || return 1
179         _umountVserverInternal  "$cfgdir"/fstab.local                              || is_ok=
180         _umountVserverInternal  "$cfgdir"/fstab       $_CHBIND "${CHBIND_OPTS[@]}" || is_ok=
181     popd >/dev/null           || return 1
182     
183     test -n "$is_ok"
184 }
185
186 # Usage: verifyInternalPackages <vserver> <style>
187 function verifyInternalPackages()
188 {
189     local pkgs res=0
190     local ERR="\
191 The following errors occured while trying to internalize the
192 packagemanagement:
193 "
194     
195     case $2 in
196         (RH)    pkgs=$(vrpm "$1" -- -q --qf '---%{NAME}---\n' rpm apt yum "${YUM_RELEASEPKGS[@]}")
197                 hasSubstring "$pkgs" ---rpm--- || {
198                     warning "$ERR
199 * The vserver does not seem to have the 'rpm' package which is required
200   for internal package management. It is suggested to install it before
201   continuing."
202                     res=1
203                     ERR=
204                 }
205                 
206                 hasSubstring "$pkgs"  ---apt--- ---yum--- || {
207                     warning "$ERR
208 * The vserver does not seem to have a depsolver like 'apt' or 'yum'
209   installed. It is suggested to install such a program before setting
210   up internal package management."
211                     res=1
212                     ERR=
213                 }
214
215                 test -n "$have_apt" || test -z "$have_yum"   || \
216                 hasSubstring "$pkgs" "${YUM_RELEASEPKGS[@]}" || {
217                     warning "$ERR
218 * yum requires a special package which describes the version of the
219   distribution. Such a package could not be found within the vserver
220   so please install it before continuing. Usually, this package is
221   named 'redhat-release' of 'fedora-release'."
222                     res=1
223                     ERR=
224                 }
225                 ;;
226     esac
227
228     return $res
229 }
230
231 function processVserver_RH()
232 {
233     local vserver=$1
234     local is_internalize=$2
235     local have_apt
236     local cfgdir
237     local i
238
239     cfgdir=$($_VSERVER_INFO "$vserver" APPDIR pkgmgmt) || \
240     cfgdir=$($_VSERVER_INFO "$vserver" APPDIR)/pkgmgmt
241
242     ## Figure out the environment....
243     have_apt=1
244     have_yum=1
245     pkgmgmt.isAptAvailable "$cfgdir" "$vdir" "$is_internalize" || have_apt=
246     pkgmgmt.isYumAvailable "$cfgdir" "$vdir" "$is_internalize" || have_yum=
247
248     local APTETCDIR=
249     local APTSTATEDIR=
250     local APTCACHEDIR=
251     local APTARCHIVDIR=
252     local RPMETCDIR=
253     local RPMSTATEDIR=
254
255     ## Create directories and assign variables where configuration
256     ## can/will be found on the host
257     if test -n "$is_internalize"; then
258         verifyInternalPackages "$vserver" RH || test -n "$IS_FORCE" ||
259             panic "
260 Can not continue; use '--force' to override this check"
261     
262         pushd "$vdir" >/dev/null
263
264         test ! -L var/lib/rpm || {
265             $_EXEC_CD /var/lib $_RM            rpm &&
266             $_EXEC_CD /var/lib $_MKDIR -m755   rpm &&
267             $_EXEC_CD /var/lib $_CHOWN rpm:rpm rpm ||
268             :
269         } </dev/null 2>/dev/null
270
271         for i in var/cache/apt/{,archives/{,partial},genpkglist,gensrclist} \
272                  var/state/{,apt/{,lists/{,partial}}} \
273                  etc/apt etc/rpm; do
274             test -d "$i" ||
275                 $_EXEC_CD /$(dirname "$i") $_MKDIR -m755 $(basename "$i") || :
276         done #2>/dev/null
277         
278         popd >/dev/null
279         
280         if test -n "$have_apt"; then
281             findDir APTETCDIR    "$cfgdir"/aptetc "$cfgdir"/base/apt/etc /etc/apt /
282         fi
283
284         findDir RPMETCDIR   "$cfgdir"/rpmetc   "$cfgdir"/base/rpm/etc    /etc/rpm /
285         findDir RPMSTATEDIR "$cfgdir"/rpmstate "$cfgdir"/base/rpm/state
286     else
287         mkdir -m755 -p "$cfgdir"
288         local need_base=
289
290         if test -n "$have_apt"; then
291             findDir APTETCDIR    "$cfgdir"/aptetc      "$cfgdir"/base/apt/etc       /
292             findDir APTSTATEDIR  "$cfgdir"/aptstate    "$cfgdir"/base/apt/state     /
293             findDir APTCACHEDIR  "$cfgdir"/aptcache    "$cfgdir"/base/apt/cache     /
294             findDir APTARCHIVDIR "$cfgdir"/aptarchives "$cfgdir"/base/apt/archives  /
295             
296             test "$APTETCDIR"    != / || APTETCDIR=$cfgdir/base/apt/etc
297             test "$APTSTATEDIR"  != / || APTSTATEDIR=$cfgdir/base/apt/state
298             test "$APTCACHEDIR"  != / || APTCACHEDIR=$cfgdir/base/apt/cache
299             test "$APTARCHIVDIR" != / || APTARCHIVDIR=$cfgdir/base/apt/archive
300
301             test -d "$cfgdir"/aptetc   -a -d "$cfgdir"/aptstate -a \
302                  -d "$cfgdir"/aptcache -a -d "$cfgdir"/aptarchives || need_base=1
303         fi
304
305         findDir RPMETCDIR     "$cfgdir"/rpmetc   "$cfgdir"/base/rpm/etc    /
306         findDir RPMSTATEDIR   "$cfgdir"/rpmstate "$cfgdir"/base/rpm/state  /
307
308         test "$RPMETCDIR"   != / || RPMETCDIR=$cfgdir/base/rpm/etc
309         test "$RPMSTATEDIR" != / || RPMSTATEDIR=$cfgdir/base/rpm/state
310
311         test -d "$cfgdir"/rpmetc -a -d "$cfgdir"/rpmstate || need_base=1
312         test ! -e "$cfgdir"/base || need_base=
313
314         test -z "$need_base" || ln -s "$PKGCFGDIR" "$cfgdir"/base
315
316         mkdir -m755 -p "$PKGCFGDIR"
317         _createDirs "$APTETCDIR" "$APTSTATEDIR" "$APTCACHEDIR" "$APTARCHIVDIR" \
318                     "$RPMETCDIR" "$RPMSTATEDIR"
319     fi
320
321     ## Copy the files...
322     if test -n "$is_internalize"; then
323         if test -n "$have_apt"; then
324             _copySecure "$vdir" "$APTETCDIR" /etc/apt
325             pushd "$vdir" >/dev/null
326                 _hashAuto /etc/apt/apt.conf '/'
327             popd >/dev/null
328         fi
329
330         _copySecure "$vdir" "$RPMETCDIR"   /etc/rpm
331         _copySecure "$vdir" "$RPMSTATEDIR" /var/lib/rpm
332
333         pushd "$vdir" >/dev/null
334             ## remove %_dbpath settings
335             _substFile /etc/rpm/macros '/^%_dbpath[ \t].*/D'
336         popd >/dev/null
337     else
338         if test -n "$have_apt"; then
339             _copySecureRev "$vdir" /etc/apt "$APTETCDIR"
340             _unhashAuto "$APTETCDIR"/apt.conf '/'
341         fi
342
343         _copySecureRev "$vdir" /etc/rpm     "$RPMETCDIR"
344         _copySecureRev "$vdir" /var/lib/rpm "$RPMSTATEDIR"
345
346         echo -e "%_dbpath\t\t$rpmdb_mntpoint" >>$RPMETCDIR/macros
347     fi
348
349     ## Cleanups...
350     if test -n "$is_internalize"; then
351         :
352     else
353         tmpdir=$($_MKTEMPDIR /var/tmp/pgmgmt.XXXXXX)
354         trap "$_RM -rf $tmpdir" EXIT
355         pushd "$vdir" >/dev/null
356         $_EXEC_CD /var/lib $_MV rpm $tmpdir/
357         $_EXEC_CD /var/lib $_LN_S "$rpmdb_mntpoint" rpm
358         $_RM -rf $tmpdir
359     fi
360
361     ## Finish it...
362     if test -n "$is_internalize"; then
363         $_TOUCH "$cfgdir"/internal
364     else
365         $_RM -f "$cfgdir"/internal
366     fi
367 }
368
369 function processVserver_Debian()
370 {
371     local vserver=$1
372     local is_internalize=$2
373
374     if test -n "$is_internalize"; then
375         echo $"Debian vservers should be internalized everytime; do not know how to handle '$vserver'" >&2
376     else
377         echo $"External packagemanagement is not supported for Debian vserver" >&2
378     fi
379
380     return 1
381 }
382
383 function processVserver()
384 {
385     local vserver=$1
386     local is_external=
387     local skip=1
388     local vdir
389
390     ! $_VSERVER_INFO -q "$vserver" RUNNING || {
391         echo $"Can not operate on running vservers; please stop '$vserver' and retry again..."
392         return 1
393     } >&2
394
395     vdir=$($_VSERVER_INFO "$vserver" VDIR) && test -d "$vdir" || {
396         echo $"Vserver '$vserver' does not seem to exist; skipping it..."
397         return 1
398     } >&2
399     
400     pkgmgmt.isInternal "$vserver" || is_external=1
401
402     case "$is_external"X"$IS_INTERNALIZE"X"$IS_EXTERNALIZE" in
403         (*X1X1) echo $"Can not externalize and internalize at the same time";;
404         (*XX)   echo $"No operation specified; try '--help' for more information";;
405         (1XX1)  echo $"Vserver '$vserver' has already external packagemanagment; skipping it...";;
406         (X1X)   echo $"Vserver '$vserver' has already internal packagemanagment; skipping it...";;
407         (*)     skip=
408     esac >&2
409
410     test -z "$skip" || return 1
411
412     local style
413     _mountFilesystems  "$vserver"       || return 1
414     pkgmgmt.guessStyle "$vserver" style || return 1
415
416     case "$style" in
417         (redhat|mandrake)       processVserver_RH     "$vserver" "$IS_INTERNALIZE";;
418         (debian)                processVserver_Debian "$vserver" "$IS_INTERNALIZE";;
419         (*)
420             echo $"Vserver style '$style' is not supported for packagemanagment" >&2
421             return 1
422     esac
423
424     _umountFilesystems "$vserver"       || return 1
425 }
426
427 tmp=$(getopt -o y --long debug,externalize,internalize,help,version,force -n "$0" -- "$@") || exit 1
428 eval set -- "$tmp"
429
430 IS_EXTERNALIZE=
431 IS_INTERNALIZE=
432 IS_YES=
433 IS_FORCE=
434
435 while true; do
436     case "$1" in
437         (--help)        showHelp $0;;
438         (--version)     showVersion;;
439         (--debug)       set -x;;
440         (--externalize) IS_EXTERNALIZE=1;;
441         (--internalize) IS_INTERNALIZE=1;;
442         (--force)       IS_FORCE=1;;
443         (-y)            IS_YES=1;;
444         (--)            shift; break;;
445         (*)             echo $"vserver: internal error; arg=='$1'" >&2; exit 1;;
446     esac
447     shift
448 done
449
450 test -n "$1" || {
451     echo $"No vserver specified; try '--help' for more information"
452     exit 1
453 } >&2
454
455
456 set -e
457 init
458
459 ok=1
460 passed=
461 for i; do
462     processVserver "$i" && passed=1 || ok=
463 done
464     
465 test -z "$ok"     || exit 0
466 test -z "$passed" || exit 1
467 exit 2