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 . /etc/debootstrap/config || exit 1
21 . /etc/debootstrap/variables || exit 1
23 [ -r /proc/1 ] || mount -t proc none /proc
24 [ -r /sys/kernel ] || mount -t sysfs none /sys
28 # use aptitude only if it's available
29 if [ -x /usr/bin/aptitude ] ; then
30 APTUPDATE="aptitude update $DPKG_OPTIONS"
31 # Debian ISOs do not contain signed Release files
32 if [ -n "$ISO" ] ; then
33 APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
34 APTUPGRADE="aptitude -y --allow-untrusted safe-upgrade $DPKG_OPTIONS"
36 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
37 APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
40 APTINSTALL="apt-get -y --no-install-recommends install $DPKG_OPTIONS"
41 APTUPDATE="apt-get update $DPKG_OPTIONS"
42 APTUPGRADE="apt-get -y upgrade $DPKG_OPTIONS"
45 if [ -z "$STAGES" ] ; then
46 STAGES='/etc/debootstrap/stages'
47 [ -d "$STAGES" ] || mkdir -p "$STAGES"
51 # helper functions {{{
54 echo "$2" > "$STAGES/$1"
56 elif grep -q 'done' "$STAGES/$1" 2>/dev/null ; then
57 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
60 echo " Executing stage ${1}"
65 # read -s emulation for dash. result is in $resp.
67 [ -t 0 ] && stty -echo
74 # define chroot mirror {{{
76 if [ -n "$KEEP_SRC_LIST" ] ; then
77 echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
81 if [ -z "$COMPONENTS" ] ; then
84 echo "Using repository components $COMPONENTS"
86 if [ -n "$ISO" ] ; then
87 echo "Adjusting sources.list for ISO (${ISO})."
88 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
90 if [ -n "$MIRROR" ] ; then
91 echo "Adding mirror entry (${MIRROR}) to sources.list."
92 echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
95 if [ -n "$MIRROR" ] ; then
96 echo "Adjusting sources.list for mirror (${MIRROR})."
97 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
104 if [ -n "$MIRROR" ] ; then
105 echo "Release matching $RELEASE - enabling LTS support in sources.list"
106 echo "deb $MIRROR ${RELEASE}-lts $COMPONENTS" >> /etc/apt/sources.list
111 # add security.debian.org:
113 unstable|sid|lenny) ;; # no security pool available
115 echo "Adding security.debian.org to sources.list."
116 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
122 # remove local chroot mirror {{{
123 remove_chrootmirror() {
124 if [ -n "$KEEP_SRC_LIST" ] ; then
125 echo "KEEP_SRC_LIST has been set, skipping remove_chrootmirror stage."
129 if [ -n "$ISO" ] ; then
130 echo "Removing ISO (${ISO}) from sources.list."
131 TMP_ISO=$(echo "$ISO" |sed 's#/#\\/#g')
132 sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
134 if [ -n "$MIRROR" ] && echo "$MIRROR" | grep -q 'file:' ; then
135 echo "Removing local mirror (${MIRROR}) from sources.list."
136 TMP_MIRROR=$(echo "$MIRROR" |sed 's#/#\\/#g')
137 sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
138 echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
139 echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
145 # set up grml repository {{{
147 if [ -n "$GRMLREPOS" ] ; then
148 # user might have provided their own apt sources.list
149 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
150 cat >> /etc/apt/sources.list.d/grml.list << EOF
151 # grml: stable repository:
152 deb http://deb.grml.org/ grml-stable main
153 deb-src http://deb.grml.org/ grml-stable main
155 # grml: testing/development repository:
156 deb http://deb.grml.org/ grml-testing main
157 deb-src http://deb.grml.org/ grml-testing main
161 if apt-get update $DPKG_OPTIONS; then
162 apt-get -y --allow-unauthenticated install grml-debian-keyring $DPKG_OPTIONS
163 apt-get update $DPKG_OPTIONS
165 # make sure we have the keys available for aptitude
166 gpg --keyserver subkeys.pgp.net --recv-keys 709BCE51568573EBC160E590F61E2E7CECDEA787
167 gpg --export 709BCE51568573EBC160E590F61E2E7CECDEA787 | apt-key add - || true # not yet sure
168 # why it's necessary, sometimes we get an error even though it works [mika]
171 # make sure we install packages from Grml's pool only if not available
173 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
174 cat >> /etc/apt/preferences << EOF
175 // debian pool (default):
177 Pin: release o=Debian
180 // main grml-repository:
182 Pin: origin deb.grml.org
190 # check available backports release version {{{
192 wget -q -O/dev/null "http://backports.debian.org/debian-backports/dists/${1}-backports/Release"
196 # feature to provide Debian backports repos {{{
198 if [ -n "$BACKPORTREPOS" ] ; then
199 if ! checkbackports "$RELEASE" ; then
200 echo "Backports for ${RELEASE} are not available." >&2
203 # user might have provided their own apt sources.list
204 if ! grep -q backports /etc/apt/sources.list.d/backports.list 2>/dev/null ; then
205 cat >> /etc/apt/sources.list.d/backports.list << EOF
206 # debian backports: ${RELEASE}-backports repository:
207 deb http://backports.debian.org/debian-backports ${RELEASE}-backports main
208 deb-src http://backports.debian.org/debian-backports ${RELEASE}-backports main
216 # set up kernel-img.conf {{{
218 if ! [ -r /etc/kernel-img.conf ] ; then
219 echo "Setting up /etc/kernel-img.conf"
220 cat > /etc/kernel-img.conf << EOF
221 # Kernel Image management overrides
222 # See kernel-img.conf(5) for details
230 # make sure services do not start up {{{
231 install_policy_rcd() {
232 if ! [ -r /usr/sbin/policy-rc.d ] ; then
234 cat > /usr/sbin/policy-rc.d << EOF
238 chmod 775 /usr/sbin/policy-rc.d
243 # make sure we have an up2date system {{{
245 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
246 echo "Running update + upgrade"
248 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
250 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
255 # remove now useless apt cache {{{
257 if [ "$RM_APTCACHE" = 'yes' ] ; then
258 echo "Cleaning apt cache."
259 apt-get clean $DPKG_OPTIONS
261 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
266 # install additional packages {{{
268 # Pre-seed the debconf database with answers. Each question will be marked
269 # as seen to prevent debconf from asking the question interactively.
270 [ -f /etc/debootstrap/debconf-selections ] && {
271 echo "Preseeding the debconf database, some lines might be skipped..."
272 debconf-set-selections < /etc/debootstrap/debconf-selections
275 if [ "$PACKAGES" = 'yes' ] ; then
276 if ! [ -r /etc/debootstrap/packages ] ; then
277 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
281 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
287 # install extra packages {{{
289 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
290 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
291 if [ -n "$PACKAGELIST" ]; then
293 # run apt again to resolve any deps
294 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
300 # check if the specified Debian package exists
302 output=$(apt-cache show "$1" 2>/dev/null)
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
321 lenny|squeeze|wheezy) KARCH='686' ;;
322 # since jessie the linux-image-686 image doesn't exist any longer
323 *) KARCH='686-pae' ;;
330 echo "Only i386 and amd64 are currently supported" >&2
334 for KPREFIX in "" "2.6-" ; do # iterate through the kernel prefixes,
335 # currently "" and "2.6-"
336 if package_exists linux-image-${KPREFIX}${KARCH} ; then
337 echo ${KPREFIX}${KARCH}
344 # install kernel packages {{{
346 if [ -n "$NOKERNEL" ] ; then
347 echo "Skipping installation of kernel packages as requested via --nokernel"
352 KVER=$(get_kernel_version)
353 if [ -n "$KVER" ] ; then
354 # note: install busybox to be able to debug initramfs
355 KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free"
356 # only add firmware-linux if we have non-free as a component
357 if expr "$COMPONENTS" : '.*non-free' >/dev/null ; then
358 KERNELPACKAGES="$KERNELPACKAGES firmware-linux"
360 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
362 echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
367 # reconfigure packages {{{
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."
380 # set password of user root {{{
383 if [ -n "$NOPASSWORD" ] ; then
384 echo "Skip setting root password as requested."
388 echo "Activating shadow passwords."
392 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
396 if [ -n "$ROOTPASSWORD" ] ; then
397 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
398 export ROOTPASSWORD=''
402 echo "Setting password for user root:"
403 while [ "$a" != "$b" ] ; do
404 printf "Enter new UNIX password for user root: "
409 printf "Retype new UNIX password for user root: "
414 if [ "$a" != "$b" ] ; then
415 echo "Sorry, passwords do not match. Retry."
419 echo root:"$a" | chpasswd $CHPASSWD_OPTION
428 # set up /etc/hosts {{{
430 if ! [ -f /etc/hosts ] ; then
431 cat > /etc/hosts << EOF
433 ::1 localhost ip6-localhost ip6-loopback
435 ff02::2 ip6-allrouters
441 # set 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"
450 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
455 # adjust timezone {{{
457 if [ -n "$TIMEZONE" ] ; then
458 echo "Adjusting /etc/localtime"
459 ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
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 echo "UUID=$(blkid -o value -s UUID $EFI) /boot/efi vfat umask=0077 0 1" >> /etc/fstab
477 cat >> /etc/fstab << EOF
478 proc /proc proc defaults 0 0
479 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
480 # some other examples:
481 # /dev/sda2 none swap sw,pri=0 0 0
482 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
483 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
484 # linux:/pub /beer nfs defaults 0 0
485 # tmpfs /tmp tmpfs size=300M 0 0
486 # /dev/sda5 none swap sw 0 0
491 # generate /etc/fstab {{{
493 # set up /etc/fstab if file is not present (cdebootstrap)
494 if [ ! -f /etc/fstab ] ; then
498 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
499 if grep -q UNCONFIGURED /etc/fstab ; then
505 # set up hostname {{{
507 if [ -n "$HOSTNAME" ] ; then
508 echo "Setting hostname to ${HOSTNAME}."
509 echo "$HOSTNAME" > /etc/hostname
511 # adjust postfix configuration
512 if [ -r /etc/postfix/main.cf ] ; then
513 # adjust hostname related options:
514 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
516 # listen on loopback interface only:
517 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
518 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
520 if [ -r /etc/mailname ] ; then
521 # adjust /etc/mailname
522 local etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
525 local mailname="$HOSTNAME"
528 local mailname="${HOSTNAME}.${etc_mail_domain}"
531 echo "Setting mailname to ${mailname}"
532 echo "$mailname" > /etc/mailname
538 # generate initrd/initramfs {{{
540 # assume the first available kernel as our main kernel
541 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
542 if [ -z "$KERNELIMG" ] ; then
543 echo 'No kernel image found, skipping initrd stuff.'>&2
547 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
550 if [ -n "$INITRD" ] ; then
551 echo "Generating initrd."
552 update-initramfs -c -t -k "$KERNELVER"
558 if [ -z "$EFI" ] ; then
562 if ! dpkg --list efibootmgr 2>/dev/null | grep -q '^ii' ; then
563 echo "Notice: efi option set but no efibootmgr package, installing it therefore."
564 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL efibootmgr
568 echo "Mounting $EFI on /boot/efi"
569 mount "$EFI" /boot/efi || return 1
571 echo "Invoking efibootmgr"
572 efibootmgr || return 1
575 # grub configuration/installation {{{
578 if [ -z "$GRUB" ] ; then
579 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
583 efi_setup || return 1
585 if [ -n "$EFI" ] ; then
586 GRUB_PACKAGE=grub-efi-amd64
591 # make sure this is pre-defined so we have sane settings for automated
592 # upgrades, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=711019
593 echo "Setting ${GRUB_PACKAGE} debconf configuration for install device to $GRUB"
594 echo "${GRUB_PACKAGE} ${GRUB_PACKAGE}/install_devices multiselect $GRUB" | debconf-set-selections
596 if ! dpkg --list ${GRUB_PACKAGE} 2>/dev/null | grep -q '^ii' ; then
597 echo "Notice: grub option set but no ${GRUB_PACKAGE} package, installing it therefore."
598 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL ${GRUB_PACKAGE}
601 if ! [ -x "$(which grub-install)" ] ; then
602 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
606 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
607 for device in $SELECTED_PARTITIONS ; do
608 GRUB="${device%%[0-9]}"
609 echo "Installing grub on ${GRUB}:"
610 grub-install --no-floppy "$GRUB"
612 rm -f /boot/grub/device.map
614 echo "Installing grub on ${GRUB}:"
616 lenny|squeeze|wheezy)
617 grub-install --no-floppy "$(readlink -f "${GRUB}")"
618 rm -f /boot/grub/device.map
621 echo "(hd0) ${GRUB}" > /boot/grub/device.map
623 rm /boot/grub/device.map
628 echo "Adjusting grub configuration for use on ${GRUB}."
630 # finally install grub
631 if [ -x /usr/sbin/update-grub ] ; then
632 UPDATEGRUB='/usr/sbin/update-grub'
633 elif [ -x /sbin/update-grub ] ; then
634 UPDATEGRUB='/sbin/update-grub'
636 echo "Error: update-grub not available, can not execute it." >&2
640 mountpoint /boot/efi &>/dev/null && umount /boot/efi
646 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
648 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
650 for script in /etc/debootstrap/chroot-scripts/* ; do
651 echo "Executing script $script"
652 $script && echo "done" || echo "failed"
657 # make sure we don't have any running processes left {{{
659 for service in ssh mdadm mdadm-raid ; do
660 if [ -x /etc/init.d/"$service" ] ; then
661 /etc/init.d/"$service" stop || true
667 # unmount /proc and make sure nothing is left {{{
669 # make sure we don't leave any sensible data
670 rm -f /etc/debootstrap/variables
672 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
674 umount /sys >/dev/null 2>/dev/null || true
675 umount /proc >/dev/null 2>/dev/null || true
682 [ -n "$1" ] && EXIT="$1" || EXIT="1"
687 # set signal handler {{{
688 trap signal_handler HUP INT QUIT TERM
691 # execute the functions {{{
693 # always execute install_policy_rcd
696 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
697 kernel packages extrapackages reconfigure hosts \
698 default_locales timezone fstab hostname initrd grub_install passwords \
699 custom_scripts upgrade_system remove_apt_cache services \
700 remove_chrootmirror; do
702 $i && stage $i 'done' || exit 1
705 # always execute the finalize stage:
709 # finally exit the chroot {{{
710 echo "Finished chroot installation, exiting."
714 ## END OF FILE #################################################################
715 # vim: ai tw=80 expandtab foldmethod=marker