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 # Latest change: Mon Apr 16 18:39:21 CEST 2007 [mika]
8 ################################################################################
10 set -e # exit on any error
12 . /etc/debootstrap/config || exit 1
13 . /etc/debootstrap/variables || exit 1
15 [ -r /proc/1 ] || mount -t proc none /proc
19 # use aptitude only if it's available
20 if [ -x /usr/bin/aptitude ] ; then
21 APTINSTALL='aptitude -y install '
22 APTUPDATE='aptitude update'
24 APTINSTALL='apt-get --force-yes -y install'
25 APTUPDATE='apt-get update'
28 if [ -z "$STAGES" ] ; then
29 STAGES='/etc/debootstrap/stages'
30 [ -d "$STAGES" ] || mkdir -p "$STAGES"
34 # helper functions {{{
37 echo "$2" > "$STAGES/$1"
39 elif grep -q done "$STAGES/$1" 2>/dev/null ; then
40 echo "Notice: stage $1 has been executed already, skipping execution therefore.">&2
46 # define chroot mirror {{{
48 if [ -n "$CHROOTMIRROR" ] ; then
49 echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
54 # set up grml repository {{{
56 if [ -n "$GRMLREPOS" ] ; then
57 cat >> /etc/apt/sources.list << EOF
59 deb http://deb.grml.org/ grml-stable main
60 deb-src http://deb.grml.org/ grml-stable main
62 # testing/development repository:
63 deb http://deb.grml.org/ grml-testing main
64 deb-src http://deb.grml.org/ grml-testing main
71 # set up kernel-img.conf {{{
73 if ! [ -r /etc/kernel-img.conf ] ; then
74 echo "Setting up /etc/kernel-img.conf"
75 cat > /etc/kernel-img.conf << EOF
76 # Kernel Image management overrides
77 # See kernel-img.conf(5) for details
85 # create default devices {{{
87 if ! [ -r /dev/hda20 ] ; then
88 echo "Creating generic devices in /dev - this might take a while..."
89 cd /dev && MAKEDEV generic
94 # install additional packages {{{
96 if [ "$PACKAGES" = 'yes' ] ; then
97 if ! [ -r /etc/debootstrap/packages ] ; then
98 echo "Error: /etc/debootstrap/packages not found, exiting."
102 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(cat /etc/debootstrap/packages) $GRMLPACKAGES
108 # install extra packages {{{
110 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
111 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
112 if [ -n "$PACKAGELIST" ]; then
114 # run apt again to resolve any deps
115 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
121 # sarge specific stuff: mkinitrd {{{
123 if [ "$RELEASE" = 'sarge' ] ; then
124 sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
129 # install kernel packages {{{
131 # do not override $KERNEL if set via config file
132 if [ -z "$KERNEL" ] ; then
133 if [ "$ARCH" = 'i386' ] ; then
135 elif [ "$ARCH" = 'amd64' ] ; then
140 if [ -n "$KERNEL" ] ; then
142 if [ "$RELEASE" = 'sarge' ] ; then
143 KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
145 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
147 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
152 # reconfigure packages {{{
154 if [ -n "$RECONFIGURE" ] ; then
155 for package in $RECONFIGURE ; do
156 dpkg --list $package 1>/dev/null 2>/dev/null && \
157 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
158 echo "Warning: $package does not exist, can not reconfigure it."
164 # set password of user root {{{
166 # Set a password, via chpasswd.
167 # Use perl rather than echo, to avoid the password
168 # showing in the process table. (However, this is normally
169 # only called when first booting the system, when root has no
170 # password at all, so that should be an unnecessary precaution).
172 # Pass in three arguments: the user, the password, and 'true' if the
173 # password has been pre-crypted (by preseeding).
175 # Taken from /var/lib/dpkg/info/passwd.config
179 # This is very annoying. chpasswd cannot handle generating md5
180 # passwords as it is not PAM-aware. Thus, I have to work around
181 # that by crypting the password myself if md5 is used.
185 if [ "$3" = true ]; then
191 LC_ALL=C LANGUAGE=C LANG=C perl -e '
192 sub CreateCryptSalt {
195 my @valid = split(//, "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
198 my $cryptsaltlen = ($md5 ? 8 : 2);
200 open (F, "</dev/urandom") || die "No /dev/urandom found!";
201 foreach (1..$cryptsaltlen) {
203 $out .= $valid[ord($in) % ($#valid + 1)];
206 return ($md5 ? "\$1\$$out\$" : $out);
209 open(P,"| chpasswd -e");
210 if ($ENV{PRECRYPTED}) {
211 print P shift().":$ENV{SETPASSWD_PW}\n";
214 crypt($ENV{SETPASSWD_PW}, CreateCryptSalt($ENV{USE_MD5})).
225 echo "Activating shadow passwords."
228 if [ -n "$ROOTPASSWORD" ] ; then
229 setpassword root "$ROOTPASSWD" false
232 echo "Setting password for user root:"
233 set +e # do not exit if passwd returns error due to missmatching passwords
236 set -e # restore default behaviour again
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 ! [ -f /etc/network/interfaces ] ; then
253 echo "Setting up /etc/network/interfaces"
254 cat >> /etc/network/interfaces << EOF
255 iface lo inet loopback
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 cat > /etc/fstab << EOF
277 $TARGET / auto defaults,errors=remount-ro 0 1
278 /sys /sys sysfs rw,nosuid,nodev,noexec 0 0
279 proc /proc proc defaults 0 0
280 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
281 # some other examples:
282 # /dev/sda2 none swap sw 0 0
283 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
284 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
285 # linux:/pub /beer nfs defaults 0 0
286 # tmpfs /tmp tmpfs size=300M 0 0
291 # generate /etc/fstab {{{
293 # set up /etc/fstab if file is not present (cdebootstrap)
294 if [ ! -f /etc/fstab ] ; then
298 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
299 if grep -q UNCONFIGURED /etc/fstab ; then
305 # set up hostname {{{
307 if [ -n "$HOSTNAME" ] ; then
308 echo "Setting hostname to ${HOSTNAME}."
309 echo "$HOSTNAME" > /etc/hostname
314 # generate initrd/initramfs {{{
316 # assume the first available kernel as our main kernel
317 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
318 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
321 if [ -n "$INITRD" ] ; then
322 if [ "$RELEASE" = 'sarge' ] ; then
323 echo "Release sarge detected, will not create an initrd."
325 echo "Generating initrd."
326 update-initramfs -c -t -k $KERNELVER
327 if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
328 GRUBINITRD="initrd /boot/initrd.img-$KERNELVER"
329 LILOINITRD=" initrd=/boot/initrd.img-$KERNELVER"
336 # grub configuration/installation {{{
338 if [ -z "$GROOT" ] ; then
339 echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
341 echo "Adjusting grub configuration for use on ${GROOT}."
343 # copy stage-files to /boot/grub/
344 [ -d /boot/grub/ ] || mkdir /boot/grub
346 [ -d /usr/lib/grub/i386-pc ] && cp /usr/lib/grub/i386-pc/* /boot/grub/
348 [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
349 # sarge ships grub files in another directory
350 [ "$RELEASE" = 'sarge' ] && cp /lib/grub/i386-pc/* /boot/grub/
352 # finally install grub
353 if [ -x /usr/sbin/update-grub ] ; then
354 UPDATEGRUB='/usr/sbin/update-grub'
356 UPDATEGRUB='/sbin/update-grub'
359 if [ -f /boot/grub/menu.lst ] ; then
360 sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
361 sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
362 # not sure why savedefault does not work for me; any ideas?
363 sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
370 # make sure we don't have any running processes left {{{
372 for service in ssh mdadm mdadm-raid ; do
373 [ -x "/etc/init.d/$service" ] && "/etc/init.d/$service" stop
378 # unmount all filesystems in chroot, make sure nothing is left {{{
380 # make sure we don't leave any sensible data
381 rm -f /etc/debootstrap/variables
382 umount -a 1>/dev/null 2>/dev/null || true
383 umount /proc 1>/dev/null 2>/dev/null || true
384 umount /proc 1>/dev/null 2>/dev/null || true
385 umount -a 1>/dev/null 2>/dev/null || true
389 # execute the functions {{{
390 stage chrootmirror && chrootmirror && stage chrootmirror done
391 stage grmlrepos && grmlrepos && stage grmlrepos done
392 stage kernelimg_conf && kernelimg_conf && stage kernelimg_conf done
393 stage makedev && makedev && stage makedev done
394 stage packages && packages && stage packages done
395 stage extrapackages && extrapackages && stage extrapackages done
396 stage mkinitrd && mkinitrd && stage mkinitrd done
397 stage kernel && kernel && stage kernel done
398 stage reconfigure && reconfigure && stage reconfigure done
399 stage hosts && hosts && stage hosts done
400 stage interfaces && interfaces && stage interfaces done
401 stage timezone && timezone && stage timezone done
402 stage fstab && fstab && stage fstab done
403 stage hostname && hostname && stage hostname done
404 stage initrd && initrd && stage initrd done
405 stage grub && grub && stage grub done
406 stage passwords && passwords && stage passwords done
407 stage services && services && stage services done
408 stage finalize && finalize && stage finalize done
411 # finally exit the chroot {{{
412 echo "Finished chroot installation, exiting."
416 ## END OF FILE #################################################################
417 # vim: ai tw=80 expandtab foldmethod=marker