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