Improve bailout function
[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 16 13:21:01 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 VERSION='0.8'
14
15 # source core functions {{{
16 . /etc/grml/lsb-functions
17 . /etc/grml/script-functions
18 # }}}
19
20 # make sure we have what we need {{{
21 check4progs debootstrap || exit 1
22 check4root || exit 1
23 # }}}
24
25 # cmdline handling {{{
26 case $* in
27    -h*|--h*)
28      einfo "$0 - wrapper around debootstrap for installing plain Debian via grml"
29      einfo "Adjust /etc/debootstrap/config and invoke $0 afterwards."
30      eend 0
31      exit 0
32    ;;
33    -v|--v*)
34      einfo "$0 version $VERSION"
35      einfo "Send bug reports to Michael Prokop <mika@grml.org>."
36      eend 0
37      exit 0
38    ;;
39 esac
40 # }}}
41
42 # without config file it won't work {{{
43 if [ -r /etc/debootstrap/config ] ; then
44    . /etc/debootstrap/config
45 else
46    eerror "/etc/debootstrap/config could not be read, exiting." ; eend 1
47    exit 1
48 fi
49 # }}}
50
51 # set/check variables {{{
52
53 # inside the chroot system the locales might not be available, so use minimum:
54 export LANG=C
55 export LC_ALL=C
56
57 if [ -z "$STAGES" ] ; then
58    STAGES='/etc/debootstrap/stages'
59    [ -d "$STAGES" ] || mkdir -p "$STAGES"
60 fi
61
62 if [ -r $STAGES/grml-debootstrap ] ; then
63    if grep -q done $STAGES/grml-debootstrap ; then
64       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
65       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
66    fi
67 fi
68
69 PARTITION=''
70 DIRECTORY=''
71
72 case $TARGET in
73   /dev/*)
74     PARTITION=1
75     ;;
76   *)
77     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
78     DIRECTORY=1
79     MNTPOINT="$TARGET"
80     MKFS=''
81     TUNE2FS=''
82     FSCK=''
83     GRUB=''
84     GROOT=''
85     ;;
86 esac
87
88 if [ -n "$ARCH" ] ; then
89    ARCHCMD="--arch $ARCH"
90    ARCHINFO=" (${ARCH})"
91 else
92    ARCH="$(dpkg --print-architecture)"
93    ARCHCMD="--arch $ARCH"
94    ARCHINFO=" (${ARCH})"
95 fi
96
97 # provide variables to chroot system
98 echo "
99 ARCH=$ARCH
100 " > /etc/debootstrap/variables
101
102 # make sure at least $TARGET is set [the partition for the new system]
103 if [ -z "$TARGET" ] ; then
104    eerror "Please adjust /etc/debootstrap/config before running ${0}" ; eend 1
105    exit 1
106 fi
107 # }}}
108
109 # helper functions {{{
110 # we want to exit smoothly and clean:
111 bailout(){
112   # make sure $TARGET is not mounted when exiting grml-debootstrap
113   if [ -n "$MNTPOINT" ] ; then
114      if grep -q $MNTPOINT /proc/mounts ; then
115         # make sure nothing is left inside chroot so we can unmount it
116         [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
117         [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
118         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys
119         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc
120         echo "Unmounting $MNTPOINT" ; umount "$MNTPOINT"
121      fi
122   fi
123   [ -n "$1" ] && EXIT="$1" || EXIT="1"
124   [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
125   exit "$EXIT"
126 }
127 trap bailout 1 2 3 15
128
129 # we want to execute all the functions only once, simple check for it:
130 stage() {
131   if [ -n "$2" ] ; then
132      echo "$2" > "$STAGES/$1"
133      return 0
134   elif grep -q done "$STAGES/$1" 2>/dev/null ; then
135      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
136      return 1
137   fi
138 }
139 # }}}
140
141 # user should recheck his configuration {{{
142 einfo "$0 - Please recheck configuration before execution:"
143 echo "
144    Target:           $TARGET"
145    case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount-point:      $MNTPOINT" ;; esac
146    [ -n "$GRUB" ] && echo "   Install grub to:  $GROOT / $GRUB"
147    case "$MNTPOINT" in "$TARGET") ;; *) echo "  Important! Continuing will delete all data from ${TARGET}!" ;; esac
148    echo
149 einfon "Is this ok for you? [y/N] "
150
151 read a
152 if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
153    eerror "Exiting as requested." ; eend 1
154    exit 1
155 fi
156 # }}}
157
158 # create filesystem {{{
159 mkfs() {
160   if [ -n "$MKFS" ] ; then
161      einfo "Running $MKFS on $TARGET"
162      $MKFS $TARGET
163      eend $?
164   fi
165 }
166 # }}}
167
168 # modify filesystem settings {{{
169 tunefs() {
170   if [ -n "$TUNE2FS" ] ; then
171      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
172      $TUNE2FS $TARGET
173      eend $?
174   fi
175 }
176 # }}}
177
178 # mount the new partition or if it's a directory do nothing at all {{{
179 mount_target() {
180   if [ -n "$DIRECTORY" ] ; then
181      einfo "Running grml-debootstrap on a directory, nothing to mount."
182   else
183      if grep -q $TARGET /proc/mounts ; then
184         eerror "$TARGET already mounted, exiting."
185      else
186        [ -n "$MNTPOINT" ] || MNTPOINT='/mnt/test'
187        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
188        einfo "Mounting $TARGET to $MNTPOINT"
189        mount -o rw,suid,dev $TARGET $MNTPOINT
190        eend $?
191      fi
192   fi
193 }
194 # }}}
195
196 # install main chroot {{{
197 debootstrap_system() {
198   einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using mirror $MIRROR"
199   $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR
200   eend $?
201 }
202 # }}}
203
204 # prepare chroot via chroot-script {{{
205 preparechroot() {
206   einfo "Preparing chroot system"
207   cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
208   chmod 755 $MNTPOINT/bin/chroot-script
209   mkdir $MNTPOINT/etc/debootstrap/
210
211   # make sure we have our files for later use via chroot-script
212   cp /etc/debootstrap/config    $MNTPOINT/etc/debootstrap/
213   cp /etc/debootstrap/packages  $MNTPOINT/etc/debootstrap/packages
214   cp /etc/debootstrap/variables $MNTPOINT/etc/debootstrap/variables
215
216   # make sure we can access network [relevant for cdebootstrap]
217   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
218
219   # setup default locales
220   [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen  $MNTPOINT/etc/locale.gen
221
222   # copy any existing existing files to chroot
223   [ -d /etc/debootstrap/boot  ] && cp -a /etc/debootstrap/boot/*  $MNTPOINT/boot/
224   [ -d /etc/debootstrap/etc   ] && cp -a /etc/debootstrap/etc/*   $MNTPOINT/etc/
225   [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
226   [ -d /etc/debootstrap/usr   ] && cp -a /etc/debootstrap/usr/*   $MNTPOINT/usr/
227   [ -d /etc/debootstrap/var   ] && cp -a /etc/debootstrap/var/*   $MNTPOINT/var/
228   eend 0
229 }
230 # }}}
231
232 # execute chroot-script {{{
233 chrootscript() {
234   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
235      mount_target
236   fi
237   if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
238      einfo "Executing chroot-script now"
239      chroot "$MNTPOINT" /bin/chroot-script
240      eend $?
241   else
242      eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
243      eend 1
244   fi
245 }
246 # }}}
247
248 # install booloader grub {{{
249 grub_install() {
250   if [ -z "$GRUB" -o -z "$GROOT" ] ; then
251      echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
252   else
253      einfo "Installing grub on ${GRUB}:"
254      grub-install --root-directory="$MNTPOINT" "(${GRUB})"
255      eend $?
256   fi
257 }
258 # }}}
259
260 # unmount $MNTPOINRT {{{
261 umount_chroot() {
262   if [ -n "$PARTITION" ] ; then
263      einfo "Unmount $MNTPOINT"
264      umount $MNTPOINT
265      eend $?
266   fi
267 }
268 # }}}
269
270 # execute filesystem check {{{
271 fscktool() {
272   if [ "$FSCK" = 'yes' ] ; then
273      [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
274      einfo "Checking filesystem on $TARGET using $FSCKTOOL"
275      $FSCKTOOL $TARGET
276      eend $?
277   fi
278 }
279 # }}}
280
281 # now execute all the functions {{{
282 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
283          chrootscript grub_install umount_chroot fscktool ; do
284     if stage $i ; then
285        $i && stage $i done || bailout 2 "i"
286     fi
287 done
288 # }}}
289
290 # stages {{{
291   echo done > $STAGES/grml-debootstrap
292 # }}}
293
294   einfo "Finished execution of $0 - enjoy your Debian system." ; eend 0
295
296 ## END OF FILE #################################################################
297 # vim: ai tw=100 expandtab foldmethod=marker