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
80 echo "Adding mirror entry (${MIRROR}) to sources.list."
81 [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
83 if [ -n "$MIRROR" ] ; then
84 echo "Adjusting sources.list for mirror (${MIRROR})."
85 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
89 # add security.debian.org:
91 unstable|sid|lenny) ;; # no security pool available
93 echo "Adding security.debian.org to sources.list."
94 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
100 # remove local chroot mirror {{{
101 remove_chrootmirror() {
102 if [ -n "$KEEP_SRC_LIST" ] ; then
103 echo "KEEP_SRC_LIST has been set, skipping remove_chrootmirror stage."
107 if [ -n "$ISO" ] ; then
108 echo "Removing ISO (${ISO}) from sources.list."
109 TMP_ISO=$(echo "$ISO" |sed 's#/#\\/#g')
110 sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
112 if [ -n "$MIRROR" -a -n "$(echo $MIRROR|grep file:)" ] ; then
113 echo "Removing local mirror (${MIRROR}) from sources.list."
114 TMP_MIRROR=$(echo "$MIRROR" |sed 's#/#\\/#g')
115 sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
116 echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
117 echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
123 # set up grml repository {{{
125 if [ -n "$GRMLREPOS" ] ; then
126 # user might have provided their own apt sources.list
127 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
128 cat >> /etc/apt/sources.list.d/grml.list << EOF
129 # grml: stable repository:
130 deb http://deb.grml.org/ grml-stable main
131 deb-src http://deb.grml.org/ grml-stable main
133 # grml: testing/development repository:
134 deb http://deb.grml.org/ grml-testing main
135 deb-src http://deb.grml.org/ grml-testing main
139 if apt-get update $DPKG_OPTIONS; then
140 apt-get -y --allow-unauthenticated install grml-debian-keyring $DPKG_OPTIONS
141 apt-get update $DPKG_OPTIONS
143 # make sure we have the keys available for aptitude
144 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
145 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
146 # why it's necessary, sometimes we get an error even though it works [mika]
149 # make sure we install packages from Grml's pool only if not available
151 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
152 cat >> /etc/apt/preferences << EOF
153 // debian pool (default):
155 Pin: release o=Debian
158 // main grml-repository:
160 Pin: origin deb.grml.org
168 # check available backports release version {{{
170 wget -q -O/dev/null http://backports.debian.org/debian-backports/dists/${1}-backports/Release
174 # feature to provide Debian backports repos {{{
176 if [ -n "$BACKPORTREPOS" ] ; then
177 if ! checkbackports $RELEASE ; then
178 echo "Backports for ${RELEASE} are not available." >&2
181 # user might have provided their own apt sources.list
182 if ! grep -q backports /etc/apt/sources.list.d/backports.list 2>/dev/null ; then
183 cat >> /etc/apt/sources.list.d/backports.list << EOF
184 # debian backports: ${RELEASE}-backports repository:
185 deb http://backports.debian.org/debian-backports ${RELEASE}-backports main
186 deb-src http://backports.debian.org/debian-backports ${RELEASE}-backports main
194 # set up kernel-img.conf {{{
196 if ! [ -r /etc/kernel-img.conf ] ; then
197 echo "Setting up /etc/kernel-img.conf"
198 cat > /etc/kernel-img.conf << EOF
199 # Kernel Image management overrides
200 # See kernel-img.conf(5) for details
208 # make sure services do not start up {{{
209 install_policy_rcd() {
210 if ! [ -r /usr/sbin/policy-rc.d ] ; then
212 cat > /usr/sbin/policy-rc.d << EOF
216 chmod 775 /usr/sbin/policy-rc.d
221 # make sure we have an up2date system {{{
223 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
224 echo "Running update + upgrade"
226 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
228 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
233 # remove now useless apt cache {{{
235 if [ "$RM_APTCACHE" = 'yes' ] ; then
236 echo "Cleaning apt cache."
237 apt-get clean $DPKG_OPTIONS
239 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
244 # install additional packages {{{
246 # Pre-seed the debconf database with answers. Each question will be marked
247 # as seen to prevent debconf from asking the question interactively.
248 [ -f /etc/debootstrap/debconf-selections ] && {
249 echo "Preseeding the debconf database, some lines might be skipped..."
250 cat /etc/debootstrap/debconf-selections | debconf-set-selections
253 if [ "$PACKAGES" = 'yes' ] ; then
254 if ! [ -r /etc/debootstrap/packages ] ; then
255 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
259 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
265 # install extra packages {{{
267 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
268 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
269 if [ -n "$PACKAGELIST" ]; then
271 # run apt again to resolve any deps
272 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
278 # check if the specified Debian package exists
280 output=$(apt-cache show "$1" 2>/dev/null)
286 # determine the kernel version postfix
287 get_kernel_version() {
288 # do not override $KERNEL if set via config file
289 if [ -n "$KERNEL" ] ; then
296 amd64) KARCH=amd64 ;;
298 echo "Only i386 and amd64 are currently supported" >&2
302 for KPREFIX in "" "2.6-" ; do # iterate through the kernel prefixes,
303 # currently "" and "2.6-"
304 if package_exists linux-image-${KPREFIX}${KARCH} ; then
305 echo ${KPREFIX}${KARCH}
312 # install kernel packages {{{
314 if [ -n "$NOKERNEL" ] ; then
315 echo "Skipping installation of kernel packages as requested via --nokernel"
320 KVER=$(get_kernel_version)
321 if [ -n "$KVER" ] ; then
322 # note: install busybox to be able to debug initramfs
323 KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free firmware-linux"
324 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
326 echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
331 # reconfigure packages {{{
333 if [ -n "$RECONFIGURE" ] ; then
334 for package in $RECONFIGURE ; do
335 if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
336 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
337 echo "Warning: $package does not exist, can not reconfigure it."
344 # set password of user root {{{
347 if [ -n "$NOPASSWORD" ] ; then
348 echo "Skip setting root password as requested."
352 echo "Activating shadow passwords."
356 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
360 if [ -n "$ROOTPASSWORD" ] ; then
361 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
362 export ROOTPASSWORD=''
366 echo "Setting password for user root:"
367 while [ "$a" != "$b" ] ; do
368 echo -n "Enter new UNIX password for user root: "
373 echo -n "Retype new UNIX password for user root: "
378 if [ "$a" != "$b" ] ; then
379 echo "Sorry, passwords do not match. Retry."
383 echo root:"$a" | chpasswd $CHPASSWD_OPTION
392 # set up /etc/hosts {{{
394 if [ -f /etc/hosts ] ; then
395 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
396 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
398 cat > /etc/hosts << EOF
399 127.0.0.1 localhost $HOSTNAME
402 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
404 # The following lines are desirable for IPv6 capable hosts
405 #::1 ip6-localhost ip6-loopback $HOSTNAME
406 ::1 ip6-localhost ip6-loopback
408 ff00::0 ip6-mcastprefix
410 ff02::2 ip6-allrouters
417 # set default locales {{{
419 if [ -n "$DEFAULT_LOCALES" ] ; then
420 if ! [ -x /usr/sbin/update-locale ] ; then
421 echo "Warning: update-locale executable not available (no locales package installed?)"
422 echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
426 /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
431 # adjust timezone {{{
433 if [ -n "$TIMEZONE" ] ; then
434 echo "Adjusting /etc/localtime"
435 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
440 # helper function for fstab() {{{
442 echo "Setting up /etc/fstab"
443 if [ -n "$TARGET_UUID" ] ; then
444 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
446 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
449 cat >> /etc/fstab << EOF
450 proc /proc proc defaults 0 0
451 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
452 # some other examples:
453 # /dev/sda2 none swap sw,pri=0 0 0
454 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
455 # //1.2.3.4/pub /smb/pub cifs user,noauto,uid=grml,gid=grml 0 0
456 # linux:/pub /beer nfs defaults 0 0
457 # tmpfs /tmp tmpfs size=300M 0 0
458 # /dev/sda5 none swap sw 0 0
463 # generate /etc/fstab {{{
465 # set up /etc/fstab if file is not present (cdebootstrap)
466 if [ ! -f /etc/fstab ] ; then
470 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
471 if grep -q UNCONFIGURED /etc/fstab ; then
477 # set up hostname {{{
479 if [ -n "$HOSTNAME" ] ; then
480 echo "Setting hostname to ${HOSTNAME}."
481 echo "$HOSTNAME" > /etc/hostname
483 # adjust postfix configuration
484 if [ -r /etc/postfix/main.cf ] ; then
485 # adjust hostname related options:
486 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
488 # listen on loopback interface only:
489 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
490 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
492 if [ -r /etc/mailname ] ; then
493 # adjust /etc/mailname
494 local etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
497 local mailname="$HOSTNAME"
500 local mailname="${HOSTNAME}.${etc_mail_domain}"
503 echo "Setting mailname to ${mailname}"
504 echo "$mailname" > /etc/mailname
510 # generate initrd/initramfs {{{
512 # assume the first available kernel as our main kernel
513 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
514 if [ -z "$KERNELIMG" ] ; then
515 echo 'No kernel image found, skipping initrd stuff.'>&2
519 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
522 if [ -n "$INITRD" ] ; then
523 echo "Generating initrd."
524 update-initramfs -c -t -k $KERNELVER
529 # grub configuration/installation {{{
532 if [ -z "$GRUB" ] ; then
533 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
537 if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
538 echo "Notice: grub option set but no grub-pc package, installing it therefore."
539 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
542 if ! [ -x "$(which grub-install)" ] ; then
543 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
547 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
548 for device in $SELECTED_PARTITIONS ; do
549 GRUB="${device%%[0-9]}"
550 echo "Installing grub on ${GRUB}:"
551 grub-install --no-floppy "$GRUB"
554 echo "Installing grub on ${GRUB}:"
555 grub-install --no-floppy "$GRUB"
558 echo "Adjusting grub configuration for use on ${GRUB}."
560 # finally install grub
561 if [ -x /usr/sbin/update-grub ] ; then
562 UPDATEGRUB='/usr/sbin/update-grub'
563 elif [ -x /sbin/update-grub ] ; then
564 UPDATEGRUB='/sbin/update-grub'
566 echo "Error: update-grub not available, can not execute it." >&2
574 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
576 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
578 for script in /etc/debootstrap/chroot-scripts/* ; do
579 echo "Executing script $script"
580 $script && echo "done" || echo "failed"
585 # make sure we don't have any running processes left {{{
587 for service in ssh mdadm mdadm-raid ; do
588 if [ -x /etc/init.d/"$service" ] ; then
589 /etc/init.d/"$service" stop || true
595 # unmount /proc and make sure nothing is left {{{
597 # make sure we don't leave any sensible data
598 rm -f /etc/debootstrap/variables
600 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
602 umount /proc >/dev/null 2>/dev/null || true
609 [ -n "$1" ] && EXIT="$1" || EXIT="1"
614 # set signal handler {{{
615 trap signal_handler HUP INT QUIT TERM
618 # execute the functions {{{
620 # always execute install_policy_rcd
623 for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
624 kernel packages extrapackages reconfigure hosts \
625 default_locales timezone fstab hostname initrd grub_install passwords \
626 custom_scripts upgrade_system remove_apt_cache services \
627 remove_chrootmirror; do
629 $i && stage $i done || exit 1
632 # always execute the finalize stage:
636 # finally exit the chroot {{{
637 echo "Finished chroot installation, exiting."
641 ## END OF FILE #################################################################
642 # vim: ai tw=80 expandtab foldmethod=marker