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 # feature to provide Debian backports repos {{{
197 if [ -n "$BACKPORTREPOS" ] ; then
198 cat >> /etc/apt/sources.list.d/backports.list << EOF
199 # debian backports: ${RELEASE}-backports repository:
200 deb ${MIRROR} ${RELEASE}-backports main
201 deb-src ${MIRROR} ${RELEASE}-backports main
207 # set up kernel-img.conf {{{
209 if ! [ -r /etc/kernel-img.conf ] ; then
210 echo "Setting up /etc/kernel-img.conf"
211 cat > /etc/kernel-img.conf << EOF
212 # Kernel Image management overrides
213 # See kernel-img.conf(5) for details
221 # make sure services do not start up {{{
222 install_policy_rcd() {
223 if ! [ -r /usr/sbin/policy-rc.d ] ; then
225 cat > /usr/sbin/policy-rc.d << EOF
229 chmod 775 /usr/sbin/policy-rc.d
234 # make sure we have an up2date system {{{
236 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
237 echo "Running update + upgrade"
239 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
241 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
246 # remove now useless apt cache {{{
248 if [ "$RM_APTCACHE" = 'yes' ] ; then
249 echo "Cleaning apt cache."
250 # shellcheck disable=SC2086
251 apt-get clean $DPKG_OPTIONS
253 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
258 # install additional packages {{{
260 # Pre-seed the debconf database with answers. Each question will be marked
261 # as seen to prevent debconf from asking the question interactively.
262 [ -f /etc/debootstrap/debconf-selections ] && {
263 echo "Preseeding the debconf database, some lines might be skipped..."
264 debconf-set-selections < /etc/debootstrap/debconf-selections
267 if [ "$PACKAGES" = 'yes' ] ; then
268 if ! [ -r /etc/debootstrap/packages ] ; then
269 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
273 # shellcheck disable=SC2086,SC2046
274 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
280 # install extra packages {{{
282 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
283 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
284 if [ -n "$PACKAGELIST" ]; then
285 # shellcheck disable=SC2086
287 # run apt again to resolve any deps
288 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
294 # check if the specified Debian package exists
296 output=$(apt-cache show "$1" 2>/dev/null)
302 # determine the kernel version postfix
303 get_kernel_version() {
304 # do not override $KERNEL if set via config file
305 if [ -n "$KERNEL" ] ; then
312 # shellcheck disable=SC2153
316 lenny|squeeze|wheezy) KARCH='686' ;;
317 # since jessie the linux-image-686 image doesn't exist any longer
318 *) KARCH='686-pae' ;;
325 echo "Only i386 and amd64 are currently supported" >&2
329 for KPREFIX in "" "2.6-" ; do # iterate through the kernel prefixes,
330 # currently "" and "2.6-"
331 if package_exists linux-image-${KPREFIX}${KARCH} ; then
332 echo ${KPREFIX}${KARCH}
339 # install kernel packages {{{
341 if [ -n "$NOKERNEL" ] ; then
342 echo "Skipping installation of kernel packages as requested via --nokernel"
347 KVER=$(get_kernel_version)
348 if [ -n "$KVER" ] ; then
349 # note: install busybox to be able to debug initramfs
350 KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free"
351 # only add firmware-linux if we have non-free as a component
352 if expr "$COMPONENTS" : '.*non-free' >/dev/null ; then
353 KERNELPACKAGES="$KERNELPACKAGES firmware-linux"
355 # shellcheck disable=SC2086
356 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
358 echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
363 # reconfigure packages {{{
365 if [ -n "$RECONFIGURE" ] ; then
366 for package in $RECONFIGURE ; do
367 if dpkg --list "$package" >/dev/null 2>&1 | grep -q '^ii' ; then
368 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure "$package" || \
369 echo "Warning: $package does not exist, can not reconfigure it."
376 # set password of user root {{{
379 if [ -n "$NOPASSWORD" ] ; then
380 echo "Skip setting root password as requested."
384 echo "Activating shadow passwords."
388 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
392 if [ -n "$ROOTPASSWORD" ] ; then
393 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
394 export ROOTPASSWORD=''
398 echo "Setting password for user root:"
399 while [ "$a" != "$b" ] ; do
400 printf "Enter new UNIX password for user root: "
405 printf "Retype new UNIX password for user root: "
410 if [ "$a" != "$b" ] ; then
411 echo "Sorry, passwords do not match. Retry."
415 echo root:"$a" | chpasswd $CHPASSWD_OPTION
424 # set up /etc/hosts {{{
426 if ! [ -f /etc/hosts ] ; then
427 cat > /etc/hosts << EOF
429 ::1 localhost ip6-localhost ip6-loopback
431 ff02::2 ip6-allrouters
437 # set default locales {{{
439 if [ -n "$DEFAULT_LOCALES" ] ; then
440 if ! [ -x /usr/sbin/update-locale ] ; then
441 echo "Warning: update-locale executable not available (no locales package installed?)"
442 echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
446 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
451 # adjust timezone {{{
453 if [ -n "$TIMEZONE" ] ; then
454 echo "Adjusting /etc/localtime"
455 ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
457 echo "Setting /etc/timezone to $TIMEZONE"
458 printf '%s\n' "$TIMEZONE" > /etc/timezone
464 # helper function for fstab() {{{
466 echo "Setting up /etc/fstab"
467 if [ -n "$TARGET_UUID" ] ; then
468 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
470 echo "Warning: couldn't identify target UUID for rootfs, your /etc/fstab might be incomplete."
473 if [ -n "$EFI" ] ; then
474 # shellcheck disable=SC2086
475 echo "UUID=$(blkid -o value -s UUID $EFI) /boot/efi vfat umask=0077 0 1" >> /etc/fstab
478 cat >> /etc/fstab << EOF
479 proc /proc proc defaults 0 0
480 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
481 # some other examples:
482 # /dev/sda2 none swap sw,pri=0 0 0
483 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
484 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
485 # linux:/pub /beer nfs defaults 0 0
486 # tmpfs /tmp tmpfs size=300M 0 0
487 # /dev/sda5 none swap sw 0 0
492 # generate /etc/fstab {{{
494 # set up /etc/fstab if file is not present (cdebootstrap)
495 if [ ! -f /etc/fstab ] ; then
499 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
500 if grep -q UNCONFIGURED /etc/fstab ; then
506 # set up hostname {{{
508 if [ -n "$HOSTNAME" ] ; then
509 echo "Setting hostname to ${HOSTNAME}."
510 echo "$HOSTNAME" > /etc/hostname
512 # adjust postfix configuration
513 if [ -r /etc/postfix/main.cf ] ; then
514 # adjust hostname related options:
515 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
517 # listen on loopback interface only:
518 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
519 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
521 if [ -r /etc/mailname ] ; then
522 # adjust /etc/mailname
523 local etc_mail_domain
524 etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
527 local mailname="$HOSTNAME"
530 local mailname="${HOSTNAME}.${etc_mail_domain}"
533 echo "Setting mailname to ${mailname}"
534 echo "$mailname" > /etc/mailname
540 # generate initrd/initramfs {{{
542 # assume the first available kernel as our main kernel
543 # shellcheck disable=SC2012
544 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
545 if [ -z "$KERNELIMG" ] ; then
546 echo 'No kernel image found, skipping initrd stuff.'>&2
550 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
553 if [ -n "$INITRD" ] ; then
554 echo "Generating initrd."
555 update-initramfs -c -t -k "$KERNELVER"
561 if [ -z "$EFI" ] ; then
565 if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
566 echo "Notice: efi option set but no efibootmgr package, installing it therefore."
567 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
571 echo "Mounting $EFI on /boot/efi"
572 mount "$EFI" /boot/efi || return 1
574 echo "Invoking efibootmgr"
575 efibootmgr || return 1
578 # grub configuration/installation {{{
581 if [ -z "$GRUB" ] ; then
582 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
586 efi_setup || return 1
588 if [ -n "$EFI" ] ; then
589 GRUB_PACKAGE=grub-efi-amd64
594 # make sure this is pre-defined so we have sane settings for automated
595 # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
596 echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
597 echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
599 if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
600 echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
601 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
604 if ! [ -x "$(command -v grub-install)" ] ; then
605 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
609 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
610 for device in $SELECTED_PARTITIONS ; do
611 GRUB="${device%%[0-9]}"
612 echo "Installing grub on ${GRUB}:"
613 grub-install --no-floppy "$GRUB"
615 rm -f /boot/grub/device.map
617 echo "Installing grub on ${GRUB}:"
619 lenny|squeeze|wheezy)
620 grub-install --no-floppy "$(readlink -f "${GRUB}")"
621 rm -f /boot/grub/device.map
624 echo "(hd0) ${GRUB}" > /boot/grub/device.map
626 rm /boot/grub/device.map
631 echo "Adjusting grub configuration for use on ${GRUB}."
633 # finally install grub
634 if [ -x /usr/sbin/update-grub ] ; then
635 UPDATEGRUB='/usr/sbin/update-grub'
636 elif [ -x /sbin/update-grub ] ; then
637 UPDATEGRUB='/sbin/update-grub'
639 echo "Error: update-grub not available, can not execute it." >&2
643 mountpoint /boot/efi &>/dev/null && umount /boot/efi
649 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
651 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
653 for script in /etc/debootstrap/chroot-scripts/* ; do
654 echo "Executing script $script"
655 $script && echo "done" || echo "failed"
660 # make sure we don't have any running processes left {{{
662 for service in ssh mdadm mdadm-raid ; do
663 if [ -x /etc/init.d/"$service" ] ; then
664 /etc/init.d/"$service" stop || true
670 # unmount /proc and make sure nothing is left {{{
672 # make sure we don't leave any sensible data
673 rm -f /etc/debootstrap/variables
675 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
677 umount /sys >/dev/null 2>/dev/null || true
678 umount /proc >/dev/null 2>/dev/null || true
685 [ -n "$1" ] && EXIT="$1" || EXIT="1"
690 # set signal handler {{{
691 trap signal_handler HUP INT QUIT TERM
694 # execute the functions {{{
696 # always execute install_policy_rcd
699 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
700 kernel packages extrapackages reconfigure hosts \
701 default_locales timezone fstab hostname initrd grub_install passwords \
702 custom_scripts upgrade_system remove_apt_cache services \
703 remove_chrootmirror; do
705 $i && stage $i 'done' || exit 1
708 # always execute the finalize stage:
712 # finally exit the chroot {{{
713 echo "Finished chroot installation, exiting."
717 ## END OF FILE #################################################################
718 # vim: ai tw=80 expandtab foldmethod=marker