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
27 # use aptitude only if it's available
28 if [ -x /usr/bin/aptitude ] ; then
29 APTUPDATE="aptitude update $DPKG_OPTIONS"
30 # Debian ISOs do not contain signed Release files
31 if [ -n "$ISO" ] ; then
32 APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
33 APTUPGRADE="aptitude -y --allow-untrusted safe-upgrade $DPKG_OPTIONS"
35 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
36 APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
39 APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
40 APTUPDATE="apt-get update $DPKG_OPTIONS"
41 APTUPGRADE="apt-get --force-yes -y upgrade $DPKG_OPTIONS"
44 if [ -z "$STAGES" ] ; then
45 STAGES='/etc/debootstrap/stages'
46 [ -d "$STAGES" ] || mkdir -p "$STAGES"
50 # helper functions {{{
53 echo "$2" > "$STAGES/$1"
55 elif grep -q 'done' "$STAGES/$1" 2>/dev/null ; then
56 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
59 echo " Executing stage ${1}"
64 # read -s emulation for dash. result is in $resp.
66 [ -t 0 ] && stty -echo
73 # define chroot mirror {{{
75 if [ -n "$KEEP_SRC_LIST" ] ; then
76 echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
80 if [ -z "$COMPONENTS" ] ; then
81 COMPONENTS='main contrib non-free'
83 echo "Using repository components $COMPONENTS"
85 if [ -n "$ISO" ] ; then
86 echo "Adjusting sources.list for ISO (${ISO})."
87 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
89 if [ -n "$MIRROR" ] ; then
90 echo "Adding mirror entry (${MIRROR}) to sources.list."
91 echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
94 if [ -n "$MIRROR" ] ; then
95 echo "Adjusting sources.list for mirror (${MIRROR})."
96 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
103 if [ -n "$MIRROR" ] ; then
104 echo "Release matching $RELEASE - enabling LTS support in sources.list"
105 echo "deb $MIRROR ${RELEASE}-lts $COMPONENTS" >> /etc/apt/sources.list
110 # add security.debian.org:
112 unstable|sid|lenny) ;; # no security pool available
114 echo "Adding security.debian.org to sources.list."
115 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
121 # remove local chroot mirror {{{
122 remove_chrootmirror() {
123 if [ -n "$KEEP_SRC_LIST" ] ; then
124 echo "KEEP_SRC_LIST has been set, skipping remove_chrootmirror stage."
128 if [ -n "$ISO" ] ; then
129 echo "Removing ISO (${ISO}) from sources.list."
130 TMP_ISO=$(echo "$ISO" |sed 's#/#\\/#g')
131 sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
133 if [ -n "$MIRROR" ] && echo "$MIRROR" | grep -q 'file:' ; then
134 echo "Removing local mirror (${MIRROR}) from sources.list."
135 TMP_MIRROR=$(echo "$MIRROR" |sed 's#/#\\/#g')
136 sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
137 echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
138 echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
144 # set up grml repository {{{
146 if [ -n "$GRMLREPOS" ] ; then
147 # user might have provided their own apt sources.list
148 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
149 cat >> /etc/apt/sources.list.d/grml.list << EOF
150 # grml: stable repository:
151 deb http://deb.grml.org/ grml-stable main
152 deb-src http://deb.grml.org/ grml-stable main
154 # grml: testing/development repository:
155 deb http://deb.grml.org/ grml-testing main
156 deb-src http://deb.grml.org/ grml-testing main
160 if apt-get update $DPKG_OPTIONS; then
161 apt-get -y --allow-unauthenticated install grml-debian-keyring $DPKG_OPTIONS
162 apt-get update $DPKG_OPTIONS
164 # make sure we have the keys available for aptitude
165 gpg --keyserver subkeys.pgp.net --recv-keys 709BCE51568573EBC160E590F61E2E7CECDEA787
166 gpg --export 709BCE51568573EBC160E590F61E2E7CECDEA787 | apt-key add - || true # not yet sure
167 # why it's necessary, sometimes we get an error even though it works [mika]
170 # make sure we install packages from Grml's pool only if not available
172 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
173 cat >> /etc/apt/preferences << EOF
174 // debian pool (default):
176 Pin: release o=Debian
179 // main grml-repository:
181 Pin: origin deb.grml.org
189 # check available backports release version {{{
191 wget -q -O/dev/null "http://backports.debian.org/debian-backports/dists/${1}-backports/Release"
195 # feature to provide Debian backports repos {{{
197 if [ -n "$BACKPORTREPOS" ] ; then
198 if ! checkbackports "$RELEASE" ; then
199 echo "Backports for ${RELEASE} are not available." >&2
202 # user might have provided their own apt sources.list
203 if ! grep -q backports /etc/apt/sources.list.d/backports.list 2>/dev/null ; then
204 cat >> /etc/apt/sources.list.d/backports.list << EOF
205 # debian backports: ${RELEASE}-backports repository:
206 deb http://backports.debian.org/debian-backports ${RELEASE}-backports main
207 deb-src http://backports.debian.org/debian-backports ${RELEASE}-backports main
215 # set up kernel-img.conf {{{
217 if ! [ -r /etc/kernel-img.conf ] ; then
218 echo "Setting up /etc/kernel-img.conf"
219 cat > /etc/kernel-img.conf << EOF
220 # Kernel Image management overrides
221 # See kernel-img.conf(5) for details
229 # make sure services do not start up {{{
230 install_policy_rcd() {
231 if ! [ -r /usr/sbin/policy-rc.d ] ; then
233 cat > /usr/sbin/policy-rc.d << EOF
237 chmod 775 /usr/sbin/policy-rc.d
242 # make sure we have an up2date system {{{
244 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
245 echo "Running update + upgrade"
247 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
249 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
254 # remove now useless apt cache {{{
256 if [ "$RM_APTCACHE" = 'yes' ] ; then
257 echo "Cleaning apt cache."
258 apt-get clean $DPKG_OPTIONS
260 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
265 # install additional packages {{{
267 # Pre-seed the debconf database with answers. Each question will be marked
268 # as seen to prevent debconf from asking the question interactively.
269 [ -f /etc/debootstrap/debconf-selections ] && {
270 echo "Preseeding the debconf database, some lines might be skipped..."
271 debconf-set-selections < /etc/debootstrap/debconf-selections
274 if [ "$PACKAGES" = 'yes' ] ; then
275 if ! [ -r /etc/debootstrap/packages ] ; then
276 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
280 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
286 # install extra packages {{{
288 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
289 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
290 if [ -n "$PACKAGELIST" ]; then
292 # run apt again to resolve any deps
293 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
299 # check if the specified Debian package exists
301 output=$(apt-cache show "$1" 2>/dev/null)
307 # determine the kernel version postfix
308 get_kernel_version() {
309 # do not override $KERNEL if set via config file
310 if [ -n "$KERNEL" ] ; then
320 lenny|squeeze|wheezy) KARCH='686' ;;
321 # since jessie the linux-image-686 image doesn't exist any longer
322 *) KARCH='686-pae' ;;
329 echo "Only i386 and amd64 are currently supported" >&2
333 for KPREFIX in "" "2.6-" ; do # iterate through the kernel prefixes,
334 # currently "" and "2.6-"
335 if package_exists linux-image-${KPREFIX}${KARCH} ; then
336 echo ${KPREFIX}${KARCH}
343 # install kernel packages {{{
345 if [ -n "$NOKERNEL" ] ; then
346 echo "Skipping installation of kernel packages as requested via --nokernel"
351 KVER=$(get_kernel_version)
352 if [ -n "$KVER" ] ; then
353 # note: install busybox to be able to debug initramfs
354 KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free firmware-linux"
355 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
357 echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
362 # reconfigure packages {{{
364 if [ -n "$RECONFIGURE" ] ; then
365 for package in $RECONFIGURE ; do
366 if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
367 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
368 echo "Warning: $package does not exist, can not reconfigure it."
375 # set password of user root {{{
378 if [ -n "$NOPASSWORD" ] ; then
379 echo "Skip setting root password as requested."
383 echo "Activating shadow passwords."
387 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
391 if [ -n "$ROOTPASSWORD" ] ; then
392 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
393 export ROOTPASSWORD=''
397 echo "Setting password for user root:"
398 while [ "$a" != "$b" ] ; do
399 printf "Enter new UNIX password for user root: "
404 printf "Retype new UNIX password for user root: "
409 if [ "$a" != "$b" ] ; then
410 echo "Sorry, passwords do not match. Retry."
414 echo root:"$a" | chpasswd $CHPASSWD_OPTION
423 # set up /etc/hosts {{{
425 if [ -f /etc/hosts ] ; then
426 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
427 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
429 cat > /etc/hosts << EOF
430 127.0.0.1 localhost $HOSTNAME
433 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
435 # The following lines are desirable for IPv6 capable hosts
436 #::1 ip6-localhost ip6-loopback $HOSTNAME
437 ::1 ip6-localhost ip6-loopback
439 ff00::0 ip6-mcastprefix
441 ff02::2 ip6-allrouters
448 # set default locales {{{
450 if [ -n "$DEFAULT_LOCALES" ] ; then
451 if ! [ -x /usr/sbin/update-locale ] ; then
452 echo "Warning: update-locale executable not available (no locales package installed?)"
453 echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
457 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
462 # adjust timezone {{{
464 if [ -n "$TIMEZONE" ] ; then
465 echo "Adjusting /etc/localtime"
466 ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
471 # helper function for fstab() {{{
473 echo "Setting up /etc/fstab"
474 if [ -n "$TARGET_UUID" ] ; then
475 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
477 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
480 cat >> /etc/fstab << EOF
481 proc /proc proc defaults 0 0
482 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
483 # some other examples:
484 # /dev/sda2 none swap sw,pri=0 0 0
485 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
486 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
487 # linux:/pub /beer nfs defaults 0 0
488 # tmpfs /tmp tmpfs size=300M 0 0
489 # /dev/sda5 none swap sw 0 0
494 # generate /etc/fstab {{{
496 # set up /etc/fstab if file is not present (cdebootstrap)
497 if [ ! -f /etc/fstab ] ; then
501 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
502 if grep -q UNCONFIGURED /etc/fstab ; then
508 # set up hostname {{{
510 if [ -n "$HOSTNAME" ] ; then
511 echo "Setting hostname to ${HOSTNAME}."
512 echo "$HOSTNAME" > /etc/hostname
514 # adjust postfix configuration
515 if [ -r /etc/postfix/main.cf ] ; then
516 # adjust hostname related options:
517 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
519 # listen on loopback interface only:
520 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
521 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
523 if [ -r /etc/mailname ] ; then
524 # adjust /etc/mailname
525 local etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
528 local mailname="$HOSTNAME"
531 local mailname="${HOSTNAME}.${etc_mail_domain}"
534 echo "Setting mailname to ${mailname}"
535 echo "$mailname" > /etc/mailname
541 # generate initrd/initramfs {{{
543 # assume the first available kernel as our main kernel
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"
560 # grub configuration/installation {{{
563 if [ -z "$GRUB" ] ; then
564 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
568 if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
569 echo "Notice: grub option set but no grub-pc package, installing it therefore."
570 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
573 if ! [ -x "$(which grub-install)" ] ; then
574 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
578 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
579 for device in $SELECTED_PARTITIONS ; do
580 GRUB="${device%%[0-9]}"
581 echo "Installing grub on ${GRUB}:"
582 grub-install --no-floppy "$GRUB"
585 echo "Installing grub on ${GRUB}:"
587 lenny|squeeze|wheezy)
588 grub-install --no-floppy "$(readlink -f "${GRUB}")"
591 echo "(hd0) ${GRUB}" > /boot/grub/device.map
593 rm /boot/grub/device.map
598 echo "Adjusting grub configuration for use on ${GRUB}."
600 # finally install grub
601 if [ -x /usr/sbin/update-grub ] ; then
602 UPDATEGRUB='/usr/sbin/update-grub'
603 elif [ -x /sbin/update-grub ] ; then
604 UPDATEGRUB='/sbin/update-grub'
606 echo "Error: update-grub not available, can not execute it." >&2
614 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
616 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
618 for script in /etc/debootstrap/chroot-scripts/* ; do
619 echo "Executing script $script"
620 $script && echo "done" || echo "failed"
625 # make sure we don't have any running processes left {{{
627 for service in ssh mdadm mdadm-raid ; do
628 if [ -x /etc/init.d/"$service" ] ; then
629 /etc/init.d/"$service" stop || true
635 # unmount /proc and make sure nothing is left {{{
637 # make sure we don't leave any sensible data
638 rm -f /etc/debootstrap/variables
640 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
642 umount /proc >/dev/null 2>/dev/null || true
649 [ -n "$1" ] && EXIT="$1" || EXIT="1"
654 # set signal handler {{{
655 trap signal_handler HUP INT QUIT TERM
658 # execute the functions {{{
660 # always execute install_policy_rcd
663 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
664 kernel packages extrapackages reconfigure hosts \
665 default_locales timezone fstab hostname initrd grub_install passwords \
666 custom_scripts upgrade_system remove_apt_cache services \
667 remove_chrootmirror; do
669 $i && stage $i 'done' || exit 1
672 # always execute the finalize stage:
676 # finally exit the chroot {{{
677 echo "Finished chroot installation, exiting."
681 ## END OF FILE #################################################################
682 # vim: ai tw=80 expandtab foldmethod=marker