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