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 '
21 APTUPDATE='aptitude update'
23 APTINSTALL='apt-get --force-yes -y install'
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 if [ -n "$ISO" ] ; then
51 echo "deb $ISO $RELEASE main contrib" > /etc/apt/sources.list
52 [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE main contrib non-free" >> /etc/apt/sources.list
54 if [ -n "$MIRROR" ] ; then
55 echo "deb $MIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
61 # set up grml repository {{{
63 if [ -n "$GRMLREPOS" ] ; then
64 # user might have provided their own apt sources.list
65 if ! grep -q grml /etc/apt/sources.list 2>/dev/null ; then
66 cat >> /etc/apt/sources.list << EOF
68 # grml: stable repository:
69 deb http://deb.grml.org/ grml-stable main
70 deb-src http://deb.grml.org/ grml-stable main
72 # grml: testing/development repository:
73 deb http://deb.grml.org/ grml-testing main
74 deb-src http://deb.grml.org/ grml-testing main
79 # make sure we have the keys available for aptitude
80 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
81 gpg --export F61E2E7CECDEA787 | apt-key add - || /bin/true # not yet sure
82 # why it's necessary, sometimes we get an error even though it works [mika]
84 # make sure we install packages from grml's pool only if not available
86 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
87 cat >> /etc/apt/preferences << EOF
88 // debian pool (default):
93 // main grml-repository:
95 Pin: origin deb.grml.org
103 # set up kernel-img.conf {{{
105 if ! [ -r /etc/kernel-img.conf ] ; then
106 echo "Setting up /etc/kernel-img.conf"
107 cat > /etc/kernel-img.conf << EOF
108 # Kernel Image management overrides
109 # See kernel-img.conf(5) for details
117 # create default devices {{{
119 if ! [ -r /dev/hda20 ] ; then
120 echo "Creating generic devices in /dev - this might take a while..."
121 cd /dev && MAKEDEV generic
126 # make sure services do not start up {{{
127 install_policy_rcd() {
128 if ! [ -r /usr/sbin/policy-rc.d ] ; then
130 cat > /usr/sbin/policy-rc.d << EOF
134 chmod 775 /usr/sbin/policy-rc.d
139 # install additional packages {{{
141 # Pre-seed the debconf database with answers. Each question will be marked
142 # as seen to prevent debconf from asking the question interactively.
143 [ -f /etc/debootstrap/debconf-selections ] && {
144 echo "Preseeding the debconf database, some lines might be skipped..."
145 cat /etc/debootstrap/debconf-selections | debconf-set-selections
148 if [ "$PACKAGES" = 'yes' ] ; then
149 if ! [ -r /etc/debootstrap/packages ] ; then
150 echo "Error: /etc/debootstrap/packages not found, exiting."
154 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
160 # install extra packages {{{
162 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
163 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
164 if [ -n "$PACKAGELIST" ]; then
166 # run apt again to resolve any deps
167 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
173 # sarge specific stuff: mkinitrd {{{
175 if [ "$RELEASE" = 'sarge' ] ; then
176 sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
181 # install kernel packages {{{
183 # do not override $KERNEL if set via config file
184 if [ -z "$KERNEL" ] ; then
185 if [ "$ARCH" = 'i386' ] ; then
187 elif [ "$ARCH" = 'amd64' ] ; then
192 if [ -n "$KERNEL" ] ; then
194 if [ "$RELEASE" = 'sarge' ] ; then
195 KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
197 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
199 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
204 # reconfigure packages {{{
206 if [ -n "$RECONFIGURE" ] ; then
207 for package in $RECONFIGURE ; do
208 dpkg --list $package 1>/dev/null 2>/dev/null && \
209 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
210 echo "Warning: $package does not exist, can not reconfigure it."
216 # set password of user root {{{
219 echo "Activating shadow passwords."
222 if [ -n "$ROOTPASSWORD" ] ; then
223 echo root:"$ROOTPASSWORD" | chpasswd -m
224 export ROOTPASSWORD=''
228 echo "Setting password for user root:"
229 while [ "$a" != "$b" ] ; do
230 echo -n "Enter new UNIX password for user root: "
233 echo -n "Retype new UNIX password for user root: "
236 if [ "$a" != "$b" ] ; then
237 echo "Sorry, passwords do not match. Retry."
241 echo root:"$a" | chpasswd -m
250 # set up /etc/hosts {{{
252 if [ -f /etc/hosts ] ; then
253 sed -i "s#127.0.0.1 .*#127.0.0.1 localhost $HOSTNAME#" /etc/hosts
254 [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
256 cat > /etc/hosts << EOF
257 127.0.0.1 localhost $HOSTNAME
260 #127.0.1.1 $HOSTNAME.example.org $HOSTNAME
262 # The following lines are desirable for IPv6 capable hosts
263 #::1 ip6-localhost ip6-loopback $HOSTNAME
264 ::1 ip6-localhost ip6-loopback
266 ff00::0 ip6-mcastprefix
268 ff02::2 ip6-allrouters
275 # set up /etc/network/interfaces {{{
277 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
278 echo "Setting up /etc/network/interfaces"
279 cat >> /etc/network/interfaces << EOF
282 iface lo inet loopback
286 # iface eth0 inet dhcp
294 # adjust timezone {{{
296 if [ -n "$TIMEZONE" ] ; then
297 echo "Adjusting /etc/localtime"
298 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
303 # helper function for fstab() {{{
305 echo "Setting up /etc/fstab"
306 if [ -n "$TARGET_UUID" ] ; then
307 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
309 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
312 cat >> /etc/fstab << EOF
313 proc /proc proc defaults 0 0
314 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
315 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
316 # some other examples:
317 # /dev/sda2 none swap sw 0 0
318 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
319 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
320 # linux:/pub /beer nfs defaults 0 0
321 # tmpfs /tmp tmpfs size=300M 0 0
326 # generate /etc/fstab {{{
328 # set up /etc/fstab if file is not present (cdebootstrap)
329 if [ ! -f /etc/fstab ] ; then
333 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
334 if grep -q UNCONFIGURED /etc/fstab ; then
340 # set up hostname {{{
342 if [ -n "$HOSTNAME" ] ; then
343 echo "Setting hostname to ${HOSTNAME}."
344 echo "$HOSTNAME" > /etc/hostname
346 # adjust postfix configuration
347 if [ -r /etc/postfix/main.cf ] ; then
348 # adjust hostname related options:
349 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
350 sed -i "s/^myhostname = .*/myhostname = $HOSTNAME/" /etc/postfix/main.cf
351 sed -i "s/^mydestination = .*/mydestination = localdomain, localhost, localhost.localdomain, $HOSTNAME/" /etc/postfix/main.cf
353 # listen on loopback interface only:
354 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
355 grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
361 # generate initrd/initramfs {{{
363 # assume the first available kernel as our main kernel
364 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
365 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
368 if [ -n "$INITRD" ] ; then
369 if [ "$RELEASE" = 'sarge' ] ; then
370 echo "Release sarge detected, will not create an initrd."
372 echo "Generating initrd."
373 update-initramfs -c -t -k $KERNELVER
374 if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
375 GRUBINITRD="initrd /boot/initrd.img-$KERNELVER"
376 LILOINITRD=" initrd=/boot/initrd.img-$KERNELVER"
383 # grub configuration/installation {{{
385 if [ -z "$GROOT" ] ; then
386 echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
388 echo "Adjusting grub configuration for use on ${GROOT}."
390 # copy stage-files to /boot/grub/
391 [ -d /boot/grub/ ] || mkdir /boot/grub
393 [ -d /usr/lib/grub/i386-pc ] && cp /usr/lib/grub/i386-pc/* /boot/grub/
395 [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
396 # sarge ships grub files in another directory
397 [ "$RELEASE" = 'sarge' ] && cp /lib/grub/i386-pc/* /boot/grub/
399 # finally install grub
400 if [ -x /usr/sbin/update-grub ] ; then
401 UPDATEGRUB='/usr/sbin/update-grub'
403 UPDATEGRUB='/sbin/update-grub'
406 if [ -f /boot/grub/menu.lst ] ; then
407 sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
408 if [ -n "$TARGET_UUID" ] ; then
409 sed -i "s|^# kopt=root=.*|# kopt=root=UUID=${TARGET_UUID} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
411 sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
413 # not sure why savedefault does not work for me; any ideas?
414 sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
421 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
423 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
425 for script in /etc/debootstrap/chroot-scripts/* ; do
426 echo "Executing script $script"
427 $script && echo "done" || echo "failed"
432 # make sure we don't have any running processes left {{{
434 for service in ssh mdadm mdadm-raid ; do
435 if [ -x /etc/init.d/"$service" ] ; then
436 /etc/init.d/"$service" stop || /bin/true
442 # unmount all filesystems in chroot, make sure nothing is left {{{
444 # make sure we don't leave any sensible data
445 rm -f /etc/debootstrap/variables
446 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
447 umount -a 1>/dev/null 2>/dev/null || true
448 umount /proc 1>/dev/null 2>/dev/null || true
449 umount /proc 1>/dev/null 2>/dev/null || true
450 umount -a 1>/dev/null 2>/dev/null || true
454 # execute the functions {{{
455 for i in chrootmirror grmlrepos kernelimg_conf makedev install_policy_rcd \
456 packages extrapackages mkinitrd kernel reconfigure hosts interfaces \
457 timezone fstab hostname initrd grub passwords custom_scripts services ; do
459 $i && stage $i done || exit 1
462 # always execute the finalize stage:
466 # finally exit the chroot {{{
467 echo "Finished chroot installation, exiting."
471 ## END OF FILE #################################################################
472 # vim: ai tw=80 expandtab foldmethod=marker