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