gnit'ified it
[util-vserver.git] / util-vserver / scripts / vserver-init
index e3ad989..b775687 100755 (executable)
@@ -1,4 +1,5 @@
-#! /bin/bash -e
+#! /bin/bash
+# $Id$
 
 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
 #  
@@ -15,6 +16,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
+set -e
 
 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
 test -e "$UTIL_VSERVER_VARS" || {
@@ -24,27 +26,34 @@ test -e "$UTIL_VSERVER_VARS" || {
 . "$UTIL_VSERVER_VARS"
 . "$PKGLIBDIR/functions"
 
-tmp=$(getopt -o d:r: --long force,dir:,pkgcfg:,hostname:,iproot:,iprootmask:,iprootbcast:,iprootdev:,help,version \
+tmp=$(getopt -o d:r: --long force,dir:,pkgcfg:,hostname:,lockfile:,help,version \
       -n "$0" -- "$@") || exit 1
 
 eval set -- "$tmp"
 
 distrib=
-root=/vservers
+root=
+root_rel=1
 dir=
 name=
-hostname=
-iproot=
-iprootmask=
-iprootbcast=
-iprootdev=
 force=
 pkgcfg=
+pkgcfgbase_rel=
+lockfile=
 
-
-function makeInode
+function makeDevEntry
 {
-    mknod -m$6 $1/$2 $3 $4 $5
+    local dst=$1/$2
+    case "$3" in
+       c|b)    mknod -m$6 "$dst"  $3 $4 $5;;
+       d)      mkdir -p -m$4 "$dst";;
+       f)      touch "$dst"
+               chmod $4 "$dst"
+               ;;
+       *)      echo "Unknown dev-entry mode '$3'" >&2
+               false
+               ;;
+    esac
 }
 
 function installBasePackages
