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 http://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 ################################################################################
13 if [ "$REPORT_TRAP_ERR" = "yes" ] || [ "$FAIL_TRAP_ERR" = "yes" ]; then
16 trap "error_handler" ERR
20 # shellcheck disable=SC1091
21 . /etc/debootstrap/config || exit 1
22 # shellcheck disable=SC1091
23 . /etc/debootstrap/variables || exit 1
25 [ -r /proc/1 ] || mount -t proc none /proc
26 [ -r /sys/kernel ] || mount -t sysfs none /sys
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"
38 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
39 APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
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"
47 if [ -z "$STAGES" ] ; then
48 STAGES='/etc/debootstrap/stages'
49 [ -d "$STAGES" ] || mkdir -p "$STAGES"
53 # helper functions {{{
56 echo "$2" > "$STAGES/$1"
58 elif grep -q 'done' "$STAGES/$1" 2>/dev/null ; then
59 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
62 echo " Executing stage ${1}"
67 # read -s emulation for dash. result is in $resp.
69 [ -t 0 ] && stty -echo
76 # define chroot mirror {{{
78 if [ -n "$KEEP_SRC_LIST" ] ; then
79 echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
83 if [ -z "$COMPONENTS" ] ; then
86 echo "Using repository components $COMPONENTS"
88 if [ -n "$ISO" ] ; then
89 echo "Adjusting sources.list for ISO (${ISO})."
90 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
92 if [ -n "$MIRROR" ] ; then
93 echo "Adding mirror entry (${MIRROR}) to sources.list."
94 echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
97 if [ -n "$MIRROR" ] ; then
98 echo "Adjusting sources.list for mirror (${MIRROR})."
99 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
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
113 # add security.debian.org:
115 unstable|sid|lenny) ;; # no security pool available
117 echo "Adding security.debian.org to sources.list."
118 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
124 # remove local chroot mirror {{{
125 remove_chrootmirror() {
126 if [ -n "$KEEP_SRC_LIST" ] ; then
127 echo "KEEP_SRC_LIST has been set, skipping remove_chrootmirror stage."
131 if [ -n "$ISO" ] ; then
132 echo "Removing ISO (${ISO}) from sources.list."
133 TMP_ISO="${ISO//\//\\\/}"
134 sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
136 if [ -n "$MIRROR" ] && echo "$MIRROR" | grep -q 'file:' ; then
137 echo "Removing local mirror (${MIRROR}) from sources.list."
138 TMP_MIRROR="${MIRROR//\//\\\/}"
139 sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
140 echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
141 echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
147 # set up grml repository {{{
149 if [ -n "$GRMLREPOS" ] ; then
150 # user might have provided their own apt sources.list
151 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
152 cat >> /etc/apt/sources.list.d/grml.list << EOF
153 # grml: stable repository:
154 deb http://deb.grml.org/ grml-stable main
155 deb-src http://deb.grml.org/ grml-stable main
157 # grml: testing/development repository:
158 deb http://deb.grml.org/ grml-testing main
159 deb-src http://deb.grml.org/ grml-testing main
163 # shellcheck disable=SC2086
164 if apt-get update $DPKG_OPTIONS; then
165 # shellcheck disable=SC2086
166 apt-get -y --allow-unauthenticated install grml-debian-keyring $DPKG_OPTIONS
167 # shellcheck disable=SC2086
168 apt-get update $DPKG_OPTIONS
170 # make sure we have the keys available for aptitude
171 gpg --keyserver subkeys.pgp.net --recv-keys 709BCE51568573EBC160E590F61E2E7CECDEA787
172 gpg --export 709BCE51568573EBC160E590F61E2E7CECDEA787 | apt-key add - || true # not yet sure
173 # why it's necessary, sometimes we get an error even though it works [mika]
176 # make sure we install packages from Grml's pool only if not available
178 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
179 cat >> /etc/apt/preferences << EOF
180 // debian pool (default):
182 Pin: release o=Debian
185 // main grml-repository:
187 Pin: origin deb.grml.org
195 # check available backports release version {{{
197 wget -q -O/dev/null "http://backports.debian.org/debian-backports/dists/${1}-backports/Release"
201 # feature to provide Debian backports repos {{{
203 if [ -n "$BACKPORTREPOS" ] ; then
204 if ! checkbackports "$RELEASE" ; then
205 echo "Backports for ${RELEASE} are not available." >&2
208 # user might have provided their own apt sources.list
209 if ! grep -q backports /etc/apt/sources.list.d/backports.list 2>/dev/null ; then
210 cat >> /etc/apt/sources.list.d/backports.list << EOF
211 # debian backports: ${RELEASE}-backports repository:
212 deb http://backports.debian.org/debian-backports ${RELEASE}-backports main
213 deb-src http://backports.debian.org/debian-backports ${RELEASE}-backports main
221 # set up kernel-img.conf {{{
223 if ! [ -r /etc/kernel-img.conf ] ; then
224 echo "Setting up /etc/kernel-img.conf"
225 cat > /etc/kernel-img.conf << EOF
226 # Kernel Image management overrides
227 # See kernel-img.conf(5) for details
235 # make sure services do not start up {{{
236 install_policy_rcd() {
237 if ! [ -r /usr/sbin/policy-rc.d ] ; then
239 cat > /usr/sbin/policy-rc.d << EOF
243 chmod 775 /usr/sbin/policy-rc.d
248 # make sure we have an up2date system {{{
250 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
251 echo "Running update + upgrade"
253 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
255 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
260 # remove now useless apt cache {{{
262 if [ "$RM_APTCACHE" = 'yes' ] ; then
263 echo "Cleaning apt cache."
264 # shellcheck disable=SC2086
265 apt-get clean $DPKG_OPTIONS
267 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
272 # install additional packages {{{
274 # Pre-seed the debconf database with answers. Each question will be marked
275 # as seen to prevent debconf from asking the question interactively.
276 [ -f /etc/debootstrap/debconf-selections ] && {
277 echo "Preseeding the debconf database, some lines might be skipped..."
278 debconf-set-selections < /etc/debootstrap/debconf-selections
281 if [ "$PACKAGES" = 'yes' ] ; then
282 if ! [ -r /etc/debootstrap/packages ] ; then
283 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
287 # shellcheck disable=SC2086,SC2046
288 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
294 # install extra packages {{{
296 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
297 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
298 if [ -n "$PACKAGELIST" ]; then
299 # shellcheck disable=SC2086
301 # run apt again to resolve any deps
302 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
308 # check if the specified Debian package exists
310 output=$(apt-cache show "$1" 2>/dev/null)
316 # determine the kernel version postfix
317 get_kernel_version() {
318 # do not override $KERNEL if set via config file
319 if [ -n "$KERNEL" ] ; then
326 # shellcheck disable=SC2153
330 lenny|squeeze|wheezy) KARCH='686' ;;
331 # since jessie the linux-image-686 image doesn't exist any longer
332 *) KARCH='686-pae' ;;
339 echo "Only i386 and amd64 are currently supported" >&2
343 for KPREFIX in "" "2.6-" ; do # iterate through the kernel prefixes,
344 # currently "" and "2.6-"
345 if package_exists linux-image-${KPREFIX}${KARCH} ; then
346 echo ${KPREFIX}${KARCH}
353 # install kernel packages {{{
355 if [ -n "$NOKERNEL" ] ; then
356 echo "Skipping installation of kernel packages as requested via --nokernel"
361 KVER=$(get_kernel_version)
362 if [ -n "$KVER" ] ; then
363 # note: install busybox to be able to debug initramfs
364 KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free"
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"
369 # shellcheck disable=SC2086
370 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
372 echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
377 # reconfigure packages {{{
379 if [ -n "$RECONFIGURE" ] ; then
380 for package in $RECONFIGURE ; do
381 if dpkg --list "$package" >/dev/null 2>&1 | grep -q '^ii' ; then
382 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure "$package" || \
383 echo "Warning: $package does not exist, can not reconfigure it."
390 # set password of user root {{{
393 if [ -n "$NOPASSWORD" ] ; then
394 echo "Skip setting root password as requested."
398 echo "Activating shadow passwords."
402 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
406 if [ -n "$ROOTPASSWORD" ] ; then
407 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
408 export ROOTPASSWORD=''
412 echo "Setting password for user root:"
413 while [ "$a" != "$b" ] ; do
414 printf "Enter new UNIX password for user root: "
419 printf "Retype new UNIX password for user root: "
424 if [ "$a" != "$b" ] ; then
425 echo "Sorry, passwords do not match. Retry."
429 echo root:"$a" | chpasswd $CHPASSWD_OPTION
438 # set up /etc/hosts {{{
440 if ! [ -f /etc/hosts ] ; then
441 cat > /etc/hosts << EOF
443 ::1 localhost ip6-localhost ip6-loopback
445 ff02::2 ip6-allrouters
451 # set default locales {{{
453 if [ -n "$DEFAULT_LOCALES" ] ; then
454 if ! [ -x /usr/sbin/update-locale ] ; then
455 echo "Warning: update-locale executable not available (no locales package installed?)"
456 echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
460 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
465 # adjust timezone {{{
467 if [ -n "$TIMEZONE" ] ; then
468 echo "Adjusting /etc/localtime"
469 ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
474 # helper function for fstab() {{{
476 echo "Setting up /etc/fstab"
477 if [ -n "$TARGET_UUID" ] ; then
478 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
480 echo "Warning: couldn't identify target UUID for rootfs, your /etc/fstab might be incomplete."
483 if [ -n "$EFI" ] ; then
484 # shellcheck disable=SC2086
485 echo "UUID=$(blkid -o value -s UUID $EFI) /boot/efi vfat umask=0077 0 1" >> /etc/fstab
488 cat >> /etc/fstab << EOF
489 proc /proc proc defaults 0 0
490 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
491 # some other examples:
492 # /dev/sda2 none swap sw,pri=0 0 0
493 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
494 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
495 # linux:/pub /beer nfs defaults 0 0
496 # tmpfs /tmp tmpfs size=300M 0 0
497 # /dev/sda5 none swap sw 0 0
502 # generate /etc/fstab {{{
504 # set up /etc/fstab if file is not present (cdebootstrap)
505 if [ ! -f /etc/fstab ] ; then
509 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
510 if grep -q UNCONFIGURED /etc/fstab ; then
516 # set up hostname {{{
518 if [ -n "$HOSTNAME" ] ; then
519 echo "Setting hostname to ${HOSTNAME}."
520 echo "$HOSTNAME" > /etc/hostname
522 # adjust postfix configuration
523 if [ -r /etc/postfix/main.cf ] ; then
524 # adjust hostname related options:
525 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
527 # listen on loopback interface only:
528 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
529 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
531 if [ -r /etc/mailname ] ; then
532 # adjust /etc/mailname
533 local etc_mail_domain
534 etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
537 local mailname="$HOSTNAME"
540 local mailname="${HOSTNAME}.${etc_mail_domain}"
543 echo "Setting mailname to ${mailname}"
544 echo "$mailname" > /etc/mailname
550 # generate initrd/initramfs {{{
552 # assume the first available kernel as our main kernel
553 # shellcheck disable=SC2012
554 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
555 if [ -z "$KERNELIMG" ] ; then
556 echo 'No kernel image found, skipping initrd stuff.'>&2
560 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
563 if [ -n "$INITRD" ] ; then
564 echo "Generating initrd."
565 update-initramfs -c -t -k "$KERNELVER"
571 if [ -z "$EFI" ] ; then
575 if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
576 echo "Notice: efi option set but no efibootmgr package, installing it therefore."
577 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
581 echo "Mounting $EFI on /boot/efi"
582 mount "$EFI" /boot/efi || return 1
584 echo "Invoking efibootmgr"
585 efibootmgr || return 1
588 # grub configuration/installation {{{
591 if [ -z "$GRUB" ] ; then
592 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
596 efi_setup || return 1
598 if [ -n "$EFI" ] ; then
599 GRUB_PACKAGE=grub-efi-amd64
604 # make sure this is pre-defined so we have sane settings for automated
605 # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
606 echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
607 echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
609 if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
610 echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
611 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
614 if ! [ -x "$(command -v grub-install)" ] ; then
615 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
619 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
620 for device in $SELECTED_PARTITIONS ; do
621 GRUB="${device%%[0-9]}"
622 echo "Installing grub on ${GRUB}:"
623 grub-install --no-floppy "$GRUB"
625 rm -f /boot/grub/device.map
627 echo "Installing grub on ${GRUB}:"
629 lenny|squeeze|wheezy)
630 grub-install --no-floppy "$(readlink -f "${GRUB}")"
631 rm -f /boot/grub/device.map
634 echo "(hd0) ${GRUB}" > /boot/grub/device.map
636 rm /boot/grub/device.map
641 echo "Adjusting grub configuration for use on ${GRUB}."
643 # finally install grub
644 if [ -x /usr/sbin/update-grub ] ; then
645 UPDATEGRUB='/usr/sbin/update-grub'
646 elif [ -x /sbin/update-grub ] ; then
647 UPDATEGRUB='/sbin/update-grub'
649 echo "Error: update-grub not available, can not execute it." >&2
653 mountpoint /boot/efi &>/dev/null && umount /boot/efi
659 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
661 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
663 for script in /etc/debootstrap/chroot-scripts/* ; do
664 echo "Executing script $script"
665 $script && echo "done" || echo "failed"
670 # make sure we don't have any running processes left {{{
672 for service in ssh mdadm mdadm-raid ; do
673 if [ -x /etc/init.d/"$service" ] ; then
674 /etc/init.d/"$service" stop || true
680 # unmount /proc and make sure nothing is left {{{
682 # make sure we don't leave any sensible data
683 rm -f /etc/debootstrap/variables
685 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
687 umount /sys >/dev/null 2>/dev/null || true
688 umount /proc >/dev/null 2>/dev/null || true
695 [ -n "$1" ] && EXIT="$1" || EXIT="1"
700 # set signal handler {{{
701 trap signal_handler HUP INT QUIT TERM
704 # execute the functions {{{
706 # always execute install_policy_rcd
709 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
710 kernel packages extrapackages reconfigure hosts \
711 default_locales timezone fstab hostname initrd grub_install passwords \
712 custom_scripts upgrade_system remove_apt_cache services \
713 remove_chrootmirror; do
715 $i && stage $i 'done' || exit 1
718 # always execute the finalize stage:
722 # finally exit the chroot {{{
723 echo "Finished chroot installation, exiting."
727 ## END OF FILE #################################################################
728 # vim: ai tw=80 expandtab foldmethod=marker