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 # Pre-seed the debconf database with answers. Each question will be marked
135 # as seen to prevent debconf from asking the question interactively.
136 [ -f /etc/debootstrap/debconf-selections ] && \
137 cat /etc/debootstrap/debconf-selections | debconf-set-selections
139 if [ "$PACKAGES" = 'yes' ] ; then
140 if ! [ -r /etc/debootstrap/packages ] ; then
141 echo "Error: /etc/debootstrap/packages not found, exiting."
145 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
151 # install extra packages {{{
153 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
154 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
155 if [ -n "$PACKAGELIST" ]; then
157 # run apt again to resolve any deps
158 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
164 # sarge specific stuff: mkinitrd {{{
166 if [ "$RELEASE" = 'sarge' ] ; then
167 sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
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 if [ "$RELEASE" = 'sarge' ] ; then
186 KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
188 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
190 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
195 # reconfigure packages {{{
197 if [ -n "$RECONFIGURE" ] ; then
198 for package in $RECONFIGURE ; do
199 dpkg --list $package 1>/dev/null 2>/dev/null && \
200 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
201 echo "Warning: $package does not exist, can not reconfigure it."
207 # set password of user root {{{
210 echo "Activating shadow passwords."
213 if [ -n "$ROOTPASSWORD" ] ; then
214 echo root:"$ROOTPASSWORD" | chpasswd -m
215 export ROOTPASSWORD=''
219 echo "Setting password for user root:"
220 while [ "$a" != "$b" ] ; do
221 echo -n "Enter new UNIX password for user root: "
224 echo -n "Retype new UNIX password for user root: "
227 if [ "$a" != "$b" ] ; then
228 echo "Sorry, passwords do not match. Retry."
232 echo root:"$a" | chpasswd -m
241 # set up /etc/hosts {{{
243 if ! [ -f /etc/hosts ] ; then
244 echo "Setting up /etc/hosts"
245 echo "127.0.0.1 localhost $HOSTNAME" > /etc/hosts
250 # set up /etc/network/interfaces {{{
252 if ! [ -r /etc/network/interfaces ] ; then
253 echo "Setting up /etc/network/interfaces"
254 cat >> /etc/network/interfaces << EOF
257 iface lo inet loopback
261 # iface eth0 inet dhcp
269 # adjust timezone {{{
271 if [ -n "$TIMEZONE" ] ; then
272 echo "Adjusting /etc/localtime"
273 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
278 # helper function for fstab() {{{
280 echo "Setting up /etc/fstab"
281 if [ -n "$TARGET_UUID" ] ; then
282 echo "/dev/disk/by-uuid/${TARGET_UUID} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
284 echo "${TARGET} / auto defaults,errors=remount-ro 0 1" > /etc/fstab
287 cat >> /etc/fstab << EOF
288 proc /proc proc defaults 0 0
289 /sys /sys sysfs noauto,rw,nosuid,nodev,noexec 0 0
290 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
291 # some other examples:
292 # /dev/sda2 none swap sw 0 0
293 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
294 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
295 # linux:/pub /beer nfs defaults 0 0
296 # tmpfs /tmp tmpfs size=300M 0 0
301 # generate /etc/fstab {{{
303 # set up /etc/fstab if file is not present (cdebootstrap)
304 if [ ! -f /etc/fstab ] ; then
308 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
309 if grep -q UNCONFIGURED /etc/fstab ; then
315 # set up hostname {{{
317 if [ -n "$HOSTNAME" ] ; then
318 echo "Setting hostname to ${HOSTNAME}."
319 echo "$HOSTNAME" > /etc/hostname
324 # generate initrd/initramfs {{{
326 # assume the first available kernel as our main kernel
327 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
328 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
331 if [ -n "$INITRD" ] ; then
332 if [ "$RELEASE" = 'sarge' ] ; then
333 echo "Release sarge detected, will not create an initrd."
335 echo "Generating initrd."
336 update-initramfs -c -t -k $KERNELVER
337 if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
338 GRUBINITRD="initrd /boot/initrd.img-$KERNELVER"
339 LILOINITRD=" initrd=/boot/initrd.img-$KERNELVER"
346 # grub configuration/installation {{{
348 if [ -z "$GROOT" ] ; then
349 echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
351 echo "Adjusting grub configuration for use on ${GROOT}."
353 # copy stage-files to /boot/grub/
354 [ -d /boot/grub/ ] || mkdir /boot/grub
356 [ -d /usr/lib/grub/i386-pc ] && cp /usr/lib/grub/i386-pc/* /boot/grub/
358 [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
359 # sarge ships grub files in another directory
360 [ "$RELEASE" = 'sarge' ] && cp /lib/grub/i386-pc/* /boot/grub/
362 # finally install grub
363 if [ -x /usr/sbin/update-grub ] ; then
364 UPDATEGRUB='/usr/sbin/update-grub'
366 UPDATEGRUB='/sbin/update-grub'
369 if [ -f /boot/grub/menu.lst ] ; then
370 sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
371 if [ -n "$TARGET_UUID" ] ; then
372 sed -i "s|^# kopt=root=.*|# kopt=root=UUID=${TARGET_UUID} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
374 sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
376 # not sure why savedefault does not work for me; any ideas?
377 sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
384 # make sure we don't have any running processes left {{{
386 for service in ssh mdadm mdadm-raid ; do
387 if [ -x /etc/init.d/"$service" ] ; then
388 /etc/init.d/"$service" stop || /bin/true
394 # unmount all filesystems in chroot, make sure nothing is left {{{
396 # make sure we don't leave any sensible data
397 rm -f /etc/debootstrap/variables
398 [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
399 umount -a 1>/dev/null 2>/dev/null || true
400 umount /proc 1>/dev/null 2>/dev/null || true
401 umount /proc 1>/dev/null 2>/dev/null || true
402 umount -a 1>/dev/null 2>/dev/null || true
406 # execute the functions {{{
407 for i in chrootmirror grmlrepos kernelimg_conf makedev install_policy_rcd \
408 packages extrapackages mkinitrd kernel reconfigure hosts interfaces \
409 timezone fstab hostname initrd grub passwords services finalize ; do
411 $i && stage $i done || exit 1
416 # finally exit the chroot {{{
417 echo "Finished chroot installation, exiting."
421 ## END OF FILE #################################################################
422 # vim: ai tw=80 expandtab foldmethod=marker