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