Merge remote-tracking branch 'origin/pr/190'
[grml-debootstrap.git] / chroot-script
1 #!/bin/bash
2 # Filename:      /etc/debootstrap/chroot-script
3 # Purpose:       script executed in chroot when installing Debian via grml-debootstrap
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see https://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8 # GRML_CHROOT_SCRIPT_MARKER - do not remove this line unless you want to keep
9 # this script as /bin/chroot-script on your new installed system
10 ################################################################################
11
12 # error_handler {{{
13 if [ "$REPORT_TRAP_ERR" = "yes" ] || [ "$FAIL_TRAP_ERR" = "yes" ]; then
14    set -E
15    set -o pipefail
16    trap "error_handler" ERR
17 fi
18 # }}}
19
20 # shellcheck disable=SC1091
21 . /etc/debootstrap/config    || exit 1
22 # shellcheck disable=SC1091
23 . /etc/debootstrap/variables || exit 1
24
25 [ -r /proc/1 ] || mount -t proc none /proc
26 [ -r /sys/kernel ] || mount -t sysfs none /sys
27
28 # variable checks {{{
29
30 # use aptitude only if it's available
31 if [ -x /usr/bin/aptitude ] ; then
32    APTUPDATE="aptitude update $DPKG_OPTIONS"
33    # Debian ISOs do not contain signed Release files
34    if [ -n "$ISO" ] ; then
35       APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
36       APTUPGRADE="aptitude -y --allow-untrusted safe-upgrade $DPKG_OPTIONS"
37    else
38       APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
39       APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
40    fi
41 else
42    APTINSTALL="apt-get -y --no-install-recommends install $DPKG_OPTIONS"
43    APTUPDATE="apt-get update $DPKG_OPTIONS"
44    APTUPGRADE="apt-get -y upgrade $DPKG_OPTIONS"
45 fi
46
47 if [ -z "$STAGES" ] ; then
48    STAGES='/etc/debootstrap/stages'
49    [ -d "$STAGES" ] || mkdir -p "$STAGES"
50 fi
51 # }}}
52
53 # helper functions {{{
54 stage() {
55   if [ -n "$2" ] ; then
56      echo "$2" > "$STAGES/$1"
57      return 0
58   elif grep -q 'done' "$STAGES/$1" 2>/dev/null ; then
59      echo "   [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
60      return 1
61   fi
62   echo "   Executing stage ${1}"
63   return 0
64 }
65
66 askpass() {
67   # read -s emulation for dash. result is in $resp.
68   set -o noglob
69   [ -t 0 ] && stty -echo
70   read -r resp
71   [ -t 0 ] && stty echo
72   set +o noglob
73 }
74 # }}}
75
76 # define chroot mirror {{{
77 chrootmirror() {
78   if [ "$KEEP_SRC_LIST" = "yes" ] ; then
79     echo "KEEP_SRC_LIST has been enabled, skipping chrootmirror stage."
80     return
81   fi
82
83   if [ -z "$COMPONENTS" ] ; then
84     COMPONENTS='main'
85   fi
86   echo "Using repository components $COMPONENTS"
87
88   if [ -n "$ISO" ] ; then
89     echo "Adjusting sources.list for ISO (${ISO})."
90     echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
91
92     if [ -n "$MIRROR" ] ; then
93       echo "Adding mirror entry (${MIRROR}) to sources.list."
94       echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
95     fi
96   else
97     if [ -n "$MIRROR" ] ; then
98       echo "Adjusting sources.list for mirror (${MIRROR})."
99       echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
100     fi
101   fi
102
103   # LTS support
104   case "$RELEASE" in
105     squeeze)
106       if [ -n "$MIRROR" ] ; then
107         echo "Release matching $RELEASE - enabling LTS support in sources.list"
108         echo "deb $MIRROR ${RELEASE}-lts $COMPONENTS" >> /etc/apt/sources.list
109       fi
110       ;;
111   esac
112
113   # add security.debian.org:
114   case "$RELEASE" in
115     unstable|sid|lenny) ;;  # no security pool available
116     squeeze|wheezy|jessie|stretch|buster)
117       echo "Adding security.debian.org to sources.list."
118       echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
119       ;;
120     *)
121       # bullseye and newer releases use a different repository layout, see
122       # https://lists.debian.org/debian-devel-announce/2019/07/msg00004.html
123       echo "Adding security.debian.org/debian-security to sources.list."
124       echo "deb http://security.debian.org/debian-security ${RELEASE}-security $COMPONENTS" >> /etc/apt/sources.list
125       ;;
126   esac
127 }
128 # }}}
129
130 # remove local chroot mirror {{{
131 remove_chrootmirror() {
132   if [ "$KEEP_SRC_LIST" = "yes" ] ; then
133     echo "KEEP_SRC_LIST has been enabled, skipping remove_chrootmirror stage."
134     return
135   fi
136
137   if [ -n "$ISO" ] ; then
138     echo "Removing ISO (${ISO}) from sources.list."
139     TMP_ISO="${ISO//\//\\\/}"
140     sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
141   else
142     if [ -n "$MIRROR" ] && echo "$MIRROR" | grep -q 'file:' ; then
143       echo "Removing local mirror (${MIRROR}) from sources.list."
144       TMP_MIRROR="${MIRROR//\//\\\/}"
145       sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
146       echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
147       echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
148     fi
149   fi
150 }
151 # }}}
152
153 # set up grml repository {{{
154 grmlrepos() {
155   if [ -z "$GRMLREPOS" ] ; then
156     return 0
157   fi
158
159   # user might have provided their own apt sources configuration
160   if [ -r /etc/apt/sources.list.d/grml.list ] ; then
161     echo "File /etc/apt/sources.list.d/grml.list exists already, not modifying."
162   else
163     echo "Setting up /etc/apt/sources.list.d/grml.list."
164     cat > /etc/apt/sources.list.d/grml.list << EOF
165 # grml: stable repository:
166   deb     [signed-by=/usr/share/keyrings/grml-archive-keyring.gpg] http://deb.grml.org/ grml-stable main
167   deb-src [signed-by=/usr/share/keyrings/grml-archive-keyring.gpg] http://deb.grml.org/ grml-stable main
168
169 # grml: testing/development repository:
170   deb     [signed-by=/usr/share/keyrings/grml-archive-keyring.gpg] http://deb.grml.org/ grml-testing main
171   deb-src [signed-by=/usr/share/keyrings/grml-archive-keyring.gpg] http://deb.grml.org/ grml-testing main
172 EOF
173   fi
174
175   # make sure we install packages from Grml's pool only if not available from Debian
176   if [ -r /etc/apt/preferences.d/grml.pref ] ; then
177     echo "File /etc/apt/preferences.d/grml.pref exists already, not modifying."
178   else
179     echo "Setting up /etc/apt/preferences.d/grml.pref."
180     cat > /etc/apt/preferences.d/grml.pref << EOF
181 Explanation: use Grml repository only after Debian ones
182 Package: *
183 Pin: origin deb.grml.org
184 Pin-Priority: 100
185 EOF
186   fi
187
188   apt-get update -o Acquire::AllowInsecureRepositories=1
189   apt-get -y --allow-unauthenticated install grml-debian-keyring
190   apt-get update
191
192   if [ "$(dpkg-query -f "\${db:Status-Status} \${db:Status-Eflag}" -W grml-debian-keyring 2>/dev/null)" != 'installed ok' ]; then
193     echo "Error: installation of grml-debian-keyring failed." >&2
194     exit 1
195   fi
196 }
197 # }}}
198
199 # feature to provide Debian backports repos {{{
200 backportrepos() {
201   if [ -n "$BACKPORTREPOS" ] ; then
202     cat >> /etc/apt/sources.list.d/backports.list << EOF
203 # debian backports: ${RELEASE}-backports repository:
204 deb     ${MIRROR} ${RELEASE}-backports main
205 deb-src ${MIRROR} ${RELEASE}-backports main
206 EOF
207   fi
208 }
209 # }}}
210
211 # set up kernel-img.conf {{{
212 kernelimg_conf() {
213   if ! [ -r /etc/kernel-img.conf ] ; then
214      echo "Setting up /etc/kernel-img.conf"
215      cat > /etc/kernel-img.conf << EOF
216 # Kernel Image management overrides
217 # See kernel-img.conf(5) for details
218 do_initrd = Yes
219 do_symlinks = Yes
220 EOF
221   fi
222 }
223 # }}}
224
225 # make sure services do not start up {{{
226 install_policy_rcd() {
227   if ! [ -r /usr/sbin/policy-rc.d ] ; then
228      export POLICYRCD=1
229      cat > /usr/sbin/policy-rc.d << EOF
230 #!/bin/sh
231 exit 101
232 EOF
233      chmod 775 /usr/sbin/policy-rc.d
234   fi
235 }
236 # }}}
237
238 # make sure we have an up2date system {{{
239 upgrade_system() {
240   if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
241     echo "Running update + upgrade"
242     $APTUPDATE
243     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
244   else
245     echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
246   fi
247 }
248
249 # }}}
250 # remove now useless apt cache {{{
251 remove_apt_cache() {
252   if [ "$RM_APTCACHE" = 'yes' ] ; then
253     echo "Cleaning apt cache."
254     # shellcheck disable=SC2086
255     apt-get clean $DPKG_OPTIONS
256   else
257     echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
258   fi
259 }
260 # }}}
261
262 # install additional packages {{{
263 packages() {
264   # Pre-seed the debconf database with answers. Each question will be marked
265   # as seen to prevent debconf from asking the question interactively.
266   [ -f /etc/debootstrap/debconf-selections ] && {
267     echo "Preseeding the debconf database, some lines might be skipped..."
268     debconf-set-selections < /etc/debootstrap/debconf-selections
269   }
270
271   if [ "$PACKAGES" = 'yes' ] ; then
272      if ! [ -r /etc/debootstrap/packages ] ; then
273        echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
274        exit 1
275      else
276        $APTUPDATE
277        # shellcheck disable=SC2086,SC2046
278        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
279      fi
280   fi
281 }
282 # }}}
283
284 # install extra packages {{{
285 extrapackages() {
286     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
287         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
288         if [ -n "$PACKAGELIST" ]; then
289             # shellcheck disable=SC2086
290             dpkg -i $PACKAGELIST
291             # run apt again to resolve any deps
292             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
293         fi
294     fi
295 }
296 # }}}
297
298 # check if the specified Debian package exists
299 package_exists() {
300   output=$(apt-cache show "$1" 2>/dev/null)
301   [ -n "$output" ]
302   return $?
303 }
304
305
306 # determine the kernel version postfix
307 get_kernel_version() {
308   # do not override $KERNEL if set via config file
309   if [ -n "$KERNEL" ] ; then
310     echo "$KERNEL"
311     return 0
312   fi
313
314   local KARCH
315
316   # shellcheck disable=SC2153
317   case "$ARCH" in
318     i386)
319       case "$RELEASE" in
320         lenny|squeeze|wheezy) KARCH='686' ;;
321         # since jessie the linux-image-686 image doesn't exist any longer
322         *) KARCH='686-pae' ;;
323       esac
324       ;;
325     amd64)
326       KARCH='amd64'
327       ;;
328     *)
329       echo "Only i386 and amd64 are currently supported" >&2
330       return 1
331   esac
332
333   for KPREFIX in "" "2.6-" ; do  # iterate through the kernel prefixes,
334                                  # currently "" and "2.6-"
335     if package_exists linux-image-${KPREFIX}${KARCH} ; then
336       echo ${KPREFIX}${KARCH}
337       return 0
338     fi
339
340   done
341 }
342
343 # install kernel packages {{{
344 kernel() {
345   if [ -n "$NOKERNEL" ] ; then
346     echo "Skipping installation of kernel packages as requested via --nokernel"
347     return 0
348   fi
349
350   $APTUPDATE
351   KVER=$(get_kernel_version)
352   if [ -n "$KVER" ] ; then
353      # note: install busybox to be able to debug initramfs
354      KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free"
355      # only add firmware-linux if we have non-free as a component
356      if expr "$COMPONENTS" : '.*non-free' >/dev/null ; then
357        KERNELPACKAGES="$KERNELPACKAGES firmware-linux"
358      fi
359      # shellcheck disable=SC2086
360      DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
361   else
362      echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
363   fi
364 }
365 # }}}
366
367 # reconfigure packages {{{
368 reconfigure() {
369   if [ -n "$RECONFIGURE" ] ; then
370      for package in $RECONFIGURE ; do
371          if dpkg --list "$package" >/dev/null 2>&1 | grep -q '^ii' ; then
372            DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure "$package" || \
373            echo "Warning: $package does not exist, can not reconfigure it."
374          fi
375      done
376   fi
377 }
378 # }}}
379
380 # set password of user root {{{
381 passwords()
382 {
383   if [ -n "$NOPASSWORD" ] ; then
384     echo "Skip setting root password as requested."
385     return 0
386   fi
387
388   echo "Activating shadow passwords."
389   shadowconfig on
390
391   CHPASSWD_OPTION=
392   if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
393      CHPASSWD_OPTION='-m'
394   fi
395
396   if [ -n "$ROOTPASSWORD" ] ; then
397      echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
398      export ROOTPASSWORD=''
399   else
400     a='1'
401     b='2'
402      echo "Setting password for user root:"
403      while [ "$a" != "$b" ] ; do
404        printf "Enter new UNIX password for user root: "
405        askpass
406        a="$resp"
407        unset resp
408        echo
409        printf "Retype new UNIX password for user root: "
410        askpass
411        b="$resp"
412        unset resp
413        echo
414        if [ "$a" != "$b" ] ; then
415          echo "Sorry, passwords do not match. Retry."
416          a='1'
417          b='2'
418        else
419          echo root:"$a" | chpasswd $CHPASSWD_OPTION
420          unset a
421          unset b
422        fi
423      done
424   fi
425 }
426 # }}}
427
428 # set up /etc/hosts {{{
429 hosts() {
430   if ! [ -f /etc/hosts ] ; then
431      cat > /etc/hosts << EOF
432 127.0.0.1       localhost
433 ::1             localhost ip6-localhost ip6-loopback
434 ff02::1         ip6-allnodes
435 ff02::2         ip6-allrouters
436 EOF
437   fi
438 }
439 # }}}
440
441 # set default locales {{{
442 default_locales() {
443   if [ -n "$DEFAULT_LOCALES" ] ; then
444     if ! [ -x /usr/sbin/update-locale ] ; then
445       echo "Warning: update-locale executable not available (no locales package installed?)"
446       echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
447       return 0
448     fi
449
450     /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
451   fi
452 }
453 # }}}
454
455 # adjust timezone {{{
456 timezone() {
457   if [ -n "$TIMEZONE" ] ; then
458     echo "Adjusting /etc/localtime"
459     ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
460
461     echo "Setting /etc/timezone to $TIMEZONE"
462     printf '%s\n' "$TIMEZONE"  > /etc/timezone
463
464   fi
465 }
466 # }}}
467
468 # helper function for fstab() {{{
469 createfstab(){
470   echo "Setting up /etc/fstab"
471   cat > /etc/fstab <<EOF
472 # /etc/fstab - created by grml-debootstrap on $(date)
473 # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
474 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
475 #
476 # After editing this file, run 'systemctl daemon-reload' to update systemd
477 # units generated from this file.
478 #
479 EOF
480
481   if [ -n "$TARGET_UUID" ] ; then
482     local rootfs_mount_options=",errors=remount-ro"
483     case "${FILESYSTEM}" in
484       f2fs)
485         # errors=remount-ro is unsupported, see https://github.com/grml/grml-debootstrap/issues/163
486         rootfs_mount_options=""
487         ;;
488     esac
489
490     echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults${rootfs_mount_options} 0   1" >> /etc/fstab
491   else
492     echo "Warning: couldn't identify target UUID for rootfs, your /etc/fstab might be incomplete."
493   fi
494
495 if [ -n "$EFI" ] ; then
496   # shellcheck disable=SC2086
497   echo "UUID=$(blkid -o value -s UUID $EFI)  /boot/efi       vfat    umask=0077      0       1" >> /etc/fstab
498 fi
499
500 cat >> /etc/fstab << EOF
501 proc           /proc        proc    defaults                      0   0
502 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
503 # some other examples:
504 # /dev/sda2       none         swap    sw,pri=0             0   0
505 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
506 # //1.2.3.4/pub   /smb/pub     cifs    user,noauto,uid=grml,gid=grml 0 0
507 # linux:/pub      /beer        nfs     defaults             0  0
508 # tmpfs           /tmp         tmpfs   size=300M            0  0
509 # /dev/sda5       none         swap    sw                   0  0
510 EOF
511 }
512 # }}}
513
514 # generate /etc/fstab {{{
515 fstab() {
516   # set up /etc/fstab if file is not present (cdebootstrap)
517   if [ ! -f /etc/fstab  ] ; then
518      createfstab
519   fi
520
521   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
522   if grep -q UNCONFIGURED /etc/fstab ; then
523      createfstab
524   fi
525 }
526 # }}}
527
528 # set up hostname {{{
529 hostname() {
530   if [ -n "$HOSTNAME" ] ; then
531      echo "Setting hostname to ${HOSTNAME}."
532      echo "$HOSTNAME" > /etc/hostname
533
534      # adjust postfix configuration
535      if [ -r /etc/postfix/main.cf ] ; then
536         # adjust hostname related options:
537         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
538
539         # listen on loopback interface only:
540         sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
541         grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
542      fi
543      if [ -r /etc/mailname ] ; then
544         # adjust /etc/mailname
545         local etc_mail_domain
546         etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
547         case "$HOSTNAME" in
548           *.*)
549             local mailname="$HOSTNAME"
550             ;;
551           *)
552             local mailname="${HOSTNAME}.${etc_mail_domain}"
553             ;;
554         esac
555         echo "Setting mailname to ${mailname}"
556         echo "$mailname" > /etc/mailname
557      fi
558   fi
559 }
560 # }}}
561
562 # generate initrd/initramfs {{{
563 initrd() {
564   # assume the first available kernel as our main kernel
565   # shellcheck disable=SC2012
566   KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
567   if [ -z "$KERNELIMG" ] ; then
568      echo 'No kernel image found, skipping initrd stuff.'>&2
569      return
570   fi
571
572   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
573
574   # generate initrd
575   if [ -n "$INITRD" ] ; then
576      echo "Generating initrd."
577      update-initramfs -c -t -k "$KERNELVER"
578   fi
579 }
580 # }}}
581
582 efi_setup() {
583   if [ -z "$EFI" ] ; then
584     return 0
585   fi
586
587   if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
588     echo "Notice: efi option set but no efibootmgr package, installing it therefore."
589     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
590   fi
591
592   mkdir -p /boot/efi
593   echo "Mounting $EFI on /boot/efi"
594   mount "$EFI" /boot/efi || return 1
595
596   # if efivarfs kernel module is loaded, but efivars isn't,
597   # then we need to mount efivarfs for efibootmgr usage
598   if ! ls /sys/firmware/efi/efivars/* &>/dev/null ; then
599     echo "Mounting efivarfs on /sys/firmware/efi/efivars"
600     mount -t efivarfs efivarfs /sys/firmware/efi/efivars
601   fi
602
603   echo "Invoking efibootmgr"
604   efibootmgr || return 1
605 }
606
607 # grub configuration/installation {{{
608 grub_install() {
609
610   if [ -z "$GRUB" ] ; then
611     echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
612     return 0
613   fi
614
615   efi_setup || return 1
616
617   if [ -n "$EFI" ] ; then
618     GRUB_PACKAGE=grub-efi-amd64
619   else
620     GRUB_PACKAGE=grub-pc
621   fi
622
623   # make sure this is pre-defined so we have sane settings for automated
624   # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
625   echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
626   echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
627
628   if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
629     echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
630     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
631   fi
632
633   if ! [ -x "$(command -v grub-install)" ] ; then
634      echo "Error: grub-install not available. (Error while installing grub package?)" >&2
635      return 1
636   fi
637
638   if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
639      for device in $SELECTED_PARTITIONS ; do
640         GRUB="${device%%[0-9]}"
641         echo "Installing grub on ${GRUB}:"
642         if ! grub-install --no-floppy "$GRUB" ; then
643           echo "Error: failed to execute 'grub-install --no-floppy $GRUB'." >&2
644           exit 1
645         fi
646
647      done
648      rm -f /boot/grub/device.map
649   else
650      echo "Installing grub on ${GRUB}:"
651      case "$RELEASE" in
652        lenny|squeeze|wheezy)
653          local grub_dev
654          grub_dev="$(readlink -f "${GRUB}")"
655          if ! grub-install --no-floppy "${grub_dev}" ; then
656            echo "Error: failed to execute 'grub-install --no-floppy ${grub_dev}'." >&2
657            exit 1
658          fi
659          rm -f /boot/grub/device.map
660          ;;
661        *)
662          echo "(hd0) ${GRUB}" > /boot/grub/device.map
663          if ! grub-install "(hd0)" ; then
664            echo "Error: failed to execute 'grub-install (hd0)'." >&2
665            exit 1
666          fi
667          rm /boot/grub/device.map
668          ;;
669      esac
670   fi
671
672   echo "Adjusting grub configuration for use on ${GRUB}."
673
674   # finally install grub
675   if [ -x /usr/sbin/update-grub ] ; then
676      UPDATEGRUB='/usr/sbin/update-grub'
677   elif [ -x /sbin/update-grub ] ; then
678      UPDATEGRUB='/sbin/update-grub'
679   else
680     echo "Error: update-grub not available, can not execute it." >&2
681     return 1
682   fi
683
684   if [ -n "${BOOT_APPEND}" ] ; then
685     echo "Adding BOOT_APPEND configuration ['${BOOT_APPEND}'] to /etc/default/grub."
686     sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" /etc/default/grub
687   fi
688
689   mountpoint /boot/efi &>/dev/null && umount /boot/efi
690
691   $UPDATEGRUB
692 }
693 # }}}
694
695 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
696 custom_scripts() {
697   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
698
699   for script in /etc/debootstrap/chroot-scripts/* ; do
700       echo "Executing script $script"
701       $script && echo "done" || echo "failed"
702   done
703 }
704 # }}}
705
706 # make sure we don't have any running processes left {{{
707 services() {
708   for service in ssh mdadm mdadm-raid ; do
709     if [ -x /etc/init.d/"$service" ] ; then
710        /etc/init.d/"$service" stop || true
711     fi
712   done
713 }
714 # }}}
715
716 # unmount /proc and make sure nothing is left {{{
717 finalize() {
718   # make sure we don't leave any sensible data
719   rm -f /etc/debootstrap/variables
720
721   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
722
723   umount /sys/firmware/efi/efivars &>/dev/null || true
724
725   umount /sys >/dev/null 2>/dev/null || true
726   umount /proc >/dev/null 2>/dev/null || true
727 }
728 # }}}
729
730 # signal handler {{{
731 signal_handler() {
732   finalize
733   [ -n "$1" ] && EXIT="$1" || EXIT="1"
734   exit "$EXIT"
735 }
736 # }}}
737
738 # set signal handler {{{
739 trap signal_handler HUP INT QUIT TERM
740 # }}}
741
742 # execute the functions {{{
743
744  # always execute install_policy_rcd
745  install_policy_rcd
746
747  for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
748      kernel packages extrapackages reconfigure hosts \
749      default_locales timezone fstab hostname initrd grub_install passwords \
750      custom_scripts upgrade_system remove_apt_cache services \
751      remove_chrootmirror; do
752      if stage $i ; then
753        $i && stage $i 'done' || exit 1
754      fi
755   done
756   # always execute the finalize stage:
757   finalize
758 # }}}
759
760 # finally exit the chroot {{{
761   echo "Finished chroot installation, exiting."
762   exit 0
763 # }}}
764
765 ## END OF FILE #################################################################
766 # vim: ai tw=80 expandtab foldmethod=marker