initial checkin
[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 PKGCFGBASE=
24 PKGCFGBASE_REL=
25 VSERVERDIRNAME=
26
27 VDIR=
28
29 _DEV_FILE=
30 _EXEC_DIR=
31
32 BUILD_INITPRE=
33 BUILD_INITPOST=
34
35 function makeDevEntry
36 {
37     local dst=$1/$2
38     case "$3" in
39         c|b)    mknod -m$6 "$dst"  $3 $4 $5;;
40         d)      mkdir -p -m$4 "$dst";;
41         f)      touch "$dst"
42                 chmod $4 "$dst"
43                 ;;
44         *)      echo "Unknown dev-entry mode '$3'" >&2
45                 false
46                 ;;
47     esac
48 }
49
50 function populateDirectory
51 {
52     local dst=$1
53     local i
54     
55     shift
56     for i; do
57         local file=
58         
59         for file in "$i"/*; do
60             test -e "$file" || continue
61             case "$file" in
62                 */CVS)  test ! -d "$file" || continue;;
63                 *.rpmsave|*.rpmnew|*.rpmorig)
64                         continue;;
65             esac
66             
67             cp -a "$file" "$dst/"
68         done
69     done
70 }
71
72 function _setRootDir
73 {
74     test -z "$ROOTDIR" || return 0
75     
76     for item in "\"$CONFDIR/.defaults/vdirbase\" 1" "/vservers"; do
77         eval set -- "$item"
78         ROOTDIR=$1
79         ROOTDIR_REL=$2
80         test ! -d "$ROOTDIR" || break
81     done
82
83     test -d "$ROOTDIR" || {
84         echo "Root-directory '$ROOTDIR' does not exist or is invalid"
85         exit 1
86     }
87 }
88
89 function _setPkgCfgBase
90 {
91     test -z "$PKGCFGBASE" || return 0
92
93     for item in "\"$CONFDIR/.defaults/apps/pkgmgmt/base\" 1" "$ROOTDIR/.pkg"; do
94         eval set -- "$item"
95         PKGCFGBASE=$1
96         PKGCFGBASE_REL=$2
97         test ! -d "$PKGCFGBASE" || break
98     done
99
100     test -d "$PKGCFGBASE"  || {
101         echo "pkgcfgbase-dir '$PKGCFGBASE' does not exists or is invalid"
102         exit 1
103     }
104 }
105
106 function _setPkgCfg
107 {
108     _setPkgCfgBase
109
110     test -z "$PKGCFGDIR" || return 0
111     PKGCFGDIR="$PKGCFGBASE/$VSERVERDIRNAME"
112 }
113
114 function _setVserverDir
115 {
116     test -z "$VSERVERDIRNAME" || return 0
117     VSERVERDIRNAME="$VSERVER_NAME"
118 }
119
120 function _setVdir
121 {
122     VDIR="$ROOTDIR/$VSERVERDIRNAME"
123 }
124
125 function say
126 {
127     test -z "$OPTION_SILENT" || return 0
128     echo "$@"
129 }
130
131 function _renameVserverCfg
132 {
133     local suffix=.~$(date +'%s')~
134     local i
135     
136     for i in "$VDIR" "$SETUP_CONFDIR"; do
137         test ! -e "$i" || {
138             mv "$i" "$i$suffix"
139             say "Renamed '$i' to '$i$suffix'"
140         }
141     done
142 }
143
144 function getDistribution
145 {
146     test -z "$DISTRIBUTION" || return 0
147
148     if test -e /etc/fedora-release; then
149         set -- $(cat /etc/fedora-release)
150         DISTRIBUTION=fdr$4
151     elif test -e /etc/redhat-release; then
152         set -- $(cat /etc/redhat-release)
153         DISTRIBUTION=rh$5
154     elif test -e /etc/debian_version; then
155         set -- $(cat /etc/debian_version)
156         DISTRIBUTION=deb$1
157     else
158         echo "Can not determine distribution; please specify it manually"
159         echo "with the '-d' option"
160         exit 1
161     fi >&2
162 }
163
164 ## Usage: initFilesystem [force]
165 function initFilesystem
166 {
167     test -z "$1" || _renameVserverCfg
168     test ! -d "$VDIR" -a ! -d "$SETUP_CONFDIR" || {
169         echo "vserver-topdirectory and/or configuration exist already; "
170         echo "please try to use '--force', or remove them manually"
171         exit 1
172     } >&2
173
174     mkdir -p "$SETUP_CONFDIR"/apps/pkgmgmt "$VDIR"/{dev/pts,etc} "$SETUP_LOCKREVDIR"
175     
176     ln -s "$VDIR"       "$SETUP_CONFDIR/vdir"
177
178     test -z "$PKGCFGDIR" || {
179         ln -s "$PKGCFGDIR" "$SETUP_CONFDIR/apps/pkgmgmt/base"
180
181         test "$_EXECDIR" = / || ln -s "$_EXECDIR" "$confdir/apps/pkgmgmt/execdir"
182     }
183
184     local spec
185     while read spec; do
186         makeDevEntry "$VDIR"/dev $spec
187     done <$_DEV_FILE
188 }
189
190 function initVariables
191 {
192     _setRootDir
193     _setVserverDir
194     _setPkgCfg
195     _setVdir
196
197     findFile _DEV_FILE      "$CONFDIR/.distributions/$DISTRIBUTION/devs"      "$DISTRIBDIR/$DISTRIBUTION/devs"     "$DISTRIBDIR/defaults/devs"
198     findDir  _EXECDIR       "$CONFDIR/.distributions/$DISTRIBUTION/execdir"   "$DISTRIBDIR/$DISTRIBUTION/execdir"  /
199     findFile BUILD_INITPRE  "$CONFDIR/.distributions/$DISTRIBUTION/initpre"   "$DISTRIBDIR/$DISTRIBUTION/initpre"  ""
200     findFile BUILD_INITPOST "$CONFDIR/.distributions/$DISTRIBUTION/initpost"  "$DISTRIBDIR/$DISTRIBUTION/initpost" ""
201     findDir  PKGDIR         "$CONFDIR/.distributions/$DISTRIBUTION/pkgs"      "$DISTRIBDIR/$DISTRIBUTION/pkgs"     /
202 }