# $Id$ --*- sh -*-- # Copyright (C) 2003 Enrico Scholz # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. function findObject { local mod=$1 local var=$2 local file= local i=X shift 2 for i; do test "$i" || continue test ! $mod "$i" || { file=$i; break; } done test -z "$i" -o "$file" || { echo "Can not find file for '$var'; aborting" exit 1 } >&2 eval "$var=\"$file\"" } function findFile { findObject -f "$@" } function findDir { findObject -d "$@" } function getPhysicalDir { ( set -P && cd "$1" && pwd ) } function _pkgMountBindDir() { test "$1" != "$2" || return 0 mount -n --bind "$1" "$2" } function _pkgSetVarsBase { case "$vserver" in /*) echo "not supported yet" exit 1 ;; *) BASEDIR=$CONFDIR/$vserver test -d "$BASEDIR" || { echo "Can not find configuration-directory" exit 1 } VDIR=$BASEDIR/vdir test -d "$VDIR" || VDIR=/vservers/$vserver VDIR=$(getPhysicalDir "$VDIR") PKGDIR=$BASEDIR/apps/pkgmgmt test -d "$PKGDIR" || { echo "Can not find configuration-directory for package-managment tools" exit 1 } findDir EXECDIR $PKGDIR/execdir / ;; esac if test -z "$WORKAROUND_106057"; then _rpmdb_mntpoint=/dev else _rpmdb_mntpoint=/.rpmdb fi } function _pkgSetVarsRPM { case "$vserver" in /*) echo "not supported yet" exit 1 ;; *) findDir RPMETCDIR $PKGDIR/rpmetc $PKGDIR/base/rpm/etc /etc/rpm findDir RPMSTATEDIR $PKGDIR/rpmstate $PKGDIR/base/rpm/state findDir RPMLIBDIR $PKGDIR/rpmlib / RPMSTATEDIR=$(getPhysicalDir "$RPMSTATEDIR") RPMETCDIR=$(getPhysicalDir "$RPMETCDIR") ;; esac } function _pkgSetVarsApt { case "$vserver" in /*) echo "not supported yet" exit 1 ;; *) findDir APTETCDIR $PKGDIR/aptetc $PKGDIR/base/apt/etc /etc/apt findDir APTSTATEDIR $PKGDIR/aptstate $PKGDIR/base/apt/state findDir APTCACHEDIR $PKGDIR/aptcache $PKGDIR/base/apt/cache findDir APTARCHIVDIR $PKGDIR/aptarchives $PKGDIR/base/apt/archives /var/cache/apt/archives ;; esac } function _pkgMountBase { : } function _pkgMountApt { : } function _pkgMountRPM { _pkgMountBindDir "$RPMETCDIR" /etc/rpm test "$RPMLIBDIR" = "/" || _pkgMountBindDir "$RPMLIBDIR" /usr/lib/rpm "$_SECURE_MOUNT" --chroot "$VDIR" -n --secure --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint" test -z "$WORKAROUND_106057" || mount -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint" } function _pkgSetEnvBase { test "$EXECDIR" = "/" || { PATH=$EXECDIR:$PATH LD_LIBRARY_PATH=$EXECDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} } export PATH LD_LIBRARY_PATH } function _pkgSetEnvApt { : } function _pkgSetEnvRPM { CUR_VSERVER=$vserver RPM_FAKE_NAMESPACE_MOUNTS=$_rpmdb_mntpoint RPM_BINARY=$_VRPM_PRELOAD export CUR_VSERVER RPM_FAKE_NAMESPACE_MOUNTS RPM_BINARY } function pkgInit { local i local vserver=$1 shift _pkgSetVarsBase for i; do case "$i" in rpm) _pkgSetVarsRPM;; apt) _pkgSetVarsApt;; *) echo "Unknown packaging flavor"; exit 1;; esac done _pkgMountBase for i; do case "$i" in rpm) _pkgMountRPM;; apt) _pkgMountApt;; esac done _pkgSetEnvBase for i; do case "$i" in rpm) _pkgSetEnvRPM;; apt) _pkgSetEnvApt;; esac done _PKG_FLAVORS="$@" _PKG_VSERVER=$vserver } function getAllVservers { local i local var=$1 for i in $CONFDIR/*; do test -d "$i" || continue test ! -e "$i"/disabled || continue test -d "$i"/vdir || continue eval "$var=\"$var ${i##$CONFDIR/}\"" done } ## Usage: getVserverCtx [ []] function getVserverStatus { test -r "$1"/run || return 1 eval read "$2" <"$1"/run test "$3" || return 0 local _tmp=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$ctx" | $_WC -l ) eval "$3"=\$_tmp test "$4" -a $_tmp = 0 || return 0 _tmp=$(readlink "$1/run") test "$_tmp" || return 1 rm -f "$_tmp" return 0 } ## Usage: isVserverRunning [] function isVserverRunning { local ctx procnum getVserverStatus "$1" ctx procnum 1 || return 1 test $procnum != 0 || return 1 test -z "$2" || eval "$2"=\$ctx return 0 }