1229eab38ab74408a845aa33294efc4a20fa06a7
[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 ################################################################################
8 # http://www.debian.org/releases/stable/i386/index.html.en
9
10 set -e # exit on any error
11
12 # variables {{{
13 PN="$(basename $0)"
14 VERSION='0.18'
15 MNTPOINT="/mnt/debootstrap.$$"
16
17 # inside the chroot system locales might not be available, so use minimum:
18 export LANG=C
19 export LC_ALL=C
20
21 # make sure interactive mode is only executed when
22 # using an empty configuration file or option --interactive
23 INTERACTIVE=''
24 # }}}
25
26 # source core functions {{{
27 . /etc/grml/lsb-functions
28 . /etc/grml/script-functions
29 # }}}
30
31 # help text {{{
32 usage() {
33   echo "$PN - wrapper around debootstrap for installing Debian
34
35 Usage: $PN [options]
36
37   -h|--help                   Print this usage information and exit.
38   -v|--version                Show summary of options and exit.
39
40   -c|--config <configfile>    Use specified configuration file,
41                               defaults to /etc/debootstrap/config
42   -i|--iso <mnt>              Mountpoint where a Debian ISO is mounted to,
43                               for use instead of fetching packages from a mirror
44   -m|--mirror <URL>           Mirror which should be used for apt-get/aptitude.
45   -p|--mntpoint <mnt>         Mountpoint used for mounting the target system.
46   -r|--release <release>      Release of new Debian system (default: stable)
47   -t|--target <target>        Target partition (/dev/...) or directory.
48
49   --boot_append <appendline>  Add specified appendline to kernel whilst booting
50   --groot <device>            Root device for usage in grub, corresponds with
51                               \$TARGET in grub syntax, like hd0,0 for /dev/sda1
52   --grub <device>             Target for grub installation. Use grub syntax
53                               for specifying, like hd0 for /dev/sda
54   --interactive               Use interactive mode (frontend)
55   --password <pwd>            Use specified password as password for user root.
56
57 Send bugreports to the grml-team: bugs@grml.org || http://grml.org/bugs/
58 "
59 }
60
61 if [ "$1" = '-h' ] || [ "$1" = '-help' ] ; then
62    usage
63    echo 'Please notice that this script requires root permissions!'
64    exit 0
65 fi
66 # }}}
67
68 # make sure we have what we need {{{
69 check4progs debootstrap dialog || exit 1
70 check4root || exit 1
71 # }}}
72
73 # source configuration file {{{
74 if [ -r /etc/debootstrap/config ] ; then
75    if [ -n "$CONFIGFILE" ] ; then
76       if ! . "$CONFIGFILE" ; then
77          eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
78       fi
79    else
80       . /etc/debootstrap/config
81    fi
82 fi
83 # }}}
84
85 # cmdline handling {{{
86
87 while [ "$#" -gt "0" ] ; do
88     case $1 in
89         -c|--config)
90             shift
91             CONFIGFILE=$1
92             ;;
93         --grub)
94             shift
95             GRUB=$1
96             ;;
97         --groot)
98             shift
99             GROOT=$1
100             ;;
101         -h|--help)
102             usage ; eend 0
103             eend 0
104             exit 0
105             ;;
106         --interactive)
107             INTERACTIVE=1
108             ;;
109         -i|--iso)
110             shift
111             [ -n "$MIRROR" ] && unset MIRROR
112             ISO=$1
113             ;;
114         -m|--mirror)
115             shift
116             MIRROR=$1
117             CHROOTMIRROR=$1
118             ;;
119         -p|--mntpoint)
120             shift
121             MNTPOINT=$1
122             ;;
123         --password)
124             shift
125             ROOTPASSWORD=$1
126             ;;
127         -r|--release)
128             shift
129             RELEASE=$1
130             ;;
131         -t|--target)
132             shift
133             TARGET=$1
134             ;;
135         -v|--version)
136             einfo "$PN - version $VERSION"
137             einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
138             eend 0
139             exit 0
140             ;;
141         *)
142             eerror "Syntax error."
143             usage ; eend 1
144             exit 1
145             ;;
146     esac
147     shift
148 done
149 # }}}
150
151 # welcome screen {{{
152 welcome_dialog()
153 {
154    dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
155 Do you want to continue installing Debian using this frontend?" 0 0
156 }
157 # }}}
158
159 # ask for target {{{
160 prompt_for_target()
161 {
162   AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
163                sed 's/*//' | \
164                grep -v 'Extended$' | \
165                gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
166
167   [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
168   PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
169                        echo "$i $(vol_id --type $i 2>/dev/null || echo [no_filesystem_yet])"
170                    done)
171
172   TARGET=$(dialog --title "$PN" --single-quoted --stdout \
173          --menu "Please select the target partition:" 0 0 0 \
174          $PARTITION_LIST)
175 }
176 # }}}
177
178 # ask for bootmanager {{{
179 prompt_for_bootmanager()
180 {
181   ADDITIONAL_PARAMS=""
182   for device in sda hda; do
183     if [ /dev/$device != ${TARGET%[0-9]} ]; then
184       grep -q $device /proc/partitions && \
185       ADDITIONAL_PARAMS=:$device:"install bootmanager grub into MBR of /dev/${device}"
186     fi
187   done
188   ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
189
190   OIFS="$IFS"; IFS=:
191
192   if echo $TARGET | grep -q "*md*" ; then
193      MBRPART=$TARGET
194   else
195      MBRPART="${TARGET%[0-9]}"
196   fi
197
198   GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
199           --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
200             mbr       "install bootmanager into MBR of $MBRPART" \
201             partition "install bootmanager into partition $TARGET" \
202             nowhere   "do not install bootmanager at all" \
203           ${ADDITIONAL_PARAMS})
204   [ $? -eq 0 ] || bailout 3
205   IFS="$OIFS"
206
207   case "$GETMBR" in
208     mbr)
209       # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
210       if echo $TARGET | grep -q "*md*" ; then
211          BOOT_PARTITION="${TARGET}"
212       else
213          BOOT_PARTITION="${TARGET%[0-9]}"
214       fi
215       ;;
216     partition)
217       BOOT_PARTITION="$TARGET"
218       ;;
219     hda)
220       BOOT_PARTITION="/dev/hda"
221       ;;
222     sda)
223       BOOT_PARTITION="/dev/sda"
224       ;;
225     nowhere)
226       BOOT_PARTITION=''
227       ;;
228   esac
229 }
230 # }}}
231
232 # ask for Debian mirror {{{
233 prompt_for_mirror()
234 {
235   MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
236             "Please enter Debian mirror you would like to use for installing packages." \
237             0 0 http://ftp.de.debian.org/debian)"
238 }
239 # }}}
240
241 # get grub's syntax for /dev/ice {{{
242 # usage example: 'grubdevice /dev/sda2' returns 'hd0,1'
243 grubdevice() {
244   if [ -z "$1" ] ; then
245      echo "Usage: grubdevice <device>">&2
246      return 1
247   fi
248
249   device="$1"
250   device_map=/boot/grub/device.map
251
252   # create device.map
253   if ! [ -f "$device_map" ] ; then
254      echo 'quit' | grub --device-map="$device_map" 1>/dev/null 2>&1
255   fi
256
257   # based on code from d-i's trunk/packages/arch/i386/grub-installer/grub-installer:
258   tmp_disk=`echo "$device" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
259                     -e 's%\(fd[0-9]*\)$%\1%' \
260                     -e 's%/part[0-9]*$%/disc%' \
261                     -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
262   tmp_part=`echo "$device" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
263                     -e 's%.*/fd[0-9]*$%%' \
264                     -e 's%.*/floppy/[0-9]*$%%' \
265                     -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
266                     -e 's%.*c[0-7]d[0-9]*p*%%'`
267   tmp_drive=$(grep -v '^#' $device_map | grep "$tmp_disk *$" | sed 's%.*\([hf]d[0-9][a-g0-9,]*\).*%\1%')
268
269   case $1 in
270      /dev/[sh]d[a-z]) # we expect something like 'hd0'
271         echo "$tmp_drive"
272         ;;
273       *) # we expect something like 'hd0,0'
274         echo "$tmp_drive" | sed "s%$%,`expr $tmp_part - 1`%" # FIXME => md0
275         ;;
276   esac
277 }
278 # }}}
279
280 # software raid setup {{{
281 config_swraid_setup()
282 {
283 TMPFILE=$(mktemp)
284 RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
285                    --menu "Which RAID level do you want to use?" 0 0 0 \
286                      raid1 "Software RAID level 1" \
287                      raid5 "Software RAID level 5")
288 [ $? -eq 0 ] || bailout 20
289
290 MD_LIST=$(for i in $(seq 0 9) ; do
291             awk '{print $4}' /proc/partitions | grep md$i /proc/partitions  || \
292             echo "/dev/md$i /dev/md$i"
293           done)
294
295 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
296 --menu "Which device do you want to use for ${RAIDLEVEL}?
297
298 Notice: activated devices will not be listed for security reasons. Anyway, please make sure the selected device is not in use already!" 0 0 0 \
299 $MD_LIST)
300 [ $? -eq 0 ] || bailout 20
301
302 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
303              sed 's/*//' | \
304              grep -v 'Extended$' | \
305              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
306 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
307 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do echo "$i $i off" ; done)
308
309 dialog --title "$PN" \
310        --single-quoted \
311        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
312        $PARTITION_LIST 2>$TMPFILE
313 RETVAL=$?
314 SELECTED_PARTITIONS="$(cat $TMPFILE)"
315
316 NUM_PARTITIONS=0
317 for i in $(cat $TMPFILE) ; do
318   NUM_PARTITIONS="$((${NUM_PARTITIONS}+1))"
319 done
320
321 echo "
322 Debug:
323
324 mdadm --create --verbose $TARGET --level=$RAIDLEVEL
325       --raid-devices=$NUM_PARTITIONS $SELECTED_PARTITIONS
326 "
327 }
328
329 prompt_for_swraid()
330 {
331 if dialog --stdout --title "$PN" \
332           --defaultno --yesno "Do you want to configure Software RAID?
333
334 Please notice that currently only RAID levels 1 and 5 are supported by ${PN}. Configuration will take place using mdadm." 0 0 ; then
335   config_swraid_setup
336 fi
337 }
338 # }}}
339
340 # interactive mode {{{
341 interactive_mode()
342 {
343   welcome_dialog
344
345   prompt_for_swraid
346
347   # do not prompt for partition dialog if swraid has been configured already
348   [ -n "$TARGET" ] || prompt_for_target
349
350   prompt_for_bootmanager
351
352   [ -n "$BOOT_PARTITION" ] && GRUB="$(grubdevice $BOOT_PARTITION)"
353   [ -n "$TARGET" ]         && GROOT="$(grubdevice $TARGET)"
354
355   prompt_for_mirror
356 }
357
358 # run interactive mode if we didn't get the according configuration yet
359 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
360    interactive_mode
361 fi
362 # }}}
363
364 # finally make sure at least $TARGET is set [the partition for the new system] {{{
365 if [ -n "$TARGET" ] ; then
366    SHORT_TARGET="${TARGET##*/}"
367 else
368    eerror "Please adjust /etc/debootstrap/config or..."
369    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
370    exit 1
371 fi
372 # }}}
373
374 # stages setup {{{
375 if [ -z "$STAGES" ] ; then
376    STAGES="/etc/debootstrap/stages_${SHORT_TARGET}"
377    [ -d "$STAGES" ] || mkdir -p "$STAGES"
378 fi
379
380 if [ -r "$STAGES"/grml-debootstrap ] ; then
381    if grep -q done $STAGES/grml-debootstrap ; then
382       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
383       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
384    fi
385 fi
386 # }}}
387
388 # partition handling {{{
389 PARTITION=''
390 DIRECTORY=''
391
392 case $TARGET in
393   /dev/*)
394     PARTITION=1
395     ;;
396   *)
397     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
398     DIRECTORY=1
399     MNTPOINT="$TARGET"
400     MKFS=''
401     TUNE2FS=''
402     FSCK=''
403     GRUB=''
404     GROOT=''
405     ;;
406 esac
407 # }}}
408
409 # architecture setup {{{
410 if [ -n "$ARCH" ] ; then
411    ARCHCMD="--arch $ARCH"
412    ARCHINFO=" (${ARCH})"
413 else
414    ARCH="$(dpkg --print-architecture)"
415    ARCHCMD="--arch $ARCH"
416    ARCHINFO=" (${ARCH})"
417 fi
418 # }}}
419
420 # make sure we have the right syntax when using an iso image {{{
421 if [ -n "$ISO" ] ; then
422    case $ISO in
423       file*) # do nothing
424       ;;
425       *)
426       ISO=file:$1
427       ;;
428    esac
429 fi
430 ISODIR=${ISO##file:}
431 ISODIR=${ISODIR%%/}
432 # }}}
433
434 # provide variables to chroot system {{{
435 CHROOT_VARIABLES="/etc/debootstrap/variables_${SHORT_TARGET}"
436 touch $CHROOT_VARIABLES
437 chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
438 [ -n "$ARCH" ]   && echo "ARCH=$ARCH"     >  $CHROOT_VARIABLES
439 [ -n "$GRUB" ]   && echo "GRUB=$GRUB"     >> $CHROOT_VARIABLES
440 [ -n "$GROOT" ]  && echo "GROOT=$GROOT"   >> $CHROOT_VARIABLES
441 [ -n "$TARGET" ] && echo "TARGET=$TARGET" >> $CHROOT_VARIABLES
442 [ -n "$ISO" ]    && echo "ISO=$ISO"       >> $CHROOT_VARIABLES
443 [ -n "$ISODIR" ] && echo "ISODIR=$ISO"    >> $CHROOT_VARIABLES
444 [ -n "$MIRROR" ] && echo "MIRROR=$MIRROR" >> $CHROOT_VARIABLES
445 [ -n "$MIRROR" ] && echo "CHROOTMIRROR=$MIRROR" >> $CHROOT_VARIABLES
446 [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
447 # }}}
448
449 # helper functions {{{
450 # we want to exit smoothly and clean:
451 bailout(){
452   # make sure $TARGET is not mounted when exiting grml-debootstrap
453   if [ -n "$MNTPOINT" ] ; then
454      if grep -q $MNTPOINT /proc/mounts ; then
455         # make sure nothing is left inside chroot so we can unmount it
456         [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
457         [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
458         # ugly, but make sure we really don't leav anything
459         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys  1>/dev/null 2>&1
460         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a    1>/dev/null 2>&1
461         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
462         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
463         [ -d "$MNTPOINT/$ISODIR" ]    && umount "$MNTPOINT/$ISODIR" 1>/dev/null 2>&1
464         einfo "Unmounting $MNTPOINT"   ; umount "$MNTPOINT" ; eend $?
465
466         # remove directory only if we used the default with process id inside the name
467         if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
468            einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
469         fi
470      fi
471   fi
472
473   [ -n "$1" ] && EXIT="$1" || EXIT="1"
474   [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
475
476   exit "$EXIT"
477 }
478 trap bailout HUP INT QUIT TERM
479
480 # we want to execute all the functions only once, simple check for it:
481 stage() {
482   if [ -n "$2" ] ; then
483      echo "$2" > "${STAGES}/${1}"
484      return 0
485   elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
486      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
487      eindent
488        ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
489      eoutdent
490      return 1
491   fi
492 }
493 # }}}
494
495 # user should recheck his configuration {{{
496 # support full automatic installation:
497 checkforrun() {
498    dialog --timeout 10 --title "$PN" \
499           --yesno "Do you want to stop at this stage?
500
501 Notice: you are running ${PN} in non-interactive mode.
502 ${PN} will install Debian ${RELEASE} on ${TARGET}.
503 Last chance to quit. Timeout of 10 seconds running....
504
505 Do you want to stop now?" 0 0 2>/dev/null
506 }
507
508 if [ -n "$AUTOINSTALL" ] ; then
509    if checkforrun ; then
510       eerror "Exiting as requested" ; eend 0
511       exit 1
512    fi
513 else # if not running automatic installation display configuration and prompt for execution:
514    einfo "$PN - Please recheck configuration before execution:"
515    echo
516    echo "   Target:          $TARGET"
517       case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
518       [ -n "$GRUB" ]    && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
519       [ -n "$RELEASE" ] && echo "   Using release:   $RELEASE"
520       [ -n "$MIRROR" ]  && echo "   Using mirror:    $MIRROR"
521       [ -n "$ISO" ]     && echo "   Using iso:       $ISO"
522       case "$MNTPOINT" in "$TARGET") ;; *) echo "   Important! Continuing will delete all data from ${TARGET}!" ;; esac
523       echo
524    einfon "Is this ok for you? [y/N] "
525    read a
526    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
527       eerror "Exiting as requested." ; eend 1
528       exit 1
529    fi
530 fi
531 # }}}
532
533 # create filesystem {{{
534 mkfs() {
535   if [ -n "$MKFS" ] ; then
536      einfo "Running $MKFS on $TARGET"
537      $MKFS $TARGET
538      eend $?
539   fi
540 }
541 # }}}
542
543 # modify filesystem settings {{{
544 tunefs() {
545   if [ -n "$TUNE2FS" ] ; then
546      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
547      $TUNE2FS $TARGET
548      eend $?
549   fi
550 }
551 # }}}
552
553 # mount the new partition or if it's a directory do nothing at all {{{
554 mount_target() {
555   if [ -n "$DIRECTORY" ] ; then
556      einfo "Running grml-debootstrap on a directory, nothing to mount."
557   else
558      if grep -q $TARGET /proc/mounts ; then
559         eerror "$TARGET already mounted, exiting."
560      else
561        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
562        einfo "Mounting $TARGET to $MNTPOINT"
563        mount -o rw,suid,dev $TARGET $MNTPOINT
564        eend $?
565      fi
566   fi
567   if [ -n "$ISODIR" ] ; then
568      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
569      mkdir -p "$MNTPOINT/$ISODIR"
570      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
571      eend $?
572   fi
573 }
574 # }}}
575
576 # install main chroot {{{
577 debootstrap_system() {
578   if ! grep -q $MNTPOINT /proc/mounts ; then
579      mount_target
580   fi
581   if grep -q $MNTPOINT /proc/mounts ; then
582      einfo "Running $DEBOOTSTRAP for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
583      [ -n "$MIRROR" ] && $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $MIRROR || \
584      $DEBOOTSTRAP $ARCHCMD $RELEASE $MNTPOINT $ISO
585      eend $?
586   else
587      eerror "Error: $MNTPOINT not mounted, can not continue."
588      eend 1
589   fi
590 }
591 # }}}
592
593 # prepare chroot via chroot-script {{{
594 preparechroot() {
595   einfo "Preparing chroot system"
596   cp $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
597   chmod 755 $MNTPOINT/bin/chroot-script
598   mkdir $MNTPOINT/etc/debootstrap/
599
600   # make sure we have our files for later use via chroot-script
601   cp /etc/debootstrap/config    $MNTPOINT/etc/debootstrap/
602   cp /etc/debootstrap/packages  $MNTPOINT/etc/debootstrap/packages
603   cp $CHROOT_VARIABLES          $MNTPOINT/etc/debootstrap/variables # do NOT use $CHROOT_VARIABLES inside chroot!
604
605   cp -a /etc/debootstrap/extrapackages/ $MNTPOINT/etc/debootstrap/
606
607   # make sure we can access network [relevant for cdebootstrap]
608   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp /etc/resolv.conf $MNTPOINT/etc/resolv.conf
609
610   # setup default locales
611   [ -n "$LOCALES" ] && cp /etc/debootstrap/locale.gen  $MNTPOINT/etc/locale.gen
612
613   # copy any existing existing files to chroot
614   [ -d /etc/debootstrap/boot  ] && cp -a /etc/debootstrap/boot/*  $MNTPOINT/boot/
615   [ -d /etc/debootstrap/etc   ] && cp -a /etc/debootstrap/etc/*   $MNTPOINT/etc/
616   [ -d /etc/debootstrap/share ] && cp -a /etc/debootstrap/share/* $MNTPOINT/share/
617   [ -d /etc/debootstrap/usr   ] && cp -a /etc/debootstrap/usr/*   $MNTPOINT/usr/
618   [ -d /etc/debootstrap/var   ] && cp -a /etc/debootstrap/var/*   $MNTPOINT/var/
619
620   # copy local network setup to chroot
621   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
622      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
623      cp /etc/network/interfaces $MNTPOINT/etc/network/interfaces
624   fi
625
626   eend 0
627 }
628 # }}}
629
630 # execute chroot-script {{{
631 chrootscript() {
632   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
633      mount_target
634   fi
635   if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
636      einfo "Executing chroot-script now"
637      chroot "$MNTPOINT" /bin/chroot-script
638      eend $?
639   else
640      eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
641      eend 1
642   fi
643 }
644 # }}}
645
646 # install booloader grub {{{
647 grub_install() {
648   if [ -z "$GRUB" -o -z "$GROOT" ] ; then
649      echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
650   else
651      einfo "Installing grub on ${GRUB}:"
652      [ -x /usr/sbin/grub-install ] && GRUBINSTALL=/usr/sbin/grub-install || GRUBINSTALL=/sbin/grub-install
653      $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
654      eend $?
655   fi
656 }
657 # }}}
658
659 # unmount $MNTPOINRT {{{
660 umount_chroot() {
661   if [ -n "$ISODIR" ] ; then
662      if grep -q "$ISODIR" /proc/mounts ; then
663         einfo "Unmount $MNTPOINT/$ISODIR"
664         umount "$MNTPOINT/$ISODIR"
665         eend $?
666      fi
667   fi
668   if grep -q "$MNTPOINT" /proc/mounts ; then
669      if [ -n "$PARTITION" ] ; then
670         einfo "Unmount $MNTPOINT"
671         umount $MNTPOINT
672         eend $?
673      fi
674   fi
675 }
676 # }}}
677
678 # execute filesystem check {{{
679 fscktool() {
680   if [ "$FSCK" = 'yes' ] ; then
681      [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
682      einfo "Checking filesystem on $TARGET using $FSCKTOOL"
683      $FSCKTOOL $TARGET
684      eend $?
685   fi
686 }
687 # }}}
688
689 # now execute all the functions {{{
690 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
691          chrootscript grub_install umount_chroot fscktool ; do
692     if stage "${i}" ; then
693        $i && stage "${i}" done || bailout 2 "i"
694     fi
695 done
696 # }}}
697
698 # stages {{{
699   echo done > $STAGES/grml-debootstrap
700 # }}}
701
702 # end dialog of autoinstallation {{{
703 if [ -n "$AUTOINSTALL" ] ; then
704    dialog --title "$PN" --msgbox \
705           "Finished execution of ${PN}. Enjoy your Debian system." 0 0
706 else
707    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
708 fi
709 # }}}
710
711 ## END OF FILE #################################################################
712 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3