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