added Vector_zeroEnd() function
[util-vserver.git] / util-vserver / 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 VSERVERDIRNAME=
24
25 VDIR=
26
27 _DEV_FILE=
28 _EXEC_DIR=
29
30 BUILD_INITPRE=
31 BUILD_INITPOST=
32
33 function makeDevEntry
34 {
35     local dst=$1/$2
36     case "$3" in
37         (c|b)   mknod -m$6 "$dst"  $3 $4 $5;;
38         (d)     mkdir -p -m$4 "$dst";;
39         (f)     touch "$dst"
40                 chmod $4 "$dst"
41                 ;;
42         (*)     echo "Unknown dev-entry mode '$3'" >&2
43                 false
44                 ;;
45     esac
46 }
47
48 function populateDirectory
49 {
50     local dst=$1
51     local i
52     
53     shift
54     for i; do
55         local file=
56         
57         for file in "$i"/*; do
58             test -e "$file" || continue
59             case "$file" in
60                 (*/CVS) test ! -d "$file" || continue;;
61                 (*.rpmsave|*.rpmnew|*.rpmorig)
62                         continue;;
63             esac
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 _setVserverDir
88 {
89     test -z "$VSERVERDIRNAME" || return 0
90     VSERVERDIRNAME="$VSERVER_NAME"
91 }
92
93 function _setVdir
94 {
95     VDIR="$ROOTDIR/$VSERVERDIRNAME"
96 }
97
98 function say
99 {
100     test -z "$OPTION_SILENT" || return 0
101     echo "$@"
102 }
103
104 function _renameVserverCfg
105 {
106     local suffix=.~$(date +'%s')~
107     local i
108     
109     for i in "$VDIR" "$SETUP_CONFDIR"; do
110         test ! -e "$i" || {
111             mv "$i" "$i$suffix"
112             say "Renamed '$i' to '$i$suffix'"
113         }
114     done
115 }
116
117 function getDistribution
118 {
119     test -z "$DISTRIBUTION" || return 0
120
121     if test -e /etc/fedora-release; then
122         set -- $(cat /etc/fedora-release)
123         DISTRIBUTION=fdr$4
124     elif test -e /etc/redhat-release; then
125         set -- $(cat /etc/redhat-release)
126         DISTRIBUTION=rh$5
127     elif test -e /etc/debian_version; then
128         set -- $(cat /etc/debian_version)
129         DISTRIBUTION=deb$1
130     elif test -e /etc/SuSE-release; then
131         set -- $(cat /etc/SuSE-release)
132         DISTRIBUTION=suse$3
133     else
134         echo \
135 "Can not determine distribution; please specify it manually
136 with the '-d' option"  >&2
137         exit 1
138     fi >&2
139 }
140
141 ## Usage: initFilesystem [force]
142 function base.initFilesystem
143 {
144     test -z "$1" || _renameVserverCfg
145     test ! -d "$VDIR" -a ! -d "$SETUP_CONFDIR" || {
146         echo \
147 "vserver-topdirectory '$VDIR' and/or configuration at '$SETUP_CONFDIR'
148 exist already; please try to use '--force', or remove them manually"
149 >&2
150         exit 1
151     } >&2
152
153     mkdir -p -m755 "$VDIR"
154     chattr -t "$VDIR"
155     mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/{dev/pts,etc} "$SETUP_LOCKREVDIR"
156     
157     ln -s "$VDIR"       "$SETUP_CONFDIR/vdir"
158
159     local spec
160     while read spec; do
161         makeDevEntry "$VDIR"/dev $spec
162     done <$_DEV_FILE
163
164     mkdir -p "$VDIR"/proc
165     findAndCopy "$VDIR"/etc/hosts         "$CONFDIR"/.defaults/files/hosts "$CONFDIR/.distributions/$DISTRIBUTION"/files/hosts \
166                                           "$DISTRIBDIR/$DISTRIBUTION"/files/hosts "$DISTRIBDIR"/defaults/files/hosts ""
167
168     for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime resolv.conf; do
169         findAndCopy "$VDIR"/etc/$i  "$CONFDIR/.defaults/files/$i" "$CONFDIR/.distributions/$DISTRIBUTION/files/$i" ""
170     done
171 }
172
173 function base.initVariables
174 {
175     _setRootDir
176     _setVserverDir
177     _setVdir
178
179     findFile _DEV_FILE      "$CONFDIR/.distributions/$DISTRIBUTION/devs"      "$DISTRIBDIR/$DISTRIBUTION/devs"     "$DISTRIBDIR/defaults/devs"
180     findDir  _EXECDIR       "$CONFDIR/.distributions/$DISTRIBUTION/execdir"   "$DISTRIBDIR/$DISTRIBUTION/execdir"  /
181     findFile BUILD_INITPRE  "$CONFDIR/.distributions/$DISTRIBUTION/initpre"   "$DISTRIBDIR/$DISTRIBUTION/initpre"  ""
182     findFile BUILD_INITPOST "$CONFDIR/.distributions/$DISTRIBUTION/initpost"  "$DISTRIBDIR/$DISTRIBUTION/initpost" ""
183 }