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 APTUPDATE='aptitude update'
22 # Debian ISOs do not contain signed Release files
23 if [ -n "$ISO" ] ; then
24 APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
25 APTUPGRADE='aptitude -y --allow-untrusted safe-upgrade'
27 APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
28 APTUPGRADE='aptitude -y safe-upgrade'
31 APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
32 APTUPDATE='apt-get update'
33 APTUPGRADE='apt-get --force-yes -y upgrade'
36 if [ -z "$STAGES" ] ; then
37 STAGES='/etc/debootstrap/stages'
38 [ -d "$STAGES" ] || mkdir -p "$STAGES"
42 # helper functions {{{
45 echo "$2" > "$STAGES/$1"
47 elif grep -q done "$STAGES/$1" 2>/dev/null ; then
48 echo " [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
51 echo " Executing stage ${1}"
56 # read -s emulation for dash. result is in $resp.
65 # define chroot mirror {{{
67 if [ -n "$KEEP_SRC_LIST" ] ; then
68 echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
72 if [ -z "$COMPONENTS" ] ; then
73 COMPONENTS='main contrib non-free'
75 echo "Using repository components $COMPONENTS"
77 if [ -n "$ISO" ] ; then
78 echo "Adjusting sources.list for ISO (${ISO})."
79 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
80 echo "Adding mirror entry (${MIRROR}) to sources.list."
81 [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
83 if [ -n "$MIRROR" ] ; then
84 echo "Adjusting sources.list for mirror (${MIRROR})."
85 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
89 # add security.debian.org:
91 unstable|sid) ;; # no security pool available
93 echo "Adding security.debian.org to sources.list."
94 echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
100 # set up grml repository {{{
102 if [ -n "$GRMLREPOS" ] ; then
103 # user might have provided their own apt sources.list
104 if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
105 cat >> /etc/apt/sources.list.d/grml.list << EOF
106 # grml: stable repository:
107 deb http://deb.grml.org/ grml-stable main
108 deb-src http://deb.grml.org/ grml-stable main
110 # grml: testing/development repository:
111 deb http://deb.grml.org/ grml-testing main
112 deb-src http://deb.grml.org/ grml-testing main
116 if apt-get update ; then
117 apt-get -y --allow-unauthenticated install grml-debian-keyring
120 # make sure we have the keys available for aptitude
121 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
122 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
123 # why it's necessary, sometimes we get an error even though it works [mika]
126 # make sure we install packages from Grml's pool only if not available
128 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
129 cat >> /etc/apt/preferences << EOF
130 // debian pool (default):
132 Pin: release o=Debian
135 // main grml-repository:
137 Pin: origin deb.grml.org
145 # set up kernel-img.conf {{{
147 if ! [ -r /etc/kernel-img.conf ] ; then
148 echo "Setting up /etc/kernel-img.conf"
149 cat > /etc/kernel-img.conf << EOF
150 # Kernel Image management overrides
151 # See kernel-img.conf(5) for details
159 # make sure services do not start up {{{
160 install_policy_rcd() {
161 if ! [ -r /usr/sbin/policy-rc.d ] ; then
163 cat > /usr/sbin/policy-rc.d << EOF
167 chmod 775 /usr/sbin/policy-rc.d
172 # make sure we have an up2date system {{{
174 if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
175 echo "Running update + upgrade"
179 echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
184 # remove now useless apt cache {{{
186 if [ "$RM_APTCACHE" = 'yes' ] ; then
187 echo "Cleaning apt cache."
190 echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
195 # install additional packages {{{
197 # Pre-seed the debconf database with answers. Each question will be marked
198 # as seen to prevent debconf from asking the question interactively.
199 [ -f /etc/debootstrap/debconf-selections ] && {
200 echo "Preseeding the debconf database, some lines might be skipped..."
201 cat /etc/debootstrap/debconf-selections | debconf-set-selections
204 if [ "$PACKAGES" = 'yes' ] ; then
205 if ! [ -r /etc/debootstrap/packages ] ; then
206 echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
210 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
216 # install extra packages {{{
218 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
219 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
220 if [ -n "$PACKAGELIST" ]; then
222 # run apt again to resolve any deps
223 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
229 # install kernel packages {{{
231 # do not override $KERNEL if set via config file
232 if [ -z "$KERNEL" ] ; then
233 if [ "$ARCH" = 'i386' ] ; then
235 elif [ "$ARCH" = 'amd64' ] ; then
240 if [ -n "$KERNEL" ] ; then
242 # note: install busybox to be able to debug initramfs
243 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox firmware-linux"
244 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
249 # reconfigure packages {{{
251 if [ -n "$RECONFIGURE" ] ; then
252 for package in $RECONFIGURE ; do
253 if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
254 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
255 echo "Warning: $package does not exist, can not reconfigure it."
262 # set password of user root {{{
265 if [ -n "$NOPASSWORD" ] ; then
266 echo "Skip setting root password as requested."
270 echo "Activating shadow passwords."
274 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
278 if [ -n "$ROOTPASSWORD" ] ; then
279 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
280 export ROOTPASSWORD=''
284 echo "Setting password for user root:"
285 while [ "$a" != "$b" ] ; do
286 echo -n "Enter new UNIX password for user root: "
291 echo -n "Retype new UNIX password for user root: "
296 if [ "$a" != "$b" ] ; then
297 echo "Sorry, passwords do not match. Retry."
301 echo root:"$a" | chpasswd $CHPASSWD_OPTION
310 # set up /etc/hosts {{{
312 if [ -f /etc/hosts ] ; then
313 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
314 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
316 cat > /etc/hosts << EOF
317 127.0.0.1 localhost $HOSTNAME
320 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
322 # The following lines are desirable for IPv6 capable hosts
323 #::1 ip6-localhost ip6-loopback $HOSTNAME
324 ::1 ip6-localhost ip6-loopback
326 ff00::0 ip6-mcastprefix
328 ff02::2 ip6-allrouters
335 # set up /etc/network/interfaces {{{
337 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
338 echo "Setting up /etc/network/interfaces"
339 cat >> /etc/network/interfaces << EOF
342 iface lo inet loopback
346 # iface eth0 inet dhcp
354 # adjust timezone {{{
356 if [ -n "$TIMEZONE" ] ; then
357 echo "Adjusting /etc/localtime"
358 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
363 # helper function for fstab() {{{
365 echo "Setting up /etc/fstab"
366 if [ -n "$TARGET_UUID" ] ; then
367 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
369 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
372 cat >> /etc/fstab << EOF
373 proc /proc proc defaults 0 0
374 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
375 # some other examples:
376 # /dev/sda2 none swap sw,pri=0 0 0
377 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
378 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
379 # linux:/pub /beer nfs defaults 0 0
380 # tmpfs /tmp tmpfs size=300M 0 0
381 # /dev/sda5 none swap sw 0 0
386 # generate /etc/fstab {{{
388 # set up /etc/fstab if file is not present (cdebootstrap)
389 if [ ! -f /etc/fstab ] ; then
393 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
394 if grep -q UNCONFIGURED /etc/fstab ; then
400 # set up hostname {{{
402 if [ -n "$HOSTNAME" ] ; then
403 echo "Setting hostname to ${HOSTNAME}."
404 echo "$HOSTNAME" > /etc/hostname
406 # adjust postfix configuration
407 if [ -r /etc/postfix/main.cf ] ; then
408 # adjust hostname related options:
409 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
411 # listen on loopback interface only:
412 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
413 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
419 # generate initrd/initramfs {{{
421 # assume the first available kernel as our main kernel
422 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
423 if [ -z "$KERNELIMG" ] ; then
424 echo 'No kernel image found, skipping initrd stuff.'>&2
428 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
431 if [ -n "$INITRD" ] ; then
432 echo "Generating initrd."
433 update-initramfs -c -t -k $KERNELVER
438 # grub configuration/installation {{{
441 if [ -z "$GRUB" ] ; then
442 echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
446 if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
447 echo "Notice: grub option set but no grub-pc package, installing it therefore."
448 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
451 if ! [ -x "$(which grub-install)" ] ; then
452 echo "Error: grub-install not available. (Error while installing grub package?)" >&2
456 if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
457 for device in $SELECTED_PARTITIONS ; do
458 GRUB="${device%%[0-9]}"
459 echo "Installing grub on ${GRUB}:"
460 grub-install --no-floppy "$GRUB"
463 echo "Installing grub on ${GRUB}:"
464 grub-install --no-floppy "$GRUB"
467 echo "Adjusting grub configuration for use on ${GRUB}."
469 # finally install grub
470 if [ -x /usr/sbin/update-grub ] ; then
471 UPDATEGRUB='/usr/sbin/update-grub'
472 elif [ -x /sbin/update-grub ] ; then
473 UPDATEGRUB='/sbin/update-grub'
475 echo "Error: update-grub not available, can not execute it." >&2
483 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
485 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
487 for script in /etc/debootstrap/chroot-scripts/* ; do
488 echo "Executing script $script"
489 $script && echo "done" || echo "failed"
494 # make sure we don't have any running processes left {{{
496 for service in ssh mdadm mdadm-raid ; do
497 if [ -x /etc/init.d/"$service" ] ; then
498 /etc/init.d/"$service" stop || true
504 # unmount /proc and make sure nothing is left {{{
506 # make sure we don't leave any sensible data
507 rm -f /etc/debootstrap/variables
509 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
511 umount /proc >/dev/null 2>/dev/null || true
518 [ -n "$1" ] && EXIT="$1" || EXIT="1"
523 # set signal handler {{{
524 trap signal_handler HUP INT QUIT TERM
527 # execute the functions {{{
529 # always execute install_policy_rcd
532 for i in chrootmirror grmlrepos kernelimg_conf \
533 kernel packages extrapackages reconfigure hosts interfaces \
534 timezone fstab hostname initrd grub_install passwords \
535 custom_scripts upgrade_system remove_apt_cache services ; do
537 $i && stage $i done || exit 1
540 # always execute the finalize stage:
544 # finally exit the chroot {{{
545 echo "Finished chroot installation, exiting."
549 ## END OF FILE #################################################################
550 # vim: ai tw=80 expandtab foldmethod=marker