- renamed 'makeInode' to 'makeDevEntry' which is more flexible
[util-vserver.git] / util-vserver / scripts / vserver-init
1 #! /bin/bash
2 # $Id$
3
4 # Copyright (C) 2003 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 set -e
20
21 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
22 test -e "$UTIL_VSERVER_VARS" || {
23     echo "Can not find util-vserver installation; aborting..."
24     exit 1
25 }
26 . "$UTIL_VSERVER_VARS"
27 . "$PKGLIBDIR/functions"
28
29 tmp=$(getopt -o d:r: --long force,dir:,pkgcfg:,hostname:,iproot:,iprootmask:,iprootbcast:,iprootdev:,help,version \
30       -n "$0" -- "$@") || exit 1
31
32 eval set -- "$tmp"
33
34 distrib=
35 root=/vservers
36 dir=
37 name=
38 hostname=
39 iproot=
40 iprootmask=
41 iprootbcast=
42 iprootdev=
43 force=
44 pkgcfg=
45
46
47 function makeDevEntry
48 {
49     local dst=$1/$2
50     case "$3" in
51         c|b)    mknod -m$6 "$dst"  $3 $4 $5;;
52         d)      mkdir -p -m$4 "$dst";;
53         f)      touch "$dst"
54                 chmod $4 "$dst"
55                 ;;
56         *)      echo "Unknown dev-entry mode '$3'" >&2
57                 false
58                 ;;
59     esac
60 }
61
62 function installBasePackages
63 {
64     local name="$1"
65     local dir="$2"
66
67     test "$dir" != / || return
68     for filelist in "$dir"/*; do
69         test -f "$filelist" || continue
70         local idx=0
71         local can_fail=false
72         local flags=
73
74         set -- $(<$filelist)
75         while test "$#" -gt 0; do
76             case "$1" in
77                 --reinstall) flags='--reinstall';;
78                 --can-fail)  can_fail=true;;
79                 *)           break;;
80             esac
81             shift
82         done
83         "$_VAPT_GET" "$name" -- install -y $flags $* || $can_fail
84     done
85 }
86
87 function populateDirectory
88 {
89     local dst=$1
90     local i
91     
92     shift
93     for i; do
94         local file=
95         
96         for file in "$i"/*; do
97             test -e "$file" || continue
98             cp -a "$file" "$dst/"
99         done
100     done
101 }
102
103 function prepareRPMDb
104 {
105     rpmdb_path=/dev
106     test -z "$WORKAROUND_106057" || {
107         rpmdb_path=/.rpmdb
108         mkdir -p "$rpmdb_path"
109     }
110     mkdir -p "$vdir$rpmdb_path"
111 }
112
113 while true; do
114     case "$1" in
115         -d)             distrib="$2"; shift 2;;
116         -r)             root="$2";    shift 2;;
117         --pkgcfg)       pkgcfg="$2";  shift 2;;
118         --force)        force=1;      shift;;
119         --dir)          dir="$2";     shift 2;;
120         --hostname)     hostname="$2"; shift 2;;
121         --iproot)       iproot="$2";   shift 2;;
122         --iprootmask)   iprootmask="$2";  shift 2;;
123         --iprootbcast)  iprootbcast="$2"; shift 2;;
124         --iprootdev)    iprootdev="$2";   shift 2;;
125         --help)         showHelp;         exit 0;;
126         --version)      showVersion;      exit 0;;
127         --)             shift; break;;
128         *)              echo "Internal error!"; exit 1;;
129     esac
130 done
131
132 test "$#" != 0 || {
133     echo "No vserver name given" >&2
134     exit 1
135 }
136
137 test "$#" = 1 || {
138     echo "Too much parameters" >&2
139     exit 1
140 }
141
142 name=$1
143 test "$dir"    || dir=$name
144 test "$pkgcfg" || pkgcfg=$root/.pkg/$name
145
146 vdir=$root/$dir
147 test ! -d "$vdir"  || {
148     test "$force" && mv "$vdir"    "$vdir".bak
149 }
150
151 confdir="$CONFDIR/$name"
152 test ! -d "$confdir" || {
153     test "$force" && mv "$confdir" "$confdir".bak
154 }
155
156 test ! -d "$vdir" -a ! -d "$confdir" || {
157     echo "vserver-topdirectory and/or configuration exist already; "
158     echo "please try to use '--force', or remove them manually"
159     exit 1
160 } >&2
161
162 test "$distrib" || {
163     if test -e /etc/redhat-release; then
164         set -- $(cat /etc/redhat-release)
165         distrib=rh$5
166     else
167         echo "Can not determine distribution; please specify it manually"
168         echo "with the '-d' option"
169         exit 1
170     fi >&2
171 }
172
173 findFile INITPRE  $CONFDIR/.distributions/$distrib/initpre  $DISTRIBDIR/$distrib/initpre        ""
174 findFile INITPOST $CONFDIR/.distributions/$distrib/initpost $DISTRIBDIR/$distrib/initpost       ""
175 findFile DEVDESCR $CONFDIR/.distributions/$distrib/devs     $DISTRIBDIR/$distrib/devs           $DISTRIBDIR/defaults/devs
176 findDir  EXECDIR  $CONFDIR/.distributions/$distrib/execdir  $DISTRIBDIR/$distrib/execdir        /
177 findDir  RPMLIB   $CONFDIR/.distributions/$distrib/rpmlib   $DISTRIBDIR/$distrib/rpmlib         /
178 findDir  PKGDIR   $CONFDIR/.distributions/$distrib/pkgs     $DISTRIBDIR/$distrib/pkgs           /
179
180 mkdir -p "$confdir"/apps/pkgmgmt "$vdir"/{dev,etc} \
181          "$pkgcfg"/{rpm/{etc,state},apt/{etc,archives/partial,cache,state/lists/partial}}
182
183 prepareRPMDb
184                  
185 ## HACK AND SECURITY ALERT!
186 ## /var/lib/rpm is hardcoded into apt-get which does not honor the
187 ## %_dbpath variable therefore
188 #mkdir -p "$vdir"/var/lib
189 #ln -s ../../dev "$vdir"/var/lib/rpm
190
191
192 while read spec; do
193     makeDevEntry "$vdir"/dev $spec
194 done <$DEVDESCR
195
196 ln -s "$vdir" "$confdir"/vdir
197 for i in hostname iproot iprootmask iprootbcast iprootdev; do
198     i_=\$$i
199     eval "v=$i_"
200     test -z "$v" || echo $v >"$confdir/$i"
201 done
202
203 ln -s "$pkgcfg" "$confdir/apps/pkgmgmt/base"
204 test "$EXECDIR" = / || ln -s "$EXECDIR" "$confdir/apps/pkgmgmt/execdir"
205 test "$RPMLIB"  = / || ln -s "$RPMLIB"  "$confdir/apps/pkgmgmt/rpmlib"
206
207 populateDirectory "$pkgcfg/apt/etc" "$DISTRIBDIR/$distrib/apt" "$CONFDIR/.distributions/$distrib/apt"
208 populateDirectory "$pkgcfg/rpm/etc" "$DISTRIBDIR/defaults/rpm" "$DISTRIBDIR/$distrib/rpm" \
209                   "$CONFDIR/.distributions/$distrib/rpm"
210
211 echo "%_dbpath $rpmdb_path" >>"$pkgcfg/rpm/etc/macros"
212                   
213 test -z "$INITPRE"  || "$INITPRE" "$name"
214 "$_VAPT_GET" "$name" -- update
215 installBasePackages "$name" "$PKGDIR"
216 "$_VAPT_GET" "$name" -- dist-upgrade -y
217 test -z "$INITPOST" || "$INITPOST" "$name"