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