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