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