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