Install only linux-image-cloud-amd64 in VMs for >=buster and amd64
[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
354   local kernel_version
355   kernel_version=$(get_kernel_version)
356
357   if [ -z "${kernel_version}" ] ; then
358     echo "Error: could not find a kernel for your system. Your system won't be able to boot itself!" >&2
359     exit 1
360   fi
361
362   # defaults (note: install busybox to be able to debug initramfs)
363   KERNELPACKAGES="linux-image-${kernel_version} linux-headers-${kernel_version} busybox firmware-linux-free"
364
365   # only add firmware-linux if we have non-free as a component
366   if expr "$COMPONENTS" : '.*non-free' >/dev/null ; then
367     KERNELPACKAGES="$KERNELPACKAGES firmware-linux"
368   fi
369
370   # when installing into a VM using buster or newer, install only
371   # linux-image-cloud-amd64 (and no headers and firmware packages)
372   if [ -n "${VMSIZE}" ] && [ "${ARCH:-}" = "amd64" ] ; then
373     case "${RELEASE}" in
374       lenny|squeeze|wheezy|jessie|stretch)
375         ;;
376       *)
377         echo "Note: installing into VM, choosing linux-image-cloud-amd64 kernel package"
378         KERNELPACKAGES="linux-image-cloud-amd64"
379         ;;
380     esac
381   fi
382
383   # shellcheck disable=SC2086
384   DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
385 }
386 # }}}
387
388 # reconfigure packages {{{
389 reconfigure() {
390   if [ -n "$RECONFIGURE" ] ; then
391      for package in $RECONFIGURE ; do
392          if dpkg --list "$package" >/dev/null 2>&1 | grep -q '^ii' ; then
393            DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure "$package" || \
394            echo "Warning: $package does not exist, can not reconfigure it."
395          fi
396      done
397   fi
398 }
399 # }}}
400
401 # set password of user root {{{
402 passwords()
403 {
404   if [ -n "$NOPASSWORD" ] ; then
405     echo "Skip setting root password as requested."
406     return 0
407   fi
408
409   echo "Activating shadow passwords."
410   shadowconfig on
411
412   CHPASSWD_OPTION=
413   if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
414      CHPASSWD_OPTION='-m'
415   fi
416
417   if [ -n "$ROOTPASSWORD" ] ; then
418      echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
419      export ROOTPASSWORD=''
420   else
421     a='1'
422     b='2'
423      echo "Setting password for user root:"
424      while [ "$a" != "$b" ] ; do
425        printf "Enter new UNIX password for user root: "
426        askpass
427        a="$resp"
428        unset resp
429        echo
430        printf "Retype new UNIX password for user root: "
431        askpass
432        b="$resp"
433        unset resp
434        echo
435        if [ "$a" != "$b" ] ; then
436          echo "Sorry, passwords do not match. Retry."
437          a='1'
438          b='2'
439        else
440          echo root:"$a" | chpasswd $CHPASSWD_OPTION
441          unset a
442          unset b
443        fi
444      done
445   fi
446 }
447 # }}}
448
449 # set up /etc/hosts {{{
450 hosts() {
451   if ! [ -f /etc/hosts ] ; then
452      cat > /etc/hosts << EOF
453 127.0.0.1       localhost
454 ::1             localhost ip6-localhost ip6-loopback
455 ff02::1         ip6-allnodes
456 ff02::2         ip6-allrouters
457 EOF
458   fi
459 }
460 # }}}
461
462 # set default locales {{{
463 default_locales() {
464   if [ -n "$DEFAULT_LOCALES" ] ; then
465     if ! [ -x /usr/sbin/update-locale ] ; then
466       echo "Warning: update-locale executable not available (no locales package installed?)"
467       echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
468       return 0
469     fi
470
471     /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
472   fi
473 }
474 # }}}
475
476 # adjust timezone {{{
477 timezone() {
478   if [ -n "$TIMEZONE" ] ; then
479     echo "Adjusting /etc/localtime"
480     ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
481
482     echo "Setting /etc/timezone to $TIMEZONE"
483     printf '%s\n' "$TIMEZONE"  > /etc/timezone
484
485   fi
486 }
487 # }}}
488
489 # helper function for fstab() {{{
490 createfstab(){
491   echo "Setting up /etc/fstab"
492   cat > /etc/fstab <<EOF
493 # /etc/fstab - created by grml-debootstrap on $(date)
494 # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
495 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
496 #
497 # After editing this file, run 'systemctl daemon-reload' to update systemd
498 # units generated from this file.
499 #
500 EOF
501
502   if [ -n "$TARGET_UUID" ] ; then
503     local rootfs_mount_options=",errors=remount-ro"
504     case "${FILESYSTEM}" in
505       f2fs)
506         # errors=remount-ro is unsupported, see https://github.com/grml/grml-debootstrap/issues/163
507         rootfs_mount_options=""
508         ;;
509     esac
510
511     echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults${rootfs_mount_options} 0   1" >> /etc/fstab
512   else
513     echo "Warning: couldn't identify target UUID for rootfs, your /etc/fstab might be incomplete."
514   fi
515
516 if [ -n "$EFI" ] ; then
517   # shellcheck disable=SC2086
518   echo "UUID=$(blkid -o value -s UUID $EFI)  /boot/efi       vfat    umask=0077      0       1" >> /etc/fstab
519 fi
520
521 cat >> /etc/fstab << EOF
522 proc           /proc        proc    defaults                      0   0
523 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
524 # some other examples:
525 # /dev/sda2       none         swap    sw,pri=0             0   0
526 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
527 # //1.2.3.4/pub   /smb/pub     cifs    user,noauto,uid=grml,gid=grml 0 0
528 # linux:/pub      /beer        nfs     defaults             0  0
529 # tmpfs           /tmp         tmpfs   size=300M            0  0
530 # /dev/sda5       none         swap    sw                   0  0
531 EOF
532 }
533 # }}}
534
535 # generate /etc/fstab {{{
536 fstab() {
537   # set up /etc/fstab if file is not present (cdebootstrap)
538   if [ ! -f /etc/fstab  ] ; then
539      createfstab
540   fi
541
542   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
543   if grep -q UNCONFIGURED /etc/fstab ; then
544      createfstab
545   fi
546 }
547 # }}}
548
549 # set up hostname {{{
550 hostname() {
551   if [ -n "$HOSTNAME" ] ; then
552      echo "Setting hostname to ${HOSTNAME}."
553      echo "$HOSTNAME" > /etc/hostname
554
555      # adjust postfix configuration
556      if [ -r /etc/postfix/main.cf ] ; then
557         # adjust hostname related options:
558         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
559
560         # listen on loopback interface only:
561         sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
562         grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
563      fi
564      if [ -r /etc/mailname ] ; then
565         # adjust /etc/mailname
566         local etc_mail_domain
567         etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
568         case "$HOSTNAME" in
569           *.*)
570             local mailname="$HOSTNAME"
571             ;;
572           *)
573             local mailname="${HOSTNAME}.${etc_mail_domain}"
574             ;;
575         esac
576         echo "Setting mailname to ${mailname}"
577         echo "$mailname" > /etc/mailname
578      fi
579   fi
580 }
581 # }}}
582
583 # generate initrd/initramfs {{{
584 initrd() {
585   # assume the first available kernel as our main kernel
586   # shellcheck disable=SC2012
587   KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
588   if [ -z "$KERNELIMG" ] ; then
589      echo 'No kernel image found, skipping initrd stuff.'>&2
590      return
591   fi
592
593   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
594
595   # generate initrd
596   if [ -n "$INITRD" ] ; then
597      echo "Generating initrd."
598      update-initramfs -c -t -k "$KERNELVER"
599   fi
600 }
601 # }}}
602
603 efi_setup() {
604   if [ -z "$EFI" ] ; then
605     return 0
606   fi
607
608   if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
609     echo "Notice: efi option set but no efibootmgr package, installing it therefore."
610     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
611   fi
612
613   mkdir -p /boot/efi
614   echo "Mounting $EFI on /boot/efi"
615   mount "$EFI" /boot/efi || return 1
616
617   # if efivarfs kernel module is loaded, but efivars isn't,
618   # then we need to mount efivarfs for efibootmgr usage
619   if ! ls /sys/firmware/efi/efivars/* &>/dev/null ; then
620     echo "Mounting efivarfs on /sys/firmware/efi/efivars"
621     mount -t efivarfs efivarfs /sys/firmware/efi/efivars
622   fi
623
624   echo "Invoking efibootmgr"
625   efibootmgr || return 1
626
627   umount /sys/firmware/efi/efivars &>/dev/null || true
628
629 }
630
631 # grub configuration/installation {{{
632 grub_install() {
633
634   if [ -z "$GRUB" ] ; then
635     echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
636     return 0
637   fi
638
639   efi_setup || return 1
640
641   if [ -n "$EFI" ] ; then
642     GRUB_PACKAGE=grub-efi-amd64
643   else
644     GRUB_PACKAGE=grub-pc
645   fi
646
647   # make sure this is pre-defined so we have sane settings for automated
648   # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
649   echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
650   echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
651
652   if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
653     echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
654     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
655   fi
656
657   if ! [ -x "$(command -v grub-install)" ] ; then
658      echo "Error: grub-install not available. (Error while installing grub package?)" >&2
659      return 1
660   fi
661
662   if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
663      for device in $SELECTED_PARTITIONS ; do
664         GRUB="${device%%[0-9]}"
665         echo "Installing grub on ${GRUB}:"
666         if ! grub-install --no-floppy "$GRUB" ; then
667           echo "Error: failed to execute 'grub-install --no-floppy $GRUB'." >&2
668           exit 1
669         fi
670
671      done
672      rm -f /boot/grub/device.map
673   else
674      echo "Installing grub on ${GRUB}:"
675      case "$RELEASE" in
676        lenny|squeeze|wheezy)
677          local grub_dev
678          grub_dev="$(readlink -f "${GRUB}")"
679          if ! grub-install --no-floppy "${grub_dev}" ; then
680            echo "Error: failed to execute 'grub-install --no-floppy ${grub_dev}'." >&2
681            exit 1
682          fi
683          rm -f /boot/grub/device.map
684          ;;
685        *)
686          echo "(hd0) ${GRUB}" > /boot/grub/device.map
687          if ! grub-install "(hd0)" ; then
688            echo "Error: failed to execute 'grub-install (hd0)'." >&2
689            exit 1
690          fi
691          rm /boot/grub/device.map
692          ;;
693      esac
694   fi
695
696   echo "Adjusting grub configuration for use on ${GRUB}."
697
698   # finally install grub
699   if [ -x /usr/sbin/update-grub ] ; then
700      UPDATEGRUB='/usr/sbin/update-grub'
701   elif [ -x /sbin/update-grub ] ; then
702      UPDATEGRUB='/sbin/update-grub'
703   else
704     echo "Error: update-grub not available, can not execute it." >&2
705     return 1
706   fi
707
708   if [ -n "${BOOT_APPEND}" ] ; then
709     echo "Adding BOOT_APPEND configuration ['${BOOT_APPEND}'] to /etc/default/grub."
710     sed -i "/GRUB_CMDLINE_LINUX_DEFAULT/ s#\"\$# ${BOOT_APPEND}\"#" /etc/default/grub
711   fi
712
713   mountpoint /boot/efi &>/dev/null && umount /boot/efi
714
715   $UPDATEGRUB
716 }
717 # }}}
718
719 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
720 custom_scripts() {
721   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
722
723   for script in /etc/debootstrap/chroot-scripts/* ; do
724       echo "Executing script $script"
725       $script && echo "done" || echo "failed"
726   done
727 }
728 # }}}
729
730 # make sure we don't have any running processes left {{{
731 services() {
732   for service in ssh mdadm mdadm-raid ; do
733     if [ -x /etc/init.d/"$service" ] ; then
734        /etc/init.d/"$service" stop || true
735     fi
736   done
737 }
738 # }}}
739
740 # unmount /proc and make sure nothing is left {{{
741 finalize() {
742   # make sure we don't leave any sensible data
743   rm -f /etc/debootstrap/variables
744
745   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
746
747   umount /sys >/dev/null 2>/dev/null || true
748   umount /proc >/dev/null 2>/dev/null || true
749 }
750 # }}}
751
752 # signal handler {{{
753 signal_handler() {
754   finalize
755   [ -n "$1" ] && EXIT="$1" || EXIT="1"
756   exit "$EXIT"
757 }
758 # }}}
759
760 # set signal handler {{{
761 trap signal_handler HUP INT QUIT TERM
762 # }}}
763
764 # execute the functions {{{
765
766  # always execute install_policy_rcd
767  install_policy_rcd
768
769  for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
770      kernel packages extrapackages reconfigure hosts \
771      default_locales timezone fstab hostname initrd grub_install passwords \
772      custom_scripts upgrade_system remove_apt_cache services \
773      remove_chrootmirror; do
774      if stage $i ; then
775        $i && stage $i 'done' || exit 1
776      fi
777   done
778   # always execute the finalize stage:
779   finalize
780 # }}}
781
782 # finally exit the chroot {{{
783   echo "Finished chroot installation, exiting."
784   exit 0
785 # }}}
786
787 ## END OF FILE #################################################################
788 # vim: ai tw=80 expandtab foldmethod=marker