Support stages, update manapge for lenny
[grml-debootstrap.git] / grml-debootstrap
1 #!/bin/sh
2 # Filename:      grml-bootstrap
3 # Purpose:       wrapper around debootstrap for installing plain Debian via grml
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Mon Apr 09 15:22:07 CEST 2007 [mika]
8 ################################################################################
9 # http://www.debian.org/releases/stable/i386/index.html.en
10
11 set -e # exit on any error
12
13 . /etc/grml/lsb-functions
14 . /etc/grml/script-functions
15
16 # inside the chroot system the locales might not be available, so use minimum:
17 export LANG=C
18 export LC_ALL=C
19
20 VERSION='0.6'
21
22 case $* in
23    -h*|--h*)
24      einfo "$0 - wrapper around debootstrap for installing plain Debian via grml"
25      einfo "Adjust /etc/debootstrap/config and invoke $0 afterwards."
26      eend 0
27      exit 0
28    ;;
29    -v|--v*)
30      einfo "$0 version $VERSION"
31      einfo "Send bug reports to Michael Prokop <mika@grml.org>."
32      eend 0
33      exit 0
34    ;;
35 esac
36
37 check4progs debootstrap || exit 1
38 check4root || exit 1
39
40 # without config file it won't work
41 if [ -r /etc/debootstrap/config ] ; then
42    . /etc/debootstrap/config
43 else
44    echo "/etc/debootstrap/config could not be read, exiting."
45    exit 1
46 fi
47
48 if [ -z "$STAGES" ] ; then
49    STAGES='/etc/debootstrap/stages'
50    [ -d "$STAGES" ] || mkdir -p "$STAGES"
51 fi
52
53 # make sure at least $TARGET is set [the partition for the new system]
54 if [ -z "$TARGET" ] ; then
55    eerror "Please adjust /etc/debootstrap/config before running ${0}" ; eend 1
56    exit 1
57 fi
58
59 bailout(){
60   # make sure $TARGET is not mounted when exiting grml-debootstrap
61   if [ -n "$TARGET" ] ; then
62      if grep -q $TARGET /proc/mounts ; then
63         echo "Unmounting $TARGET"
64         umount "$TARGET"
65      fi
66   fi
67   [ -n "$1" ] && EXIT="$1" || EXIT="1"
68   exit "$EXIT"
69 }
70
71 stage() {
72   # error handling
73   if [ -z "$1" ] ; then
74      eerror 'Error: stage() requires at least one argument.'
75      eend 1
76      return 1
77   fi
78   if [ -z "$STAGES" ] ; then
79      eerror 'Error: $STAGES not set.'
80      eend 1
81      return 1
82   fi
83   # main function
84   if [ -f "$STAGES/$1" ] ; then
85      if grep -q done "$STAGES/$1" ; then
86         ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
87      fi
88   else
89      echo "$2" > "$STAGES/$1"
90   fi
91 }
92
93 trap bailout 1 2 3 15
94
95 PARTITION=''
96 DIRECTORY=''
97
98 case $TARGET in
99   /dev/*)
100     PARTITION=1
101     ;;
102   *)
103     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
104     DIRECTORY=1
105     MNTPOINT="$TARGET"
106     MKFS=''
107     TUNE2FS=''
108     FSCK=''
109     GRUB=''
110     GROOT=''
111     ;;
112 esac
113
114 # user should recheck his configuration
115 einfo "$0 - Please recheck configuration before execution:"
116 echo "
117    Target:           $TARGET"
118    case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount-point:      $MNTPOINT" ;; esac
119    [ -n "$GRUB" ] && echo "   Install grub to:  $GROOT / $GRUB"
120    case "$MNTPOINT" in "$TARGET") ;; *) echo "  Important! Continuing will delete all data from ${TARGET}!" ;; esac
121    echo
122 einfon "Is this ok for you? [y/N] "
123
124 read a
125 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
126    eerror "Exiting as requested." ; eend 1
127    exit 1
128 fi
129
130 if [ -n "$MKFS" ] ; then
131    stage mkfs
132    einfo "Running $MKFS on $TARGET"
133    $MKFS $TARGET
134    eend $?
135    stage mkfs done
136 fi
137
138
139 if [ -n "$TUNE2FS" ] ; then
140    stage tune2fs
141    einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
142    $TUNE2FS $TARGET
143    eend $?
144    stage tune2fs done
145 fi
146
147
148 # now mount the new partition or if it's a directory do nothing at all
149 if [ -n "$DIRECTORY" ] ; then
150    einfo "Running grml-debootstrap on a directory, nothing to mount."
151 else
152    if grep -q $TARGET /proc/mounts ; then
153       eerror "$TARGET already mounted, exiting."
154    else
155      [ -n "$MNTPOINT" ] || MNTPOINT='/mnt/test'
156      [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
157      einfo "Mounting $TARGET to $MNTPOINT"
158      mount -o rw,suid,dev $TARGET $MNTPOINT
159      eend $?
160    fi
161 fi
162
163 # get main packages from a debian-mirror
164 if [ -n "$ARCH" ] ; then
165    ARCHCMD="--arch $ARCH"
166    ARCHINFO=" (${ARCH})"
167 else
168    ARCH="$(dpkg --print-architecture)"
169    ARCHCMD="--arch $ARCH"
170    ARCHINFO=" (${ARCH})"
171 fi
172
173 stage debootstrap
174 einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using mirror $MIRROR"
175 $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR
176 stage debootstrap done
177 eend $?
178
179 stage preparechroot
180 einfo "Preparing chroot system"
181   cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
182   chmod 755 $MNTPOINT/bin/chroot-script
183   mkdir $MNTPOINT/etc/debootstrap/
184
185   # make sure we have our files for later use via chroot-script
186   cp /etc/debootstrap/config $MNTPOINT/etc/debootstrap/
187   cp /etc/debootstrap/packages $MNTPOINT/etc/debootstrap/packages
188
189   # make sure we can access network [relevant for cdebootstrap]
190   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
191
192   # setup default locales
193   [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen  $MNTPOINT/etc/locale.gen
194
195   # copy any existing existing files to chroot
196   [ -d /etc/debootstrap/boot  ] && cp -a /etc/debootstrap/boot/*  $MNTPOINT/boot/
197   [ -d /etc/debootstrap/etc   ] && cp -a /etc/debootstrap/etc/*   $MNTPOINT/etc/
198   [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
199   [ -d /etc/debootstrap/usr   ] && cp -a /etc/debootstrap/usr/*   $MNTPOINT/usr/
200   [ -d /etc/debootstrap/var   ] && cp -a /etc/debootstrap/var/*   $MNTPOINT/var/
201 eend 0
202 stage preparechroot done
203
204 stage chrootscript
205 einfo "Executing chroot-script now"
206 chroot "$MNTPOINT" /bin/chroot-script
207 eend $?
208 stage chrootscript done
209
210 # einfo "Removing chroot-script"
211 # rm -f  $MNTPOINT/bin/chroot-script
212 # rm -rf $MNTPOINT/etc/debootstrap/
213 # eend $?
214
215 if [ -z "$GRUB" -o -z "$GROOT" ] ; then
216    echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
217 else
218    stage grubinstall
219    einfo "Installing grub on ${GRUB}:"
220    grub-install --root-directory="$MNTPOINT" "(${GRUB})"
221    eend $?
222    stage grubinstall done
223 fi
224
225 if [ -n "$PARTITION" ] ; then
226    einfo "Unmount $MNTPOINT"
227    umount $MNTPOINT
228    eend $?
229 fi
230
231 if [ "$FSCK" = 'yes' ] ; then
232    stage fscktool
233    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
234    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
235    $FSCKTOOL $TARGET
236    eend $?
237    stage fscktool done
238 fi
239
240 einfo "All stages have been executed, to rerun grml-debootstrap please manually remove ${STAGES}."
241 einfo "Finished execution of $0 - enjoy your Debian system." ; eend 0
242
243 ## END OF FILE #################################################################