added workaround for https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=106057
[util-vserver.git] / util-vserver / scripts / vserver-init
1 #! /bin/bash -e
2
3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 #  
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.
8 #  
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.
13 #  
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.
17
18
19 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
20 test -e "$UTIL_VSERVER_VARS" || {
21     echo "Can not find util-vserver installation; aborting..."
22     exit 1
23 }
24 . "$UTIL_VSERVER_VARS"
25 . "$PKGLIBDIR/functions"
26
27 tmp=$(getopt -o d:r: --long force,dir:,pkgcfg:,hostname:,iproot:,iprootmask:,iprootbcast:,iprootdev:,help,version \
28       -n "$0" -- "$@") || exit 1
29
30 eval set -- "$tmp"
31
32 distrib=
33 root=/vservers
34 dir=
35 name=
36 hostname=
37 iproot=
38 iprootmask=
39 iprootbcast=
40 iprootdev=
41 force=
42 pkgcfg=
43
44
45 function makeInode
46 {
47     mknod -m$6 $1/$2 $3 $4 $5
48 }
49
50 function installBasePackages
51 {
52     local name="$1"
53     local dir="$2"
54
55     test "$dir" != / || return
56     for filelist in "$dir"/*; do
57         test -e "$filelist" || continue
58         local idx=0
59         local can_fail=false
60         local flags=
61
62         set -- $(<$filelist)
63         while test "$#" -gt 0; do
64             case "$1" in
65                 --reinstall) flags='--reinstall';;
66                 --can-fail)  can_fail=true;;
67                 *)           break;;
68             esac
69             shift
70         done
71         "$_VAPT_GET" "$name" -- install $flags "$@"  || $can_fail
72     done
73 }
74
75 while true; do
76     case "$1" in
77         -d)             distrib="$2"; shift 2;;
78         -r)             root="$2";    shift 2;;
79         --pkgcfg)       pkgcfg="$2";  shift 2;;
80         --force)        force=1;      shift;;
81         --dir)          dir="$2";     shift 2;;
82         --hostname)     hostname="$2"; shift 2;;
83         --iproot)       iproot="$2";   shift 2;;
84         --iprootmask)   iprootmask="$2";  shift 2;;
85         --iprootbcast)  iprootbcast="$2"; shift 2;;
86         --iprootdev)    iprootdev="$2";   shift 2;;
87         --help)         showHelp;         exit 0;;
88         --version)      showVersion;      exit 0;;
89         --)             shift; break;;
90         *)              echo "Internal error!"; exit 1;;
91     esac
92 done
93
94 test "$#" != 0 || {
95     echo "No vserver name given" >&2
96     exit 1
97 }
98
99 test "$#" = 1 || {
100     echo "Too much parameters" >&2
101     exit 1
102 }
103
104 name=$1
105 test "$dir"    || dir=$name
106 test "$pkgcfg" || pkgcfg=$root/.pkg/$name
107
108 vdir=$root/$dir
109 test ! -d "$vdir"  || {
110     test "$force" && mv "$vdir"    "$vdir".bak
111 }
112
113 confdir="$CONFDIR/$name"
114 test ! -d "$confdir" || {
115     test "$force" && mv "$confdir" "$confdir".bak
116 }
117
118 test ! -d "$vdir" -a ! -d "$confdir" || {
119     echo "vserver-topdirectory and/or configuration exist already; "
120     echo "please try to use '--force', or remove them manually"
121     exit 1
122 } >&2
123
124 test "$distrib" || {
125     if test -e /etc/redhat-release; then
126         set -- $(cat /etc/redhat-release)
127         distrib=rh$5
128     else
129         echo "Can not determine distribution; please specify it manually"
130         echo "with the '-d' option"
131         exit 1
132     fi >&2
133 }
134
135 findFile INITPRE  $CONFDIR/.distributions/$distrib/initpre  $DISTRIBDIR/$distrib/initpre        ""
136 findFile INITPOST $CONFDIR/.distributions/$distrib/initpost $DISTRIBDIR/$distrib/initpost       ""
137 findFile DEVDESCR $CONFDIR/.distributions/$distrib/devs     $DISTRIBDIR/$distrib/devs           $DISTRIBDIR/defaults/devs
138 findDir  APTTMPL  $CONFDIR/.distributions/$distrib/apt      $DISTRIBDIR/$distrib/apt
139 findDir  RPMTMPL  $CONFDIR/.distributions/$distrib/rpm      $DISTRIBDIR/$distrib/rpm            $DISTRIBDIR/defaults/rpm
140 findDir  EXECDIR  $CONFDIR/.distributions/$distrib/execdir  $DISTRIBDIR/$distrib/execdir        /
141 findDir  RPMLIB   $CONFDIR/.distributions/$distrib/rpmlib   $DISTRIBDIR/$distrib/rpmlib         /
142 findDir  PKGDIR   $CONFDIR/.distributions/$distrib/pkgs     $DISTRIBDIR/$distrib/pkgs           /
143
144 mkdir -p "$confdir"/apps/pkgmgmt "$vdir"/{dev,/var/lib/rpm,/etc} \
145          "$pkgcfg"/{rpm/{etc,state},apt/{etc,archives/partial,cache,state/lists/partial}}
146  
147 while read spec; do
148     makeInode "$vdir"/dev $spec
149 done <$DEVDESCR
150
151 ln -s "$vdir" "$confdir"/vdir
152 for i in hostname iproot iprootmask iprootbcast iprootdev; do
153     i_=\$$i
154     eval "v=$i_"
155     test -z "$v" || echo $v >"$confdir/$i"
156 done
157
158 ln -s "$pkgcfg" "$confdir/apps/pkgmgmt/base"
159 test "$EXECDIR" = / || ln -s "$EXECDIR" "$confdir/apps/pkgmgmt/execdir"
160 test "$RPMLIB"  = / || ln -s "$RPMLIB"  "$confdir/apps/pkgmgmt/rpmlib"
161
162 cp -a "$RPMTMPL/"* "$pkgcfg/rpm/etc/"
163 cp -a "$APTTMPL/"* "$pkgcfg/apt/etc/"
164
165
166 test -z "$INITPRE" || "$INITPRE" "$name"
167 "$_VAPT_GET" "$name" -- update
168 installBasePackages "$name" "$PKGDIR"
169 "$_VAPT_GET" "$name" -- dist-upgrade
170 test -z "$INITPOST" || "$INITPOST" "$name"