3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
23 declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1}
34 test -n "$_fo_i" || continue
35 test ! $_fo_mod "$_fo_i" || { _fo_file=$_fo_i; break; }
38 test -z "$_fo_i" -o -n "$_fo_file" || {
39 echo "Can not find file for '$_fo_var'; aborting"
43 eval "$_fo_var=\"$_fo_file\""
59 test ! -s "$dst" || return 0
65 test -n "$tmp" -a -s "$tmp" || return 0
66 $_CP -af "$tmp" "$dst"
69 function getPhysicalDir
71 ( set -P && cd "$1" && pwd )
74 ## Usage: logging <message>
77 if test -n "$_VS_LOGFILE"; then
78 echo "$@" >>"$_VS_LOGFILE"
84 ## Usage: warning <message>
87 if test -n "$_VS_ERRFILE"; then
88 echo "$@" >>"$_VS_ERRFILE"
94 ## Usage: panic <message>
97 if test -n "$_VS_ERRFILE"; then
98 echo "$@" >>"$_VS_ERRFILE"
106 ## Usage: execute <message>
109 test -z "${DEBUG_EXEC:-}" || echo "$@"
110 test "${DEBUG_EXEC:-}" = noexec || exec "$@"
114 ## Usage: isNumber <arg>
118 let tmp=$1+0 2>/dev/null || test -z "${1//0/}" -a -n "$1" || return 1
122 ## Usage: colorize <style> <command>
132 findFile cfile "$__CONFDIR"/.defaults/styles/"$style" ''
133 if test -n "$cfile"; then
137 (bold) echo -ne "\e[1m";;
138 (emph) echo -ne "\e[34m";;
139 (info) echo -ne "\e[0;34m";;
140 (warn*) echo -ne "\e[1;31m";;
141 (error) echo -ne "\e[1;33;41m";;
151 ## Usage: xtermTitle <title>
155 echo -ne "\e]0;$@\007"
159 ## Usage: lock <lockfile> [<timeout>]
162 local tmp=$($_MKTEMP /tmp/vserver-lock.XXXXXX)
166 $_LOCKFILE "$1" $tmp $2 &
167 $_GREP -q true $tmp 2>/dev/null || return 1
169 _VS_LOCKS="$! $_VS_LOCKS"
172 ## Usage: unlock [<num>]
179 while test "$#" -gt 0; do
180 kill -HUP "$1" >/dev/null || :
182 test "$num" != 1 || break
183 test -z "$num" || let --num
188 function _pkgMountBindDir()
190 test "$1" != "$2" || return 0
192 $_MOUNT -n --bind "$1" "$2"
195 function _pkgSetVarsBase
199 if test -d "$vserver/vdir"; then
201 VDIR=$(getPhysicalDir "$vserver/vdir")
203 PKGDIR=$BASEDIR/apps/pkgmgmt
204 test -d "$PKGDIR" || {
205 echo "Can not find configuration-directory for package-managment tools"
208 findDir EXECDIR $PKGDIR/execdir /
210 VDIR=$(getPhysicalDir "$vserver")
215 BASEDIR=$__CONFDIR/$vserver
216 test -d "$BASEDIR" || {
217 echo "Can not find configuration-directory"
222 test -d "$VDIR" || VDIR=$__DEFAULT_VSERVERDIR/$vserver
223 VDIR=$(getPhysicalDir "$VDIR")
225 PKGDIR=$BASEDIR/apps/pkgmgmt
226 test -d "$PKGDIR" || {
227 echo "Can not find configuration-directory for package-managment tools"
231 findDir EXECDIR $PKGDIR/execdir /
236 if test -z "$WORKAROUND_106057"; then
239 _rpmdb_mntpoint=/.rpmdb
243 function _pkgSetVarsRPM
245 if test -n "$PKGDIR"; then
246 findDir RPMETCDIR $PKGDIR/rpmetc $PKGDIR/base/rpm/etc /etc/rpm
247 findDir RPMSTATEDIR $PKGDIR/rpmstate $PKGDIR/base/rpm/state
249 findDir RPMLIBDIR $PKGDIR/rpmlib /
252 findDir RPMETCDIR "$VDIR"/etc/rpm /etc/rpm
253 findDir RPMSTATEDIR "$VDIR"/var/lib/rpm
257 RPMSTATEDIR=$(getPhysicalDir "$RPMSTATEDIR")
258 RPMETCDIR=$(getPhysicalDir "$RPMETCDIR")
261 function _pkgSetVarsApt
263 if test -n "$PKGDIR"; then
264 findDir APTETCDIR $PKGDIR/aptetc $PKGDIR/base/apt/etc /etc/apt
265 findDir APTSTATEDIR $PKGDIR/aptstate $PKGDIR/base/apt/state
266 findDir APTCACHEDIR $PKGDIR/aptcache $PKGDIR/base/apt/cache
267 findDir APTARCHIVDIR $PKGDIR/aptarchives $PKGDIR/base/apt/archives /var/cache/apt/archives
269 findDir APTETCDIR "$VDIR"/etc/apt /etc/apt
270 findDir APTSTATEDIR "$VDIR"/var/state/apt
271 findDir APTCACHEDIR "$VDIR"/var/cache/apt
272 findDir APTARCHIVDIR "$VDIR"/var/cache/apt/archives /var/cache/apt/archives
275 findFile APT_CONFIG "$APTETCDIR"/apt.conf ""
276 test -z "$APT_CONFIG" || export APT_CONFIG
279 function _pkgSetVarsYum
281 if test -n "$PKGDIR"; then
282 findDir YUMETCDIR $PKGDIR/yumetc $PKGDIR/base/yum/etc /etc
283 findDir YUMCACHEDIR $PKGDIR/yumcache $PKGDIR/base/yum/cache
285 findDir YUMETCDIR "$VDIR"/etc /etc
286 findDir YUMCACHEDIR "$VDIR"/var/cache/yum
291 function _pkgMountBase
296 function _pkgMountApt
301 function _pkgMountYum
306 function _pkgMountRPM
308 _pkgMountBindDir "$RPMETCDIR" /etc/rpm
309 test "$RPMLIBDIR" = "/" || _pkgMountBindDir "$RPMLIBDIR" /usr/lib/rpm
311 pushd "$VDIR" >/dev/null
313 $_SECURE_MOUNT --chroot -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
314 test -z "$WORKAROUND_106057" || mount -n --bind "$RPMSTATEDIR" "$_rpmdb_mntpoint"
316 test -e "$VDIR"/proc/self/status || \
317 $_SECURE_MOUNT --chroot -n -t proc none /proc
322 function _pkgSetEnvBase
324 test "$EXECDIR" = "/" || {
326 LD_LIBRARY_PATH=$EXECDIR${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
329 export PATH LD_LIBRARY_PATH
332 function _pkgSetEnvApt
337 function _pkgSetEnvYum
342 function _pkgSetEnvRPM
345 RPM_FAKE_NAMESPACE_MOUNTS=$_rpmdb_mntpoint
346 RPM_BINARY=$_VRPM_PRELOAD
348 export CUR_VSERVER RPM_FAKE_NAMESPACE_MOUNTS RPM_BINARY
360 rpm) _pkgSetVarsRPM;;
361 apt) _pkgSetVarsApt;;
362 yum) _pkgSetVarsYum;;
363 *) echo "Unknown packaging flavor" >&2; exit 1;;
386 _PKG_VSERVER=$vserver
389 function isAvoidNamespace
393 $_VSERVER_INFO - FEATURE namespace || return 0
394 cfgdir=$($_VSERVER_INFO "$1" CFGDIR) || return 0
395 test ! -e "$cfgdir"/namespace || return 1
396 test -e "$__CONFDIR"/.defaults/nonamespace -o \
397 -e "$cfgdir"/nonamespace
400 function getAllVservers
403 declare -a _ga_tmp=()
405 for _ga_i in $__CONFDIR/*; do
406 test -d "$_ga_i" || continue
407 test ! -e "$_ga_i"/disabled || continue
408 test -d "$_ga_i"/vdir || continue
413 _ga_tmp=( "${_ga_tmp[@]}" "${_ga_i##$__CONFDIR/}")
416 eval $1='( "${_ga_tmp[@]}" )'
419 ## Usage: _getProcNumberCount <ctx> <var>
420 function _getProcNumberCount
423 local _gp_procnr_cnt=0
425 # Use /proc/virtual from kernel 2.6 when possible
426 if test -d "/proc/virtual"; then
427 set -- $($_GREP '^PROC:' "/proc/virtual/$1/limit" 2>/dev/null)
430 _gp_procnr_cnt=$($_VPS ax | $_AWK '{print $2}' | $_GREP -x "$1" | $_WC -l )
433 let _gp_procnr_cnt=_gp_procnr_cnt+0
434 eval $_gp_var=\$_gp_procnr_cnt
437 ## Usage: getVserverCtx <vdir> <result-varname> [<procnumber-varname> [<do-cleanup>]]
438 ## Returns: 0 iff vserver is running
439 function getVserverStatus
441 test -r "$1"/run || return 1
444 read _gvs_ctx <"$1"/run
447 test -n "$3" || return 0
449 _getProcNumberCount "$_gvs_ctx" _gvs_tmp
452 if test "$_gvs_tmp" = 0; then
453 local runfile=$($_READLINK "$1/run")
454 test -z "$4" || $_RM -f "$runfile"
461 ## Usage: isCtxRunning <ctx>
462 function isCtxRunning
465 _getProcNumberCount "$1" _tmp
469 ## Usage: isVserverRunning <vdir> [<ctx-varname>]
470 function isVserverRunning
472 local _ivr_ctx _ivr_procnum
474 getVserverStatus "$1" _ivr_ctx _ivr_procnum 1 || return 1
475 test "$_ivr_procnum" != 0 || return 1
476 test -z "$2" || eval "$2"=\$_ivr_ctx
480 ## Called as 'getFileValue <varname> <filename>+'
481 function getFileValue
487 findFile _gfv_file "$@" ''
488 test -n "$_gfv_file" -a -r "$_gfv_file" || return 0
489 eval read "$_gfv_var" <"$_gfv_file"
492 ## Called as 'getFileArray <varname> <filename>'
493 function getFileArray
495 test -r "$2" || return 1
497 local IFS=$_VS_NEWLINE
498 eval "$1"='( $(< "$2") )'
501 function checkComponents
512 (core) test -x "$_CHBIND" || failed=1;;
513 (build) test -x "$_VSERVER_BUILD" || failed=1;;
514 (sysv) test -x "$__INITRDDIR/vserver" || failed=1;;
515 (devel) test -d "$__INCLUDEDIR/vserver.h" || failed=1;;
516 (*) echo "Unknown component '$i'" >&2
521 test -z "$failed" || {
530 ## Usage: isKernelAPI <ver> [<cmp-modifier>]
534 api=$($_VSERVER_INFO - APIVER) || api=0
535 test $[ $api ] -${2:-ge} $[ $1 ]
538 ## Usage: callInNamespace <vserver> <command> <args>*
539 function callInNamespace
543 isAvoidNamespace "$1" || \
544 ctx=$( $_VSERVER_INFO "$1" CONTEXT f ) || ctx=
547 if test -n "$ctx"; then
548 $_VNAMESPACE --enter "$ctx" -- "$@"
554 ## Usage: setDefaultTTY <vdir> [<fallback-tty>]
555 function setDefaultTTY
559 cfgdir=$($_VSERVER_INFO "$1" APPDIR init) || cfgdir=
560 findObject -e ttyname \
561 ${cfgdir:+"$cfgdir"/tty} \
562 "$__CONFDIR/.defaults/apps/init/tty" \
569 ## Usage: pkgmgmt.guessStyle <vserver> <resultvar>
570 function pkgmgmt.guessStyle()
573 _pgs_vdir=$($_VSERVER_INFO "$1" VDIR) || {
574 echo $"Can not determine vserver-root" >&2
577 local _pgs_cfgdir=$($_VSERVER_INFO "$1" APPDIR pkgmgmt) || :
579 if test -n "$_pgs_cfgdir" -a -e "$_pgs_cfgdir"/style; then
580 read style <"$_pgs_cfgdir"/style
581 elif test -e "$_pgs_vdir"/etc/redhat-release -o -e "$_pgs_vdir"/etc/fedora-release; then
583 elif test -e "$_pgs_vdir"/etc/mandrake-release; then
585 elif test -e "$_pgs_vdir"/etc/debian_version; then
587 elif test -e "$_pgs_vdir"/etc/SuSE-release; then
590 echo $"Can not determine packagemanagement style" >&2
598 ## Usage: pkgmgmt.isInternal <vserver>
599 ## returns true iff <vserver> is configured for internal packagemanagement
600 ## A typical application is
602 ## | pkgmgmt.isInternal "$vserver" || is_external=1
603 function pkgmgmt.isInternal
605 local cfgdir=$($_VSERVER_INFO "$1" APPDIR pkgmgmt) || :
607 test -z "$cfgdir" -o ! -d "$cfgdir" -o -e "$cfgdir"/internal
610 ## Usage: pkgmgmt.isAptAvailable <cfgdir> <vdir> [<is-internal>]
611 function pkgmgmt.isAptAvailable
615 local is_internal="$3"
618 if test -n "$is_internal"; then
620 test -d "$cfgdir"/base/apt -o -d "$cfgdir"/aptetc || have_apt=
623 for i in /bin /usr/bin /usr/local/bin; do
624 test ! -x "$vdir$i"/apt-get || { have_apt=1; break; }
628 test -n "$have_apt" && return 0 || return 1
631 ## Usage: pkgmgmt.isYumAvailable <cfgdir> <vdir> [<is-internal>]
632 function pkgmgmt.isYumAvailable
636 local is_internal="$3"
639 if test -n "$is_internal"; then
641 test -d "$cfgdir"/base/yum -o -d "$cfgdir"/yumetc || have_yum=
644 for i in /bin /usr/bin /usr/local/bin; do
645 test ! -x "$vdir$i"/yum || { have_yum=1; break; }
649 test -n "$have_yum" && return 0 || return 1
653 function vshelper.doSanityCheck
655 local vshelper this_xid i
656 declare -a warnings=()
657 local solution_disable=
658 local solution_sysctl=
660 vshelper.isEnabled && vshelper.isEnabled warning || return 0
662 this_xid=$($_VSERVER_INFO - XID) ||
663 panic $"Failed to determine current context; aborting..."
665 ## Do nothing in other xid's; the helper will be executed in xid 0 only
666 test "$this_xid" = 0 || return 0
668 local proc_file=/proc/sys/kernel/vshelper
670 if ! test -r "$proc_file"; then
672 warnings=( "${warnings[@]}"
673 $"File '$proc_file' does not exist but is required for vshelper setup" )
676 vshelper=$(cat "$proc_file")
678 $_CMP -s "$vshelper" "$_VSHELPER" || {
679 warnings=( "${warnings[@]}"
680 $"The configured vshelper '$vshelper' does not match the 'vshelper'
681 script of the util-vserver package"
688 test -d "$__VSHELPERSTATEDIR" || {
689 warnings=( "${warnings[@]}"
691 The vshelper state-directory '$__VSHELPERSTATEDIR' does not exist; since
692 it is created by 'make install', this indicates a serious problem with
693 your util-vserver installation" )
697 test "${#warnings[@]}" -eq 0 || {
699 The following problem(s) were encountered while verifying vshelper
702 for i in "${warnings[@]}"; do
708 To fix this, you can:"
710 test -z "$solution_disable" || warning $"\
711 * disable vshelper entirely by executing
712 | touch \"$__CONFDIR/.defaults/apps/vshelper/disabled\"
713 * disable only this message by executing
714 | touch \"$__CONFDIR/.defaults/apps/vshelper/warning-disabled\""
716 test -x "$solution_sysctl" || warning $"\
717 * configure the util-vserver vshelper script, e.g. by adding
718 | kernel.vshelper = \"$_VSHELPER\"
719 to /etc/sysctl.conf and rebooting the machine, or by executing
720 | echo \"$_VSHELPER\" >$proc_file"
730 ## Usage: vshelper.isEnabled [<style>] [<vserver>]
731 function vshelper.isEnabled
733 local f=${1:+$1-}disabled
734 test ! -e "$__CONFDIR"/.defaults/apps/vshelper/"$f" || return 1
735 $_VSERVER_INFO - FEATURE vshelper || return 1
736 if test -n "$2"; then
738 appdir=$($_VSERVER_INFO "$2" APPDIR vshelper) || return 0
739 test -z "$2" -o ! -e "$appdir/$f" || return 1
745 ## Usage: vshelper.isDebug [<vserver>]
746 function vshelper.isDebug
748 test ! -e "$__CONFDIR"/.defaults/apps/vshelper/debug || return 1
749 $_VSERVER_INFO - FEATURE vshelper || return 1
750 if test -n "$1"; then
752 appdir=$($_VSERVER_INFO "$1" APPDIR vshelper) || return 1
753 test -z "$1" -o ! -e "$appdir/debug" || return 1
759 function vshelper._getHandlerInternal
764 shift ## HACK: see below the note about the 'set -u' mode
766 while test "$#" -ge 2; do
771 test "$_vghi_mod" "$_vghi_obj" || continue
774 eval $_vghi_var=\$_vghi_obj
777 read _vghi_tmp <"$_vghi_obj"
778 eval $_vghi_var=:\$_vghi_tmp
780 (*) panic $"Internal error, unexpected modifier '$_vghi_mod'"
788 ## Usage: vshelper.getHandler <result-var> <vserver> <action>
789 function vshelper.getHandler
792 _vgh_appdir=$($_VSERVER_INFO "$2" APPDIR vshelper) || _vgh_appdir=
794 declare -a _vgh_search_list=( X )
795 ## HACK: when we are in 'set -u' mode, empty lists are causing errors
797 test -z "$_vgh_appdir" || _vgh_search_list=( "${_vgh_search_list[@]}" -x "$_vgh_appdir/$3" )
798 test -z "$_vgh_appdir" || _vgh_search_list=( "${_vgh_search_list[@]}" -e "$_vgh_appdir/action" )
799 _vgh_search_list=( "${_vgh_search_list[@]}" -x "$__CONFDIR"/.defaults/apps/vshelper/"$3" )
800 _vgh_search_list=( "${_vgh_search_list[@]}" -e "$__CONFDIR"/.defaults/apps/vshelper/action )
802 ! vshelper._getHandlerInternal "$1" "${_vgh_search_list[@]}" || return 0
806 ## Usage: vshelper.init <vserver> [<method> <args>*]
807 function vshelper.doInit
809 vshelper.isEnabled || return 0
812 xid=$($_VSERVER_INFO "$1" CONTEXT false) && test -n "$xid" || {
813 warning $"vshelper.init: can not determine xid of vserver '$vserver'; returned value was '$xid'"
817 local f="$__VSHELPERSTATEDIR/$xid"
824 if test -n "$2"; then
837 ## Usage: vshelper.doDestroy <vserver> <xid>
838 function vshelper.doDestroy
840 vshelper.isEnabled || return 0
842 $_RM -f "$__VSHELPERSTATEDIR/$2"
845 ## Usage: vshelper.initSync <vserver> <pipe-varname>
846 function vshelper.initSync
849 _vis_tmpdir=$($_MKTEMPDIR /tmp/vserver-stop.XXXXXX) || {
850 warning $"Failed to generate temporary directory for vshelper sync"
854 local _vis_fifo="$_vis_tmpdir"/pipe
855 $_MKFIFO -m700 "$_vis_fifo"
856 vshelper.doInit "$1" sync "$_vis_fifo"
860 ## Usage: vshelper.getSyncTimeout <vserver> <varname>
861 function vshelper.getSyncTimeout
863 local _vgst_appdir _vgst_file _vgst_tmp
864 _vgst_appdir=$($_VSERVER_INFO "$1" APPDIR vshelper) || _vgst_appdir=
866 findFile _vgst_file ${_vgst_appdir:+"$_vgst_appdir"/sync-timeout} "$__CONFDIR"/.defaults/apps/vshelper/sync-timeout ''
867 test -n "$_vgst_file" || return 1
868 read _vgst_tmp <"$_vgst_file"
873 function _rpmFake.getCapFlags
877 if test -n "$ctx" && ! $_VSERVER_INFO - FEATURE migrate; then
878 set -- $($_CHCONTEXT_COMPAT --xid 1 \
879 $_SH -c "$_CAT /proc/[0-9]*/status | $_EGREP '^(CapBset|s_context|ctxflags)'" | \
880 $_GREP -B 1 -A 1 "^s_context: $ctx " | \
881 $_SED -e '1,3p;d' | $_AWK '{ print $2 }')
886 if test -n "$3"; then
887 RPM_FAKE_CAP=$[ ~0x$1 ]
890 RPM_FAKE_CAP=$[ ~0xd40c04ff ]
895 function rpmFake.init
899 vdir=$($_VSERVER_INFO "$1" VDIR) || vdir="$1"
900 ctx=$($_VSERVER_INFO "$1" CONTEXT) || ctx=
903 panic $"Can not find chroot environment at '$vdir' for '$1'"
905 _rpmFake.getCapFlags "$ctx"
907 RPM_FAKE_CHROOT=$vdir
911 function rpmFake.exec
913 export RPM_FAKE_CHROOT RPM_FAKE_CTX RPM_FAKE_CAP RPM_FAKE_FLAGS
915 LD_PRELOAD=$_RPM_FAKE_SO${LD_PRELOAD:+:$LD_PRELOAD} \