added vwait
[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             isRegularFile "$file" || continue
59             
60             cp -a "$file" "$dst/"
61         done
62     done
63 }
64
65 function _setRootDir
66 {
67     test -z "$ROOTDIR" || return 0
68     
69     for item in "\"$__CONFDIR/.defaults/vdirbase\" 1" "$__DEFAULT_VSERVERDIR"; do
70         eval set -- "$item"
71         ROOTDIR=$1
72         ROOTDIR_REL=$2
73         test ! -d "$ROOTDIR" || break
74     done
75
76     test -d "$ROOTDIR" || {
77         echo "Root-directory '$ROOTDIR' does not exist or is invalid" >&2
78         exit 1
79     }
80 }
81
82 function _setVserverDir
83 {
84     test -z "$VSERVERDIRNAME" || return 0
85     VSERVERDIRNAME="$VSERVER_NAME"
86 }
87
88 function _setVdir
89 {
90     VDIR="$ROOTDIR/$VSERVERDIRNAME"
91 }
92
93 function say
94 {
95     test -z "$OPTION_SILENT" || return 0
96     echo "$@"
97 }
98
99 function _renameVserverCfg
100 {
101     local suffix=.~$(date +'%s')~
102     local i
103     
104     for i in "$VDIR" "$SETUP_CONFDIR"; do
105         test ! -e "$i" || {
106             mv "$i" "$i$suffix"
107             say "Renamed '$i' to '$i$suffix'"
108         }
109     done
110 }
111
112
113 ## Usage: getDistribution [<default>]
114 function getDistribution
115 {
116     test -z "$DISTRIBUTION" || return 0
117
118     if test -e /etc/fedora-release; then
119         set -- $(cat /etc/fedora-release)
120         DISTRIBUTION=fdr$4
121     elif test -e /etc/redhat-release; then
122         set -- $(cat /etc/redhat-release)
123         DISTRIBUTION=rh$5
124     elif test -e /etc/debian_version; then
125         set -- $(cat /etc/debian_version)
126         DISTRIBUTION=deb$1
127     elif test -e /etc/SuSE-release; then
128         set -- $(cat /etc/SuSE-release)
129         DISTRIBUTION=suse$3
130     elif test -e /etc/gentoo-release; then
131         set -- $(cat /etc/gentoo-release)
132         DISTRIBUTION=gentoo$5
133     elif test -e /etc/slackware-release; then
134         set -- $(cat /etc/slackware-release)
135         DISTRIBUTION=slackware$2
136     elif test -n "$1"; then
137         DISTRIBUTION=$1
138     else
139         echo \
140 "Can not determine distribution; please specify it manually
141 with the '-d' option"  >&2
142         exit 1
143     fi >&2
144 }
145
146 ## Usage: initFilesystem [force]
147 function base.initFilesystem
148 {
149     test -z "$1" || _renameVserverCfg
150     test ! -d "$VDIR" -a ! -d "$SETUP_CONFDIR" || {
151         echo \
152 "vserver-topdirectory '$VDIR' and/or configuration at '$SETUP_CONFDIR'
153 exist already; please try to use '--force', or remove them manually"
154 >&2
155         exit 1
156     } >&2
157
158     mkdir -p -m755 "$VDIR"
159     chattr -t "$VDIR"
160     mkdir -p -m755 "$SETUP_CONFDIR"/apps "$VDIR"/{dev/pts,etc}
161     
162     ln -s "$VDIR"       "$SETUP_CONFDIR/vdir"
163
164     local spec
165     while read spec; do
166         makeDevEntry "$VDIR"/dev $spec
167     done <$_DEV_FILE
168
169     mkdir -p "$VDIR"/proc
170     findAndCopy "$VDIR"/etc/hosts         "$__CONFDIR"/.defaults/files/hosts "$__CONFDIR/.distributions/$DISTRIBUTION"/files/hosts \
171                                           "$__DISTRIBDIR/$DISTRIBUTION"/files/hosts "$__DISTRIBDIR"/defaults/files/hosts ""
172
173     for i in nsswitch.conf krb5.conf krb.conf krb.realms ldap.conf localtime resolv.conf; do
174         findAndCopy "$VDIR"/etc/$i  "$__CONFDIR/.defaults/files/$i" "$__CONFDIR/.distributions/$DISTRIBUTION/files/$i" ""
175     done
176 }
177
178 function base.initVariables
179 {
180     _setRootDir
181     _setVserverDir
182     _setVdir
183
184     findFile _DEV_FILE      "$__CONFDIR/.distributions/$DISTRIBUTION/devs"      "$__DISTRIBDIR/$DISTRIBUTION/devs"     "$__DISTRIBDIR/defaults/devs"
185     findDir  _EXECDIR       "$__CONFDIR/.distributions/$DISTRIBUTION/execdir"   "$__DISTRIBDIR/$DISTRIBUTION/execdir"  /
186     findFile BUILD_INITPRE  "$__CONFDIR/.distributions/$DISTRIBUTION/initpre"   "$__DISTRIBDIR/$DISTRIBUTION/initpre"  ""
187     findFile BUILD_INITPOST "$__CONFDIR/.distributions/$DISTRIBUTION/initpost"  "$__DISTRIBDIR/$DISTRIBUTION/initpost" ""
188 }