01c7665276225e0e8e2d7cc902522543bee24944
[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 prog_real=`readlink -f -- $0`
15 VERSION='0.23'
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 Bootstrap options:
39
40   -m, --mirror=URL       Mirror which should be used for apt-get/aptitude.
41   -i, --iso=mnt          Mountpoint where a Debian ISO is mounted to, for use
42                            instead of fetching packages from a mirror.
43   -r, --release=name     Release of new Debian system (default: stable).
44   -t, --target=target    Target partition (/dev/...) or directory.
45   -p, --mntpoint=mnt     Mountpoint used for mounting the target system.
46       --debopt=params    Extra parameters passed to the debootstrap.
47       --interactive      Use interactive mode (frontend).
48       --nodebootstrap    Skip debootstrap, only do configuration to the target.
49
50 Configuration options:
51
52
53   -c, --config=file      Use specified configuration file, defaults to
54                            /etc/debootstrap/config
55   -d, --confdir=path     Place of config files for debootstrap, defaults to
56                            /etc/debootstrap
57       --packages[=f]     Install packages defined in <confdir>/packages. Option
58                            arg: alternative package list file.
59       --debconf[=f]      Pre-seed packages using <confdir>/debconf-selections.
60                            Option arg: alternative pre-seed db file.
61       --keep_src_list    Do not overwrite user provided apt sources.list.
62       --hostname=name    Hostname of Debian system.
63       --password=pwd     Use specified password as password for user root.
64
65       --bootappend=line  Add specified appendline to kernel whilst booting.
66       --groot=device     Root device for usage in grub, corresponds with
67                            $TARGET in grub syntax, like hd0,0 for /dev/sda1.
68       --grub=device      Target for grub installation. Use grub syntax for
69                            specifying, like hd0 for /dev/sda.
70
71 Other options:
72
73   -v, --verbose          Increase verbosity.
74   -h, --help             Print this usage information and exit.
75   -V, --version          Show summary of options and exit.
76
77 Send bugreports to the grml-team: bugs@grml.org || http://grml.org/bugs/
78 "
79 }
80
81 if [ "$1" = '-h' ] || [ "$1" = '-help' ] ; then
82    usage
83    echo 'Please notice that this script requires root permissions!'
84    exit 0
85 fi
86 # }}}
87
88 # make sure we have what we need {{{
89 check4progs debootstrap dialog || exit 1
90 check4root || exit 1
91 # }}}
92
93 # source configuration file {{{
94 if [ -r /etc/debootstrap/config ] ; then
95    if [ -n "$CONFIGFILE" ] ; then
96       einfo "Using config file $CONFIGFILE."
97       if ! . "$CONFIGFILE" ; then
98          eerror "Error reading config file $CONFIGFILE" ; eend 1 ; exit 1
99       fi
100    else
101       . /etc/debootstrap/config
102    fi
103 fi
104 # }}}
105
106 # cmdline handling {{{
107 # source external command line parameter-processing script
108 . $prog_real.clp
109
110 # == business-logic of command line parameter-processing
111
112 # source configuration file in <confdir> if supplied. {{{
113 [ "$_opt_confdir" ] && {
114   CONFFILES=$_opt_confdir
115   einfo "Using config files under $CONFFILES/."
116   if ! [ -r "$CONFFILES/config" ] ; then
117     eerror "Error: config file $CONFFILES/config not found."; eend 1; exit 1
118   fi
119   if ! . "$CONFFILES/config" ; then
120     eerror "Error reading config file $CONFFILES/config" ; eend 1 ; exit 1
121   fi
122 }
123 # }}}
124
125 [ "$_opt_mirror" ]              && MIRROR=$_opt_mirror
126 [ "$_opt_iso" ]                 && ISO=$_opt_iso
127 [ "$_opt_release" ]             && RELEASE=$_opt_release
128 [ "$_opt_target" ]              && TARGET=$_opt_target
129 [ "$_opt_mntpoint" ]            && MNTPOINT=$_opt_mntpoint
130 [ "$_opt_debopt" ]              && DEBOOTSTRAP_OPT=$_opt_debopt
131 [ "$_opt_interactive" ]         && INTERACTIVE=1
132 [ "$_opt_config" ]              && CONFIGFILE=$_opt_config
133 [ "$_opt_packages_set" ]        && PACKAGES='yes'
134 [ "$_opt_debconf_set" ]         && DEBCONF='yes'
135 [ "$_opt_keep_src_list" ]       && KEEP_SRC_LIST='yes'
136 [ "$_opt_hostname" ]            && HOSTNAME=$_opt_hostname
137 [ "$_opt_password" ]            && ROOTPASSWORD=$_opt_password
138 [ "$_opt_bootappend" ]          && BOOT_APPEND=$_opt_bootappend
139 [ "$_opt_groot" ]               && GROOT=$_opt_groot
140 [ "$_opt_grub" ]                && GRUB=$_opt_grub
141 [ "$_opt_verbose" ]             && VERBOSE="-v"
142
143 [ "$_opt_help" ] && {
144   usage ; eend 0
145   eend 0
146   exit 0
147 }
148
149 [ "$_opt_version" ] && {
150   einfo "$PN - version $VERSION"
151   einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
152   eend 0
153   exit 0
154 }
155 # }}}
156
157 # welcome screen {{{
158 welcome_dialog()
159 {
160    dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
161 Do you want to continue installing Debian using this frontend?" 0 0
162 }
163 # }}}
164
165 # ask for target {{{
166 prompt_for_target()
167 {
168   AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
169                sed 's/*//' | \
170                grep -v 'Extended$' | \
171                gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
172
173   if [ -z "$AVAILABLE_PARTITIONS" ] ; then
174      dialog --title "$PN" --trim \
175      --msgbox "Sorry, no partitions found. Please configure your
176      harddisks (see /proc/partitions) using a tool like fdisk,
177      cfdisk, gpart, gparted,..." 0 0
178      exit 0
179   fi
180
181   PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
182                        echo "$i $(vol_id --type $i 2>/dev/null || echo [no_filesystem_yet])"
183                    done)
184
185   TARGET=$(dialog --title "$PN" --single-quoted --stdout \
186          --menu "Please select the target partition:" 0 0 0 \
187          $PARTITION_LIST)
188 }
189 # }}}
190
191 # ask for bootmanager {{{
192 prompt_for_bootmanager()
193 {
194   ADDITIONAL_PARAMS=""
195   for device in sda hda; do
196     if [ /dev/$device != ${TARGET%[0-9]} ]; then
197       grep -q $device /proc/partitions && \
198       ADDITIONAL_PARAMS=:$device:"install bootmanager grub into MBR of /dev/${device}"
199     fi
200   done
201   ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
202
203   OIFS="$IFS"; IFS=:
204
205   if echo $TARGET | grep -q "/dev/md" ; then
206      MBRPART="all disks of Software RAID $TARGET"
207   else
208      MBRPART="MBR of ${TARGET%[0-9]}"
209   fi
210
211   GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
212           --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
213             mbr       "install bootmanager into $MBRPART" \
214             partition "install bootmanager into partition $TARGET" \
215             nowhere   "do not install bootmanager at all" \
216           ${ADDITIONAL_PARAMS})
217   [ $? -eq 0 ] || bailout 3
218   IFS="$OIFS"
219
220   case "$GETMBR" in
221     mbr)
222       # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
223       if echo $TARGET | grep -q "*md*" ; then
224          BOOT_PARTITION="${TARGET}"
225       else
226          BOOT_PARTITION="${TARGET%[0-9]}"
227       fi
228       ;;
229     partition)
230       BOOT_PARTITION="$TARGET"
231       ;;
232     hda)
233       BOOT_PARTITION="/dev/hda"
234       ;;
235     sda)
236       BOOT_PARTITION="/dev/sda"
237       ;;
238     nowhere)
239       BOOT_PARTITION=''
240       ;;
241   esac
242 }
243 # }}}
244
245 # ask for Debian release {{{
246 prompt_for_release()
247 {
248   RELEASE="$(dialog --stdout --title "${PN}" --default-item etch --menu \
249             "Please enter the Debian release you would like to use for installation:" \
250             0 50 3 \
251             etch   Debian/stable \
252             lenny  Debian/testing \
253             sid    Debian/unstable)"
254 }
255 # }}}
256
257 # ask for hostname {{{
258 prompt_for_hostname()
259 {
260   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
261             "Please enter the hostname you would like to use for installation:" \
262             0 0 grml)"
263 }
264 # }}}
265
266 # ask for password {{{
267 prompt_for_password()
268 {
269      ROOTPW1='PW1'
270      ROOTPW2='PW2'
271      while [ "$ROOTPW1" != "$ROOTPW2" ]; do
272        ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
273        "Please enter the password for the root account:" 10 60)
274        ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
275        "Please enter the password for the root account again for \
276        confirmation:" 10 60)
277
278        if [ "$ROOTPW1" != "$ROOTPW2" ]; then
279          $(dialog --stdout --title "${PN}" --ok-label \
280          "Retry" --msgbox "Passwords do not match!" 10 60)
281        fi
282      done
283      ROOTPASSWORD="$ROOTPW1"
284 }
285 # }}}
286
287 # ask for Debian mirror {{{
288 prompt_for_mirror()
289 {
290   MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
291             "Please enter Debian mirror you would like to use for installing packages." \
292             0 0 http://ftp.de.debian.org/debian)"
293 }
294 # }}}
295
296 # get grub's syntax for /dev/ice {{{
297 # usage example: 'grubdevice /dev/sda2' returns 'hd0,1'
298 grubdevice() {
299   if [ -z "$1" ] ; then
300      echo "Usage: grubdevice <device>">&2
301      return 1
302   fi
303
304   device="$1"
305   device_map=/boot/grub/device.map
306
307   # create device.map
308   if ! [ -f "$device_map" ] ; then
309      echo 'quit' | grub --device-map="$device_map" 1>/dev/null 2>&1
310   fi
311
312   # based on code from d-i's trunk/packages/arch/i386/grub-installer/grub-installer:
313   tmp_disk=`echo "$device" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
314                     -e 's%\(fd[0-9]*\)$%\1%' \
315                     -e 's%/part[0-9]*$%/disc%' \
316                     -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
317   tmp_part=`echo "$device" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
318                     -e 's%.*/fd[0-9]*$%%' \
319                     -e 's%.*/floppy/[0-9]*$%%' \
320                     -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
321                     -e 's%.*c[0-7]d[0-9]*p*%%'`
322   tmp_drive=$(grep -v '^#' $device_map | grep "$tmp_disk *$" | sed 's%.*\([hf]d[0-9][a-g0-9,]*\).*%\1%')
323
324   case $1 in
325      /dev/[sh]d[a-z]) # we expect something like 'hd0'
326         echo "$tmp_drive"
327         ;;
328       *) # we expect something like 'hd0,0'
329         echo "$tmp_drive" | sed "s%$%,`expr $tmp_part - 1`%" # FIXME => md0
330         ;;
331   esac
332 }
333 # }}}
334
335 # software raid setup {{{
336 config_swraid_setup()
337 {
338 TMPFILE=$(mktemp)
339
340 # Currently we support only raid1:
341 RAIDLEVEL='raid1'
342
343 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
344 #                   --menu "Which RAID level do you want to use?" 0 0 0 \
345 #                     raid1 "Software RAID level 1" \
346 #                     raid5 "Software RAID level 5")
347 #[ $? -eq 0 ] || bailout 20
348
349 MD_LIST=$(for i in $(seq 0 9) ; do
350             awk '{print $4}' /proc/partitions | grep -q md$i || \
351             echo "/dev/md$i /dev/md$i"
352           done)
353
354 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
355 --menu "Which device do you want to use for ${RAIDLEVEL}?
356
357 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 \
358 $MD_LIST)
359 [ $? -eq 0 ] || bailout 20
360
361 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
362              sed 's/*//' | \
363              grep -v 'Extended$' | \
364              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
365 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
366 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
367                      echo "$i $(vol_id --type $i 2>/dev/null || echo [no_filesystem_yet]) off"
368                  done)
369
370 dialog --title "$PN" --separate-output \
371        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
372        $PARTITION_LIST 2>$TMPFILE
373 RETVAL=$?
374 SELECTED_PARTITIONS="$(cat $TMPFILE)"
375
376 NUM_PARTITIONS=0
377 for i in $(cat $TMPFILE) ; do
378    NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
379 done
380
381 ERRORFILE=$(mktemp)
382 set +e
383 # TODO: better error handling?
384 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
385       --raid-devices="${NUM_PARTITIONS}" ${SELECTED_PARTITIONS} 1>/dev/null 2>$ERRORFILE
386 RC=$?
387 set -e
388
389 if [ "$RC" = 0 ] ; then
390    dialog --title "$PN" --msgbox \
391    "Creating $TARGET was successful." 0 0
392    rm -f "$TMPFILE" "$ERRORFILE"
393 else
394    dialog --title "$PN" --msgbox \
395    "There was an error setting up $TARGET:
396
397 $(cat $ERRORFILE)
398
399 Exiting." 0 0
400    rm -f "$TMPFILE" "$ERRORFILE"
401    exit 1
402 fi
403
404 }
405
406 prompt_for_swraid()
407 {
408 if dialog --stdout --title "$PN" \
409           --defaultno --yesno "Do you want to configure Software RAID?
410
411 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
412   config_swraid_setup
413 fi
414 }
415 # }}}
416
417 # make sure the user is aware of the used configuration {{{
418 checkconfiguration()
419 {
420 if [ -n "$AUTOINSTALL" ] ; then
421    if checkforrun ; then
422       eerror "Exiting as requested" ; eend 0
423       exit 1
424    fi
425 elif [ -n "$INTERACTIVE" ] ; then
426
427    INFOTEXT="Please recheck configuration before execution:
428    "
429    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
430    Target:          $TARGET"
431    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
432    Install grub:    $GRUB"
433    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
434    Using release:   $RELEASE"
435    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
436    Using hostname   $HOSTNAME"
437    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
438    Using mirror:    $MIRROR"
439
440    INFOTEXT="$INFOTEXT
441
442 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
443
444    dialog --title "$PN" --no-collapse \
445           --yesno "$INFOTEXT" 0 0
446
447 else # if not running automatic installation display configuration and prompt for execution:
448    einfo "$PN - Please recheck configuration before execution:"
449    echo
450    echo "   Target:          $TARGET"
451       case "$MNTPOINT" in "$TARGET") ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
452       [ -n "$GRUB" ]    && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
453       [ -n "$RELEASE" ] && echo "   Using release:   $RELEASE"
454       [ -n "$MIRROR" ]  && echo "   Using mirror:    $MIRROR"
455       [ -n "$ISO" ]     && echo "   Using iso:       $ISO"
456       case "$MNTPOINT" in "$TARGET") ;; *) echo "   Important! Continuing will delete all data from ${TARGET}!" ;; esac
457       echo
458    einfon "Is this ok for you? [y/N] "
459    read a
460    if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
461       eerror "Exiting as requested." ; eend 1
462       exit 1
463    fi
464 fi
465 }
466 # }}}
467
468 # interactive mode {{{
469 interactive_mode()
470 {
471   welcome_dialog
472
473   prompt_for_swraid
474
475   # do not prompt for partition dialog if swraid has been configured already
476   [ -n "$TARGET" ] || prompt_for_target
477
478   prompt_for_bootmanager
479
480   prompt_for_release
481
482   prompt_for_hostname
483
484   prompt_for_password
485
486   # use first disk of sw-raid for grub by default, install grub on
487   # all involved disks later on
488   if echo "$TARGET" | grep -q '/dev/md' ; then
489      if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
490         # use hdX and not hdX,Y for $GRUB
491         TMPDEVICE=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}') # use first disk only
492         GRUB="$(grubdevice ${TMPDEVICE%%[0-9]})" # like: hd0
493         GROOT="$(grubdevice ${TMPDEVICE})"       # like: hd0,0
494         echo "debug: GRUB = $GRUB - GROOT = $GROOT" >/tmp/debug # FIXME
495      fi
496   else
497      [ -n "$BOOT_PARTITION" ] && GRUB="$(grubdevice $BOOT_PARTITION)"
498      [ -n "$TARGET" ]         && GROOT="$(grubdevice $TARGET)"
499   fi
500
501   prompt_for_mirror
502
503   checkconfiguration
504 }
505
506 # run interactive mode if we didn't get the according configuration yet
507 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
508    # only target might be unset, so make sure the INTERACTIVE flag is set as well
509    INTERACTIVE=1
510    interactive_mode
511 fi
512 # }}}
513
514 # finally make sure at least $TARGET is set [the partition for the new system] {{{
515 if [ -n "$TARGET" ] ; then
516    SHORT_TARGET="${TARGET##*/}"
517 else
518    eerror "Please adjust $CONFFILES/config or..."
519    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
520    exit 1
521 fi
522 # }}}
523
524 # stages setup {{{
525 if [ -z "$STAGES" ] ; then
526    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
527    [ -d "$STAGES" ] || mkdir -p "$STAGES"
528 fi
529
530 if [ -r "$STAGES"/grml-debootstrap ] ; then
531    if grep -q done $STAGES/grml-debootstrap ; then
532       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
533       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
534    fi
535 fi
536 # }}}
537
538 # partition handling {{{
539 PARTITION=''
540 DIRECTORY=''
541
542 case $TARGET in
543   /dev/*)
544     PARTITION=1
545     ;;
546   *)
547     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
548     DIRECTORY=1
549     MNTPOINT="$TARGET"
550     MKFS=''
551     TUNE2FS=''
552     FSCK=''
553     GRUB=''
554     GROOT=''
555     ;;
556 esac
557 # }}}
558
559 # architecture setup {{{
560 if [ -n "$ARCH" ] ; then
561    ARCHCMD="--arch $ARCH"
562    ARCHINFO=" (${ARCH})"
563 else
564    ARCH="$(dpkg --print-architecture)"
565    ARCHCMD="--arch $ARCH"
566    ARCHINFO=" (${ARCH})"
567 fi
568 # }}}
569
570 # make sure we have the right syntax when using an iso image {{{
571 if [ -n "$ISO" ] ; then
572    case $ISO in
573       file*) # do nothing
574       ;;
575       *)
576       ISO=file:$1
577       ;;
578    esac
579 fi
580 ISODIR=${ISO##file:}
581 ISODIR=${ISODIR%%/}
582 # }}}
583
584 # helper functions {{{
585 # we want to exit smoothly and clean:
586 bailout(){
587   # make sure $TARGET is not mounted when exiting grml-debootstrap
588   if [ -n "$MNTPOINT" ] ; then
589      if grep -q $MNTPOINT /proc/mounts ; then
590         # make sure nothing is left inside chroot so we can unmount it
591         [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
592         [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
593         # ugly, but make sure we really don't leav anything
594         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /sys  1>/dev/null 2>&1
595         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a    1>/dev/null 2>&1
596         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
597         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount /proc 1>/dev/null 2>&1
598         [ -d "$MNTPOINT/$ISODIR" ]    && umount "$MNTPOINT/$ISODIR" 1>/dev/null 2>&1
599         einfo "Unmounting $MNTPOINT"   ; umount "$MNTPOINT" ; eend $?
600
601         if [ -n "$STAGES" ] ; then
602            echo -n "Removing stages directory ${STAGES}: "
603            rm -rf "$STAGES" && echo done
604         fi
605
606         # remove directory only if we used the default with process id inside the name
607         if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
608            einfo "Removing directory ${MNTPOINT}" ; rmdir $MNTPOINT ; eend $?
609         fi
610      fi
611   fi
612
613   [ -n "$1" ] && EXIT="$1" || EXIT="1"
614   [ -n "$3" ] && einfo "Notice: just remove $STAGES/$3 to reexecute the stage"
615
616   exit "$EXIT"
617 }
618 trap bailout HUP INT QUIT TERM
619
620 # we want to execute all the functions only once, simple check for it:
621 stage() {
622   if [ -n "$2" ] ; then
623      echo "$2" > "${STAGES}/${1}"
624      return 0
625   elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
626      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
627      eindent
628        ewarn "To reexecute it clean up the according directory inside $STAGES" ; eend 0
629      eoutdent
630      return 1
631   fi
632 }
633 # }}}
634
635 # user should recheck his configuration {{{
636 # support full automatic installation:
637 checkforrun() {
638    dialog --timeout 10 --title "$PN" \
639           --yesno "Do you want to stop at this stage?
640
641 Notice: you are running ${PN} in non-interactive mode.
642 ${PN} will install Debian ${RELEASE} on ${TARGET}.
643 Last chance to quit. Timeout of 10 seconds running....
644
645 Do you want to stop now?" 0 0 2>/dev/null
646 }
647
648 # create filesystem {{{
649 mkfs() {
650   if [ -n "$MKFS" ] ; then
651      einfo "Running $MKFS on $TARGET"
652      $MKFS $TARGET
653      TARGET_UUID="$(vol_id -u $TARGET 2>/dev/null || echo '')"
654      eend $?
655   fi
656 }
657 # }}}
658
659 # modify filesystem settings {{{
660 tunefs() {
661   if [ -n "$TUNE2FS" ] ; then
662      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
663      $TUNE2FS $TARGET
664      eend $?
665   fi
666 }
667 # }}}
668
669 # mount the new partition or if it's a directory do nothing at all {{{
670 mount_target() {
671   if [ -n "$DIRECTORY" ] ; then
672      einfo "Running grml-debootstrap on a directory, nothing to mount."
673   else
674      if grep -q $TARGET /proc/mounts ; then
675         eerror "$TARGET already mounted, exiting."
676      else
677        [ -d "$MNTPOINT" ] || mkdir -p "$MNTPOINT"
678        einfo "Mounting $TARGET to $MNTPOINT"
679        mount -o rw,suid,dev $TARGET $MNTPOINT
680        eend $?
681      fi
682   fi
683   if [ -n "$ISODIR" ] ; then
684      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
685      mkdir -p "$MNTPOINT/$ISODIR"
686      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
687      eend $?
688   fi
689 }
690 # }}}
691
692 # install main chroot {{{
693 debootstrap_system() {
694   if ! grep -q $MNTPOINT /proc/mounts ; then
695      mount_target
696   fi
697   if [ "$_opt_nodebootstrap" ]; then
698      einfo "Skipping debootstrap as requested."
699      return
700   fi
701   if grep -q $MNTPOINT /proc/mounts || [ -n "$DIRECTORY" ] ; then
702      einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}${ISO}"
703      [ -n "$MIRROR" ] && $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR || \
704      $DEBOOTSTRAP $ARCHCMD $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
705      eend $?
706   else
707      eerror "Error: $MNTPOINT not mounted, can not continue."
708      eend 1
709   fi
710 }
711 # }}}
712
713 # prepare chroot via chroot-script {{{
714 preparechroot() {
715   einfo "Preparing chroot system"
716
717   # provide variables to chroot system
718   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
719   touch $CHROOT_VARIABLES
720   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
721   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
722   [ -n "$ARCH" ]         && echo "ARCH=$ARCH"                 >> $CHROOT_VARIABLES
723   [ -n "$GROOT" ]        && echo "GROOT=$GROOT"               >> $CHROOT_VARIABLES
724   [ -n "$GRUB" ]         && echo "GRUB=$GRUB"                 >> $CHROOT_VARIABLES
725   [ -n "$HOSTNAME" ]     && echo "HOSTNAME=$HOSTNAME"         >> $CHROOT_VARIABLES
726   [ -n "$ISODIR" ]       && echo "ISODIR=$ISO"                >> $CHROOT_VARIABLES
727   [ -n "$ISO" ]          && echo "ISO=$ISO"                   >> $CHROOT_VARIABLES
728   [ -n "$MIRROR" ]       && echo "CHROOTMIRROR=$MIRROR"       >> $CHROOT_VARIABLES
729   [ -n "$MIRROR" ]       && echo "MIRROR=$MIRROR"             >> $CHROOT_VARIABLES
730   [ -n "$KEEP_SRC_LIST" ] && echo "KEEP_SRC_LIST=$KEEP_SRC_LIST" >> $CHROOT_VARIABLES
731   [ -n "$ROOTPASSWORD" ] && echo "ROOTPASSWORD=$ROOTPASSWORD" >> $CHROOT_VARIABLES
732   [ -n "$TARGET" ]       && echo "TARGET=$TARGET"             >> $CHROOT_VARIABLES
733   [ -n "$TARGET_UUID" ]  && echo "TARGET_UUID=$TARGET_UUID"   >> $CHROOT_VARIABLES
734
735   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
736   chmod 755 $MNTPOINT/bin/chroot-script
737   mkdir $MNTPOINT/etc/debootstrap/
738
739   # make sure we have our files for later use via chroot-script
740   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
741   # make sure we adjust the configuration variables accordingly:
742   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
743   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
744   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
745   sed -i "s#GROOT=.*#GROOT=\"$GROOT\"#"       $MNTPOINT/etc/debootstrap/config
746
747   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
748     $MNTPOINT/etc/debootstrap/packages
749   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
750   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
751     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
752
753   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
754   cp $VERBOSE $CHROOT_VARIABLES          $MNTPOINT/etc/debootstrap/variables
755
756   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
757
758   # make sure we can access network [relevant for cdebootstrap]
759   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
760
761   # provide system's /etc/hosts to the target:
762   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
763      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
764      sed -i "s#127.0.0.1 .*#127.0.0.1       localhost  $HOSTNAME#" /etc/hosts
765   fi
766
767   # setup default locales
768   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
769
770   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
771   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
772
773   # copy any existing files to chroot
774   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
775   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
776   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
777   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
778   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
779   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
780   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
781
782   # copy local network setup to chroot
783   if [ -r /etc/network/interfaces -a ! -r "${MNTPOINT}"/etc/network/interfaces ] ; then
784      [ -d $MNTPOINT/etc/network ] || mkdir $MNTPOINT/etc/network
785      cp $VERBOSE /etc/network/interfaces $MNTPOINT/etc/network/interfaces
786   fi
787
788   eend 0
789 }
790 # }}}
791
792 # execute chroot-script {{{
793 chrootscript() {
794   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
795      mount_target
796   fi
797   if [ -x "$MNTPOINT/bin/chroot-script" ] ; then
798      einfo "Executing chroot-script now"
799      chroot "$MNTPOINT" /bin/chroot-script
800      eend $?
801   else
802      eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
803      eend 1
804   fi
805 }
806 # }}}
807
808 # install booloader grub {{{
809 grub_install() {
810   if [ -z "$GRUB" -o -z "$GROOT" ] ; then
811      echo "Notice: \$GRUB or \$GROOT not defined, will not install grub therefor."
812   elif [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
813      for device in $SELECTED_PARTITIONS ; do
814         # TMPDEVICE=$(echo ${SELECTED_PARTITIONS} | awk '{print $1}')
815         # GRUB="$(grubdevice ${TMPDEVICE})"
816         # GRUB="$(grubdevice ${TMPDEVICE%%[0-9]})"
817         # GRUB=$(grubdevice $device)
818         GRUB="$(grubdevice ${device%%[0-9]})"
819         einfo "Installing grub on ${GRUB}:"
820         [ -x /usr/sbin/grub-install ] && GRUBINSTALL="/usr/sbin/grub-install --no-floppy" || GRUBINSTALL="/sbin/grub-install --no-floppy"
821         $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
822         eend $?
823      done
824   else
825      einfo "Installing grub on ${GRUB}:"
826      [ -x /usr/sbin/grub-install ] && GRUBINSTALL="/usr/sbin/grub-install --no-floppy" || GRUBINSTALL="/sbin/grub-install --no-floppy"
827      $GRUBINSTALL --root-directory="$MNTPOINT" "(${GRUB})"
828      eend $?
829   fi
830 }
831 # }}}
832
833 # unmount $MNTPOINRT {{{
834 umount_chroot() {
835   if [ -n "$ISODIR" ] ; then
836      if grep -q "$ISODIR" /proc/mounts ; then
837         einfo "Unmount $MNTPOINT/$ISODIR"
838         umount "$MNTPOINT/$ISODIR"
839         eend $?
840      fi
841   fi
842   if grep -q "$MNTPOINT" /proc/mounts ; then
843      if [ -n "$PARTITION" ] ; then
844         einfo "Unmount $MNTPOINT"
845         umount $MNTPOINT
846         eend $?
847      fi
848   fi
849 }
850 # }}}
851
852 # execute filesystem check {{{
853 fscktool() {
854   if [ "$FSCK" = 'yes' ] ; then
855      [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
856      einfo "Checking filesystem on $TARGET using $FSCKTOOL"
857      $FSCKTOOL $TARGET
858      eend $?
859   fi
860 }
861 # }}}
862
863 # now execute all the functions {{{
864 for i in mkfs tunefs mount_target debootstrap_system preparechroot \
865          chrootscript grub_install umount_chroot fscktool ; do
866     if stage "${i}" ; then
867        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "i"
868     fi
869 done
870 # }}}
871
872 # finalize {{{
873 einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
874 einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
875
876 # Remove temporary mountpoint again
877 if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
878    einfo "Removing directory ${MNTPOINT}" ; rmdir "$MNTPOINT" ; eend $?
879 fi
880 # }}}
881
882 # end dialog of autoinstallation {{{
883 if [ -n "$AUTOINSTALL" ] ; then
884    dialog --title "$PN" --msgbox \
885           "Finished execution of ${PN}. Enjoy your Debian system." 0 0
886 else
887    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
888 fi
889 # }}}
890
891 ## END OF FILE #################################################################
892 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=3