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