5a7204462e46e826f82312b4c62500dc346caf17
[util-vserver.git] / scripts / vserver-build.debootstrap
1 # $Id: vserver-build.debootstrap,v 1.13 2005/07/04 18:14:29 ensc Exp $  --*- 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     pushd . &>/dev/null
52     DEBOOTSTRAP=$(which debootstrap 2>/dev/null) || {
53         dir=$($_MKTEMPDIR /var/tmp/debootstrap.XXXXXX)
54         workdir=$dir
55         dst=$dir/debootstrap.deb
56         
57         findFile DEBOOTSTRAP_URI "$__CONFDIR"/.defaults/apps/debootstrap/uri "$__PKGLIBDEFAULTDIR"/debootstrap.uri
58         read tmp <$DEBOOTSTRAP_URI
59         case "$tmp" in
60         (/*)                    ln -s "$tmp" "$dst";;
61         (http://*|ftp://*)
62             echo $"\
63 Could not find local version of 'debootstrap'; downloading it from
64 ${tmp}..."
65             $_WGET -nv -O "$dst" "$tmp" || {
66                 $_CAT <<EOF >&2
67 ERROR: Could not download the debootstrap package from
68
69    $dst
70
71 Usually, this means that Debian released a new version which is unknown
72 to util-vserver and removed the known ones. To fix this, go to
73
74    http://ftp.debian.org/debian/pool/main/d/debootstrap/
75
76 (or a nearby mirror) and search the URL for the most recent *.deb
77 package matching your platform. Then, put this URL into
78
79    $__CONFDIR/.defaults/apps/debootstrap/uri
80
81 and retry the vserver-build command again.
82 EOF
83                 exit 1
84             }
85             ;;
86         (*)                     echo $"Unsupported URI scheme '$tmp'" >&2
87                                 exit 1;;
88         esac
89         cd $dir
90         ar x "$dst"
91         tar xzf data.tar.gz
92         export DEBOOTSTRAP_DIR=`pwd`/usr/lib/debootstrap
93         DEBOOTSTRAP=`pwd`/usr/sbin/debootstrap
94     }
95     popd &>/dev/null
96
97     test -x "$DEBOOTSTRAP" || { echo $"Can not find debootstrap at '$DEBOOTSTRAP'" >&2; exit 1; }
98 }
99
100 function fixupDebian
101 {
102     $_RM -rf "$1"/dev
103     $_MV "$1"/dev.X "$1"/dev
104 }
105
106
107 ###   main starts here <---
108
109
110 tmp=$(getopt -o '+d:m:' --long debug -n "$0" -- "$@") || exit 1
111 eval set -- "$tmp"
112
113 init
114
115 while true; do
116     case "$1" in
117         -d)             DISTRIBUTION=$2; shift;;
118         -m)             mirror=$2;       shift;;
119         --debug)        set -x;;
120         --)             shift; break ;;
121         *)              echo "vserver-build.debootstrap: internal error: unrecognized option '$1'" >&2
122                         exit 1
123                         ;;
124     esac
125     shift
126 done
127
128 getDistribution
129
130 initVariables  
131 initFilesystem "$OPTION_FORCE"
132
133 setup_writeOption "$VSERVER_NAME"
134 setup_writeInitialFstab
135
136 findDebootstrap
137
138 test -z "$BUILD_INITPRE"  || "$BUILD_INITPRE" "$SETUP_CONFDIR"  "$UTIL_VSERVER_VARS"
139 mv "$VDIR"/dev "$VDIR"/dev.X
140 "$DEBOOTSTRAP" "$@" "$DISTRIBUTION" "$VDIR" "$mirror" || :      ## HACK: ignore all errors...
141 fixupDebian "$VDIR"
142 test -z "$BUILD_INITPOST" || "$BUILD_INITPOST" "$SETUP_CONFDIR" "$UTIL_VSERVER_VARS"
143
144 base.setSuccess