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 [ -n "$KEEP_SRC_LIST" ] && return
57 [ -z "$COMPONENTS" ] && COMPONENTS='main contrib non-free'
59 if [ -n "$ISO" ] ; then
60 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
61 [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
63 if [ -n "$MIRROR" ] ; then
64 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
68 # add security.debian.org:
70 unstable|sid) ;; # no security pool available
72 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
78 # set up grml repository {{{
80 if [ -n "$GRMLREPOS" ] ; then
81 # user might have provided their own apt sources.list
82 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
83 cat >> /etc/apt/sources.list.d/grml.list << EOF
85 # grml: stable repository:
86 deb http://deb.grml.org/ grml-stable main
87 deb-src http://deb.grml.org/ grml-stable main
89 # grml: testing/development repository:
90 deb http://deb.grml.org/ grml-testing main
91 deb-src http://deb.grml.org/ grml-testing main
96 if apt-get update ; then
97 apt-get -y --allow-unauthenticated install grml-debian-keyring
100 # make sure we have the keys available for aptitude
101 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
102 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
103 # why it's necessary, sometimes we get an error even though it works [mika]
106 # make sure we install packages from grml's pool only if not available
108 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
109 cat >> /etc/apt/preferences << EOF
110 // debian pool (default):
112 Pin: release o=Debian
115 // main grml-repository:
117 Pin: origin deb.grml.org
125 # set up kernel-img.conf {{{
127 if ! [ -r /etc/kernel-img.conf ] ; then
128 echo "Setting up /etc/kernel-img.conf"
129 cat > /etc/kernel-img.conf << EOF
130 # Kernel Image management overrides
131 # See kernel-img.conf(5) for details
139 # make sure services do not start up {{{
140 install_policy_rcd() {
141 if ! [ -r /usr/sbin/policy-rc.d ] ; then
143 cat > /usr/sbin/policy-rc.d << EOF
147 chmod 775 /usr/sbin/policy-rc.d
152 # remove now useless apt cache {{{
154 if [ "$RM_APTCACHE" = 'yes' ] ; then
155 echo "Cleaning apt cache."
158 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
163 # install additional packages {{{
165 # Pre-seed the debconf database with answers. Each question will be marked
166 # as seen to prevent debconf from asking the question interactively.
167 [ -f /etc/debootstrap/debconf-selections ] && {
168 echo "Preseeding the debconf database, some lines might be skipped..."
169 cat /etc/debootstrap/debconf-selections | debconf-set-selections
172 if [ "$PACKAGES" = 'yes' ] ; then
173 if ! [ -r /etc/debootstrap/packages ] ; then
174 echo "Error: /etc/debootstrap/packages not found, exiting."
178 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
184 # install extra packages {{{
186 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
187 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
188 if [ -n "$PACKAGELIST" ]; then
190 # run apt again to resolve any deps
191 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
197 # install kernel packages {{{
199 # do not override $KERNEL if set via config file
200 if [ -z "$KERNEL" ] ; then
201 if [ "$ARCH" = 'i386' ] ; then
203 elif [ "$ARCH" = 'amd64' ] ; then
208 if [ -n "$KERNEL" ] ; then
210 # note: install busybox to be able to debug initramfs
211 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox firmware-linux"
212 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
217 # reconfigure packages {{{
219 if [ -n "$RECONFIGURE" ] ; then
220 for package in $RECONFIGURE ; do
221 if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
222 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
223 echo "Warning: $package does not exist, can not reconfigure it."
230 # set password of user root {{{
233 echo "Activating shadow passwords."
237 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
242 if [ -n "$ROOTPASSWORD" ] ; then
243 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
244 export ROOTPASSWORD=''
248 echo "Setting password for user root:"
249 while [ "$a" != "$b" ] ; do
250 echo -n "Enter new UNIX password for user root: "
255 echo -n "Retype new UNIX password for user root: "
260 if [ "$a" != "$b" ] ; then
261 echo "Sorry, passwords do not match. Retry."
265 echo root:"$a" | chpasswd $CHPASSWD_OPTION
274 # set up /etc/hosts {{{
276 if [ -f /etc/hosts ] ; then
277 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
278 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
280 cat > /etc/hosts << EOF
281 127.0.0.1 localhost $HOSTNAME
284 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
286 # The following lines are desirable for IPv6 capable hosts
287 #::1 ip6-localhost ip6-loopback $HOSTNAME
288 ::1 ip6-localhost ip6-loopback
290 ff00::0 ip6-mcastprefix
292 ff02::2 ip6-allrouters
299 # set up /etc/network/interfaces {{{
301 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
302 echo "Setting up /etc/network/interfaces"
303 cat >> /etc/network/interfaces << EOF
306 iface lo inet loopback
310 # iface eth0 inet dhcp
318 # adjust timezone {{{
320 if [ -n "$TIMEZONE" ] ; then
321 echo "Adjusting /etc/localtime"
322 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
327 # helper function for fstab() {{{
329 echo "Setting up /etc/fstab"
330 if [ -n "$TARGET_UUID" ] ; then
331 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
333 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
336 cat >> /etc/fstab << EOF
337 proc /proc proc defaults 0 0
338 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
339 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
340 # some other examples:
341 # /dev/sda2 none swap sw,pri=0 0 0
342 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
343 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
344 # linux:/pub /beer nfs defaults 0 0
345 # tmpfs /tmp tmpfs size=300M 0 0
346 # /dev/sda5 none swap sw 0 0
351 # generate /etc/fstab {{{
353 # set up /etc/fstab if file is not present (cdebootstrap)
354 if [ ! -f /etc/fstab ] ; then
358 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
359 if grep -q UNCONFIGURED /etc/fstab ; then
365 # set up hostname {{{
367 if [ -n "$HOSTNAME" ] ; then
368 echo "Setting hostname to ${HOSTNAME}."
369 echo "$HOSTNAME" > /etc/hostname
371 # adjust postfix configuration
372 if [ -r /etc/postfix/main.cf ] ; then
373 # adjust hostname related options:
374 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
376 # listen on loopback interface only:
377 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
378 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
384 # generate initrd/initramfs {{{
386 # assume the first available kernel as our main kernel
387 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
388 if [ -z "$KERNELIMG" ] ; then
389 echo 'No kernel image found, skipping initrd stuff.'>&2
393 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
396 if [ -n "$INITRD" ] ; then
397 echo "Generating initrd."
398 update-initramfs -c -t -k $KERNELVER
403 # grub configuration/installation {{{
406 if [ -z "$GRUB" ] ; then
407 echo "Notice: \$GRUB not defined, will not install grub therefore."
411 if ! [ -x "$(which grub-install)" ] ; then
412 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
416 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
417 for device in $SELECTED_PARTITIONS ; do
418 GRUB="${device%%[0-9]}"
419 echo "Installing grub on ${GRUB}:"
420 grub-install --no-floppy "$GRUB"
423 echo "Installing grub on ${GRUB}:"
424 grub-install --no-floppy "$GRUB"
427 echo "Adjusting grub configuration for use on ${GRUB}."
429 # finally install grub
430 if [ -x /usr/sbin/update-grub ] ; then
431 UPDATEGRUB='/usr/sbin/update-grub'
432 elif [ -x /sbin/update-grub ] ; then
433 UPDATEGRUB='/sbin/update-grub'
435 echo "Error: update-grub not available, can not execute it." >&2
443 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
445 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
447 for script in /etc/debootstrap/chroot-scripts/* ; do
448 echo "Executing script $script"
449 $script && echo "done" || echo "failed"
454 # make sure we don't have any running processes left {{{
456 for service in ssh mdadm mdadm-raid ; do
457 if [ -x /etc/init.d/"$service" ] ; then
458 /etc/init.d/"$service" stop || true
464 # unmount /proc and make sure nothing is left {{{
466 # make sure we don't leave any sensible data
467 rm -f /etc/debootstrap/variables
469 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
471 umount /proc >/dev/null 2>/dev/null || true
478 [ -n "$1" ] && EXIT="$1" || EXIT="1"
483 # set signal handler {{{
484 trap signal_handler HUP INT QUIT TERM
487 # execute the functions {{{
489 # always execute install_policy_rcd
492 for i in chrootmirror grmlrepos kernelimg_conf \
493 kernel packages extrapackages reconfigure hosts interfaces \
494 timezone fstab hostname initrd grub_install passwords \
495 custom_scripts remove_apt_cache services ; do
497 $i && stage $i done || exit 1
500 # always execute the finalize stage:
504 # finally exit the chroot {{{
505 echo "Finished chroot installation, exiting."
509 ## END OF FILE #################################################################
510 # vim: ai tw=80 expandtab foldmethod=marker