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