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