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