minor optimizations
[util-vserver.git] / util-vserver / scripts / vserver-build.debootstrap
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 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.initVariables
33
34     test "${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=$(mktemp -d /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://*)      $_WGET -nv -O "$dst" "$tmp";;
62         (*)                     echo $"Unsupported URI scheme '$tmp'" >&2
63                                 exit 1;;
64         esac
65         cd $dir
66         ar x "$dst"
67         tar xzf data.tar.gz
68         export DEBOOTSTRAP_DIR=`pwd`/usr/lib/debootstrap
69         DEBOOTSTRAP=`pwd`/usr/sbin/debootstrap
70     }
71     popd &>/dev/null
72
73     test -x "$DEBOOTSTRAP" || { echo $"Can not find debootstrap at '$DEBOOTSTRAP'" >&2; exit 1; }
74 }
75
76 function fixupDebian
77 {
78     rm -rf "$1"/dev
79     mv "$1"/dev.X "$1"/dev
80 }
81
82
83 ###   main starts here <---
84
85
86 tmp=$(getopt -o '+d:m:' --long debug -n "$0" -- "$@") || exit 1
87 eval set -- "$tmp"
88
89 init
90
91 while true; do
92     case "$1" in
93         -d)             DISTRIBUTION=$2; shift;;
94         -m)             mirror=$2;       shift;;
95         --debug)        set -x;;
96         --)             shift; break ;;
97         *)              echo "vserver-build.skeleton: internal error: unrecognized option '$1'" >&2
98                         exit 1
99                         ;;
100     esac
101     shift
102 done
103
104 getDistribution
105
106 initVariables  
107 initFilesystem "$OPTION_FORCE"
108
109 setup_writeOption "$VSERVER_NAME"
110 setup_writeInitialFstab
111
112 findDebootstrap
113
114 test -z "$BUILD_INITPRE"  || "$BUILD_INITPRE" "$SETUP_CONFDIR"  "$UTIL_VSERVER_VARS"
115 mv "$VDIR"/dev "$VDIR"/dev.X
116 "$DEBOOTSTRAP" "$DISTRIBUTION" "$VDIR" "$mirror" || :   ## HACK: ignore all errors...
117 fixupDebian "$VDIR"
118 test -z "$BUILD_INITPOST" || "$BUILD_INITPOST" "$SETUP_CONFDIR" "$UTIL_VSERVER_VARS"