added variables for _CHROOT_RM and _FAKE_RUNLEVEL
[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=
36 root_rel=1
37 dir=
38 name=
39 hostname=
40 iproot=
41 iprootmask=
42 iprootbcast=
43 iprootdev=
44 force=
45 pkgcfg=
46 pkgcfgbase_rel=
47
48 function makeDevEntry
49 {
50     local dst=$1/$2
51     case "$3" in
52         c|b)    mknod -m$6 "$dst"  $3 $4 $5;;
53         d)      mkdir -p -m$4 "$dst";;
54         f)      touch "$dst"
55                 chmod $4 "$dst"
56                 ;;
57         *)      echo "Unknown dev-entry mode '$3'" >&2
58                 false
59                 ;;
60     esac
61 }
62
63 function installBasePackages
64 {
65     local name="$1"
66     local dir="$2"
67
68     test "$dir" != / || return
69     for filelist in "$dir"/*; do
70         test -f "$filelist" || continue
71         local idx=0
72         local can_fail=false
73         local flags=
74
75         set -- $(<$filelist)
76         while test "$#" -gt 0; do
77             case "$1" in
78                 --reinstall) flags='--reinstall';;
79                 --can-fail)  can_fail=true;;
80                 *)           break;;
81             esac
82             shift
83         done
84         "$_VAPT_GET" "$name" -- install -y $flags $* || $can_fail
85     done
86 }
87
88 function populateDirectory
89 {
90     local dst=$1
91     local i
92     
93     shift
94     for i; do
95         local file=
96         
97         for file in "$i"/*; do
98             test -e "$file" || continue
99             cp -a "$file" "$dst/"
100         done
101     done
102 }
103
104 function prepareRPMDb
105 {
106     rpmdb_path=/dev
107     test -z "$WORKAROUND_106057" || {
108         rpmdb_path=/.rpmdb
109         mkdir -p "$rpmdb_path"
110     }
111     mkdir -p "$vdir$rpmdb_path"
112 }
113
114 function importGPGPubKeys
115 {
116     local dst=$1
117     local pkgs
118     declare -a pkgs
119     local i
120     
121     shift
122     for i; do
123         local file=
124         for file in "$i"/*; do
125             test -f "$file" || continue
126             pkgs[${#pkgs[*]}]=$file
127         done
128     done
129
130     test "$pkgs" || return 0
131     "$_VRPM" "$dst" -- --import "${pkgs[@]}"
132 }
133
134 while true; do
135     case "$1" in
136         -d)             distrib="$2"; shift 2;;
137         -r)             root="$2";    shift 2;;
138         --pkgcfg)       pkgcfg="$2";  shift 2;;
139         --force)        force=1;      shift;;
140         --dir)          dir="$2";     shift 2;;
141         --hostname)     hostname="$2"; shift 2;;
142         --iproot)       iproot="$2";   shift 2;;
143         --iprootmask)   iprootmask="$2";  shift 2;;
144         --iprootbcast)  iprootbcast="$2"; shift 2;;
145         --iprootdev)    iprootdev="$2";   shift 2;;
146         --help)         showHelp;         exit 0;;
147         --version)      showVersion;      exit 0;;
148         --)             shift; break;;
149         *)              echo "Internal error!"; exit 1;;
150     esac
151 done
152
153 test "$#" != 0 || {
154     echo "No vserver name given" >&2
155     exit 1
156 }
157
158 test "$#" = 1 || {
159     echo "Too much parameters" >&2
160     exit 1
161 }
162
163 name=$1
164
165 test "$root"    || {
166     for item in "$CONFDIR/.defaults/vdirbase 1" "/vservers"; do
167         set -- $item
168         root=$1
169         root_rel=$2
170         test ! -d "$root" || break
171     done
172 }
173
174 test -d "$root" || {
175     echo "Root-directory '$root' does not exists or is invalid"
176     exit 1
177 }
178     
179 test "$pkgcfg"  || {
180     pkgcfgbase=
181     for item in "$CONFDIR/.defaults/apps/pkgmgmt/base 1" "$root/.pkg"; do
182         set -- $item
183         pkgcfgbase=$1
184         pkgcfgbase_rel=$2
185         test ! -d "$pkgcfgbase" || break
186     done
187
188     test -d "$pkgcfgbase"  || {
189         echo "pkgcfgbase-dir '$pkgcfgbase' does not exists or is invalid"
190         exit 1
191     }
192
193     pkgcfg="$pkgcfgbase/$name"
194 }
195
196 test "$dir"        || dir=$name
197
198 vdir=$root/$dir
199 test ! -d "$vdir"  || {
200     test "$force" && mv "$vdir"    "$vdir".bak
201 }
202
203 confdir="$CONFDIR/$name"
204 test ! -d "$confdir" || {
205     test "$force" && mv "$confdir" "$confdir".bak
206 }
207
208 test ! -d "$vdir" -a ! -d "$confdir" || {
209     echo "vserver-topdirectory and/or configuration exist already; "
210     echo "please try to use '--force', or remove them manually"
211     exit 1
212 } >&2
213
214 test "$distrib" || {
215     if test -e /etc/redhat-release; then
216         set -- $(cat /etc/redhat-release)
217         distrib=rh$5
218     else
219         echo "Can not determine distribution; please specify it manually"
220         echo "with the '-d' option"
221         exit 1
222     fi >&2
223 }
224
225 findFile INITPRE  $CONFDIR/.distributions/$distrib/initpre  $DISTRIBDIR/$distrib/initpre        ""
226 findFile INITPOST $CONFDIR/.distributions/$distrib/initpost $DISTRIBDIR/$distrib/initpost       ""
227 findFile DEVDESCR $CONFDIR/.distributions/$distrib/devs     $DISTRIBDIR/$distrib/devs           $DISTRIBDIR/defaults/devs
228 findDir  EXECDIR  $CONFDIR/.distributions/$distrib/execdir  $DISTRIBDIR/$distrib/execdir        /
229 findDir  RPMLIB   $CONFDIR/.distributions/$distrib/rpmlib   $DISTRIBDIR/$distrib/rpmlib         /
230 findDir  PKGDIR   $CONFDIR/.distributions/$distrib/pkgs     $DISTRIBDIR/$distrib/pkgs           /
231
232 mkdir -p "$confdir"/apps/pkgmgmt "$vdir"/{dev/pts,etc} \
233          "$pkgcfg"/{rpm/{etc,state},apt/{etc,archives/partial,cache,state/lists/partial}}
234
235 prepareRPMDb
236                  
237 ## HACK AND SECURITY ALERT!
238 ## /var/lib/rpm is hardcoded into apt-get which does not honor the
239 ## %_dbpath variable therefore
240 #mkdir -p "$vdir"/var/lib
241 #ln -s ../../dev "$vdir"/var/lib/rpm
242
243
244 while read spec; do
245     makeDevEntry "$vdir"/dev $spec
246 done <$DEVDESCR
247
248 ln -s "$vdir" "$confdir"/vdir
249 for i in hostname iproot iprootmask iprootbcast iprootdev; do
250     i_=\$$i
251     eval "v=$i_"
252     test -z "$v" || echo $v >"$confdir/$i"
253 done
254
255 ln -s "$pkgcfg" "$confdir/apps/pkgmgmt/base"
256 test "$EXECDIR" = / || ln -s "$EXECDIR" "$confdir/apps/pkgmgmt/execdir"
257 test "$RPMLIB"  = / || ln -s "$RPMLIB"  "$confdir/apps/pkgmgmt/rpmlib"
258
259 populateDirectory "$pkgcfg/apt/etc" "$DISTRIBDIR/$distrib/apt" "$CONFDIR/.distributions/$distrib/apt"
260 populateDirectory "$pkgcfg/rpm/etc" "$DISTRIBDIR/defaults/rpm" "$DISTRIBDIR/$distrib/rpm" \
261                   "$CONFDIR/.distributions/$distrib/rpm"
262
263 echo "%_dbpath $rpmdb_path" >>"$pkgcfg/rpm/etc/macros"
264                   
265 test -z "$INITPRE"  || "$INITPRE" "$name"
266 importGPGPubKeys "$name" "$CONFDIR/.distributions/$distrib/pubkeys" "$DISTRIBDIR/$distrib/pubkeys"
267 "$_VAPT_GET" "$name" -- update
268 installBasePackages "$name" "$PKGDIR"
269 "$_VAPT_GET" "$name" -- dist-upgrade -y
270 test -z "$INITPOST" || "$INITPOST" "$name"