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 ################################################################################
12 . /etc/debootstrap/config || exit 1
13 . /etc/debootstrap/variables || exit 1
15 [ -r /proc/1 ] || mount -t proc none /proc
19 # use aptitude only if it's available
20 if [ -x /usr/bin/aptitude ] ; then
21 APTUPDATE="aptitude update $DPKG_OPTIONS"
22 # Debian ISOs do not contain signed Release files
23 if [ -n "$ISO" ] ; then
24 APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
25 APTUPGRADE="aptitude -y --allow-untrusted safe-upgrade $DPKG_OPTIONS"
27 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
28 APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
31 APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
32 APTUPDATE="apt-get update $DPKG_OPTIONS"
33 APTUPGRADE="apt-get --force-yes -y upgrade $DPKG_OPTIONS"
36 if [ -z "$STAGES" ] ; then
37 STAGES='/etc/debootstrap/stages'
38 [ -d "$STAGES" ] || mkdir -p "$STAGES"
42 # helper functions {{{
45 echo "$2" > "$STAGES/$1"
47 elif grep -q 'done' "$STAGES/$1" 2>/dev/null ; then
48 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
51 echo " Executing stage ${1}"
56 # read -s emulation for dash. result is in $resp.
58 [ -t 0 ] && stty -echo
65 # define chroot mirror {{{
67 if [ -n "$KEEP_SRC_LIST" ] ; then
68 echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
72 if [ -z "$COMPONENTS" ] ; then
73 COMPONENTS='main contrib non-free'
75 echo "Using repository components $COMPONENTS"
77 if [ -n "$ISO" ] ; then
78 echo "Adjusting sources.list for ISO (${ISO})."
79 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
81 if [ -n "$MIRROR" ] ; then
82 echo "Adding mirror entry (${MIRROR}) to sources.list."
83 echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
86 if [ -n "$MIRROR" ] ; then
87 echo "Adjusting sources.list for mirror (${MIRROR})."
88 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
95 if [ -n "$MIRROR" ] ; then
96 echo "Release matching $RELEASE - enabling LTS support in sources.list"
97 echo "deb $MIRROR ${RELEASE}-lts $COMPONENTS" >> /etc/apt/sources.list
102 # add security.debian.org:
104 unstable|sid|lenny) ;; # no security pool available
106 echo "Adding security.debian.org to sources.list."
107 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
113 # remove local chroot mirror {{{
114 remove_chrootmirror() {
115 if [ -n "$KEEP_SRC_LIST" ] ; then
116 echo "KEEP_SRC_LIST has been set, skipping remove_chrootmirror stage."
120 if [ -n "$ISO" ] ; then
121 echo "Removing ISO (${ISO}) from sources.list."
122 TMP_ISO=$(echo "$ISO" |sed 's#/#\\/#g')
123 sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
125 if [ -n "$MIRROR" ] && echo "$MIRROR" | grep -q 'file:' ; then
126 echo "Removing local mirror (${MIRROR}) from sources.list."
127 TMP_MIRROR=$(echo "$MIRROR" |sed 's#/#\\/#g')
128 sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
129 echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
130 echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
136 # set up grml repository {{{
138 if [ -n "$GRMLREPOS" ] ; then
139 # user might have provided their own apt sources.list
140 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
141 cat >> /etc/apt/sources.list.d/grml.list << EOF
142 # grml: stable repository:
143 deb http://deb.grml.org/ grml-stable main
144 deb-src http://deb.grml.org/ grml-stable main
146 # grml: testing/development repository:
147 deb http://deb.grml.org/ grml-testing main
148 deb-src http://deb.grml.org/ grml-testing main
152 if apt-get update $DPKG_OPTIONS; then
153 apt-get -y --allow-unauthenticated install grml-debian-keyring $DPKG_OPTIONS
154 apt-get update $DPKG_OPTIONS
156 # make sure we have the keys available for aptitude
157 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
158 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
159 # why it's necessary, sometimes we get an error even though it works [mika]
162 # make sure we install packages from Grml's pool only if not available
164 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
165 cat >> /etc/apt/preferences << EOF
166 // debian pool (default):
168 Pin: release o=Debian
171 // main grml-repository:
173 Pin: origin deb.grml.org
181 # check available backports release version {{{
183 wget -q -O/dev/null "http://backports.debian.org/debian-backports/dists/${1}-backports/Release"
187 # feature to provide Debian backports repos {{{
189 if [ -n "$BACKPORTREPOS" ] ; then
190 if ! checkbackports "$RELEASE" ; then
191 echo "Backports for ${RELEASE} are not available." >&2
194 # user might have provided their own apt sources.list
195 if ! grep -q backports /etc/apt/sources.list.d/backports.list 2>/dev/null ; then
196 cat >> /etc/apt/sources.list.d/backports.list << EOF
197 # debian backports: ${RELEASE}-backports repository:
198 deb http://backports.debian.org/debian-backports ${RELEASE}-backports main
199 deb-src http://backports.debian.org/debian-backports ${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 apt-get clean $DPKG_OPTIONS
252 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
257 # install additional packages {{{
259 # Pre-seed the debconf database with answers. Each question will be marked
260 # as seen to prevent debconf from asking the question interactively.
261 [ -f /etc/debootstrap/debconf-selections ] && {
262 echo "Preseeding the debconf database, some lines might be skipped..."
263 debconf-set-selections < /etc/debootstrap/debconf-selections
266 if [ "$PACKAGES" = 'yes' ] ; then
267 if ! [ -r /etc/debootstrap/packages ] ; then
268 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
272 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
278 # install extra packages {{{
280 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
281 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
282 if [ -n "$PACKAGELIST" ]; then
284 # run apt again to resolve any deps
285 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
291 # check if the specified Debian package exists
293 output=$(apt-cache show "$1" 2>/dev/null)
299 # determine the kernel version postfix
300 get_kernel_version() {
301 # do not override $KERNEL if set via config file
302 if [ -n "$KERNEL" ] ; then
312 lenny|squeeze|wheezy) KARCH='686' ;;
313 # since jessie the linux-image-686 image doesn't exist any longer
314 *) KARCH='686-pae' ;;
321 echo "Only i386 and amd64 are currently supported" >&2
325 for KPREFIX in "" "2.6-" ; do # iterate through the kernel prefixes,
326 # currently "" and "2.6-"
327 if package_exists linux-image-${KPREFIX}${KARCH} ; then
328 echo ${KPREFIX}${KARCH}
335 # install kernel packages {{{
337 if [ -n "$NOKERNEL" ] ; then
338 echo "Skipping installation of kernel packages as requested via --nokernel"
343 KVER=$(get_kernel_version)
344 if [ -n "$KVER" ] ; then
345 # note: install busybox to be able to debug initramfs
346 KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free firmware-linux"
347 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
349 echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
354 # reconfigure packages {{{
356 if [ -n "$RECONFIGURE" ] ; then
357 for package in $RECONFIGURE ; do
358 if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
359 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
360 echo "Warning: $package does not exist, can not reconfigure it."
367 # set password of user root {{{
370 if [ -n "$NOPASSWORD" ] ; then
371 echo "Skip setting root password as requested."
375 echo "Activating shadow passwords."
379 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
383 if [ -n "$ROOTPASSWORD" ] ; then
384 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
385 export ROOTPASSWORD=''
389 echo "Setting password for user root:"
390 while [ "$a" != "$b" ] ; do
391 printf "Enter new UNIX password for user root: "
396 printf "Retype new UNIX password for user root: "
401 if [ "$a" != "$b" ] ; then
402 echo "Sorry, passwords do not match. Retry."
406 echo root:"$a" | chpasswd $CHPASSWD_OPTION
415 # set up /etc/hosts {{{
417 if [ -f /etc/hosts ] ; then
418 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
419 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
421 cat > /etc/hosts << EOF
422 127.0.0.1 localhost $HOSTNAME
425 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
427 # The following lines are desirable for IPv6 capable hosts
428 #::1 ip6-localhost ip6-loopback $HOSTNAME
429 ::1 ip6-localhost ip6-loopback
431 ff00::0 ip6-mcastprefix
433 ff02::2 ip6-allrouters
440 # set default locales {{{
442 if [ -n "$DEFAULT_LOCALES" ] ; then
443 if ! [ -x /usr/sbin/update-locale ] ; then
444 echo "Warning: update-locale executable not available (no locales package installed?)"
445 echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
449 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
454 # adjust timezone {{{
456 if [ -n "$TIMEZONE" ] ; then
457 echo "Adjusting /etc/localtime"
458 ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
463 # helper function for fstab() {{{
465 echo "Setting up /etc/fstab"
466 if [ -n "$TARGET_UUID" ] ; then
467 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
469 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
472 cat >> /etc/fstab << EOF
473 proc /proc proc defaults 0 0
474 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
475 # some other examples:
476 # /dev/sda2 none swap sw,pri=0 0 0
477 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
478 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
479 # linux:/pub /beer nfs defaults 0 0
480 # tmpfs /tmp tmpfs size=300M 0 0
481 # /dev/sda5 none swap sw 0 0
486 # generate /etc/fstab {{{
488 # set up /etc/fstab if file is not present (cdebootstrap)
489 if [ ! -f /etc/fstab ] ; then
493 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
494 if grep -q UNCONFIGURED /etc/fstab ; then
500 # set up hostname {{{
502 if [ -n "$HOSTNAME" ] ; then
503 echo "Setting hostname to ${HOSTNAME}."
504 echo "$HOSTNAME" > /etc/hostname
506 # adjust postfix configuration
507 if [ -r /etc/postfix/main.cf ] ; then
508 # adjust hostname related options:
509 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
511 # listen on loopback interface only:
512 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
513 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
515 if [ -r /etc/mailname ] ; then
516 # adjust /etc/mailname
517 local etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
520 local mailname="$HOSTNAME"
523 local mailname="${HOSTNAME}.${etc_mail_domain}"
526 echo "Setting mailname to ${mailname}"
527 echo "$mailname" > /etc/mailname
533 # generate initrd/initramfs {{{
535 # assume the first available kernel as our main kernel
536 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
537 if [ -z "$KERNELIMG" ] ; then
538 echo 'No kernel image found, skipping initrd stuff.'>&2
542 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
545 if [ -n "$INITRD" ] ; then
546 echo "Generating initrd."
547 update-initramfs -c -t -k "$KERNELVER"
552 # grub configuration/installation {{{
555 if [ -z "$GRUB" ] ; then
556 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
560 if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
561 echo "Notice: grub option set but no grub-pc package, installing it therefore."
562 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
565 if ! [ -x "$(which grub-install)" ] ; then
566 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
570 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
571 for device in $SELECTED_PARTITIONS ; do
572 GRUB="${device%%[0-9]}"
573 echo "Installing grub on ${GRUB}:"
574 grub-install --no-floppy "$GRUB"
577 echo "Installing grub on ${GRUB}:"
578 grub-install --no-floppy "$GRUB"
581 echo "Adjusting grub configuration for use on ${GRUB}."
583 # finally install grub
584 if [ -x /usr/sbin/update-grub ] ; then
585 UPDATEGRUB='/usr/sbin/update-grub'
586 elif [ -x /sbin/update-grub ] ; then
587 UPDATEGRUB='/sbin/update-grub'
589 echo "Error: update-grub not available, can not execute it." >&2
597 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
599 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
601 for script in /etc/debootstrap/chroot-scripts/* ; do
602 echo "Executing script $script"
603 $script && echo "done" || echo "failed"
608 # make sure we don't have any running processes left {{{
610 for service in ssh mdadm mdadm-raid ; do
611 if [ -x /etc/init.d/"$service" ] ; then
612 /etc/init.d/"$service" stop || true
618 # unmount /proc and make sure nothing is left {{{
620 # make sure we don't leave any sensible data
621 rm -f /etc/debootstrap/variables
623 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
625 umount /proc >/dev/null 2>/dev/null || true
632 [ -n "$1" ] && EXIT="$1" || EXIT="1"
637 # set signal handler {{{
638 trap signal_handler HUP INT QUIT TERM
641 # execute the functions {{{
643 # always execute install_policy_rcd
646 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
647 kernel packages extrapackages reconfigure hosts \
648 default_locales timezone fstab hostname initrd grub_install passwords \
649 custom_scripts upgrade_system remove_apt_cache services \
650 remove_chrootmirror; do
652 $i && stage $i 'done' || exit 1
655 # always execute the finalize stage:
659 # finally exit the chroot {{{
660 echo "Finished chroot installation, exiting."
664 ## END OF FILE #################################################################
665 # vim: ai tw=80 expandtab foldmethod=marker