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 set -e # exit on any error
11 . /etc/debootstrap/config || exit 1
12 . /etc/debootstrap/variables || exit 1
14 [ -r /proc/1 ] || mount -t proc none /proc
18 # use aptitude only if it's available
19 if [ -x /usr/bin/aptitude ] ; then
20 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
21 APTUPDATE='aptitude update'
23 APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
24 APTUPDATE='apt-get update'
27 if [ -z "$STAGES" ] ; then
28 STAGES='/etc/debootstrap/stages'
29 [ -d "$STAGES" ] || mkdir -p "$STAGES"
33 # helper functions {{{
36 echo "$2" > "$STAGES/$1"
38 elif grep -q done "$STAGES/$1" 2>/dev/null ; then
39 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
42 echo " Executing stage ${1}"
47 # read -s emulation for dash. result is in $resp.
56 # define chroot mirror {{{
58 [ -n "$KEEP_SRC_LIST" ] && return
59 [ -z "$COMPONENTS" ] && COMPONENTS='main contrib non-free'
61 if [ -n "$ISO" ] ; then
62 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
63 [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
64 # add security.debian.org:
66 unstable|sid) ;; # no security pool available
68 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
72 if [ -n "$MIRROR" ] ; then
73 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
74 # add security.debian.org:
76 unstable|sid) ;; # no security pool available
78 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
86 # set up grml repository {{{
88 if [ -n "$GRMLREPOS" ] ; then
89 # user might have provided their own apt sources.list
90 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
91 cat >> /etc/apt/sources.list.d/grml.list << EOF
93 # grml: stable repository:
94 deb http://deb.grml.org/ grml-stable main
95 deb-src http://deb.grml.org/ grml-stable main
97 # grml: testing/development repository:
98 deb http://deb.grml.org/ grml-testing main
99 deb-src http://deb.grml.org/ grml-testing main
104 if apt-get update ; then
105 apt-get -y --allow-unauthenticated install grml-debian-keyring
108 # make sure we have the keys available for aptitude
109 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
110 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
111 # why it's necessary, sometimes we get an error even though it works [mika]
114 # make sure we install packages from grml's pool only if not available
116 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
117 cat >> /etc/apt/preferences << EOF
118 // debian pool (default):
120 Pin: release o=Debian
123 // main grml-repository:
125 Pin: origin deb.grml.org
133 # set up kernel-img.conf {{{
135 if ! [ -r /etc/kernel-img.conf ] ; then
136 echo "Setting up /etc/kernel-img.conf"
137 cat > /etc/kernel-img.conf << EOF
138 # Kernel Image management overrides
139 # See kernel-img.conf(5) for details
147 # make sure services do not start up {{{
148 install_policy_rcd() {
149 if ! [ -r /usr/sbin/policy-rc.d ] ; then
151 cat > /usr/sbin/policy-rc.d << EOF
155 chmod 775 /usr/sbin/policy-rc.d
160 # remove now useless apt cache {{{
162 if [ "$RM_APTCACHE" = 'yes' ] ; then
163 echo "Cleaning apt cache."
166 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
171 # install additional packages {{{
173 # Pre-seed the debconf database with answers. Each question will be marked
174 # as seen to prevent debconf from asking the question interactively.
175 [ -f /etc/debootstrap/debconf-selections ] && {
176 echo "Preseeding the debconf database, some lines might be skipped..."
177 cat /etc/debootstrap/debconf-selections | debconf-set-selections
180 if [ "$PACKAGES" = 'yes' ] ; then
181 if ! [ -r /etc/debootstrap/packages ] ; then
182 echo "Error: /etc/debootstrap/packages not found, exiting."
186 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
192 # install extra packages {{{
194 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
195 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
196 if [ -n "$PACKAGELIST" ]; then
198 # run apt again to resolve any deps
199 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
205 # install kernel packages {{{
207 # do not override $KERNEL if set via config file
208 if [ -z "$KERNEL" ] ; then
209 if [ "$ARCH" = 'i386' ] ; then
211 elif [ "$ARCH" = 'amd64' ] ; then
216 if [ -n "$KERNEL" ] ; then
218 # note: install busybox to be able to debug initramfs
219 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox firmware-linux"
220 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
225 # reconfigure packages {{{
227 if [ -n "$RECONFIGURE" ] ; then
228 for package in $RECONFIGURE ; do
229 dpkg --list $package 1>/dev/null 2>/dev/null && \
230 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
231 echo "Warning: $package does not exist, can not reconfigure it."
237 # set password of user root {{{
240 echo "Activating shadow passwords."
244 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
249 if [ -n "$ROOTPASSWORD" ] ; then
250 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
251 export ROOTPASSWORD=''
255 echo "Setting password for user root:"
256 while [ "$a" != "$b" ] ; do
257 echo -n "Enter new UNIX password for user root: "
262 echo -n "Retype new UNIX password for user root: "
267 if [ "$a" != "$b" ] ; then
268 echo "Sorry, passwords do not match. Retry."
272 echo root:"$a" | chpasswd $CHPASSWD_OPTION
281 # set up /etc/hosts {{{
283 if [ -f /etc/hosts ] ; then
284 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
285 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
287 cat > /etc/hosts << EOF
288 127.0.0.1 localhost $HOSTNAME
291 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
293 # The following lines are desirable for IPv6 capable hosts
294 #::1 ip6-localhost ip6-loopback $HOSTNAME
295 ::1 ip6-localhost ip6-loopback
297 ff00::0 ip6-mcastprefix
299 ff02::2 ip6-allrouters
306 # set up /etc/network/interfaces {{{
308 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
309 echo "Setting up /etc/network/interfaces"
310 cat >> /etc/network/interfaces << EOF
313 iface lo inet loopback
317 # iface eth0 inet dhcp
325 # adjust timezone {{{
327 if [ -n "$TIMEZONE" ] ; then
328 echo "Adjusting /etc/localtime"
329 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
334 # helper function for fstab() {{{
336 echo "Setting up /etc/fstab"
337 if [ -n "$TARGET_UUID" ] ; then
338 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
340 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
343 cat >> /etc/fstab << EOF
344 proc /proc proc defaults 0 0
345 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
346 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
347 # some other examples:
348 # /dev/sda2 none swap sw,pri=0 0 0
349 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
350 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
351 # linux:/pub /beer nfs defaults 0 0
352 # tmpfs /tmp tmpfs size=300M 0 0
353 # /dev/sda5 none swap sw 0 0
358 # generate /etc/fstab {{{
360 # set up /etc/fstab if file is not present (cdebootstrap)
361 if [ ! -f /etc/fstab ] ; then
365 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
366 if grep -q UNCONFIGURED /etc/fstab ; then
372 # set up hostname {{{
374 if [ -n "$HOSTNAME" ] ; then
375 echo "Setting hostname to ${HOSTNAME}."
376 echo "$HOSTNAME" > /etc/hostname
378 # adjust postfix configuration
379 if [ -r /etc/postfix/main.cf ] ; then
380 # adjust hostname related options:
381 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
383 # listen on loopback interface only:
384 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
385 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
391 # generate initrd/initramfs {{{
393 # assume the first available kernel as our main kernel
394 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
395 if [ -z "$KERNELIMG" ] ; then
396 echo 'No kernel image found, skipping initrd stuff.'>&2
400 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
403 if [ -n "$INITRD" ] ; then
404 echo "Generating initrd."
405 update-initramfs -c -t -k $KERNELVER
410 # grub configuration/installation {{{
413 if [ -z "$GRUB" ] ; then
414 echo "Notice: \$GRUB not defined, will not install grub therefore."
418 if ! [ -x "$(which grub-install)" ] ; then
419 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
423 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
424 for device in $SELECTED_PARTITIONS ; do
425 GRUB="${device%%[0-9]}"
426 echo "Installing grub on ${GRUB}:"
427 grub-install --no-floppy "$GRUB"
430 echo "Installing grub on ${GRUB}:"
431 grub-install --no-floppy "$GRUB"
434 echo "Adjusting grub configuration for use on ${GRUB}."
436 # finally install grub
437 if [ -x /usr/sbin/update-grub ] ; then
438 UPDATEGRUB='/usr/sbin/update-grub'
439 elif [ -x /sbin/update-grub ] ; then
440 UPDATEGRUB='/sbin/update-grub'
442 echo "Error: update-grub not available, can not execute it." >&2
450 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
452 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
454 for script in /etc/debootstrap/chroot-scripts/* ; do
455 echo "Executing script $script"
456 $script && echo "done" || echo "failed"
461 # make sure we don't have any running processes left {{{
463 for service in ssh mdadm mdadm-raid ; do
464 if [ -x /etc/init.d/"$service" ] ; then
465 /etc/init.d/"$service" stop || true
471 # unmount /proc and make sure nothing is left {{{
473 # make sure we don't leave any sensible data
474 rm -f /etc/debootstrap/variables
476 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
478 umount /proc >/dev/null 2>/dev/null || true
485 [ -n "$1" ] && EXIT="$1" || EXIT="1"
490 # set signal handler {{{
491 trap signal_handler HUP INT QUIT TERM
494 # execute the functions {{{
496 # always execute install_policy_rcd
499 for i in chrootmirror grmlrepos kernelimg_conf \
500 kernel packages extrapackages reconfigure hosts interfaces \
501 timezone fstab hostname initrd grub_install passwords \
502 custom_scripts remove_apt_cache services ; do
504 $i && stage $i done || exit 1
507 # always execute the finalize stage:
511 # finally exit the chroot {{{
512 echo "Finished chroot installation, exiting."
516 ## END OF FILE #################################################################
517 # vim: ai tw=80 expandtab foldmethod=marker