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