merge with 0.30.213
[util-vserver.git] / scripts / vserver-build.functions
1 # $Id: vserver-build.functions 2515 2007-03-18 15:32:22Z dhozac $       --*- sh -*--
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 # Expected environment:
19 #     $VSERVER_NAME ... name of vserver
20
21 ROOTDIR=
22 ROOTDIR_REL=
23 CACHEDIR=
24 CACHEDIR_REL=
25 VSERVERDIRNAME=
26
27 VDIR=
28
29 _DEV_FILE=
30 _EXEC_DIR=
31
32 BUILD_INITPRE=
33 BUILD_INITPOST=
34
35 __BASE_GENERATED_FILES=()
36 __BASE_SUCCESS=
37
38 function makeDevEntry
39 {
40     local dst=$1/$2
41     case "$3" in
42         (c|b)   mknod -m$6 "$dst"  $3 $4 $5;;
43         (d)     mkdir -p -m$4 "$dst";;
44         (f)     touch "$dst"
45                 chmod $4 "$dst"
46                 ;;
47         (*)     echo "Unknown dev-entry mode '$3'" >&2
48                 false
49                 ;;
50     esac
51 }
52
53 function populateDev
54 {
55     local spec
56
57     mkdir -p -m755 "$VDIR"/dev/pts
58
59     while read spec; do
60         makeDevEntry "$VDIR"/dev $spec
61     done <$_DEV_FILE
62 }
63
64 function populateDirectory
65 {
66     local dst=$1
67     local i
68     
69     shift
70     for i; do
71         local file=
72         
73         for file in "$i"/*; do
74             isRegularFile "$file" || continue
75             
76             cp -a "$file" "$dst/"
77         done
78     done
79 }
80
81 function _setRootDir
82 {
83     test -z "$ROOTDIR" || return 0
84     
85     for item in "\"$__CONFDIR/.defaults/vdirbase\" 1" "$__DEFAULT_VSERVERDIR"; do
86         eval set -- "$item"
87         ROOTDIR=$1
88         ROOTDIR_REL=$2
89         test ! -d "$ROOTDIR" || break
90     done
91
92     test -d "$ROOTDIR" || {
93         echo "Root-directory '$ROOTDIR' does not exist or is invalid" >&2
94         exit 1
95     }
96 }
97
98 function _setCacheDir
99 {
100     test -z "$CACHEDIR" || return 0
101     
102     for item in "\"$__CONFDIR/.defaults/cachebase\" 1" "$__PKGCACHEDIR"; do
103         eval set -- "$item"
104         CACHEDIR=$1
105         CACHEDIR_REL=$2
106         test ! -d "$CACHEDIR" || break
107     done
108
109     test -d "$CACHEDIR" || {
110         echo "Cache-directory '$CACHEDIR' does not exist or is invalid" >&2
111         exit 1
112     }
113 }
114
115 function _setVserverDirName
116 {
117     test -z "$VSERVERDIRNAME" || return 0
118     VSERVERDIRNAME="$VSERVER_NAME"
119 }
120
121 function _setVdir
122 {
123     VDIR="$ROOTDIR/$VSERVERDIRNAME"
124 }
125
126 function say
127 {
128     test -z "$OPTION_SILENT" || return 0
129     echo "$@"
130 }
131
132 function _renameVserverCfg
133 {
134     local suffix=.~$(date +'%s')~
135     local i
136     
137     for i in "$VDIR" "$SETUP_CONFDIR"; do
138         test ! -e "$i" || isDirectoryEmpty "$i" || {
139             mv "$i" "$i$suffix"
140             say "Renamed '$i' to '$i$suffix'"
141         }
142     done
143 }
144
145
146 ## Usage: getDistribution [<default>] [<ignore-config>]
147 function getDistribution
148 {
149     local ignore_config=$2
150     
151     if test -z "$DISTRIBUTION"; then
152         if test -e /etc/fedora-release; then
153             set -- $(cat /etc/fedora-release)
154             DISTRIBUTION=fdr$4
155         elif test -e /etc/redhat-release; then
156             set -- $(cat /etc/redhat-release)
157             DISTRIBUTION=rh$5
158         elif test -e /etc/debian_version; then
159             set -- $(cat /etc/debian_version)
160             DISTRIBUTION=deb$1
161         elif test -e /etc/SuSE-release; then
162             set -- $(cat /etc/SuSE-release)
163             DISTRIBUTION=suse$3
164         elif test -e /etc/gentoo-release; then
165             set -- $(cat /etc/gentoo-release)
166             DISTRIBUTION=gentoo$5
167         elif test -e /etc/slackware-version; then
168             set -- $(cat /etc/slackware-version)
169             DISTRIBUTION=slackware$2
170         elif test -n "$1"; then
171             DISTRIBUTION=$1
172         else
173             colpanic $"\
174 ERROR: Can not determine distribution; please specify it manually with
175   the '-d' option."
176         fi
177     fi
178
179     test -n "$ignore_config" -o \
180          -d "$__CONFDIR/.distributions/$DISTRIBUTION" -o \
181          -d "$__DISTRIBDIR/$DISTRIBUTION" ||
182             colpanic $"\
183 ERROR: Can not find configuration for the distribution '$DISTRIBUTION';
184   please read http://linux-vserver.org/HowToRegisterNewDistributions
185   for information how to add support for your own distribution."
186 }
187
188 function base._addGeneratedFile
189 {
190     __BASE_GENERATED_FILES=( "${__BASE_GENERATED_FILES[@]}" "$@" )
191 }
192
193 ## Usage: initFilesystem [force]
194 function base.initFilesystem
195 {
196     test -z "$1" || _renameVserverCfg
197     { isDirectoryEmpty "$VDIR" && test  ! -e "$SETUP_CONFDIR"; } || colpanic $"\
198 vserver-topdirectory '$VDIR' and/or configuration at '$SETUP_CONFDIR'
199 exist already; please try to use '--force', or remove them manually."
200
201     mkdir -p -m755 "$VDIR"
202     $_SETATTR --~barrier "$VDIR"
203     base._addGeneratedFile "$VDIR"
204     
205     mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/etc
206     base._addGeneratedFile "$SETUP_CONFDIR"
207     
208     ln -s "$VDIR"                     "$SETUP_CONFDIR/vdir"
209     ln -s "$CACHEDIR/$VSERVERDIRNAME" "$SETUP_CONFDIR/cache"
210
211     populateDev
212
213     mkdir -p "$VDIR"/proc
214     findAndCopy "$VDIR"/etc/hosts         "$__CONFDIR"/.defaults/files/hosts "$__CONFDIR/.distributions/$DISTRIBUTION"/files/hosts \
215                                           "$__DISTRIBDIR/$DISTRIBUTION"/files/hosts "$__DISTRIBDIR"/defaults/files/hosts ""
216
217     for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime resolv.conf; do
218         findAndCopy "$VDIR"/etc/$i  "$__CONFDIR/.defaults/files/$i" "$__CONFDIR/.distributions/$DISTRIBUTION/files/$i" ""
219     done
220 }
221
222 function base._initVariables
223 {
224     _setRootDir
225     _setCacheDir
226     _setVserverDirName
227     _setVdir
228
229     findFile _DEV_FILE      "$__CONFDIR/.distributions/$DISTRIBUTION/devs"      "$__DISTRIBDIR/$DISTRIBUTION/devs"     "$__DISTRIBDIR/defaults/devs"
230     findDir  _EXECDIR       "$__CONFDIR/.distributions/$DISTRIBUTION/execdir"   "$__DISTRIBDIR/$DISTRIBUTION/execdir"  /
231     findFile BUILD_INITPRE  "$__CONFDIR/.distributions/$DISTRIBUTION/initpre"   "$__DISTRIBDIR/$DISTRIBUTION/initpre"  ""
232     findFile BUILD_INITPOST "$__CONFDIR/.distributions/$DISTRIBUTION/initpost"  "$__DISTRIBDIR/$DISTRIBUTION/initpost" ""
233 }
234
235 function base.__cleanup
236 {
237     test -z "$OPTION_KEEP"    || return 0
238     test -z "$__BASE_SUCCESS" || return 0
239     
240     rm -rf "${__BASE_GENERATED_FILES[@]}"
241 }
242
243 function base.init
244 {
245     test -z "$SETUP_CONTEXT" || ! $_VSERVER_INFO -q "$SETUP_CONTEXT" RUNNING || \
246         panic $"\
247 Context '$SETUP_CONTEXT' is already in use. Please select another one."
248
249     trap "base.__cleanup" EXIT
250     base._initVariables
251 }
252
253 function base.setSuccess
254 {
255     __BASE_SUCCESS=1
256 }