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 "$CHROOTMIRROR" ] && echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" >> /etc/apt/sources.list
54 if [ -n "$CHROOTMIRROR" ] ; then
55 echo "deb $CHROOTMIRROR $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 echo "Setting up /etc/hosts"
254 echo "127.0.0.1 localhost $HOSTNAME" > /etc/hosts
257 if [ -n "$HOSTNAME" ] ; then
258 sed -i "s/grml/$HOSTNAME/g" /etc/hosts
263 # set up /etc/network/interfaces {{{
265 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
266 echo "Setting up /etc/network/interfaces"
267 cat >> /etc/network/interfaces << EOF
270 iface lo inet loopback
274 # iface eth0 inet dhcp
282 # adjust timezone {{{
284 if [ -n "$TIMEZONE" ] ; then
285 echo "Adjusting /etc/localtime"
286 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
291 # helper function for fstab() {{{
293 echo "Setting up /etc/fstab"
294 if [ -n "$TARGET_UUID" ] ; then
295 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
297 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
300 cat >> /etc/fstab << EOF
301 proc /proc proc defaults 0 0
302 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
303 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
304 # some other examples:
305 # /dev/sda2 none swap sw 0 0
306 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
307 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
308 # linux:/pub /beer nfs defaults 0 0
309 # tmpfs /tmp tmpfs size=300M 0 0
314 # generate /etc/fstab {{{
316 # set up /etc/fstab if file is not present (cdebootstrap)
317 if [ ! -f /etc/fstab ] ; then
321 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
322 if grep -q UNCONFIGURED /etc/fstab ; then
328 # set up hostname {{{
330 if [ -n "$HOSTNAME" ] ; then
331 echo "Setting hostname to ${HOSTNAME}."
332 echo "$HOSTNAME" > /etc/hostname
334 # adjust postfix configuration
335 if [ -r /etc/postfix/main.cf ] ; then
336 # adjust hostname related options:
337 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
338 sed -i "s/^myhostname = .*/myhostname = $HOSTNAME/" /etc/postfix/main.cf
339 sed -i "s/^mydestination = .*/mydestination = localdomain, localhost, localhost.localdomain, $HOSTNAME/" /etc/postfix/main.cf
341 # listen on loopback interface only:
342 sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
343 grep inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
349 # generate initrd/initramfs {{{
351 # assume the first available kernel as our main kernel
352 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
353 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
356 if [ -n "$INITRD" ] ; then
357 if [ "$RELEASE" = 'sarge' ] ; then
358 echo "Release sarge detected, will not create an initrd."
360 echo "Generating initrd."
361 update-initramfs -c -t -k $KERNELVER
362 if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
363 GRUBINITRD="initrd /boot/initrd.img-$KERNELVER"
364 LILOINITRD=" initrd=/boot/initrd.img-$KERNELVER"
371 # grub configuration/installation {{{
373 if [ -z "$GROOT" ] ; then
374 echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
376 echo "Adjusting grub configuration for use on ${GROOT}."
378 # copy stage-files to /boot/grub/
379 [ -d /boot/grub/ ] || mkdir /boot/grub
381 [ -d /usr/lib/grub/i386-pc ] && cp /usr/lib/grub/i386-pc/* /boot/grub/
383 [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
384 # sarge ships grub files in another directory
385 [ "$RELEASE" = 'sarge' ] && cp /lib/grub/i386-pc/* /boot/grub/
387 # finally install grub
388 if [ -x /usr/sbin/update-grub ] ; then
389 UPDATEGRUB='/usr/sbin/update-grub'
391 UPDATEGRUB='/sbin/update-grub'
394 if [ -f /boot/grub/menu.lst ] ; then
395 sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
396 if [ -n "$TARGET_UUID" ] ; then
397 sed -i "s|^# kopt=root=.*|# kopt=root=UUID=${TARGET_UUID} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
399 sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
401 # not sure why savedefault does not work for me; any ideas?
402 sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
409 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
411 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
413 for script in /etc/debootstrap/chroot-scripts/* ; do
414 echo "Executing script $script"
415 $script && echo "done" || echo "failed"
420 # make sure we don't have any running processes left {{{
422 for service in ssh mdadm mdadm-raid ; do
423 if [ -x /etc/init.d/"$service" ] ; then
424 /etc/init.d/"$service" stop || /bin/true
430 # unmount all filesystems in chroot, make sure nothing is left {{{
432 # make sure we don't leave any sensible data
433 rm -f /etc/debootstrap/variables
434 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
435 umount -a 1>/dev/null 2>/dev/null || true
436 umount /proc 1>/dev/null 2>/dev/null || true
437 umount /proc 1>/dev/null 2>/dev/null || true
438 umount -a 1>/dev/null 2>/dev/null || true
442 # execute the functions {{{
443 for i in chrootmirror grmlrepos kernelimg_conf makedev install_policy_rcd \
444 packages extrapackages mkinitrd kernel reconfigure hosts interfaces \
445 timezone fstab hostname initrd grub passwords custom_scripts services ; do
447 $i && stage $i done || exit 1
450 # always execute the finalize stage:
454 # finally exit the chroot {{{
455 echo "Finished chroot installation, exiting."
459 ## END OF FILE #################################################################
460 # vim: ai tw=80 expandtab foldmethod=marker