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