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