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
259 # set up /etc/network/interfaces {{{
261 if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
262 echo "Setting up /etc/network/interfaces"
263 cat >> /etc/network/interfaces << EOF
266 iface lo inet loopback
270 # iface eth0 inet dhcp
278 # adjust timezone {{{
280 if [ -n "$TIMEZONE" ] ; then
281 echo "Adjusting /etc/localtime"
282 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
287 # helper function for fstab() {{{
289 echo "Setting up /etc/fstab"
290 if [ -n "$TARGET_UUID" ] ; then
291 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
293 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
296 cat >> /etc/fstab << EOF
297 proc /proc proc defaults 0 0
298 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
299 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
300 # some other examples:
301 # /dev/sda2 none swap sw 0 0
302 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
303 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
304 # linux:/pub /beer nfs defaults 0 0
305 # tmpfs /tmp tmpfs size=300M 0 0
310 # generate /etc/fstab {{{
312 # set up /etc/fstab if file is not present (cdebootstrap)
313 if [ ! -f /etc/fstab ] ; then
317 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
318 if grep -q UNCONFIGURED /etc/fstab ; then
324 # set up hostname {{{
326 if [ -n "$HOSTNAME" ] ; then
327 echo "Setting hostname to ${HOSTNAME}."
328 echo "$HOSTNAME" > /etc/hostname
330 # adjust postfix configuration
331 if [ -r /etc/postfix/main.cf ] ; then
332 sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
338 # generate initrd/initramfs {{{
340 # assume the first available kernel as our main kernel
341 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
342 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
345 if [ -n "$INITRD" ] ; then
346 if [ "$RELEASE" = 'sarge' ] ; then
347 echo "Release sarge detected, will not create an initrd."
349 echo "Generating initrd."
350 update-initramfs -c -t -k $KERNELVER
351 if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
352 GRUBINITRD="initrd /boot/initrd.img-$KERNELVER"
353 LILOINITRD=" initrd=/boot/initrd.img-$KERNELVER"
360 # grub configuration/installation {{{
362 if [ -z "$GROOT" ] ; then
363 echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
365 echo "Adjusting grub configuration for use on ${GROOT}."
367 # copy stage-files to /boot/grub/
368 [ -d /boot/grub/ ] || mkdir /boot/grub
370 [ -d /usr/lib/grub/i386-pc ] && cp /usr/lib/grub/i386-pc/* /boot/grub/
372 [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
373 # sarge ships grub files in another directory
374 [ "$RELEASE" = 'sarge' ] && cp /lib/grub/i386-pc/* /boot/grub/
376 # finally install grub
377 if [ -x /usr/sbin/update-grub ] ; then
378 UPDATEGRUB='/usr/sbin/update-grub'
380 UPDATEGRUB='/sbin/update-grub'
383 if [ -f /boot/grub/menu.lst ] ; then
384 sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
385 if [ -n "$TARGET_UUID" ] ; then
386 sed -i "s|^# kopt=root=.*|# kopt=root=UUID=${TARGET_UUID} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
388 sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
390 # not sure why savedefault does not work for me; any ideas?
391 sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
398 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
400 [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
402 for script in /etc/debootstrap/chroot-scripts/* ; do
403 echo "Executing script $script"
404 $script && echo "done" || echo "failed"
409 # make sure we don't have any running processes left {{{
411 for service in ssh mdadm mdadm-raid ; do
412 if [ -x /etc/init.d/"$service" ] ; then
413 /etc/init.d/"$service" stop || /bin/true
419 # unmount all filesystems in chroot, make sure nothing is left {{{
421 # make sure we don't leave any sensible data
422 rm -f /etc/debootstrap/variables
423 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
424 umount -a 1>/dev/null 2>/dev/null || true
425 umount /proc 1>/dev/null 2>/dev/null || true
426 umount /proc 1>/dev/null 2>/dev/null || true
427 umount -a 1>/dev/null 2>/dev/null || true
431 # execute the functions {{{
432 for i in chrootmirror grmlrepos kernelimg_conf makedev install_policy_rcd \
433 packages extrapackages mkinitrd kernel reconfigure hosts interfaces \
434 timezone fstab hostname initrd grub passwords custom_scripts services ; do
436 $i && stage $i done || exit 1
439 # always execute the finalize stage:
443 # finally exit the chroot {{{
444 echo "Finished chroot installation, exiting."
448 ## END OF FILE #################################################################
449 # vim: ai tw=80 expandtab foldmethod=marker