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