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 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
22 APTUPDATE='aptitude update'
23 APTUPGRADE='aptitude -y safe-upgrade'
25 APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
26 APTUPDATE='apt-get update'
27 APTUPGRADE='apt-get --force-yes -y upgrade'
30 if [ -z "$STAGES" ] ; then
31 STAGES='/etc/debootstrap/stages'
32 [ -d "$STAGES" ] || mkdir -p "$STAGES"
36 # helper functions {{{
39 echo "$2" > "$STAGES/$1"
41 elif grep -q done "$STAGES/$1" 2>/dev/null ; then
42 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
45 echo " Executing stage ${1}"
50 # read -s emulation for dash. result is in $resp.
59 # define chroot mirror {{{
61 if [ -n "$KEEP_SRC_LIST" ] ; then
62 echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
66 if [ -z "$COMPONENTS" ] ; then
67 COMPONENTS='main contrib non-free'
69 echo "Using repository components $COMPONENTS"
71 if [ -n "$ISO" ] ; then
72 echo "Adjusting sources.list for ISO (${ISO})."
73 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
74 echo "Adding mirror entry (${MIRROR}) to sources.list."
75 [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
77 if [ -n "$MIRROR" ] ; then
78 echo "Adjusting sources.list for mirror (${MIRROR})."
79 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
83 # add security.debian.org:
85 unstable|sid) ;; # no security pool available
87 echo "Adding security.debian.org to sources.list."
88 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
94 # set up grml repository {{{
96 if [ -n "$GRMLREPOS" ] ; then
97 # user might have provided their own apt sources.list
98 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
99 cat >> /etc/apt/sources.list.d/grml.list << EOF
100 # grml: stable repository:
101 deb http://deb.grml.org/ grml-stable main
102 deb-src http://deb.grml.org/ grml-stable main
104 # grml: testing/development repository:
105 deb http://deb.grml.org/ grml-testing main
106 deb-src http://deb.grml.org/ grml-testing main
110 if apt-get update ; then
111 apt-get -y --allow-unauthenticated install grml-debian-keyring
114 # make sure we have the keys available for aptitude
115 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
116 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
117 # why it's necessary, sometimes we get an error even though it works [mika]
120 # make sure we install packages from Grml's pool only if not available
122 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
123 cat >> /etc/apt/preferences << EOF
124 // debian pool (default):
126 Pin: release o=Debian
129 // main grml-repository:
131 Pin: origin deb.grml.org
139 # set up kernel-img.conf {{{
141 if ! [ -r /etc/kernel-img.conf ] ; then
142 echo "Setting up /etc/kernel-img.conf"
143 cat > /etc/kernel-img.conf << EOF
144 # Kernel Image management overrides
145 # See kernel-img.conf(5) for details
153 # make sure services do not start up {{{
154 install_policy_rcd() {
155 if ! [ -r /usr/sbin/policy-rc.d ] ; then
157 cat > /usr/sbin/policy-rc.d << EOF
161 chmod 775 /usr/sbin/policy-rc.d
166 # make sure we have an up2date system {{{
168 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
169 echo "Running update + upgrade"
173 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
178 # remove now useless apt cache {{{
180 if [ "$RM_APTCACHE" = 'yes' ] ; then
181 echo "Cleaning apt cache."
184 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
189 # install additional packages {{{
191 # Pre-seed the debconf database with answers. Each question will be marked
192 # as seen to prevent debconf from asking the question interactively.
193 [ -f /etc/debootstrap/debconf-selections ] && {
194 echo "Preseeding the debconf database, some lines might be skipped..."
195 cat /etc/debootstrap/debconf-selections | debconf-set-selections
198 if [ "$PACKAGES" = 'yes' ] ; then
199 if ! [ -r /etc/debootstrap/packages ] ; then
200 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
204 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
210 # install extra packages {{{
212 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
213 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
214 if [ -n "$PACKAGELIST" ]; then
216 # run apt again to resolve any deps
217 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
223 # install kernel packages {{{
225 # do not override $KERNEL if set via config file
226 if [ -z "$KERNEL" ] ; then
227 if [ "$ARCH" = 'i386' ] ; then
229 elif [ "$ARCH" = 'amd64' ] ; then
234 if [ -n "$KERNEL" ] ; then
236 # note: install busybox to be able to debug initramfs
237 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox firmware-linux"
238 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
243 # reconfigure packages {{{
245 if [ -n "$RECONFIGURE" ] ; then
246 for package in $RECONFIGURE ; do
247 if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
248 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
249 echo "Warning: $package does not exist, can not reconfigure it."
256 # set password of user root {{{
259 if [ -n "$NOPASSWORD" ] ; then
260 echo "Skip setting root password as requested."
264 echo "Activating shadow passwords."
268 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
272 if [ -n "$ROOTPASSWORD" ] ; then
273 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
274 export ROOTPASSWORD=''
278 echo "Setting password for user root:"
279 while [ "$a" != "$b" ] ; do
280 echo -n "Enter new UNIX password for user root: "
285 echo -n "Retype new UNIX password for user root: "
290 if [ "$a" != "$b" ] ; then
291 echo "Sorry, passwords do not match. Retry."
295 echo root:"$a" | chpasswd $CHPASSWD_OPTION
304 # set up /etc/hosts {{{
306 if [ -f /etc/hosts ] ; then
307 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
308 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
310 cat > /etc/hosts << EOF
311 127.0.0.1 localhost $HOSTNAME
314 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
316 # The following lines are desirable for IPv6 capable hosts
317 #::1 ip6-localhost ip6-loopback $HOSTNAME
318 ::1 ip6-localhost ip6-loopback
320 ff00::0 ip6-mcastprefix
322 ff02::2 ip6-allrouters
329 # set up /etc/network/interfaces {{{
331 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
332 echo "Setting up /etc/network/interfaces"
333 cat >> /etc/network/interfaces << EOF
336 iface lo inet loopback
340 # iface eth0 inet dhcp
348 # adjust timezone {{{
350 if [ -n "$TIMEZONE" ] ; then
351 echo "Adjusting /etc/localtime"
352 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
357 # helper function for fstab() {{{
359 echo "Setting up /etc/fstab"
360 if [ -n "$TARGET_UUID" ] ; then
361 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
363 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
366 cat >> /etc/fstab << EOF
367 proc /proc proc defaults 0 0
368 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
369 # some other examples:
370 # /dev/sda2 none swap sw,pri=0 0 0
371 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
372 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
373 # linux:/pub /beer nfs defaults 0 0
374 # tmpfs /tmp tmpfs size=300M 0 0
375 # /dev/sda5 none swap sw 0 0
380 # generate /etc/fstab {{{
382 # set up /etc/fstab if file is not present (cdebootstrap)
383 if [ ! -f /etc/fstab ] ; then
387 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
388 if grep -q UNCONFIGURED /etc/fstab ; then
394 # set up hostname {{{
396 if [ -n "$HOSTNAME" ] ; then
397 echo "Setting hostname to ${HOSTNAME}."
398 echo "$HOSTNAME" > /etc/hostname
400 # adjust postfix configuration
401 if [ -r /etc/postfix/main.cf ] ; then
402 # adjust hostname related options:
403 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
405 # listen on loopback interface only:
406 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
407 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
413 # generate initrd/initramfs {{{
415 # assume the first available kernel as our main kernel
416 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
417 if [ -z "$KERNELIMG" ] ; then
418 echo 'No kernel image found, skipping initrd stuff.'>&2
422 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
425 if [ -n "$INITRD" ] ; then
426 echo "Generating initrd."
427 update-initramfs -c -t -k $KERNELVER
432 # grub configuration/installation {{{
435 if [ -z "$GRUB" ] ; then
436 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
440 if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
441 echo "Notice: grub option set but no grub-pc package, installing it therefore."
442 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
445 if ! [ -x "$(which grub-install)" ] ; then
446 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
450 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
451 for device in $SELECTED_PARTITIONS ; do
452 GRUB="${device%%[0-9]}"
453 echo "Installing grub on ${GRUB}:"
454 grub-install --no-floppy "$GRUB"
457 echo "Installing grub on ${GRUB}:"
458 grub-install --no-floppy "$GRUB"
461 echo "Adjusting grub configuration for use on ${GRUB}."
463 # finally install grub
464 if [ -x /usr/sbin/update-grub ] ; then
465 UPDATEGRUB='/usr/sbin/update-grub'
466 elif [ -x /sbin/update-grub ] ; then
467 UPDATEGRUB='/sbin/update-grub'
469 echo "Error: update-grub not available, can not execute it." >&2
477 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
479 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
481 for script in /etc/debootstrap/chroot-scripts/* ; do
482 echo "Executing script $script"
483 $script && echo "done" || echo "failed"
488 # make sure we don't have any running processes left {{{
490 for service in ssh mdadm mdadm-raid ; do
491 if [ -x /etc/init.d/"$service" ] ; then
492 /etc/init.d/"$service" stop || true
498 # unmount /proc and make sure nothing is left {{{
500 # make sure we don't leave any sensible data
501 rm -f /etc/debootstrap/variables
503 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
505 umount /proc >/dev/null 2>/dev/null || true
512 [ -n "$1" ] && EXIT="$1" || EXIT="1"
517 # set signal handler {{{
518 trap signal_handler HUP INT QUIT TERM
521 # execute the functions {{{
523 # always execute install_policy_rcd
526 for i in chrootmirror grmlrepos kernelimg_conf \
527 kernel packages extrapackages reconfigure hosts interfaces \
528 timezone fstab hostname initrd grub_install passwords \
529 custom_scripts upgrade_system remove_apt_cache services ; do
531 $i && stage $i done || exit 1
534 # always execute the finalize stage:
538 # finally exit the chroot {{{
539 echo "Finished chroot installation, exiting."
543 ## END OF FILE #################################################################
544 # vim: ai tw=80 expandtab foldmethod=marker