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