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