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 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 # define chroot mirror {{{
49 [ -n "$KEEP_SRC_LIST" ] && return
50 [ -z "$COMPONENTS" ] && COMPONENTS='main contrib non-free'
52 if [ -n "$ISO" ] ; then
53 echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
54 [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
56 if [ -n "$MIRROR" ] ; then
57 echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
63 # set up grml repository {{{
65 if [ -n "$GRMLREPOS" ] ; then
66 # user might have provided their own apt sources.list
67 if ! grep -q grml /etc/apt/sources.list 2>/dev/null ; then
68 cat >> /etc/apt/sources.list << EOF
70 # grml: stable repository:
71 deb http://deb.grml.org/ grml-stable main
72 deb-src http://deb.grml.org/ grml-stable main
74 # grml: testing/development repository:
75 deb http://deb.grml.org/ grml-testing main
76 deb-src http://deb.grml.org/ grml-testing main
81 # make sure we have the keys available for aptitude
82 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
83 gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
84 # why it's necessary, sometimes we get an error even though it works [mika]
86 # make sure we install packages from grml's pool only if not available
88 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
89 cat >> /etc/apt/preferences << EOF
90 // debian pool (default):
95 // main grml-repository:
97 Pin: origin deb.grml.org
105 # set up kernel-img.conf {{{
107 if ! [ -r /etc/kernel-img.conf ] ; then
108 echo "Setting up /etc/kernel-img.conf"
109 cat > /etc/kernel-img.conf << EOF
110 # Kernel Image management overrides
111 # See kernel-img.conf(5) for details
119 # make sure services do not start up {{{
120 install_policy_rcd() {
121 if ! [ -r /usr/sbin/policy-rc.d ] ; then
123 cat > /usr/sbin/policy-rc.d << EOF
127 chmod 775 /usr/sbin/policy-rc.d
132 # remove now useless apt cache {{{
134 find /var/cache/apt/archives -name '*.deb' -exec rm -f '{}' \;
138 # install additional packages {{{
140 # Pre-seed the debconf database with answers. Each question will be marked
141 # as seen to prevent debconf from asking the question interactively.
142 [ -f /etc/debootstrap/debconf-selections ] && {
143 echo "Preseeding the debconf database, some lines might be skipped..."
144 cat /etc/debootstrap/debconf-selections | debconf-set-selections
147 if [ "$PACKAGES" = 'yes' ] ; then
148 if ! [ -r /etc/debootstrap/packages ] ; then
149 echo "Error: /etc/debootstrap/packages not found, exiting."
153 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
159 # install extra packages {{{
161 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
162 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
163 if [ -n "$PACKAGELIST" ]; then
165 # run apt again to resolve any deps
166 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
172 # install kernel packages {{{
174 # do not override $KERNEL if set via config file
175 if [ -z "$KERNEL" ] ; then
176 if [ "$ARCH" = 'i386' ] ; then
178 elif [ "$ARCH" = 'amd64' ] ; then
183 if [ -n "$KERNEL" ] ; then
185 # note: install busybox to be able to debug initramfs
186 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox"
187 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
192 # reconfigure packages {{{
194 if [ -n "$RECONFIGURE" ] ; then
195 for package in $RECONFIGURE ; do
196 dpkg --list $package 1>/dev/null 2>/dev/null && \
197 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
198 echo "Warning: $package does not exist, can not reconfigure it."
204 # set password of user root {{{
207 echo "Activating shadow passwords."
211 if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
216 if [ -n "$ROOTPASSWORD" ] ; then
217 echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
218 export ROOTPASSWORD=''
222 echo "Setting password for user root:"
223 while [ "$a" != "$b" ] ; do
224 echo -n "Enter new UNIX password for user root: "
227 echo -n "Retype new UNIX password for user root: "
230 if [ "$a" != "$b" ] ; then
231 echo "Sorry, passwords do not match. Retry."
235 echo root:"$a" | chpasswd $CHPASSWD_OPTION
244 # set up /etc/hosts {{{
246 if [ -f /etc/hosts ] ; then
247 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
248 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
250 cat > /etc/hosts << EOF
251 127.0.0.1 localhost $HOSTNAME
254 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
256 # The following lines are desirable for IPv6 capable hosts
257 #::1 ip6-localhost ip6-loopback $HOSTNAME
258 ::1 ip6-localhost ip6-loopback
260 ff00::0 ip6-mcastprefix
262 ff02::2 ip6-allrouters
269 # set up /etc/network/interfaces {{{
271 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
272 echo "Setting up /etc/network/interfaces"
273 cat >> /etc/network/interfaces << EOF
276 iface lo inet loopback
280 # iface eth0 inet dhcp
288 # adjust timezone {{{
290 if [ -n "$TIMEZONE" ] ; then
291 echo "Adjusting /etc/localtime"
292 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
297 # helper function for fstab() {{{
299 echo "Setting up /etc/fstab"
300 if [ -n "$TARGET_UUID" ] ; then
301 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
303 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
306 cat >> /etc/fstab << EOF
307 proc /proc proc defaults 0 0
308 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
309 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
310 # some other examples:
311 # /dev/sda2 none swap sw,pri=0 0 0
312 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
313 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
314 # linux:/pub /beer nfs defaults 0 0
315 # tmpfs /tmp tmpfs size=300M 0 0
316 # /dev/sda5 none swap sw 0 0
321 # generate /etc/fstab {{{
323 # set up /etc/fstab if file is not present (cdebootstrap)
324 if [ ! -f /etc/fstab ] ; then
328 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
329 if grep -q UNCONFIGURED /etc/fstab ; then
335 # set up hostname {{{
337 if [ -n "$HOSTNAME" ] ; then
338 echo "Setting hostname to ${HOSTNAME}."
339 echo "$HOSTNAME" > /etc/hostname
341 # adjust postfix configuration
342 if [ -r /etc/postfix/main.cf ] ; then
343 # adjust hostname related options:
344 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
346 # listen on loopback interface only:
347 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
348 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
354 # generate initrd/initramfs {{{
356 # assume the first available kernel as our main kernel
357 KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
358 if [ -z "$KERNELIMG" ] ; then
359 echo 'No kernel image found, skipping initrd stuff.'>&2
363 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
366 if [ -n "$INITRD" ] ; then
367 echo "Generating initrd."
368 update-initramfs -c -t -k $KERNELVER
373 # grub configuration/installation {{{
375 if [ -z "$GRUB" ] ; then
376 echo "Warning: \$GRUB is not defined, will not adjust grub configuration therefore."
378 echo "Adjusting grub configuration for use on ${GRUB}."
380 # finally install grub
381 if [ -x /usr/sbin/update-grub ] ; then
382 UPDATEGRUB='/usr/sbin/update-grub'
384 UPDATEGRUB='/sbin/update-grub'
393 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
395 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
397 for script in /etc/debootstrap/chroot-scripts/* ; do
398 echo "Executing script $script"
399 $script && echo "done" || echo "failed"
404 # make sure we don't have any running processes left {{{
406 for service in ssh mdadm mdadm-raid ; do
407 if [ -x /etc/init.d/"$service" ] ; then
408 /etc/init.d/"$service" stop || true
414 # unmount all filesystems in chroot, make sure nothing is left {{{
416 # make sure we don't leave any sensible data
417 rm -f /etc/debootstrap/variables
418 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
419 umount -a 1>/dev/null 2>/dev/null || true
420 umount /proc 1>/dev/null 2>/dev/null || true
421 umount /proc 1>/dev/null 2>/dev/null || true
422 umount -a 1>/dev/null 2>/dev/null || true
429 [ -n "$1" ] && EXIT="$1" || EXIT="1"
434 # set signal handler {{{
435 trap signal_handler HUP INT QUIT TERM
438 # execute the functions {{{
440 # always execute install_policy_rcd
443 for i in chrootmirror grmlrepos kernelimg_conf \
444 remove_apt_cache kernel packages extrapackages reconfigure hosts interfaces \
445 timezone fstab hostname initrd grub_config passwords custom_scripts \
448 $i && stage $i done || exit 1
451 # always execute the finalize stage:
455 # finally exit the chroot {{{
456 echo "Finished chroot installation, exiting."
460 ## END OF FILE #################################################################
461 # vim: ai tw=80 expandtab foldmethod=marker