@@ -54,7 +63,7 @@ function installBasePackages
 
     test "$dir" != / || return
     for filelist in "$dir"/*; do
-       test -e "$filelist" || continue
+       test -f "$filelist" || continue
        local idx=0
        local can_fail=false
        local flags=
@@ -68,10 +77,56 @@ function installBasePackages
            esac
            shift
        done
-       "$_VAPT_GET" "$name" -- install $flags "$@"  || $can_fail
+       "$_VAPT_GET" "$name" -- install -y $flags $* || $can_fail
     done
 }
 
+function populateDirectory
+{
+    local dst=$1
+    local i
+    
+    shift
+    for i; do
+       local file=
+       
+       for file in "$i"/*; do
+           test -e "$file" || continue
+           cp -a "$file" "$dst/"
+       done
+    done
+}
+
+function prepareRPMDb
+{
+    rpmdb_path=/dev
+    test -z "$WORKAROUND_106057" || {
+       rpmdb_path=/.rpmdb
+       mkdir -p "$rpmdb_path"
+    }
+    mkdir -p "$vdir$rpmdb_path"
+}
+
+function importGPGPubKeys
+{
+    local dst=$1
+    local pkgs
+    declare -a pkgs
+    local i
+    
+    shift
+    for i; do
+       local file=
+       for file in "$i"/*; do
+           test -f "$file" || continue
+           pkgs[${#pkgs[*]}]=$file
+       done
+    done
+
+    test "$pkgs" || return 0
+    "$_VRPM" "$dst" -- --import "${pkgs[@]}"
+}
+
 while true; do
     case "$1" in
        -d)             distrib="$2"; shift 2;;
@@ -79,11 +134,7 @@ while true; do
        --pkgcfg)       pkgcfg="$2";  shift 2;;
        --force)        force=1;      shift;;
        --dir)          dir="$2";     shift 2;;
-       --hostname)     hostname="$2"; shift 2;;
-       --iproot)       iproot="$2";   shift 2;;
-       --iprootmask)   iprootmask="$2";  shift 2;;
-       --iprootbcast)  iprootbcast="$2"; shift 2;;
-       --iprootdev)    iprootdev="$2";   shift 2;;
+       --lockfile)     lockfile="$2";    shift 2;;
        --help)         showHelp;         exit 0;;
        --version)      showVersion;      exit 0;;
        --)             shift; break;;
@@ -102,8 +153,41 @@ test "$#" = 1 || {
 }
 
 name=$1
-test "$dir"    || dir=$name
-test "$pkgcfg" || pkgcfg=$root/.pkg/$name
+
+test "$root"    || {
+    for item in "$CONFDIR/.defaults/vdirbase 1" "/vservers"; do
+       set -- $item
+       root=$1
+       root_rel=$2
+       test ! -d "$root" || break
+    done
+}
+
+test -d "$root" || {
+    echo "Root-directory '$root' does not exists or is invalid"
+    exit 1
+}
+
+test "$lockfile" || lockfile=/var/run/vservers/$name
+    
+test "$pkgcfg"  || {
+    pkgcfgbase=
+    for item in "$CONFDIR/.defaults/apps/pkgmgmt/base 1" "$root/.pkg"; do
+       set -- $item
+       pkgcfgbase=$1
+       pkgcfgbase_rel=$2
+       test ! -d "$pkgcfgbase" || break
+    done
+
+    test -d "$pkgcfgbase"  || {
+       echo "pkgcfgbase-dir '$pkgcfgbase' does not exists or is invalid"
+       exit 1
+    }
+
+    pkgcfg="$pkgcfgbase/$name"
+}
+
+test "$dir"        || dir=$name
 
 vdir=$root/$dir
 test ! -d "$vdir"  || {
@@ -135,21 +219,32 @@ test "$distrib" || {
 findFile INITPRE  $CONFDIR/.distributions/$distrib/initpre  $DISTRIBDIR/$distrib/initpre       ""
 findFile INITPOST $CONFDIR/.distributions/$distrib/initpost $DISTRIBDIR/$distrib/initpost      ""
 findFile DEVDESCR $CONFDIR/.distributions/$distrib/devs     $DISTRIBDIR/$distrib/devs          $DISTRIBDIR/defaults/devs
-findDir  APTTMPL  $CONFDIR/.distributions/$distrib/apt      $DISTRIBDIR/$distrib/apt
-findDir  RPMTMPL  $CONFDIR/.distributions/$distrib/rpm      $DISTRIBDIR/$distrib/rpm           $DISTRIBDIR/defaults/rpm
 findDir  EXECDIR  $CONFDIR/.distributions/$distrib/execdir  $DISTRIBDIR/$distrib/execdir       /
 findDir  RPMLIB   $CONFDIR/.distributions/$distrib/rpmlib   $DISTRIBDIR/$distrib/rpmlib        /
 findDir  PKGDIR   $CONFDIR/.distributions/$distrib/pkgs     $DISTRIBDIR/$distrib/pkgs          /
 
-mkdir -p "$confdir"/apps/pkgmgmt "$vdir"/{dev,/var/lib/rpm,/etc} \
+mkdir -p "$confdir"/apps/pkgmgmt "$vdir"/{dev/pts,etc} \
          "$pkgcfg"/{rpm/{etc,state},apt/{etc,archives/partial,cache,state/lists/partial}}
+
+ln -s "$lockfile"     "$confdir"/run
+ln -s "$lockfile/rev" "$confdir"/run.rev
+
+prepareRPMDb
+                
+## HACK AND SECURITY ALERT!
+## /var/lib/rpm is hardcoded into apt-get which does not honor the
+## %_dbpath variable therefore
+## see ???
+mkdir -p "$vdir"/var/lib
+ln -s "$rpmdb_path" "$vdir"/var/lib/rpm
+
+
 while read spec; do
-    makeInode "$vdir"/dev $spec
+    makeDevEntry "$vdir"/dev $spec
 done <$DEVDESCR
 
 ln -s "$vdir" "$confdir"/vdir
-for i in hostname iproot iprootmask iprootbcast iprootdev; do
+for i in hostname; do
     i_=\$$i
     eval "v=$i_"
     test -z "$v" || echo $v >"$confdir/$i"
@@ -159,12 +254,15 @@ ln -s "$pkgcfg" "$confdir/apps/pkgmgmt/base"
 test "$EXECDIR" = / || ln -s "$EXECDIR" "$confdir/apps/pkgmgmt/execdir"
 test "$RPMLIB"  = / || ln -s "$RPMLIB"  "$confdir/apps/pkgmgmt/rpmlib"
 
-cp -a "$RPMTMPL/"* "$pkgcfg/rpm/etc/"
-cp -a "$APTTMPL/"* "$pkgcfg/apt/etc/"
-
+populateDirectory "$pkgcfg/apt/etc" "$DISTRIBDIR/$distrib/apt" "$CONFDIR/.distributions/$distrib/apt"
+populateDirectory "$pkgcfg/rpm/etc" "$DISTRIBDIR/defaults/rpm" "$DISTRIBDIR/$distrib/rpm" \
+                 "$CONFDIR/.distributions/$distrib/rpm"
 
-test -z "$INITPRE" || "$INITPRE" "$name"
+echo "%_dbpath $rpmdb_path" >>"$pkgcfg/rpm/etc/macros"
+                 
+test -z "$INITPRE"  || "$INITPRE" "$name"
+importGPGPubKeys "$name" "$CONFDIR/.distributions/$distrib/pubkeys" "$DISTRIBDIR/$distrib/pubkeys"
 "$_VAPT_GET" "$name" -- update
 installBasePackages "$name" "$PKGDIR"
-"$_VAPT_GET" "$name" -- dist-upgrade
+"$_VAPT_GET" "$name" -- dist-upgrade -y
 test -z "$INITPOST" || "$INITPOST" "$name"