4 # Copyright (C) 2004,2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
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
24 . "$UTIL_VSERVER_VARS"
26 . "$_LIB_VSERVER_BUILD_FUNCTIONS"
27 . "$_LIB_VSERVER_BUILD_FUNCTIONS_PKGMGMT"
28 . "$__PKGLIBDIR/vserver.functions"
33 $"Usage: $0 --externalize|--internalize [-y] [--] <vserver-name>
35 Report bugs to <$PACKAGE_BUGREPORT>."
39 function showVersion()
42 vpkg $PACKAGE_VERSION -- shows information about packages in vservers
43 This program is part of $PACKAGE_STRING
45 Copyright (C) 2004,2005 Enrico Scholz
46 This program is free software; you may redistribute it under the terms of
47 the GNU General Public License. This program has absolutely no warranty."
53 if test -z "$WORKAROUND_106057"; then
56 rpmdb_mntpoint=/.rpmdb
60 function _createDirs()
63 test -n "$i" || continue
68 function _copySecure()
75 ( cd "$srcdir" && tar chf - '.' ) | \
76 ( cd "$chroot" && $_EXEC_CD "$dstdir" $_TAR xf - )
79 function _copySecureRev()
85 ( cd "$chroot" && $_EXEC_CD "$srcdir" $_TAR cf - '.' ) | \
86 ( cd "$dstdir" && tar xf - )
89 ## Usage: _substFile <filename> <sed-expression>
95 $_CHROOT_SH testfile "$file" || return 0
97 local tmp=$($_MKTEMP pkgmgmt_subst.XXXXXX)
98 trap "$_RM -f $tmp" EXIT
100 $_CHROOT_SH cat "$file" | \
101 $_SED -e "$expr" >$tmp
103 $_CHROOT_SH cat "$file" | $_CMP -s $tmp - || \
104 $_CHROOT_SH truncate "$file" <$tmp
114 $_CHROOT_SH testfile "$file" || return 0
116 local tmp=$($_MKTEMP apt.conf.XXXXXX)
117 trap "$_RM -f $tmp" EXIT
119 $_CHROOT_SH cat "$file" | \
120 $_SED -e "s|^\([^$hash].*@autogenerated@\)|$hash$hash\1|" >$tmp
122 $_CHROOT_SH cat "$file" | $_CMP -s $tmp - || \
123 $_CHROOT_SH truncate "$file" <$tmp
128 function _unhashAuto()
130 test -e "$1" || return 0
133 local tmp=$($_MKTEMP apt.conf.XXXXXX)
134 trap "$_RM -f $tmp" EXIT
136 $_SED -e "s|^$hash$hash\(.*@autogenerated@\)|\1|" "$1" >$tmp
137 $_CMP -s "$tmp" "$1" || \
143 function _mountFilesystemsInternal()
146 test -e "$fstab" || return 0
149 pushd "$vdir" >/dev/null
150 "$@" $_SECURE_MOUNT -n -a --chroot --fstab "$fstab"
154 function _mountFilesystems()
157 cfgdir=$($_VSERVER_INFO "$1" CFGDIR) || {
158 echo "Can not determine configuration directory for '$1'; ..." >&2
161 test -n "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$1"
163 _mountFilesystemsInternal "$cfgdir"/fstab || return 1
164 _mountFilesystemsInternal "$cfgdir"/fstab.local || return 1
165 _mountFilesystemsInternal "$cfgdir"/fstab.remote "${CHBIND_CMD[@]}" || return 1
168 function _umountFilesystems()
171 cfgdir=$($_VSERVER_INFO "$1" CFGDIR) || {
172 echo "Can not determine configuration directory for '$1'; ..." >&2
175 local vdir=$cfgdir/vdir
177 test -n "$_HAVE_CHBIND_OPTIONS" || _generateChbindOptions "$1"
179 pushd "$vdir/" >/dev/null || return 1
180 _umountVserverInternal "$cfgdir"/fstab.remote "${CHBIND_CMD[@]}" || is_ok=
181 _umountVserverInternal "$cfgdir"/fstab.local || is_ok=
182 _umountVserverInternal "$cfgdir"/fstab || is_ok=
183 popd >/dev/null || return 1
188 # Usage: verifyInternalPackages <vserver> <style>
189 function verifyInternalPackages()
193 The following errors occured while trying to internalize the
198 (RH) pkgs=$(vrpm "$1" -- -q --qf '---%{NAME}---\n' rpm apt yum "${YUM_RELEASEPKGS[@]}")
199 hasSubstring "$pkgs" ---rpm--- || {
201 * The vserver does not seem to have the 'rpm' package which is required
202 for internal package management. It is suggested to install it before
208 hasSubstring "$pkgs" ---apt--- ---yum--- || {
210 * The vserver does not seem to have a depsolver like 'apt' or 'yum'
211 installed. It is suggested to install such a program before setting
212 up internal package management."
217 test -n "$have_apt" || test -z "$have_yum" || \
218 hasSubstring "$pkgs" "${YUM_RELEASEPKGS[@]}" || {
220 * yum requires a special package which describes the version of the
221 distribution. Such a package could not be found within the vserver
222 so please install it before continuing. Usually, this package is
223 named 'redhat-release' of 'fedora-release'."
233 function processVserver_RH()
236 local is_internalize=$2
241 cfgdir=$($_VSERVER_INFO "$vserver" APPDIR pkgmgmt) || \
242 cfgdir=$($_VSERVER_INFO "$vserver" APPDIR)/pkgmgmt
244 ## Figure out the environment....
247 pkgmgmt.isAptAvailable "$cfgdir" "$vdir" "$is_internalize" || have_apt=
248 pkgmgmt.isYumAvailable "$cfgdir" "$vdir" "$is_internalize" || have_yum=
257 ## Create directories and assign variables where configuration
258 ## can/will be found on the host
259 if test -n "$is_internalize"; then
260 verifyInternalPackages "$vserver" RH || test -n "$IS_FORCE" ||
262 Can not continue; use '--force' to override this check"
264 pushd "$vdir" >/dev/null
266 test ! -L var/lib/rpm || {
267 $_EXEC_CD /var/lib $_RM rpm &&
268 $_EXEC_CD /var/lib $_MKDIR -m755 rpm &&
269 $_EXEC_CD /var/lib $_CHOWN rpm:rpm rpm ||
271 } </dev/null 2>/dev/null
273 for i in var/cache/apt/{,archives/{,partial},genpkglist,gensrclist} \
274 var/state/{,apt/{,lists/{,partial}}} \
277 $_EXEC_CD /$(dirname "$i") $_MKDIR -m755 $(basename "$i") || :
282 if test -n "$have_apt"; then
283 findDir APTETCDIR "$cfgdir"/aptetc "$cfgdir"/base/apt/etc /etc/apt /
286 findDir RPMETCDIR "$cfgdir"/rpmetc "$cfgdir"/base/rpm/etc /etc/rpm /
287 findDir RPMSTATEDIR "$cfgdir"/rpmstate "$cfgdir"/base/rpm/state
289 mkdir -m755 -p "$cfgdir"
292 if test -n "$have_apt"; then
293 findDir APTETCDIR "$cfgdir"/aptetc "$cfgdir"/base/apt/etc /
294 findDir APTSTATEDIR "$cfgdir"/aptstate "$cfgdir"/base/apt/state /
295 findDir APTCACHEDIR "$cfgdir"/aptcache "$cfgdir"/base/apt/cache /
296 findDir APTARCHIVDIR "$cfgdir"/aptarchives "$cfgdir"/base/apt/archives /
298 test "$APTETCDIR" != / || APTETCDIR=$cfgdir/base/apt/etc
299 test "$APTSTATEDIR" != / || APTSTATEDIR=$cfgdir/base/apt/state
300 test "$APTCACHEDIR" != / || APTCACHEDIR=$cfgdir/base/apt/cache
301 test "$APTARCHIVDIR" != / || APTARCHIVDIR=$cfgdir/base/apt/archive
303 test -d "$cfgdir"/aptetc -a -d "$cfgdir"/aptstate -a \
304 -d "$cfgdir"/aptcache -a -d "$cfgdir"/aptarchives || need_base=1
307 findDir RPMETCDIR "$cfgdir"/rpmetc "$cfgdir"/base/rpm/etc /
308 findDir RPMSTATEDIR "$cfgdir"/rpmstate "$cfgdir"/base/rpm/state /
310 test "$RPMETCDIR" != / || RPMETCDIR=$cfgdir/base/rpm/etc
311 test "$RPMSTATEDIR" != / || RPMSTATEDIR=$cfgdir/base/rpm/state
313 test -d "$cfgdir"/rpmetc -a -d "$cfgdir"/rpmstate || need_base=1
314 test ! -e "$cfgdir"/base || need_base=
316 test -z "$need_base" || ln -s "$PKGCFGDIR" "$cfgdir"/base
318 mkdir -m755 -p "$PKGCFGDIR"
319 _createDirs "$APTETCDIR" "$APTSTATEDIR" "$APTCACHEDIR" "$APTARCHIVDIR" \
320 "$RPMETCDIR" "$RPMSTATEDIR"
324 if test -n "$is_internalize"; then
325 if test -n "$have_apt"; then
326 _copySecure "$vdir" "$APTETCDIR" /etc/apt
327 pushd "$vdir" >/dev/null
328 _hashAuto /etc/apt/apt.conf '/'
332 _copySecure "$vdir" "$RPMETCDIR" /etc/rpm
333 _copySecure "$vdir" "$RPMSTATEDIR" /var/lib/rpm
335 pushd "$vdir" >/dev/null
336 ## remove %_dbpath settings
337 _substFile /etc/rpm/macros '/^%_dbpath[ \t].*/D'
340 if test -n "$have_apt"; then
341 _copySecureRev "$vdir" /etc/apt "$APTETCDIR"
342 _unhashAuto "$APTETCDIR"/apt.conf '/'
345 _copySecureRev "$vdir" /etc/rpm "$RPMETCDIR"
346 _copySecureRev "$vdir" /var/lib/rpm "$RPMSTATEDIR"
348 echo -e "%_dbpath\t\t$rpmdb_mntpoint" >>$RPMETCDIR/macros
352 if test -n "$is_internalize"; then
355 tmpdir=$($_MKTEMPDIR -p /var/tmp pkgmgmt.XXXXXX)
356 trap "$_RM -rf $tmpdir" EXIT
357 pushd "$vdir" >/dev/null
358 $_EXEC_CD /var/lib $_MV rpm $tmpdir/
359 $_EXEC_CD /var/lib $_LN_S "$rpmdb_mntpoint" rpm
364 if test -n "$is_internalize"; then
365 $_TOUCH "$cfgdir"/internal
367 $_RM -f "$cfgdir"/internal
371 function processVserver_Debian()
374 local is_internalize=$2
376 if test -n "$is_internalize"; then
377 echo $"Debian vservers should be internalized everytime; do not know how to handle '$vserver'" >&2
379 echo $"External packagemanagement is not supported for Debian vserver" >&2
385 function processVserver()
392 ! $_VSERVER_INFO -q "$vserver" RUNNING || {
393 echo $"Can not operate on running vservers; please stop '$vserver' and retry again..."
397 vdir=$($_VSERVER_INFO "$vserver" VDIR) && test -d "$vdir" || {
398 echo $"Vserver '$vserver' does not seem to exist; skipping it..."
402 _setVserverDir "$vserver"
405 pkgmgmt.initVariables
407 pkgmgmt.isInternal "$vserver" || is_external=1
409 case "$is_external"X"$IS_INTERNALIZE"X"$IS_EXTERNALIZE" in
410 (*X1X1) echo $"Can not externalize and internalize at the same time";;
411 (*XX) echo $"No operation specified; try '--help' for more information";;
412 (1XX1) echo $"Vserver '$vserver' has already external packagemanagment; skipping it...";;
413 (X1X) echo $"Vserver '$vserver' has already internal packagemanagment; skipping it...";;
417 test -z "$skip" || return 1
420 _mountFilesystems "$vserver" || return 1
421 pkgmgmt.guessStyle "$vserver" style || return 1
424 (redhat|mandrake) processVserver_RH "$vserver" "$IS_INTERNALIZE";;
425 (debian) processVserver_Debian "$vserver" "$IS_INTERNALIZE";;
427 echo $"Vserver style '$style' is not supported for packagemanagment" >&2
431 _umountFilesystems "$vserver" || return 1
434 tmp=$(getopt -o y --long debug,externalize,internalize,help,version,force -n "$0" -- "$@") || exit 1
444 (--help) showHelp $0;;
445 (--version) showVersion;;
447 (--externalize) IS_EXTERNALIZE=1;;
448 (--internalize) IS_INTERNALIZE=1;;
449 (--force) IS_FORCE=1;;
452 (*) echo $"vserver: internal error; arg=='$1'" >&2; exit 1;;
458 echo $"No vserver specified; try '--help' for more information"
469 processVserver "$i" && passed=1 || ok=
472 test -z "$ok" || exit 0
473 test -z "$passed" || exit 1