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