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 ################################################################################
9 . /etc/debootstrap/config || exit 1
10 . /etc/debootstrap/variables || exit 1
12 [ -r /proc/1 ] || mount -t proc none /proc
16 # use aptitude only if it's available
17 if [ -x /usr/bin/aptitude ] ; then
18 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
19 APTUPDATE='aptitude update'
21 APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
22 APTUPDATE='apt-get update'
25 if [ -z "$STAGES" ] ; then
26 STAGES='/etc/debootstrap/stages'
27 [ -d "$STAGES" ] || mkdir -p "$STAGES"
31 # helper functions {{{
34 echo "$2" > "$STAGES/$1"
36 elif grep -q done "$STAGES/$1" 2>/dev/null ; then
37 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
40 echo " Executing stage ${1}"
45 # read -s emulation for dash. result is in $resp.
54 # define chroot mirror {{{
56 if [ -n "$KEEP_SRC_LIST" ] ; then
57 echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
61 if [ -z "$COMPONENTS" ] ; then
62 COMPONENTS='main contrib non-free'
64 echo "Using repository components $COMPONENTS"
66 if [ -n "$ISO" ] ; then
67 echo "Adjusting sources.list for ISO (${ISO})."
68 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
69 echo "Adding mirror entry (${MIRROR}) to sources.list."
70 [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
72 if [ -n "$MIRROR" ] ; then
73 echo "Adjusting sources.list for mirror (${MIRROR})."
74 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
78 # add security.debian.org:
80 unstable|sid) ;; # no security pool available
82 echo "Adding security.debian.org to sources.list."
83 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
89 # set up grml repository {{{
91 if [ -n "$GRMLREPOS" ] ; then
92 # user might have provided their own apt sources.list
93 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
94 cat >> /etc/apt/sources.list.d/grml.list << EOF
96 # grml: stable repository:
97 deb http://deb.grml.org/ grml-stable main
98 deb-src http://deb.grml.org/ grml-stable main
100 # grml: testing/development repository:
101 deb http://deb.grml.org/ grml-testing main
102 deb-src http://deb.grml.org/ grml-testing main
107 if apt-get update ; then
108 apt-get -y --allow-unauthenticated install grml-debian-keyring
111 # make sure we have the keys available for aptitude
112 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
113 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
114 # why it's necessary, sometimes we get an error even though it works [mika]
117 # make sure we install packages from grml's pool only if not available
119 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
120 cat >> /etc/apt/preferences << EOF
121 // debian pool (default):
123 Pin: release o=Debian
126 // main grml-repository:
128 Pin: origin deb.grml.org
136 # set up kernel-img.conf {{{
138 if ! [ -r /etc/kernel-img.conf ] ; then
139 echo "Setting up /etc/kernel-img.conf"
140 cat > /etc/kernel-img.conf << EOF
141 # Kernel Image management overrides
142 # See kernel-img.conf(5) for details
150 # make sure services do not start up {{{
151 install_policy_rcd() {
152 if ! [ -r /usr/sbin/policy-rc.d ] ; then
154 cat > /usr/sbin/policy-rc.d << EOF
158 chmod 775 /usr/sbin/policy-rc.d
163 # remove now useless apt cache {{{
165 if [ "$RM_APTCACHE" = 'yes' ] ; then
166 echo "Cleaning apt cache."
169 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
174 # install additional packages {{{
176 # Pre-seed the debconf database with answers. Each question will be marked
177 # as seen to prevent debconf from asking the question interactively.
178 [ -f /etc/debootstrap/debconf-selections ] && {
179 echo "Preseeding the debconf database, some lines might be skipped..."
180 cat /etc/debootstrap/debconf-selections | debconf-set-selections
183 if [ "$PACKAGES" = 'yes' ] ; then
184 if ! [ -r /etc/debootstrap/packages ] ; then
185 echo "Error: /etc/debootstrap/packages not found, exiting."
189 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
195 # install extra packages {{{
197 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
198 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
199 if [ -n "$PACKAGELIST" ]; then
201 # run apt again to resolve any deps
202 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
208 # install kernel packages {{{
210 # do not override $KERNEL if set via config file
211 if [ -z "$KERNEL" ] ; then
212 if [ "$ARCH" = 'i386' ] ; then
214 elif [ "$ARCH" = 'amd64' ] ; then
219 if [ -n "$KERNEL" ] ; then
221 # note: install busybox to be able to debug initramfs
222 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox firmware-linux"
223 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
228 # reconfigure packages {{{
230 if [ -n "$RECONFIGURE" ] ; then
231 for package in $RECONFIGURE ; do
232 if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
233 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
234 echo "Warning: $package does not exist, can not reconfigure it."
241 # set password of user root {{{
244 echo "Activating shadow passwords."
248 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
253 if [ -n "$ROOTPASSWORD" ] ; then
254 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
255 export ROOTPASSWORD=''
259 echo "Setting password for user root:"
260 while [ "$a" != "$b" ] ; do
261 echo -n "Enter new UNIX password for user root: "
266 echo -n "Retype new UNIX password for user root: "
271 if [ "$a" != "$b" ] ; then
272 echo "Sorry, passwords do not match. Retry."
276 echo root:"$a" | chpasswd $CHPASSWD_OPTION
285 # set up /etc/hosts {{{
287 if [ -f /etc/hosts ] ; then
288 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
289 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
291 cat > /etc/hosts << EOF
292 127.0.0.1 localhost $HOSTNAME
295 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
297 # The following lines are desirable for IPv6 capable hosts
298 #::1 ip6-localhost ip6-loopback $HOSTNAME
299 ::1 ip6-localhost ip6-loopback
301 ff00::0 ip6-mcastprefix
303 ff02::2 ip6-allrouters
310 # set up /etc/network/interfaces {{{
312 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
313 echo "Setting up /etc/network/interfaces"
314 cat >> /etc/network/interfaces << EOF
317 iface lo inet loopback
321 # iface eth0 inet dhcp
329 # adjust timezone {{{
331 if [ -n "$TIMEZONE" ] ; then
332 echo "Adjusting /etc/localtime"
333 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
338 # helper function for fstab() {{{
340 echo "Setting up /etc/fstab"
341 if [ -n "$TARGET_UUID" ] ; then
342 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
344 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
347 cat >> /etc/fstab << EOF
348 proc /proc proc defaults 0 0
349 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
350 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
351 # some other examples:
352 # /dev/sda2 none swap sw,pri=0 0 0
353 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
354 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
355 # linux:/pub /beer nfs defaults 0 0
356 # tmpfs /tmp tmpfs size=300M 0 0
357 # /dev/sda5 none swap sw 0 0
362 # generate /etc/fstab {{{
364 # set up /etc/fstab if file is not present (cdebootstrap)
365 if [ ! -f /etc/fstab ] ; then
369 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
370 if grep -q UNCONFIGURED /etc/fstab ; then
376 # set up hostname {{{
378 if [ -n "$HOSTNAME" ] ; then
379 echo "Setting hostname to ${HOSTNAME}."
380 echo "$HOSTNAME" > /etc/hostname
382 # adjust postfix configuration
383 if [ -r /etc/postfix/main.cf ] ; then
384 # adjust hostname related options:
385 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
387 # listen on loopback interface only:
388 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
389 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
395 # generate initrd/initramfs {{{
397 # assume the first available kernel as our main kernel
398 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
399 if [ -z "$KERNELIMG" ] ; then
400 echo 'No kernel image found, skipping initrd stuff.'>&2
404 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
407 if [ -n "$INITRD" ] ; then
408 echo "Generating initrd."
409 update-initramfs -c -t -k $KERNELVER
414 # grub configuration/installation {{{
417 if [ -z "$GRUB" ] ; then
418 echo "Notice: \$GRUB not defined, will not install grub therefore."
422 if ! [ -x "$(which grub-install)" ] ; then
423 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
427 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
428 for device in $SELECTED_PARTITIONS ; do
429 GRUB="${device%%[0-9]}"
430 echo "Installing grub on ${GRUB}:"
431 grub-install --no-floppy "$GRUB"
434 echo "Installing grub on ${GRUB}:"
435 grub-install --no-floppy "$GRUB"
438 echo "Adjusting grub configuration for use on ${GRUB}."
440 # finally install grub
441 if [ -x /usr/sbin/update-grub ] ; then
442 UPDATEGRUB='/usr/sbin/update-grub'
443 elif [ -x /sbin/update-grub ] ; then
444 UPDATEGRUB='/sbin/update-grub'
446 echo "Error: update-grub not available, can not execute it." >&2
454 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
456 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
458 for script in /etc/debootstrap/chroot-scripts/* ; do
459 echo "Executing script $script"
460 $script && echo "done" || echo "failed"
465 # make sure we don't have any running processes left {{{
467 for service in ssh mdadm mdadm-raid ; do
468 if [ -x /etc/init.d/"$service" ] ; then
469 /etc/init.d/"$service" stop || true
475 # unmount /proc and make sure nothing is left {{{
477 # make sure we don't leave any sensible data
478 rm -f /etc/debootstrap/variables
480 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
482 umount /proc >/dev/null 2>/dev/null || true
489 [ -n "$1" ] && EXIT="$1" || EXIT="1"
494 # set signal handler {{{
495 trap signal_handler HUP INT QUIT TERM
498 # execute the functions {{{
500 # always execute install_policy_rcd
503 for i in chrootmirror grmlrepos kernelimg_conf \
504 kernel packages extrapackages reconfigure hosts interfaces \
505 timezone fstab hostname initrd grub_install passwords \
506 custom_scripts remove_apt_cache services ; do
508 $i && stage $i done || exit 1
511 # always execute the finalize stage:
515 # finally exit the chroot {{{
516 echo "Finished chroot installation, exiting."
520 ## END OF FILE #################################################################
521 # vim: ai tw=80 expandtab foldmethod=marker