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