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
45 # define chroot mirror {{{
47 if [ -n "$ISO" ] ; then
48 echo "deb $ISO $RELEASE main contrib" > /etc/apt/sources.list
49 [ -n "$CHROOTMIRROR" ] && echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" >> /etc/apt/sources.list
51 if [ -n "$CHROOTMIRROR" ] ; then
52 echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
58 # set up grml repository {{{
60 if [ -n "$GRMLREPOS" ] ; then
61 cat >> /etc/apt/sources.list << EOF
63 # grml: stable repository:
64 deb http://deb.grml.org/ grml-stable main
65 deb-src http://deb.grml.org/ grml-stable main
67 # grml: testing/development repository:
68 deb http://deb.grml.org/ grml-testing main
69 deb-src http://deb.grml.org/ grml-testing main
72 # make sure we have the keys available for aptitude
73 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
74 gpg --export F61E2E7CECDEA787 | apt-key add - || /bin/true # not yet sure
75 # why it's necessary, sometimes we get an error even though it works [mika]
77 # make sure we install packages from grml's pool only if not available
79 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
80 cat >> /etc/apt/preferences << EOF
81 // debian pool (default):
86 // main grml-repository:
88 Pin: origin deb.grml.org
96 # set up kernel-img.conf {{{
98 if ! [ -r /etc/kernel-img.conf ] ; then
99 echo "Setting up /etc/kernel-img.conf"
100 cat > /etc/kernel-img.conf << EOF
101 # Kernel Image management overrides
102 # See kernel-img.conf(5) for details
110 # create default devices {{{
112 if ! [ -r /dev/hda20 ] ; then
113 echo "Creating generic devices in /dev - this might take a while..."
114 cd /dev && MAKEDEV generic
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 # install additional packages {{{
134 if [ "$PACKAGES" = 'yes' ] ; then
135 if ! [ -r /etc/debootstrap/packages ] ; then
136 echo "Error: /etc/debootstrap/packages not found, exiting."
140 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
146 # install extra packages {{{
148 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
149 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
150 if [ -n "$PACKAGELIST" ]; then
152 # run apt again to resolve any deps
153 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
159 # sarge specific stuff: mkinitrd {{{
161 if [ "$RELEASE" = 'sarge' ] ; then
162 sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
167 # install kernel packages {{{
169 # do not override $KERNEL if set via config file
170 if [ -z "$KERNEL" ] ; then
171 if [ "$ARCH" = 'i386' ] ; then
173 elif [ "$ARCH" = 'amd64' ] ; then
178 if [ -n "$KERNEL" ] ; then
180 if [ "$RELEASE" = 'sarge' ] ; then
181 KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
183 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
185 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
190 # reconfigure packages {{{
192 if [ -n "$RECONFIGURE" ] ; then
193 for package in $RECONFIGURE ; do
194 dpkg --list $package 1>/dev/null 2>/dev/null && \
195 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
196 echo "Warning: $package does not exist, can not reconfigure it."
202 # set password of user root {{{
205 echo "Activating shadow passwords."
208 if [ -n "$ROOTPASSWORD" ] ; then
209 echo root:"$ROOTPASSWORD" | chpasswd -m
210 export ROOTPASSWORD=''
214 echo "Setting password for user root:"
215 while [ "$a" != "$b" ] ; do
216 echo -n "Enter new UNIX password for user root: "
219 echo -n "Retype new UNIX password for user root: "
222 if [ "$a" != "$b" ] ; then
223 echo "Sorry, passwords do not match. Retry."
227 echo root:"$a" | chpasswd -m
236 # set up /etc/hosts {{{
238 if ! [ -f /etc/hosts ] ; then
239 echo "Setting up /etc/hosts"
240 echo "127.0.0.1 localhost $HOSTNAME" > /etc/hosts
245 # set up /etc/network/interfaces {{{
247 if ! [ -r /etc/network/interfaces ] ; then
248 echo "Setting up /etc/network/interfaces"
249 cat >> /etc/network/interfaces << EOF
252 iface lo inet loopback
256 # iface eth0 inet dhcp
264 # adjust timezone {{{
266 if [ -n "$TIMEZONE" ] ; then
267 echo "Adjusting /etc/localtime"
268 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
273 # helper function for fstab() {{{
275 echo "Setting up /etc/fstab"
276 if [ -n "$TARGET_UUID" ] ; then
277 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
279 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
282 cat >> /etc/fstab << EOF
283 proc /proc proc defaults 0 0
284 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
285 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
286 # some other examples:
287 # /dev/sda2 none swap sw 0 0
288 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
289 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
290 # linux:/pub /beer nfs defaults 0 0
291 # tmpfs /tmp tmpfs size=300M 0 0
296 # generate /etc/fstab {{{
298 # set up /etc/fstab if file is not present (cdebootstrap)
299 if [ ! -f /etc/fstab ] ; then
303 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
304 if grep -q UNCONFIGURED /etc/fstab ; then
310 # set up hostname {{{
312 if [ -n "$HOSTNAME" ] ; then
313 echo "Setting hostname to ${HOSTNAME}."
314 echo "$HOSTNAME" > /etc/hostname
319 # generate initrd/initramfs {{{
321 # assume the first available kernel as our main kernel
322 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
323 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
326 if [ -n "$INITRD" ] ; then
327 if [ "$RELEASE" = 'sarge' ] ; then
328 echo "Release sarge detected, will not create an initrd."
330 echo "Generating initrd."
331 update-initramfs -c -t -k $KERNELVER
332 if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
333 GRUBINITRD="initrd /boot/initrd.img-$KERNELVER"
334 LILOINITRD=" initrd=/boot/initrd.img-$KERNELVER"
341 # grub configuration/installation {{{
343 if [ -z "$GROOT" ] ; then
344 echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
346 echo "Adjusting grub configuration for use on ${GROOT}."
348 # copy stage-files to /boot/grub/
349 [ -d /boot/grub/ ] || mkdir /boot/grub
351 [ -d /usr/lib/grub/i386-pc ] && cp /usr/lib/grub/i386-pc/* /boot/grub/
353 [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
354 # sarge ships grub files in another directory
355 [ "$RELEASE" = 'sarge' ] && cp /lib/grub/i386-pc/* /boot/grub/
357 # finally install grub
358 if [ -x /usr/sbin/update-grub ] ; then
359 UPDATEGRUB='/usr/sbin/update-grub'
361 UPDATEGRUB='/sbin/update-grub'
364 if [ -f /boot/grub/menu.lst ] ; then
365 sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
366 if [ -n "$TARGET_UUID" ] ; then
367 sed -i "s|^# kopt=root=.*|# kopt=root=UUID=${TARGET_UUID} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
369 sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
371 # not sure why savedefault does not work for me; any ideas?
372 sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
379 # make sure we don't have any running processes left {{{
381 for service in ssh mdadm mdadm-raid ; do
382 if [ -x /etc/init.d/"$service" ] ; then
383 /etc/init.d/"$service" stop || /bin/true
389 # unmount all filesystems in chroot, make sure nothing is left {{{
391 # make sure we don't leave any sensible data
392 rm -f /etc/debootstrap/variables
393 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
394 umount -a 1>/dev/null 2>/dev/null || true
395 umount /proc 1>/dev/null 2>/dev/null || true
396 umount /proc 1>/dev/null 2>/dev/null || true
397 umount -a 1>/dev/null 2>/dev/null || true
401 # execute the functions {{{
402 for i in chrootmirror grmlrepos kernelimg_conf makedev install_policy_rcd \
403 packages extrapackages mkinitrd kernel reconfigure hosts interfaces \
404 timezone fstab hostname initrd grub passwords services finalize ; do
406 $i && stage $i done || exit 1
411 # finally exit the chroot {{{
412 echo "Finished chroot installation, exiting."
416 ## END OF FILE #################################################################
417 # vim: ai tw=80 expandtab foldmethod=marker