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