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