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