use $(LIBENSCVECTOR) instead of libensc_vector.a
[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
118 ## Usage: getDistribution [<default>]
119 function getDistribution
120 {
121     test -z "$DISTRIBUTION" || return 0
122
123     if test -e /etc/fedora-release; then
124         set -- $(cat /etc/fedora-release)
125         DISTRIBUTION=fdr$4
126     elif test -e /etc/redhat-release; then
127         set -- $(cat /etc/redhat-release)
128         DISTRIBUTION=rh$5
129     elif test -e /etc/debian_version; then
130         set -- $(cat /etc/debian_version)
131         DISTRIBUTION=deb$1
132     elif test -e /etc/SuSE-release; then
133         set -- $(cat /etc/SuSE-release)
134         DISTRIBUTION=suse$3
135     elif test -e /etc/gentoo-release; then
136         set -- $(cat /etc/gentoo-release)
137         DISTRIBUTION=gentoo$5
138     elif test -e /etc/slackware-release; then
139         set -- $(cat /etc/slackware-release)
140         DISTRIBUTION=slackware$2
141     elif test -n "$1"; then
142         DISTRIBUTION=$1
143     else
144         echo \
145 "Can not determine distribution; please specify it manually
146 with the '-d' option"  >&2
147         exit 1
148     fi >&2
149 }
150
151 ## Usage: initFilesystem [force]
152 function base.initFilesystem
153 {
154     test -z "$1" || _renameVserverCfg
155     test ! -d "$VDIR" -a ! -d "$SETUP_CONFDIR" || {
156         echo \
157 "vserver-topdirectory '$VDIR' and/or configuration at '$SETUP_CONFDIR'
158 exist already; please try to use '--force', or remove them manually"
159 >&2
160         exit 1
161     } >&2
162
163     mkdir -p -m755 "$VDIR"
164     chattr -t "$VDIR"
165     mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/{dev/pts,etc}
166     
167     ln -s "$VDIR"       "$SETUP_CONFDIR/vdir"
168
169     local spec
170     while read spec; do
171         makeDevEntry "$VDIR"/dev $spec
172     done <$_DEV_FILE
173
174     mkdir -p "$VDIR"/proc
175     findAndCopy "$VDIR"/etc/hosts         "$__CONFDIR"/.defaults/files/hosts "$__CONFDIR/.distributions/$DISTRIBUTION"/files/hosts \
176                                           "$__DISTRIBDIR/$DISTRIBUTION"/files/hosts "$__DISTRIBDIR"/defaults/files/hosts ""
177
178     for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime resolv.conf; do
179         findAndCopy "$VDIR"/etc/$i  "$__CONFDIR/.defaults/files/$i" "$__CONFDIR/.distributions/$DISTRIBUTION/files/$i" ""
180     done
181 }
182
183 function base.initVariables
184 {
185     _setRootDir
186     _setVserverDir
187     _setVdir
188
189     findFile _DEV_FILE      "$__CONFDIR/.distributions/$DISTRIBUTION/devs"      "$__DISTRIBDIR/$DISTRIBUTION/devs"     "$__DISTRIBDIR/defaults/devs"
190     findDir  _EXECDIR       "$__CONFDIR/.distributions/$DISTRIBUTION/execdir"   "$__DISTRIBDIR/$DISTRIBUTION/execdir"  /
191     findFile BUILD_INITPRE  "$__CONFDIR/.distributions/$DISTRIBUTION/initpre"   "$__DISTRIBDIR/$DISTRIBUTION/initpre"  ""
192     findFile BUILD_INITPOST "$__CONFDIR/.distributions/$DISTRIBUTION/initpost"  "$__DISTRIBDIR/$DISTRIBUTION/initpost" ""
193 }