Support MKFS_OPTS variable + use -F option in mkfs.ext* when running in force mode
[grml-debootstrap.git] / grml-debootstrap
1 #!/bin/bash
2 # Filename:      grml-debootstrap
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
9 # variables {{{
10 PN="$(basename $0)"
11 VERSION="$(dpkg-query --show --showformat='${Version}' "$PN")"
12 VERSION="${VERSION:-unknown}"
13 MNTPOINT="/mnt/debootstrap.$$"
14
15 # defaults
16 [ -n "$CHROOT_SCRIPTS" ] || CHROOT_SCRIPTS='yes'
17 [ -n "$CONFFILES" ] || CONFFILES='/etc/debootstrap'
18 [ -n "$DEBCONF" ] || DEBCONF='yes'
19 [ -n "$DEBIAN_FRONTEND" ] || DEBIAN_FRONTEND='noninteractive'
20 [ -n "$DEBOOTSTRAP" ] || DEBOOTSTRAP='debootstrap'
21 [ -n "$DEFAULT_LOCALES" ] || DEFAULT_LOCALES='en_US.UTF-8'
22 [ -n "$DEFAULT_LANGUAGE" ] || DEFAULT_LANGUAGE='en_US:en'
23 [ -n "$EXTRAPACKAGES" ] || EXTRAPACKAGES='yes'
24 [ -n "$FALLBACK_MIRROR" ] || FALLBACK_MIRROR='http://http.debian.net/debian'
25 [ -n "$FORCE" ] || FORCE=''
26 [ -n "$HOSTNAME" ] || HOSTNAME='grml'
27 [ -n "$INITRD" ] || INITRD='yes'
28 [ -n "$INSTALL_NOTES" ] || INSTALL_NOTES='/etc/debootstrap/install_notes'
29 [ -n "$LOCALES" ] || LOCALES='yes'
30 [ -n "$MIRROR" ] || MIRROR="$FALLBACK_MIRROR"
31 [ -n "$MKFS" ] || MKFS='mkfs.ext4'
32 [ -n "$MKFS_OPTS" ] || MKFS_OPTS=''
33 [ -n "$PACKAGES" ] || PACKAGES='yes'
34 [ -n "$PRE_SCRIPTS" ] || PRE_SCRIPTS='yes'
35 [ -n "$RECONFIGURE" ] || RECONFIGURE='console-data'
36 [ -n "$RELEASE" ] || RELEASE='jessie'
37 [ -n "$RM_APTCACHE" ] || RM_APTCACHE='yes'
38 [ -n "$SCRIPTS" ] || SCRIPTS='yes'
39 [ -n "$SECURE" ] || SECURE='yes'
40 [ -n "$TIMEZONE" ] || TIMEZONE='Europe/Vienna'
41 [ -n "$TUNE2FS" ] || TUNE2FS='tune2fs -c0 -i0'
42 [ -n "$UPGRADE_SYSTEM" ] || UPGRADE_SYSTEM='yes'
43 [ -n "$VMSIZE" ] || VMSIZE="2G"
44 [ -n "$FIXED_DISK_IDENTIFIERS" ] || FIXED_DISK_IDENTIFIERS="no"
45
46 # inside the chroot system locales might not be available, so use minimum:
47 export LANG=C
48 export LC_ALL=C
49
50 # make sure interactive mode is only executed when
51 # using an empty configuration file or option --interactive
52 INTERACTIVE=''
53 # }}}
54
55 # help text {{{
56 usage() {
57   echo "$PN - wrapper around debootstrap for installing Debian
58
59 Usage: $PN [options]
60
61 Bootstrap options:
62
63   -m, --mirror <URL>     Mirror which should be used for apt-get/aptitude.
64   -i, --iso <mnt>        Mountpoint where a Debian ISO is mounted to, for use
65                          instead of fetching packages from a mirror.
66   -r, --release <name>   Release of new Debian system (default: jessie).
67   -t, --target <target>  Target partition (/dev/...) or directory where the
68                          system should be installed to.
69   -p, --mntpoint <mnt>   Mountpoint used for mounting the target system,
70                          has no effect if -t is given and represents a directory.
71       --debopt <params>  Extra parameters passed to the debootstrap command.
72       --interactive      Use interactive mode (frontend).
73       --nodebootstrap    Skip debootstrap, only do configuration to the target.
74       --grub <device>    Target for grub installation. Usage example: /dev/sda
75       --arch <arch>      Set target architecture, use for installing i386 on amd64.
76       --filesystem <fs>  Filesystem that should be used when target is a partition
77                          or Virtual Machine (see --vmfile).
78       --force            Do not prompt for user acknowledgement.
79
80 Options for Virtual Machine deployment:
81
82       --vm               Set up a Virtual Machine, instead of plainly installing
83                          to a partition or directory, to be combined with --target,
84                          like: --vm --target /dev/mapper/your-vm-disk
85       --vmfile           Like --vm, but install into a file (raw format).
86                          Example: --vmfile --target /mnt/sda1/qemu.img
87       --vmsize <size>    Use specified size for size of VM file (default: 2G).
88                          Syntax as supported by qemu-img, like: --vmsize 3G
89
90 Configuration options:
91
92   -c, --config <file>      Use specified configuration file, defaults to
93                              /etc/debootstrap/config
94   -d, --confdir <path>     Place of config files for debootstrap, defaults
95                              to /etc/debootstrap
96       --packages <file>    Install packages defined in specified list file
97                              instead of using /etc/debootstrap/packages.
98       --nopackages         Skip installation of packages defined in
99                              /etc/debootstrap/packages
100       --nokernel           Skip installation of default kernel images.
101       --nointerfaces       Do not copy /etc/network/interfaces from host system
102                            to target system.
103                            (This option is automatically enabled when using --vmfile.)
104       --defaultinterfaces  Install a default /etc/network/interfaces file (enabling
105                            DHCP for eth0) instead of taking over config from host system.
106       --debconf <file>     Pre-seed packages using specified pre-seed db file.
107       --grmlrepos          Enable Grml's Debian repository (deb.grml.org).
108       --backportrepos      Enable Debian's backports repository (backports.debian.org).
109       --keep_src_list      Do not overwrite user provided apt sources.list.
110       --hostname <name>    Hostname of Debian system.
111       --nopassword         Do not prompt for the root password.
112       --password <pwd>     Use specified password as password for user root.
113       --bootappend <line>  Add specified appendline to kernel whilst booting.
114       --chroot-scripts <d> Execute chroot scripts from specified directory.
115       --pre-scripts <dir>  Execute scripts from specified directory (before chroot-scripts).
116       --scripts <dir>      Execute scripts from specified directory (after chroot-scripts).
117
118 Other options:
119
120   -v, --verbose            Increase verbosity.
121       --debug              Execute in very verbose way.
122   -h, --help               Print this usage information and exit.
123   -V, --version            Show summary of options and exit.
124
125 Usage examples can be found in the grml-debootstrap manpage.
126 Send bugreports to the grml-team: bugs (at) grml.org || http://grml.org/bugs/
127 "
128 }
129
130 if [ "$1" = '-h' ] || [ "$1" = '-help' ] || [ "$1" = "--help" ] ; then
131    usage
132    echo 'Please notice that this script requires root permissions!'
133    exit 0
134 fi
135 # }}}
136
137 # early helper functions {{{
138 GOOD='\e[32;01m'
139 WARN='\e[33;01m'
140 BAD='\e[31;01m'
141 NORMAL='\e[0m'
142 HILITE='\e[36;01m'
143 BRACKET='\e[34;01m'
144
145 einfo() {
146   einfon "$1\n"
147   return 0
148 }
149
150 einfon() {
151   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
152   printf " ${GOOD}*${NORMAL} $*"
153   LAST_E_CMD=einfon
154   return 0
155 }
156
157 eerror() {
158   [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
159   printf " ${BAD}*${NORMAL} $*\n" >&2
160   LAST_E_CMD=eerror
161   return 0
162 }
163
164 eend() {
165   local retval="${1:-0}"
166   shift
167   if [ $retval -gt 0 ]; then
168     printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
169   fi
170   return $retval
171 }
172
173 check4root(){
174   if [ "$(id -u 2>/dev/null)" != 0 ] ; then
175     echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
176   fi
177 }
178
179 check4progs(){
180   local RC=''
181   for arg in $* ; do
182     which $arg >/dev/null 2>&1 || RC="$arg"
183   done
184   if [ -n "$RC" ] ; then
185      echo "$RC not installed"
186      return 1
187   fi
188 }
189 # }}}
190
191 # helper functions {{{
192 cleanup() {
193   if [ -n "$CHROOT_VARIABLES" ] ; then
194     einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
195   fi
196
197   if [ -n "$STAGES" ] ; then
198     einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
199   fi
200
201   # Remove temporary mountpoint again
202   if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
203     rmdir "$MNTPOINT" 2>/dev/null
204   fi
205
206   # make sure $TARGET is not mounted when exiting grml-debootstrap
207   if [ -n "$MNTPOINT" ] ; then
208     if grep -q "$MNTPOINT" /proc/mounts ; then
209       # make sure nothing is left inside chroot so we can unmount it
210       [ -x "$MNTPOINT"/etc/init.d/ssh   ] && "$MNTPOINT"/etc/init.d/ssh stop
211       [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
212
213       [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a >/dev/null 2>&1
214
215       # ugly, but make sure we really don't leave anything (/proc /proc and
216       # /dev /dev are intended, trying to work around timing issues, see #657023)
217       for ARG in /sys /proc /proc /dev /dev ; do
218         [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1
219         umount "$MNTPOINT"/$ARG >/dev/null 2>&1
220       done
221
222       if [ -n "$ISODIR" ] ; then
223         [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1
224       fi
225
226       if [ -n "$DIRECTORY" ] ; then
227         einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
228       else
229         einfo "Unmounting $MNTPOINT"
230         umount "$MNTPOINT"
231         eend $?
232       fi
233
234       if [ -n "$STAGES" ] ; then
235         echo -n "Removing stages directory ${STAGES}: "
236         rm -rf "$STAGES" && echo done
237       fi
238
239       # remove directory only if we used the default with process id inside the name
240       if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
241         einfo "Removing directory ${MNTPOINT}"
242         rmdir "$MNTPOINT"
243         eend $?
244       fi
245     fi
246   fi
247
248   if [ -n "${ORIG_TARGET}" ] ; then
249     einfo "Removing loopback mount of file ${ORIG_TARGET}."
250     kpartx -d "${ORIG_TARGET}" ; eend $?
251   fi
252 }
253
254 # we want to exit smoothly and clean:
255 bailout(){
256
257   cleanup
258
259   [ -n "$1" ] && EXIT="$1" || EXIT="1"
260   [ -n "$2" ] && einfo "Notice: remove $STAGES/$2 to reexecute the stage"
261
262   exit "$EXIT"
263 }
264 trap bailout HUP INT QUIT TERM
265
266 # we want to execute all the functions only once, simple check for it:
267 stage() {
268   if [ -n "$2" ] ; then
269      echo "$2" > "${STAGES}/${1}"
270      return 0
271   elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
272      ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
273      ewarn "  To reexecute it clean up the according directory inside $STAGES" ; eend 0
274      return 1
275   fi
276 }
277 # }}}
278
279 # make sure we have what we need {{{
280 check4progs debootstrap || bailout 1
281 # }}}
282
283 # source main configuration file {{{
284 if [ -r /etc/debootstrap/config ] ; then
285   . /etc/debootstrap/config
286 fi
287 # }}}
288
289 # cmdline handling {{{
290 # source external command line parameter-processing script
291 if [ -r ./cmdlineopts.clp ] ; then
292    . ./cmdlineopts.clp
293 elif [ -r /usr/share/grml-debootstrap/functions/cmdlineopts.clp ] ; then
294    . /usr/share/grml-debootstrap/functions/cmdlineopts.clp
295 else
296    eerror "Error: cmdline function file not found, exiting."
297    eend 1
298    bailout 1
299 fi
300
301 # == business-logic of command line parameter-processing
302
303 # source configuration file in <confdir> if supplied. {{{
304 [ "$_opt_confdir" ] && {
305   CONFFILES=$_opt_confdir
306   einfo "Using config files under $CONFFILES/."
307   if ! [ -r "$CONFFILES/config" ] ; then
308     eerror "Error: config file $CONFFILES/config not found."; eend 1; bailout 1
309   fi
310   if ! . "$CONFFILES/config" ; then
311     eerror "Error reading config file $CONFFILES/config" ; eend 1 ; bailout 1
312   fi
313   # restore the command line parameter value
314   CONFFILES=$_opt_confdir
315 }
316 # }}}
317
318 [ "$_opt_mirror" ]              && MIRROR=$_opt_mirror
319 [ "$_opt_iso" ]                 && ISO=$_opt_iso
320 [ "$_opt_release" ]             && RELEASE=$_opt_release
321 [ "$_opt_target" ]              && TARGET=$_opt_target
322 [ "$_opt_vm" ]                  && VIRTUAL=1
323 [ "$_opt_vmfile" ]              && VMFILE=1 && VIRTUAL=1
324 [ "$_opt_vmsize" ]              && VMSIZE=$_opt_vmsize
325 [ "$_opt_mntpoint" ]            && MNTPOINT=$_opt_mntpoint
326 [ "$_opt_debopt" ]              && DEBOOTSTRAP_OPT=$_opt_debopt
327 [ "$_opt_interactive" ]         && INTERACTIVE=1
328 [ "$_opt_config" ]              && CONFIGFILE=$_opt_config
329 [ "$_opt_filesystem" ]          && MKFS="mkfs.$_opt_filesystem"
330 [ "$_opt_packages_set" ]        && PACKAGES='yes'
331 [ "$_opt_nopackages" ]          && PACKAGES=''
332 [ "$_opt_debconf_set" ]         && DEBCONF='yes'
333 [ "$_opt_scripts_set" ]         && SCRIPTS='yes'
334 [ "$_opt_pre_scripts_set" ]     && PRE_SCRIPTS='yes'
335 [ "$_opt_chroot_scripts_set" ]  && CHROOT_SCRIPTS='yes'
336 [ "$_opt_keep_src_list" ]       && KEEP_SRC_LIST='yes'
337 [ "$_opt_grmlrepos" ]           && GRMLREPOS='yes'
338 [ "$_opt_backportrepos" ]       && BACKPORTREPOS='yes'
339 [ "$_opt_hostname" ]            && HOSTNAME=$_opt_hostname
340 [ "$_opt_password" ]            && ROOTPASSWORD=$_opt_password
341 [ "$_opt_nopassword" ]          && NOPASSWORD='yes'
342 [ "$_opt_defaultinterfaces" ]   && DEFAULTINTERFACES="true"
343 [ "$_opt_nointerfaces" ]        && NOINTERFACES="true"
344 [ "$_opt_nokernel" ]            && NOKERNEL="true"
345 [ "$_opt_bootappend" ]          && BOOT_APPEND=$_opt_bootappend
346 [ "$_opt_grub" ]                && GRUB=$_opt_grub
347 [ "$_opt_arch" ]                && ARCH=$_opt_arch
348 [ "$_opt_insecure" ]            && echo "Warning: --insecure is deprecated, continuing anyway."
349 [ "$_opt_force" ]               && FORCE=$_opt_force
350 [ "$_opt_verbose" ]             && VERBOSE="-v"
351 [ "$_opt_debug" ]               && DEBUG="true"
352
353 if [ "$DEBUG" = "true" ] ; then
354   set -x
355 fi
356
357 [ "$_opt_help" ] && {
358   usage ; eend 0
359   eend 0
360   exit 0
361 }
362
363 [ "$_opt_version" ] && {
364   einfo "$PN - version $VERSION"
365   einfo "Send bug reports to bugs@grml.org or http://grml.org/bugs/"
366   eend 0
367   exit 0
368 }
369 # }}}
370
371 # check for root permissions {{{
372 if ! check4root ; then
373    echo "For usage instructions please execute '$PN --help'."
374    bailout 1
375 fi
376 # }}}
377
378 # make sure we have what we need {{{
379 if [ -n "$VIRTUAL" ] ; then
380   check4progs kpartx mksh parted qemu-img || bailout 1
381 fi
382 # }}}
383
384 # source specified configuration file {{{
385 if [ -n "$CONFIGFILE" ] ; then
386    einfo "Reading specified config file $CONFIGFILE."
387    if ! . "$CONFIGFILE" ; then
388       eerror "Error reading config file $CONFIGFILE" ; eend 1 ; bailout 1
389    fi
390 fi
391 # }}}
392
393 # backwards compability checks {{{
394 if [ -n "$GROOT" ] ; then
395    eerror "Error: you seem to have \$GROOT configured."
396    eerror "This variable is no longer supported, please visit the"
397    eerror "grml-debootstrap documentation for details."
398    eend 1
399    bailout 1
400 fi
401
402 if echo "$GRUB" | grep -q '^hd' ; then
403    eerror "Error: this syntax for the grub configuration variable is no longer supported."
404    eerror "Please do not use hd... any longer but /dev/sdX instead."
405    eend 1
406    bailout 1
407 fi
408 # }}}
409
410 # welcome screen {{{
411 welcome_dialog()
412 {
413    dialog --title "$PN" --yesno "Welcome to the interactive configuration of ${PN}.
414 Do you want to continue installing Debian using this frontend?" 0 0 || bailout 0
415 }
416 # }}}
417
418 # ask for target {{{
419 prompt_for_target()
420 {
421   AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
422                sed 's/*//' | \
423                grep -v 'Extended$' | \
424                gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}'; ls /dev/md* 2>/dev/null || true);
425
426   if [ -z "$AVAILABLE_PARTITIONS" ] ; then
427      dialog --title "$PN" --trim \
428      --msgbox "Sorry, no partitions found. Please configure your
429      harddisks (see /proc/partitions) using a tool like fdisk,
430      cfdisk, gpart, gparted,..." 0 0
431      bailout 1
432   fi
433
434   PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
435                        echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet])"
436                    done)
437
438   TARGET=$(dialog --title "$PN" --single-quoted --stdout \
439          --menu "Please select the target partition:" 0 0 0 \
440          $PARTITION_LIST)
441   [ $? -eq 0 ] || bailout 1
442 }
443 # }}}
444
445 # ask for bootmanager {{{
446 prompt_for_bootmanager()
447 {
448   ADDITIONAL_PARAMS=""
449
450   if echo "$TARGET" | grep -q "/dev/md" ; then
451      MBRPART="all disks of Software RAID $TARGET"
452   else
453      # figure out whole disk device
454      found=
455      for device in /dev/disk/by-id/*
456      do
457         [ $(readlink -f $device) = ${TARGET} ] || continue
458         found=1
459         break
460      done
461      [ -n "$found" ] && MBRDISK=$(echo ${device}|sed -e 's/-part[0-9][0-9]*$//')
462      if [ -e "$MBRDISK" ]; then
463         MBRDISK=$(readlink -f $MBRDISK)
464      else
465         # fall back to old behaviour
466         MBRDISK=$(echo ${TARGET} | sed -e 's/[0-9][0-9]*$//')
467      fi
468
469      MBRPART="MBR of $MBRDISK"
470   fi
471
472   for device in cciss/c0d0 sda hda; do
473     if [ "/dev/$device" != "${MBRDISK}" ]; then
474       grep -q $device /proc/partitions && \
475       ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS:$device:install bootmanager grub into MBR of /dev/$device"
476     fi
477   done
478   ADDITIONAL_PARAMS=${ADDITIONAL_PARAMS#:}
479
480   OIFS="$IFS"; IFS=:
481
482   GETMBR=$(dialog --stdout --title "$PN" --default-item mbr \
483           --menu "Where do you want to install the bootmanager grub?" 0 0 0 \
484             mbr       "install bootmanager into $MBRPART" \
485             nowhere   "do not install bootmanager at all" \
486           ${ADDITIONAL_PARAMS})
487   [ $? -eq 0 ] || bailout 3
488   IFS="$OIFS"
489
490   case "$GETMBR" in
491     mbr)
492       # /dev/md0: has to be installed in MBR of /dev/md0 and not in /dev/md:
493       if echo "$TARGET" | grep -q "/dev/md" ; then
494         GRUB="$TARGET"
495       else
496         GRUB="$MBRDISK"
497       fi
498       ;;
499     hda)
500       GRUB="/dev/hda"
501       ;;
502     sda)
503       GRUB="/dev/sda"
504       ;;
505     nowhere)
506       GRUB=''
507       ;;
508   esac
509 }
510 # }}}
511
512 # ask for Debian release {{{
513 prompt_for_release()
514 {
515   [ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='jessie'
516   RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \
517             "Please enter the Debian release you would like to use for installation:" \
518             0 50 5 \
519             lenny    Debian/5.0 \
520             squeeze  Debian/6.0 \
521             wheezy   Debian/7.0 \
522             jessie   Debian/8.0 \
523             sid      Debian/unstable)"
524   [ $? -eq 0 ] || bailout
525 }
526 # }}}
527
528 # ask for hostname {{{
529 prompt_for_hostname()
530 {
531   HOSTNAME="$(dialog --stdout --title "${PN}" --inputbox \
532             "Please enter the hostname you would like to use for installation:" \
533             0 0 $HOSTNAME)"
534   [ $? -eq 0 ] || bailout
535 }
536 # }}}
537
538 # ask for password {{{
539 prompt_for_password()
540 {
541   if [ "$_opt_nopassword" ] ; then
542     einfo "Skip asking for root password as requested."
543     return 0
544   fi
545
546   ROOTPW1='PW1'
547   ROOTPW2='PW2'
548   while [ "$ROOTPW1" != "$ROOTPW2" ]; do
549     ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
550     "Please enter the password for the root account:" 10 60)
551     [ $? -eq 0 ] || bailout
552     ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
553     "Please enter the password for the root account again for \
554     confirmation:" 10 60)
555     [ $? -eq 0 ] || bailout
556
557     if [ "$ROOTPW1" != "$ROOTPW2" ]; then
558       $(dialog --stdout --title "${PN}" --ok-label \
559       "Retry" --msgbox "Passwords do not match!" 10 60)
560     fi
561   done
562   ROOTPASSWORD="$ROOTPW1"
563 }
564 # }}}
565
566 # ask for Debian mirror {{{
567 prompt_for_mirror()
568 {
569   [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
570
571   CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
572           --menu "Where do you want to install from?" 0 0 0 \
573             net   "install via network (downloading from mirror)" \
574             local "install from local directory/mirror"
575           )
576   [ $? -eq 0 ] || bailout
577
578   if [ "$CHOOSE_MIRROR" = 'net' ] ; then
579      [ -n "$MIRROR" ] || MIRROR='http://http.debian.net/debian'
580      MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
581                "Please enter Debian mirror you would like to use for installing packages." \
582                0 0 $MIRROR)"
583      [ $? -eq 0 ] || bailout
584   else # CHOOSE_MIRROR == local
585      [ -n "$ISO" ] || ISO='/mnt/mirror'
586      ISO="$(dialog --stdout --title "${PN}" --inputbox \
587                "Please enter directory name you would like to use for installing packages." \
588                0 0 $ISO)"
589      [ $? -eq 0 ] || bailout
590   fi
591 }
592 # }}}
593
594 # software raid setup {{{
595 config_swraid_setup()
596 {
597 TMPFILE=$(mktemp)
598
599 # Currently we support only raid1:
600 RAIDLEVEL='raid1'
601
602 #RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
603 #                   --menu "Which RAID level do you want to use?" 0 0 0 \
604 #                     raid1 "Software RAID level 1" \
605 #                     raid5 "Software RAID level 5")
606 #[ $? -eq 0 ] || bailout 20
607
608 MD_LIST=$(for i in $(seq 0 9) ; do
609             awk '{print $4}' /proc/partitions | grep -q md$i || \
610             echo "/dev/md$i /dev/md$i"
611           done)
612
613 TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
614 --menu "Which device do you want to use for ${RAIDLEVEL}?
615
616 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 \
617 $MD_LIST)
618 [ $? -eq 0 ] || bailout 20
619
620 AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
621              sed 's/*//' | \
622              grep -v 'Extended$' | \
623              gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
624 [ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
625 PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
626                      echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
627                  done)
628
629 dialog --title "$PN" --separate-output \
630        --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
631        $PARTITION_LIST 2>$TMPFILE
632 [ $? -eq 0 ] || bailout
633 RETVAL=$?
634 SELECTED_PARTITIONS="$(cat $TMPFILE)"
635
636 NUM_PARTITIONS=0
637 for i in $(cat $TMPFILE) ; do
638    NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
639 done
640
641 # force metadata version 0.90 for lenny so old grub can boot from this array.
642 METADATA_VERSION=""
643 if [ $RELEASE = "lenny" ]; then
644    METADATA_VERSION="-e0"
645 fi
646
647 ERRORFILE=$(mktemp)
648 yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
649       --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
650 RC=$?
651 if [ "$RC" = 0 ] ; then
652    dialog --title "$PN" --msgbox \
653    "Creating $TARGET was successful." 0 0
654    rm -f "$TMPFILE" "$ERRORFILE"
655 else
656    dialog --title "$PN" --msgbox \
657    "There was an error setting up $TARGET:
658
659 $(cat $ERRORFILE)
660
661 Exiting." 0 0
662    rm -f "$TMPFILE" "$ERRORFILE"
663    bailout 1
664 fi
665
666 }
667
668 prompt_for_swraid()
669 {
670 if dialog --stdout --title "$PN" \
671           --defaultno --yesno "Do you want to configure Software RAID?
672
673 Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
674   config_swraid_setup
675 fi
676 }
677 # }}}
678
679 # user should recheck his configuration {{{
680 # support full automatic installation:
681 checkforrun() {
682    dialog --timeout 10 --title "$PN" \
683           --yesno "Do you want to stop at this stage?
684
685 Notice: you are running ${PN} in non-interactive mode.
686 ${PN} will install Debian ${RELEASE} on ${TARGET}.
687 Last chance to quit. Timeout of 10 seconds running....
688
689 Do you want to stop now?" 0 0 2>/dev/null
690 }
691 # }}}
692
693 # make sure the user is aware of the used configuration {{{
694 checkconfiguration()
695 {
696 if [ -n "$AUTOINSTALL" ] ; then
697    if checkforrun ; then
698       eerror "Exiting as requested" ; eend 0
699       bailout 1
700    fi
701 elif [ -n "$INTERACTIVE" ] ; then
702
703    INFOTEXT="Please recheck configuration before execution:
704    "
705    [ -n "$TARGET" ]  && INFOTEXT="$INFOTEXT
706    Target:          $TARGET"
707    [ -n "$GRUB" ]    && INFOTEXT="$INFOTEXT
708    Install grub:    $GRUB"
709    [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
710    Using release:   $RELEASE"
711    [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
712    Using hostname:  $HOSTNAME"
713    [ -n "$MIRROR" ]  && INFOTEXT="$INFOTEXT
714    Using mirror:    $MIRROR"
715    [ -n "$ISO" ]  && INFOTEXT="$INFOTEXT
716    Using ISO:       $ISO"
717    [ -n "$ARCH" ]  && INFOTEXT="$INFOTEXT
718    Using arch:      $ARCH"
719
720    INFOTEXT="$INFOTEXT
721
722 Is this ok for you? Notice: selecting 'No' will exit ${PN}."
723
724    dialog --title "$PN" --no-collapse \
725           --yesno "$INFOTEXT" 0 0
726    [ $? -eq 0 ] || bailout 0
727
728 else # if not running automatic installation display configuration and prompt for execution:
729    einfo "$PN [${VERSION}] - Please recheck configuration before execution:"
730    echo
731    echo "   Target:          $TARGET"
732
733    # do not display if MNTPOINT is the default one
734    case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo "   Mount point:     $MNTPOINT" ;; esac
735
736    if [ -n "$VIRTUAL" ] ; then
737       echo "   Install grub:    yes"
738    else
739      [ -n "$GRUB" ]     && echo "   Install grub:    $GRUB" || echo "   Install grub:    no"
740    fi
741
742    [ -n "$RELEASE" ]  && echo "   Using release:   $RELEASE"
743    [ -n "$HOSTNAME" ] && echo "   Using hostname:  $HOSTNAME"
744    [ -n "$MIRROR" ]   && echo "   Using mirror:    $MIRROR"
745    [ -n "$ISO" ]      && echo "   Using ISO:       $ISO"
746    [ -n "$ARCH" ]     && echo "   Using arch:      $ARCH"
747    if [ -n "$VIRTUAL" ] ; then
748       echo "   Deploying as Virtual Machine."
749       if [ -n "$VMSIZE" -a -n "$VMFILE" ]; then
750          echo "   Using Virtual Disk file with size of ${VMSIZE}."
751       fi
752    fi
753
754    if [ ! -t 0 -a -z "$ROOTPASSWORD" -a -z "$NOPASSWORD" ] ; then
755       echo
756       echo "   You do not have a TTY allocated, your password will be shown in"
757       echo "   plaintext on the terminal! If you are using SSH, try its -t option!"
758    fi
759
760    echo
761    echo "   Important! Continuing will delete all data from ${TARGET}!"
762
763    if [ -n "$FORCE" ] ; then
764      einfo "Skip user acknowledgement as requested via --force option."
765    else
766      echo
767      einfon "Is this ok for you? [y/N] "
768      read a
769      if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
770         eerror "Exiting as requested." ; eend 1
771         bailout 1
772      fi
773    fi
774 fi
775 }
776 # }}}
777
778 # interactive mode {{{
779 interactive_mode()
780 {
781   check4progs dialog || bailout 1
782
783   welcome_dialog
784
785   prompt_for_release
786
787   prompt_for_swraid
788
789   prompt_for_target
790
791   prompt_for_bootmanager
792
793   prompt_for_hostname
794
795   prompt_for_password
796
797   prompt_for_mirror
798 }
799
800 # run interactive mode if we didn't get the according configuration yet
801 if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
802    # only target might be unset, so make sure the INTERACTIVE flag is set as well
803    INTERACTIVE=1
804    interactive_mode
805 fi
806 # }}}
807
808 # architecture setup {{{
809 if [ -n "$ARCH" ] ; then
810    ARCHCMD="--arch $ARCH"
811    ARCHINFO=" (${ARCH})"
812 else
813    ARCH="$(dpkg --print-architecture)"
814    ARCHCMD="--arch $ARCH"
815    ARCHINFO=" (${ARCH})"
816 fi
817 # }}}
818
819 # It is not possible to build amd64 on i686. {{{
820 CURRENT_ARCH="$(uname -m)"
821 if [ "$CURRENT_ARCH" != "x86_64" ] ; then
822    if [ "$ARCH" = "amd64" ] ; then
823       eerror "It is not possible to build amd64 on $CURRENT_ARCH." ; eend 1
824       bailout 1
825    fi
826 fi
827 # }}}
828
829 checkconfiguration
830
831 # finally make sure at least $TARGET is set [the partition for the new system] {{{
832 if [ -n "$TARGET" ] ; then
833    SHORT_TARGET="${TARGET##*/}"
834 else
835    eerror "Please adjust $CONFFILES/config or..."
836    eerror "... use the interactive version for configuration before running ${0}" ; eend 1
837    bailout 1
838 fi
839 # }}}
840
841 # stages setup {{{
842 if [ -z "$STAGES" ] ; then
843    STAGES="/var/cache/grml-debootstrap/stages_${SHORT_TARGET}"
844    [ -d "$STAGES" ] || mkdir -p "$STAGES"
845 fi
846
847 if [ -r "$STAGES"/grml-debootstrap ] ; then
848    if grep -q done $STAGES/grml-debootstrap ; then
849       eerror "Error: grml-debootstrap has been executed already, won't continue therefore."
850       eerror "If you want to re-execute grml-debootstrap just manually remove ${STAGES}" ; eend 1
851    fi
852 fi
853 # }}}
854
855 # partition handling {{{
856 PARTITION=''
857 DIRECTORY=''
858
859 set_target_directory(){
860     # assume we are installing into a directory, don't run mkfs and grub related stuff therefore
861     DIRECTORY=1
862     MNTPOINT="$TARGET"
863     MKFS=''
864     TUNE2FS=''
865     FSCK=''
866     # make sure we normalise the path to an absolute directory name so something like:
867     #  mkdir -p foo/a bar/a; (cd foo; grml-debootstrap -t a)&; (cd bar; grml-debootstrap -t a)&; wait
868     # works
869     TARGET="$(readlink -f $TARGET)"
870 }
871
872 if [ -b "$TARGET" ] || [ -n "$VIRTUAL" ] ; then
873     PARTITION=1
874 else
875     set_target_directory
876 fi
877 # }}}
878
879 # make sure we have the right syntax when using an iso image {{{
880 if [ -n "$ISO" ] ; then
881    case $ISO in
882       file*) # do nothing
883       ;;
884       *)
885       ISO=file:$ISO
886       ;;
887    esac
888 fi
889 ISODIR=${ISO##file:}
890 ISODIR=${ISODIR%%/}
891 # }}}
892
893 # Debian ISOs do not contain signed Release files {{{
894 if [ -n "$ISO" ] ; then
895     DEBOOTSTRAP_OPT="$DEBOOTSTRAP_OPT --no-check-gpg"
896 fi
897 # }}}
898
899 # create filesystem {{{
900 mkfs() {
901   if [ -n "$DIRECTORY" ] ; then
902      einfo "Running grml-debootstrap on a directory, skipping mkfs stage."
903      return 0
904   fi
905
906   if grep -q "$TARGET" /proc/mounts ; then
907     eerror "$TARGET already mounted, exiting to avoid possible damage. (Manually unmount $TARGET)" ; eend 1
908     bailout 1
909   fi
910
911   # mkfs.ext* might prompt with "/dev/sdX# contains a ext* file system
912   # created on ... Proceed anyway? (y,n)" which we want to skip in force mode
913   if [ -n "$MKFS" ] && [ -n "$FORCE" ] ; then
914     case "$MKFS" in
915       mkfs.ext*)
916         einfo "Enabling force option (-F) for mkfs.ext* tool as requested via --force switch."
917         MKFS_OPTS="$MKFS_OPTS -F"
918         eend 0
919         ;;
920     esac
921   fi
922
923   if [ -n "$MKFS" ] ; then
924     einfo "Running $MKFS $MKFS_OPTS on $TARGET"
925     $MKFS $MKFS_OPTS $TARGET ; RC=$?
926
927     if [ "$FIXED_DISK_IDENTIFIERS" = "yes" ] ; then
928       if ! echo "$MKFS" | grep -q "mkfs.ext" ; then
929         eerror "Not changing disk uuid for $TARGET because $MKFS doesn't seem to match for ext{2,3,4} file system"
930         eend 1
931         bailout 1
932       else
933         einfo "Changing disk uuid for $TARGET to fixed (non-random) value using tune2fs"
934         tune2fs "$TARGET" -U 26ada0c0-1165-4098-884d-aafd2220c2c6
935         eend $?
936       fi
937     fi
938
939     # make sure /dev/disk/by-uuid/... is up2date, otherwise grub
940     # will fail to detect the uuid in the chroot
941     if echo "$TARGET" | grep -q "/dev/md" ; then
942       blockdev --rereadpt "${TARGET}"
943     elif ! [ -n "$VIRTUAL" ] ; then
944       blockdev --rereadpt "${TARGET%%[0-9]*}"
945     fi
946     # give the system 2 seconds, otherwise we might run into
947     # race conditions :-/
948     sleep 2
949
950     eval $(blkid -o udev $TARGET 2>/dev/null)
951     [ -n "$ID_FS_UUID" ] && TARGET_UUID="$ID_FS_UUID" || TARGET_UUID=""
952
953     eend $RC
954   fi
955 }
956 # }}}
957
958 # modify filesystem settings {{{
959 tunefs() {
960   if [ -n "$TUNE2FS" ] && echo "$MKFS" | grep -q "mkfs.ext" ; then
961      einfo "Disabling automatic filesystem check on $TARGET via tune2fs"
962      $TUNE2FS $TARGET
963      eend $?
964   fi
965 }
966 # }}}
967
968 # mount the new partition or if it's a directory do nothing at all {{{
969 mount_target() {
970   if [ -n "$DIRECTORY" ] ; then
971      einfo "Running grml-debootstrap on a directory, nothing to mount."
972   else
973      if grep -q $TARGET /proc/mounts ; then
974         ewarn "$TARGET already mounted, continuing anyway." ; eend 0
975      else
976        if ! [ -d "${MNTPOINT}" ] ; then
977           [ -n "$VIRTUAL" ] || mkdir -p "${MNTPOINT}"
978        fi
979        einfo "Mounting $TARGET to $MNTPOINT"
980        mkdir -p "$MNTPOINT"
981        mount -o rw,suid,dev $TARGET $MNTPOINT
982        eend $?
983      fi
984   fi
985   if [ -n "$ISODIR" ] ; then
986      einfo "Mounting Debian image loopback to $MNTPOINT/$ISODIR."
987      mkdir -p "$MNTPOINT/$ISODIR"
988      mount --bind "$ISODIR" "$MNTPOINT/$ISODIR"
989      eend $?
990   fi
991 }
992 # }}}
993
994 # prepare VM image for usage with debootstrap {{{
995 prepare_vm() {
996   if [ -z "$VIRTUAL" ] ; then
997      return 0 # be quiet by intention
998   fi
999
1000   if [ -b "$TARGET" -a -n "$VMFILE" ] ; then
1001      eerror "Error: specified virtual disk target ($TARGET) is an existing block device."
1002      eend 1
1003      bailout 1
1004   fi
1005   if [ ! -b "$TARGET" -a -z "$VMFILE" ] ; then
1006      eerror "Error: specified virtual disk target ($TARGET) does not exist yet."
1007      eend 1
1008      bailout 1
1009   fi
1010
1011   ORIG_TARGET="$TARGET" # store for later reuse
1012
1013   if [ -n "$VMFILE" ]; then
1014     qemu-img create -f raw "${TARGET}" "${VMSIZE}"
1015   fi
1016   echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | dd of="$TARGET" conv=notrunc
1017   dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of="$TARGET"
1018   if [ "$FIXED_DISK_IDENTIFIERS" = "yes" ] ; then
1019     einfo "Adjusting disk signature to a fixed (non-random) value"
1020     MBRTMPFILE=$(mktemp)
1021     dd if="${TARGET}" of="${MBRTMPFILE}" bs=512 count=1
1022     echo -en "\x41\x41\x41\x41\x41" | dd of="${MBRTMPFILE}" conv=notrunc seek=440 bs=1
1023     dd if="${MBRTMPFILE}" of="${TARGET}" conv=notrunc
1024     eend $?
1025   fi
1026   parted -s "${TARGET}" 'mkpart primary ext4 2M -1'
1027
1028   # if dm-mod isn't available then kpartx will fail with
1029   # "Is device-mapper driver missing from kernel? [...]"
1030   if ! kpartx -av $TARGET >/dev/null 2>&1 || ! grep -q device-mapper /proc/misc >/dev/null 2>&1 ; then
1031     einfo "Device-mapper not ready yet, trying to load dm-mod module."
1032     modprobe dm-mod ; eend $?
1033   fi
1034
1035   # make sure loop module is present
1036   if ! losetup -f >/dev/null 2>&1; then
1037     einfo "Can not find a usable loop device, retrying after loading loop module."
1038     modprobe loop
1039     if losetup -f >/dev/null 2>&1; then
1040       einfo "Found a usable loop device now, continuing."
1041     else
1042       eerror "Error finding usable loop device" ; eend 1
1043       bailout 1
1044     fi
1045   fi
1046
1047   DEVINFO=$(kpartx -av $TARGET) # 'add map loop1p1 (253:0): 0 6289408 linear /dev/loop1 2048'
1048   if [ -z "${DEVINFO}" ] ; then
1049     eerror "Error setting up loopback device." ; eend 1
1050     bailout 1
1051   fi
1052
1053   # hopefully this always works as expected
1054   LOOP=$(echo ${DEVINFO} | sed 's/.* linear //; s/ [[:digit:]]*//') # '/dev/loop1'
1055   BLOCKDEV=$(echo "${DEVINFO}" | sed -e 's/.* (\(.*:.*\)).*/\1/')   # '253:0'
1056   LOOP_PART="$(echo ${DEVINFO##add map } | sed 's/ .*//')" # '/dev/loop1p1'
1057   export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
1058
1059   blockdev --rereadpt "${LOOP}"
1060
1061   if [ -z "$TARGET" ] ; then
1062      eerror "Error: target could not be set to according /dev/mapper/* device." ; eend 1
1063      bailout 1
1064   fi
1065 }
1066 # }}}
1067
1068 # make VM image bootable and unmount it {{{
1069 finalize_vm() {
1070   if [ -z "${VIRTUAL}" ] ; then
1071      return 0
1072   fi
1073
1074   if ! mount "${TARGET}" "${MNTPOINT}" ; then
1075     eerror "Error: Mounting ${TARGET} failed, can not continue." ; eend 1
1076     bailout 1
1077   fi
1078
1079   einfo "Installing Grub as bootloader."
1080   mount -t proc none "${MNTPOINT}"/proc
1081   mount -t sysfs none "${MNTPOINT}"/sys
1082   mount --bind /dev "${MNTPOINT}"/dev
1083
1084   mkdir -p "${MNTPOINT}/boot/grub"
1085   if ! [ -d "${MNTPOINT}"/usr/lib/grub/i386-pc/ ] ; then
1086      eerror "Error: grub not installed inside Virtual Machine. Can not install bootloader." ; eend 1
1087      bailout 1
1088   fi
1089
1090   cp "${MNTPOINT}"/usr/lib/grub/i386-pc/* "${MNTPOINT}/boot/grub/"
1091   chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2
1092   dd if="${MNTPOINT}/tmp/core.img" of="${ORIG_TARGET}" conv=notrunc seek=4
1093   rm -f "${MNTPOINT}/tmp/core.img"
1094
1095   einfo "Updating grub configuration file."
1096   if [ -n "$BOOT_APPEND" ] ; then
1097      sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" "${MNTPOINT}"/etc/default/grub
1098   fi
1099   chroot "${MNTPOINT}" update-grub
1100
1101   umount "${MNTPOINT}"/proc
1102   umount "${MNTPOINT}"/sys
1103   umount "${MNTPOINT}"/dev
1104
1105   einfo "Adjusting grub.cfg for successful boot sequence."
1106   # ugly but needed to boot grub acordingly
1107   sed -i "s;set root=.*;set root='(hd0,msdos1)';" "${MNTPOINT}"/boot/grub/grub.cfg
1108   sed -i "s;root=[^ ]\+;root=/dev/sda1;" "${MNTPOINT}"/boot/grub/grub.cfg
1109
1110   umount "${MNTPOINT}"
1111   kpartx -d "${ORIG_TARGET}" >/dev/null
1112 }
1113 # }}}
1114
1115 # install main chroot {{{
1116 debootstrap_system() {
1117   if [ "$_opt_nodebootstrap" ]; then
1118      einfo "Skipping debootstrap as requested."
1119      return
1120   fi
1121
1122   if grep -q "$MNTPOINT" /proc/mounts || [ -n "$DIRECTORY" ] ; then
1123     :
1124   else
1125     eerror "Error: $MNTPOINT not mounted, can not continue."
1126     eend 1 ; exit 1
1127   fi
1128
1129   if [ -n "$ISO" ] ; then
1130     einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${ISO}"
1131     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO"
1132     $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $ISO
1133     RC=$?
1134   else
1135     einfo "Running $DEBOOTSTRAP $DEBOOTSTRAP_OPT for release ${RELEASE}${ARCHINFO} using ${MIRROR}"
1136     einfo "Executing: $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR"
1137     $DEBOOTSTRAP $ARCHCMD $KEYRING $DEBOOTSTRAP_OPT $RELEASE $MNTPOINT $MIRROR
1138     RC=$?
1139   fi
1140
1141   if [ $RC -ne 0 ] ; then
1142     if [ -r "$MNTPOINT/debootstrap/debootstrap.log" ] && \
1143       [ -s "$MNTPOINT/debootstrap/debootstrap.log" ] ; then
1144       einfo "Presenting last ten lines of debootstrap.log:"
1145       tail -10 $MNTPOINT/debootstrap/debootstrap.log
1146       einfo "End of debootstrap.log"
1147     fi
1148   fi
1149
1150   eend $RC
1151 }
1152 # }}}
1153
1154 # prepare chroot via chroot-script {{{
1155 preparechroot() {
1156   einfo "Preparing chroot system"
1157
1158   # provide variables to chroot system
1159   CHROOT_VARIABLES="/var/cache/grml-debootstrap/variables_${SHORT_TARGET}"
1160   touch $CHROOT_VARIABLES
1161   chmod 600 $CHROOT_VARIABLES # make sure nobody except root can read it
1162   echo "# Configuration of ${PN}"                              > $CHROOT_VARIABLES
1163   [ -n "$ARCH" ]                && echo "ARCH=\"$ARCH\""                               >> $CHROOT_VARIABLES
1164   [ -n "$BACKPORTREPOS" ]       && echo "BACKPORTREPOS=\"$BACKPORTREPOS\""             >> $CHROOT_VARIABLES
1165   [ -n "$CHROOT_SCRIPTS" ]      && echo "CHROOT_SCRIPTS=\"$CHROOT_SCRIPTS\""           >> $CHROOT_VARIABLES
1166   [ -n "$CONFFILES" ]           && echo "CONFFILES=\"$CONFFILES\""                     >> $CHROOT_VARIABLES
1167   [ -n "$DEBCONF" ]             && echo "DEBCONF=\"$DEBCONF\""                         >> $CHROOT_VARIABLES
1168   [ -n "$DEBIAN_FRONTEND" ]     && echo "DEBIAN_FRONTEND=\"$DEBIAN_FRONTEND\""         >> $CHROOT_VARIABLES
1169   [ -n "$DEBOOTSTRAP" ]         && echo "DEBOOTSTRAP=\"$DEBOOTSTRAP\""                 >> $CHROOT_VARIABLES
1170   [ -n "$DEFAULT_LOCALES" ]     && echo "DEFAULT_LOCALES=\"$DEFAULT_LOCALES\""         >> $CHROOT_VARIABLES
1171   [ -n "$DEFAULT_LANGUAGE" ]    && echo "DEFAULT_LANGUAGE=\"$DEFAULT_LANGUAGE\""       >> $CHROOT_VARIABLES
1172   [ -n "$EXTRAPACKAGES" ]       && echo "EXTRAPACKAGES=\"$EXTRAPACKAGES\""             >> $CHROOT_VARIABLES
1173   [ -n "$FALLBACK_MIRROR" ]     && echo "FALLBACK_MIRROR=\"$FALLBACK_MIRROR\""         >> $CHROOT_VARIABLES
1174   [ -n "$FORCE" ]               && echo "FORCE=\"$FORCE\""                             >> $CHROOT_VARIABLES
1175   [ -n "$GRMLREPOS" ]           && echo "GRMLREPOS=\"$GRMLREPOS\""                     >> $CHROOT_VARIABLES
1176   [ -n "$GRUB" ]                && echo "GRUB=\"$GRUB\""                               >> $CHROOT_VARIABLES
1177   [ -n "$HOSTNAME" ]            && echo "HOSTNAME=\"$HOSTNAME\""                       >> $CHROOT_VARIABLES
1178   [ -n "$INITRD" ]              && echo "INITRD=\"$INITRD\""                           >> $CHROOT_VARIABLES
1179   [ -n "$INSTALL_NOTES" ]       && echo "INSTALL_NOTES=\"$INSTALL_NOTES\""             >> $CHROOT_VARIABLES
1180   [ -n "$ISODIR" ]              && echo "ISODIR=\"$ISO\""                              >> $CHROOT_VARIABLES
1181   [ -n "$ISO" ]                 && echo "ISO=\"$ISO\""                                 >> $CHROOT_VARIABLES
1182   [ -n "$KEEP_SRC_LIST" ]       && echo "KEEP_SRC_LIST=\"$KEEP_SRC_LIST\""             >> $CHROOT_VARIABLES
1183   [ -n "$LOCALES" ]             && echo "LOCALES=\"$LOCALES\""                         >> $CHROOT_VARIABLES
1184   [ -n "$MIRROR" ]              && echo "MIRROR=\"$MIRROR\""                           >> $CHROOT_VARIABLES
1185   [ -n "$MKFS" ]                && echo "MKFS=\"$MKFS\""                               >> $CHROOT_VARIABLES
1186   [ -n "$NOPASSWORD" ]          && echo "NOPASSWORD=\"true\""                          >> $CHROOT_VARIABLES
1187   [ -n "$NOKERNEL" ]            && echo "NOKERNEL=\"true\""                            >> $CHROOT_VARIABLES
1188   [ -n "$PACKAGES" ]            && echo "PACKAGES=\"$PACKAGES\""                       >> $CHROOT_VARIABLES
1189   [ -n "$PRE_SCRIPTS" ]         && echo "PRE_SCRIPTS=\"$PRE_SCRIPTS\""                 >> $CHROOT_VARIABLES
1190   [ -n "$RECONFIGURE" ]         && echo "RECONFIGURE=\"$RECONFIGURE\""                 >> $CHROOT_VARIABLES
1191   [ -n "$RELEASE" ]             && echo "RELEASE=\"$RELEASE\""                         >> $CHROOT_VARIABLES
1192   [ -n "$RM_APTCACHE" ]         && echo "RM_APTCACHE=\"$RM_APTCACHE\""                 >> $CHROOT_VARIABLES
1193   [ -n "$ROOTPASSWORD" ]        && echo "ROOTPASSWORD=\"$ROOTPASSWORD\""               >> $CHROOT_VARIABLES
1194   [ -n "$SCRIPTS" ]             && echo "SCRIPTS=\"$SCRIPTS\""                         >> $CHROOT_VARIABLES
1195   [ -n "$SECURE" ]              && echo "SECURE=\"$SECURE\""                           >> $CHROOT_VARIABLES
1196   [ -n "$SELECTED_PARTITIONS" ] && echo "SELECTED_PARTITIONS=\"$SELECTED_PARTITIONS\"" >> $CHROOT_VARIABLES
1197   [ -n "$TARGET" ]              && echo "TARGET=\"$TARGET\""                           >> $CHROOT_VARIABLES
1198   [ -n "$UPGRADE_SYSTEM" ]      && echo "UPGRADE_SYSTEM=\"$UPGRADE_SYSTEM\""           >> $CHROOT_VARIABLES
1199   [ -n "$TARGET_UUID" ]         && echo "TARGET_UUID=\"$TARGET_UUID\""                 >> $CHROOT_VARIABLES
1200   [ -n "$TIMEZONE" ]            && echo "TIMEZONE=\"$TIMEZONE\""                       >> $CHROOT_VARIABLES
1201   [ -n "$TUNE2FS" ]             && echo "TUNE2FS=\"$TUNE2FS\""                         >> $CHROOT_VARIABLES
1202   [ -n "$VMSIZE" ]              && echo "VMSIZE=\"$VMSIZE\""                           >> $CHROOT_VARIABLES
1203
1204   cp $VERBOSE $CONFFILES/chroot-script $MNTPOINT/bin/chroot-script
1205   chmod 755 $MNTPOINT/bin/chroot-script
1206   [ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/
1207
1208   # make sure we have our files for later use via chroot-script
1209   cp $VERBOSE $CONFFILES/config    $MNTPOINT/etc/debootstrap/
1210   # make sure we adjust the configuration variables accordingly:
1211   sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" $MNTPOINT/etc/debootstrap/config
1212   sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#"    $MNTPOINT/etc/debootstrap/config
1213   sed -i "s#GRUB=.*#GRUB=\"$GRUB\"#"          $MNTPOINT/etc/debootstrap/config
1214
1215   # install notes:
1216   if [ -n "$INSTALL_NOTES" ] ; then
1217      [ -r "$INSTALL_NOTES" ] && cp "$INSTALL_NOTES" $MNTPOINT/etc/debootstrap/
1218   fi
1219
1220   # package selection:
1221   cp $VERBOSE ${_opt_packages:-$CONFFILES/packages} \
1222     $MNTPOINT/etc/debootstrap/packages
1223
1224   # debconf preseeding:
1225   _opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections}
1226   [ -f $_opt_debconf -a "$DEBCONF" = 'yes' ] && \
1227     cp $VERBOSE $_opt_debconf $MNTPOINT/etc/debootstrap/debconf-selections
1228
1229   # copy scripts that should be executed inside the chroot:
1230   _opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/}
1231   [ -d $_opt_chroot_scripts -a "$CHROOT_SCRIPTS" = 'yes' ] && {
1232     mkdir -p $MNTPOINT/etc/debootstrap/chroot-scripts
1233     cp -a $VERBOSE $_opt_chroot_scripts/* $MNTPOINT/etc/debootstrap/chroot-scripts/
1234   }
1235
1236   # notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead!
1237   cp $VERBOSE $CHROOT_VARIABLES  $MNTPOINT/etc/debootstrap/variables
1238
1239   cp $VERBOSE -a -L $CONFFILES/extrapackages/ $MNTPOINT/etc/debootstrap/
1240
1241   # make sure we can access network [relevant for cdebootstrap]
1242   [ -f "$MNTPOINT/etc/resolv.conf" ] || cp $VERBOSE /etc/resolv.conf $MNTPOINT/etc/resolv.conf
1243
1244   # provide system's /etc/hosts to the target:
1245   if ! [ -f "$MNTPOINT/etc/hosts" ] ; then
1246      cp $VERBOSE /etc/hosts $MNTPOINT/etc/hosts
1247   fi
1248
1249   # setup default locales
1250   [ -n "$LOCALES" ] && cp $VERBOSE $CONFFILES/locale.gen  $MNTPOINT/etc/locale.gen
1251
1252   # MAKEDEV is just a forking bomb crap, let's do it on our own instead :)
1253   ( cd $MNTPOINT/dev && tar zxf /etc/debootstrap/devices.tar.gz )
1254
1255   # copy any existing files to chroot
1256   [ -d $CONFFILES/bin   ] && cp $VERBOSE -a -L $CONFFILES/bin/*   $MNTPOINT/bin/
1257   [ -d $CONFFILES/boot  ] && cp $VERBOSE -a -L $CONFFILES/boot/*  $MNTPOINT/boot/
1258   [ -d $CONFFILES/etc   ] && cp $VERBOSE -a -L $CONFFILES/etc/*   $MNTPOINT/etc/
1259   [ -d $CONFFILES/sbin  ] && cp $VERBOSE -a -L $CONFFILES/sbin/*  $MNTPOINT/sbin/
1260   [ -d $CONFFILES/share ] && cp $VERBOSE -a -L $CONFFILES/share/* $MNTPOINT/share/
1261   [ -d $CONFFILES/usr   ] && cp $VERBOSE -a -L $CONFFILES/usr/*   $MNTPOINT/usr/
1262   [ -d $CONFFILES/var   ] && cp $VERBOSE -a -L $CONFFILES/var/*   $MNTPOINT/var/
1263
1264   # network setup
1265   DEFAULT_INTERFACES="# /etc/network/interfaces - generated by grml-debootstrap
1266
1267 # Include files from /etc/network/interfaces.d when using
1268 # ifupdown v0.7.44 or newer:
1269 #source-directory /etc/network/interfaces.d
1270
1271 auto lo
1272 iface lo inet loopback
1273
1274 allow-hotplug eth0
1275 iface eth0 inet dhcp
1276 "
1277
1278   if [ -n "$NOINTERFACES" ] ; then
1279     einfo "Not installing /etc/network/interfaces as requested via --nointerfaces option" ; eend 0
1280   elif [ -n "$DEFAULT_INTERFACES" ] ; then
1281     einfo "Installing default /etc/network/interfaces as requested via --defaultinterfaces options."
1282     echo "$DEFAULT_INTERFACES" > "${MNTPOINT}/etc/network/interfaces"
1283     eend $?
1284   elif [ -n "$VIRTUAL" ] ; then
1285     einfo "Setting up Virtual Machine, installing default /etc/network/interfaces"
1286     echo "$DEFAULT_INTERFACES" > "${MNTPOINT}/etc/network/interfaces"
1287     eend $?
1288   elif [ -r /etc/network/interfaces ] ; then
1289     einfo "Copying /etc/network/interfaces from host to target system"
1290     cp $VERBOSE /etc/network/interfaces "${MNTPOINT}/etc/network/interfaces"
1291     eend $?
1292   else
1293     ewarn "Couldn't read /etc/network/interfaces, installing default /etc/network/interfaces"
1294     echo "$DEFAULT_INTERFACES" > "${MNTPOINT}/etc/network/interfaces"
1295     eend $?
1296   fi
1297
1298   # install config file providing some example entries
1299   if [ -r /etc/network/interfaces.examples -a ! -r "$MNTPOINT/etc/network/interfaces.examples" ] ; then
1300      cp /etc/network/interfaces.examples "$MNTPOINT/etc/network/interfaces.examples"
1301   fi
1302
1303   eend 0
1304 }
1305 # }}}
1306
1307 # execute all scripts in /etc/debootstrap/pre-scripts/ {{{
1308 execute_pre_scripts() {
1309    # make sure we have $MNTPOINT available for our scripts
1310    export MNTPOINT
1311    if [ -d "$_opt_pre_scripts" ] || [ "$PRE_SCRIPTS" = 'yes' ] ; then
1312       [ -d "$_opt_pre_scripts" ] && pre_scripts="$_opt_pre_scripts" || pre_scripts="$CONFFILES/pre-scripts/"
1313       for script in ${pre_scripts}/* ; do
1314          if [ -x "$script" ] ; then
1315             einfo "Executing pre-script $script"
1316             $script ; eend $?
1317          fi
1318       done
1319    fi
1320 }
1321 # }}}
1322
1323 # execute all scripts in /etc/debootstrap/scripts/ {{{
1324 execute_scripts() {
1325    # make sure we have $MNTPOINT available for our scripts
1326    export MNTPOINT
1327    if [ -d "$_opt_scripts" ] || [ "$SCRIPTS" = 'yes' ] ; then
1328       [ -d "$_opt_scripts" ] && scripts="$_opt_scripts" || scripts="$CONFFILES/scripts/"
1329       for script in ${scripts}/* ; do
1330          if [ -x "$script" ] ; then
1331             einfo "Executing script $script"
1332             $script ; eend $?
1333          fi
1334       done
1335    fi
1336 }
1337 # }}}
1338
1339 # execute chroot-script {{{
1340 chrootscript() {
1341   if ! [ -r "$MNTPOINT/bin/chroot-script" ] ; then
1342     mount_target
1343   fi
1344
1345   if ! [ -x "$MNTPOINT/bin/chroot-script" ] ; then
1346     eerror "Fatal: $MNTPOINT/bin/chroot-script could not be found."
1347     eend 1
1348   else
1349     einfo "Executing chroot-script now"
1350     mount --bind /dev "$MNTPOINT"/dev
1351     if [ "$DEBUG" = "true" ] ; then
1352       chroot "$MNTPOINT" /bin/sh -x /bin/chroot-script ; RC=$?
1353     else
1354       chroot "$MNTPOINT" /bin/chroot-script ; RC=$?
1355     fi
1356     umount "$MNTPOINT"/dev
1357     eend $RC
1358   fi
1359
1360   # finally get rid of chroot-script again, there's no good reason to
1361   # keep it on the installed system
1362   if grep -q GRML_CHROOT_SCRIPT_MARKER "${MNTPOINT}/bin/chroot-script" ; then
1363     einfo "Removing chroot-script again"
1364     rm -f "${MNTPOINT}/bin/chroot-script"
1365     eend $?
1366   else
1367     einfo "Keeping chroot-script as string GRML_CHROOT_SCRIPT_MARKER could not be found"
1368     eend 0
1369   fi
1370 }
1371 # }}}
1372
1373 # unmount $MNTPOINT {{{
1374 umount_chroot() {
1375
1376   # display installation notes:
1377   if [ -n "$INSTALL_NOTES" ] ; then
1378      [ -r "${MNTPOINT}/${INSTALL_NOTES}" ] && cat "${MNTPOINT}/${INSTALL_NOTES}"
1379   fi
1380
1381   if [ -n "$ISODIR" ] ; then
1382      if grep -q "$ISODIR" /proc/mounts ; then
1383         einfo "Unmount $MNTPOINT/$ISODIR"
1384         umount "$MNTPOINT/$ISODIR"
1385         eend $?
1386      fi
1387   fi
1388
1389   if grep -q "$MNTPOINT" /proc/mounts ; then
1390      if [ -n "$PARTITION" ] ; then
1391         einfo "Unmount $MNTPOINT"
1392         umount $MNTPOINT
1393         eend $?
1394      fi
1395   fi
1396 }
1397 # }}}
1398
1399 # execute filesystem check {{{
1400 fscktool() {
1401  if [ -n "$VIRTUAL" ] ; then
1402    einfo "Skipping filesystem check because we deploy a virtual machine."
1403    return 0
1404  fi
1405
1406  if [ "$FSCK" = 'yes' ] ; then
1407    [ -n "$FSCKTOOL" ] || FSCKTOOL="fsck.${MKFS#mkfs.}"
1408    einfo "Checking filesystem on $TARGET using $FSCKTOOL"
1409    $FSCKTOOL $TARGET
1410    eend $?
1411  fi
1412 }
1413 # }}}
1414
1415 # now execute all the functions {{{
1416 for i in prepare_vm mkfs tunefs mount_target debootstrap_system \
1417          preparechroot execute_pre_scripts chrootscript execute_scripts \
1418          umount_chroot finalize_vm fscktool ; do
1419     if stage "${i}" ; then
1420        $i && ( stage "${i}" done && rm -f "${STAGES}/${i}" ) || bailout 2 "$i"
1421     fi
1422 done
1423
1424 cleanup
1425 # }}}
1426
1427 # end dialog of autoinstallation {{{
1428 if [ -n "$AUTOINSTALL" ] ; then
1429    if dialog --title "${PN}" --pause "Finished execution of ${PN}.
1430 Automatically rebooting in 10 seconds.
1431
1432 Choose Cancel to skip rebooting." 10 60 10 ; then
1433      noeject noprompt reboot
1434   fi
1435 else
1436    einfo "Finished execution of ${PN}. Enjoy your Debian system." ; eend 0
1437 fi
1438 # }}}
1439
1440 ## END OF FILE #################################################################
1441 # vim: ai tw=100 expandtab foldmethod=marker shiftwidth=2