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
62 # add security.debian.org:
64 unstable|sid) ;; # no security pool available
66 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
70 if [ -n "$MIRROR" ] ; then
71 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
72 # add security.debian.org:
74 unstable|sid) ;; # no security pool available
76 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
84 # set up grml repository {{{
86 if [ -n "$GRMLREPOS" ] ; then
87 # user might have provided their own apt sources.list
88 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
89 cat >> /etc/apt/sources.list.d/grml.list << EOF
91 # grml: stable repository:
92 deb http://deb.grml.org/ grml-stable main
93 deb-src http://deb.grml.org/ grml-stable main
95 # grml: testing/development repository:
96 deb http://deb.grml.org/ grml-testing main
97 deb-src http://deb.grml.org/ grml-testing main
102 if apt-get update ; then
103 apt-get -y --allow-unauthenticated install grml-debian-keyring
106 # make sure we have the keys available for aptitude
107 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
108 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
109 # why it's necessary, sometimes we get an error even though it works [mika]
112 # make sure we install packages from grml's pool only if not available
114 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
115 cat >> /etc/apt/preferences << EOF
116 // debian pool (default):
118 Pin: release o=Debian
121 // main grml-repository:
123 Pin: origin deb.grml.org
131 # set up kernel-img.conf {{{
133 if ! [ -r /etc/kernel-img.conf ] ; then
134 echo "Setting up /etc/kernel-img.conf"
135 cat > /etc/kernel-img.conf << EOF
136 # Kernel Image management overrides
137 # See kernel-img.conf(5) for details
145 # make sure services do not start up {{{
146 install_policy_rcd() {
147 if ! [ -r /usr/sbin/policy-rc.d ] ; then
149 cat > /usr/sbin/policy-rc.d << EOF
153 chmod 775 /usr/sbin/policy-rc.d
158 # remove now useless apt cache {{{
160 if [ "$RM_APTCACHE" = 'yes' ] ; then
161 echo "Cleaning apt cache."
164 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
169 # install additional packages {{{
171 # Pre-seed the debconf database with answers. Each question will be marked
172 # as seen to prevent debconf from asking the question interactively.
173 [ -f /etc/debootstrap/debconf-selections ] && {
174 echo "Preseeding the debconf database, some lines might be skipped..."
175 cat /etc/debootstrap/debconf-selections | debconf-set-selections
178 if [ "$PACKAGES" = 'yes' ] ; then
179 if ! [ -r /etc/debootstrap/packages ] ; then
180 echo "Error: /etc/debootstrap/packages not found, exiting."
184 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
190 # install extra packages {{{
192 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
193 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
194 if [ -n "$PACKAGELIST" ]; then
196 # run apt again to resolve any deps
197 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
203 # install kernel packages {{{
205 # do not override $KERNEL if set via config file
206 if [ -z "$KERNEL" ] ; then
207 if [ "$ARCH" = 'i386' ] ; then
209 elif [ "$ARCH" = 'amd64' ] ; then
214 if [ -n "$KERNEL" ] ; then
216 # note: install busybox to be able to debug initramfs
217 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox firmware-linux"
218 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
223 # reconfigure packages {{{
225 if [ -n "$RECONFIGURE" ] ; then
226 for package in $RECONFIGURE ; do
227 if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
228 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
229 echo "Warning: $package does not exist, can not reconfigure it."
236 # set password of user root {{{
239 echo "Activating shadow passwords."
243 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
248 if [ -n "$ROOTPASSWORD" ] ; then
249 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
250 export ROOTPASSWORD=''
254 echo "Setting password for user root:"
255 while [ "$a" != "$b" ] ; do
256 echo -n "Enter new UNIX password for user root: "
261 echo -n "Retype new UNIX password for user root: "
266 if [ "$a" != "$b" ] ; then
267 echo "Sorry, passwords do not match. Retry."
271 echo root:"$a" | chpasswd $CHPASSWD_OPTION
280 # set up /etc/hosts {{{
282 if [ -f /etc/hosts ] ; then
283 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
284 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
286 cat > /etc/hosts << EOF
287 127.0.0.1 localhost $HOSTNAME
290 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
292 # The following lines are desirable for IPv6 capable hosts
293 #::1 ip6-localhost ip6-loopback $HOSTNAME
294 ::1 ip6-localhost ip6-loopback
296 ff00::0 ip6-mcastprefix
298 ff02::2 ip6-allrouters
305 # set up /etc/network/interfaces {{{
307 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
308 echo "Setting up /etc/network/interfaces"
309 cat >> /etc/network/interfaces << EOF
312 iface lo inet loopback
316 # iface eth0 inet dhcp
324 # adjust timezone {{{
326 if [ -n "$TIMEZONE" ] ; then
327 echo "Adjusting /etc/localtime"
328 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
333 # helper function for fstab() {{{
335 echo "Setting up /etc/fstab"
336 if [ -n "$TARGET_UUID" ] ; then
337 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
339 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
342 cat >> /etc/fstab << EOF
343 proc /proc proc defaults 0 0
344 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
345 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
346 # some other examples:
347 # /dev/sda2 none swap sw,pri=0 0 0
348 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
349 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
350 # linux:/pub /beer nfs defaults 0 0
351 # tmpfs /tmp tmpfs size=300M 0 0
352 # /dev/sda5 none swap sw 0 0
357 # generate /etc/fstab {{{
359 # set up /etc/fstab if file is not present (cdebootstrap)
360 if [ ! -f /etc/fstab ] ; then
364 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
365 if grep -q UNCONFIGURED /etc/fstab ; then
371 # set up hostname {{{
373 if [ -n "$HOSTNAME" ] ; then
374 echo "Setting hostname to ${HOSTNAME}."
375 echo "$HOSTNAME" > /etc/hostname
377 # adjust postfix configuration
378 if [ -r /etc/postfix/main.cf ] ; then
379 # adjust hostname related options:
380 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
382 # listen on loopback interface only:
383 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
384 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
390 # generate initrd/initramfs {{{
392 # assume the first available kernel as our main kernel
393 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
394 if [ -z "$KERNELIMG" ] ; then
395 echo 'No kernel image found, skipping initrd stuff.'>&2
399 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
402 if [ -n "$INITRD" ] ; then
403 echo "Generating initrd."
404 update-initramfs -c -t -k $KERNELVER
409 # grub configuration/installation {{{
412 if [ -z "$GRUB" ] ; then
413 echo "Notice: \$GRUB not defined, will not install grub therefore."
417 if ! [ -x "$(which grub-install)" ] ; then
418 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
422 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
423 for device in $SELECTED_PARTITIONS ; do
424 GRUB="${device%%[0-9]}"
425 echo "Installing grub on ${GRUB}:"
426 grub-install --no-floppy "$GRUB"
429 echo "Installing grub on ${GRUB}:"
430 grub-install --no-floppy "$GRUB"
433 echo "Adjusting grub configuration for use on ${GRUB}."
435 # finally install grub
436 if [ -x /usr/sbin/update-grub ] ; then
437 UPDATEGRUB='/usr/sbin/update-grub'
438 elif [ -x /sbin/update-grub ] ; then
439 UPDATEGRUB='/sbin/update-grub'
441 echo "Error: update-grub not available, can not execute it." >&2
449 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
451 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
453 for script in /etc/debootstrap/chroot-scripts/* ; do
454 echo "Executing script $script"
455 $script && echo "done" || echo "failed"
460 # make sure we don't have any running processes left {{{
462 for service in ssh mdadm mdadm-raid ; do
463 if [ -x /etc/init.d/"$service" ] ; then
464 /etc/init.d/"$service" stop || true
470 # unmount /proc and make sure nothing is left {{{
472 # make sure we don't leave any sensible data
473 rm -f /etc/debootstrap/variables
475 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
477 umount /proc >/dev/null 2>/dev/null || true
484 [ -n "$1" ] && EXIT="$1" || EXIT="1"
489 # set signal handler {{{
490 trap signal_handler HUP INT QUIT TERM
493 # execute the functions {{{
495 # always execute install_policy_rcd
498 for i in chrootmirror grmlrepos kernelimg_conf \
499 kernel packages extrapackages reconfigure hosts interfaces \
500 timezone fstab hostname initrd grub_install passwords \
501 custom_scripts remove_apt_cache services ; do
503 $i && stage $i done || exit 1
506 # always execute the finalize stage:
510 # finally exit the chroot {{{
511 echo "Finished chroot installation, exiting."
515 ## END OF FILE #################################################################
516 # vim: ai tw=80 expandtab foldmethod=marker