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