ready for tagging
[util-vserver.git] / scripts / vserver-build.debootstrap
1 # $Id: vserver-build.debootstrap 2574 2007-07-31 19:43:17Z dhozac $     --*- 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 function init
19 {
20     workdir=
21     MIRROR=
22     options=
23 }
24
25 function initVariables
26 {
27     base.init
28
29     test -n "${MIRROR}" || {
30         local aux
31         findFile aux "$__CONFDIR"/.distributions/"$DISTRIBUTION"/debootstrap.mirror \
32                      "$__CONFDIR"/.defaults/apps/debootstrap/mirror \
33                      "$__DISTRIBDIR"/"$DISTRIBUTION"/debootstrap.mirror \
34                      "$__PKGLIBDEFAULTDIR"/debootstrap.mirror ''
35         test -z "$aux" || read MIRROR <"$aux"
36     }
37     local file
38     findFile file "$__CONFDIR"/.distributions/"$DISTRIBUTION"/debootstrap.options \
39                   "$__CONFDIR"/.defaults/apps/debootstrap/options ''
40     test -z "$file" || read options <"$file"
41 }
42
43 function initFilesystem
44 {
45     base.initFilesystem "$1"
46
47     mkdir -p "$SETUP_CONFDIR"/apps/pkgmgmt
48     touch "$SETUP_CONFDIR"/apps/pkgmgmt/internal
49 }
50
51 function findDebootstrap
52 {
53     test ! -x "$DEBOOTSTRAP" || return 0
54
55     pushd . &>/dev/null
56     DEBOOTSTRAP=$(which debootstrap 2>/dev/null) || {
57         dir=$($_MKTEMPDIR -p /var/tmp debootstrap.XXXXXX)
58         workdir=$dir
59         dst=$dir/debootstrap.deb
60
61         base._addGeneratedFile "$workdir"
62         
63         findFile DEBOOTSTRAP_URI "$__CONFDIR"/.defaults/apps/debootstrap/uri "$__PKGLIBDEFAULTDIR"/debootstrap.uri
64         read tmp <$DEBOOTSTRAP_URI
65         case "$tmp" in
66         (/*)                    ln -s "$tmp" "$dst";;
67         (http://*|ftp://*)
68             echo $"\
69 Could not find local version of 'debootstrap'; downloading it from
70 ${tmp}..."
71             $_WGET -nv -O "$dst" "$tmp" || {
72                 $_CAT <<EOF >&2
73 ERROR: Could not download the debootstrap package from
74
75    $tmp
76
77 Usually, this means that Debian released a new version which is unknown
78 to util-vserver and removed the known ones. To fix this, go to
79
80    http://ftp.debian.org/debian/pool/main/d/debootstrap/
81
82 (or a nearby mirror) and search the URL for the most recent *.deb
83 package matching your platform. Then, put this URL into
84
85    $__CONFDIR/.defaults/apps/debootstrap/uri
86
87 and retry the vserver-build command again.
88 EOF
89                 exit 1
90             }
91             ;;
92         (*)                     echo $"Unsupported URI scheme '$tmp'" >&2
93                                 exit 1;;
94         esac
95         cd $dir
96         ar x "$dst"
97         tar xzf data.tar.gz
98         export DEBOOTSTRAP_DIR=`pwd`/usr/lib/debootstrap
99         DEBOOTSTRAP=`pwd`/usr/sbin/debootstrap
100
101         local arch
102         arch=$(uname -i 2>/dev/null) || \
103             arch=$(arch 2>/dev/null) ||
104             arch=
105
106         ## Wash the calculated architecture
107         case $arch in
108             (i?86|athlon)       arch=i386;;
109         esac
110
111         test -z "$arch" || echo "$arch" >$DEBOOTSTRAP_DIR/arch
112     }
113     popd &>/dev/null
114
115     test -x "$DEBOOTSTRAP" || { echo $"Can not find debootstrap at '$DEBOOTSTRAP'" >&2; exit 1; }
116 }
117
118 function fixupDebian
119 {
120     $_RM -rf "$1"/dev
121     $_MV "$1"/dev.X "$1"/dev
122 }
123
124
125 ###   main starts here <---
126
127
128 tmp=$(getopt -o '+d:m:s:' --long debug -n "$0" -- "$@") || exit 1
129 eval set -- "$tmp"
130
131 init
132
133 while true; do
134     case "$1" in
135         (-d)            DISTRIBUTION=$2; shift;;
136         (-m)            MIRROR=$2;       shift;;
137         (-s)            script=$2;       shift;;
138         (--debug)       set -x;;
139         (--)            shift; break ;;
140         (*)             echo "vserver-build.debootstrap: internal error: unrecognized option '$1'" >&2
141                         exit 1
142                         ;;
143     esac
144     shift
145 done
146
147 getDistribution '' 1
148
149 initVariables  
150 initFilesystem "$OPTION_FORCE"
151
152 setup_writeOption "$VSERVER_NAME"
153 setup_writeInitialFstab
154
155 findDebootstrap
156
157 if test -z "$script"; then
158     findFile script "$__CONFDIR/.distributions/$DISTRIBUTION/debootstrap.script" \
159                     "$__DISTRIBDIR/$DISTRIBUTION/debootstrap.script" ''
160 fi
161
162 export MIRROR
163 test -z "$BUILD_INITPRE"  || "$BUILD_INITPRE"  "$SETUP_CONFDIR" "$UTIL_VSERVER_VARS"
164 mv "$VDIR"/dev "$VDIR"/dev.X
165 "$_VNAMESPACE" --new -- \
166     "$DEBOOTSTRAP" $options "$@" "$DISTRIBUTION" "$VDIR" "$MIRROR" "$script" || :   ## HACK: ignore all errors...
167 fixupDebian "$VDIR"
168 test -z "$BUILD_INITPOST" || "$BUILD_INITPOST" "$SETUP_CONFDIR" "$UTIL_VSERVER_VARS"
169
170 $_RM -fr "$workdir"
171 base.setSuccess