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 echo 'deb http://grml.org/repos/ ./' >> /etc/apt/sources.list
62 # set up kernel-img.conf {{{
64 if ! [ -r /etc/kernel-img.conf ] ; then
65 echo "Setting up /etc/kernel-img.conf"
66 cat > /etc/kernel-img.conf << EOF
67 # Kernel Image management overrides
68 # See kernel-img.conf(5) for details
76 # create default devices {{{
78 if ! [ -r /dev/hda20 ] ; then
79 echo "Creating generic devices in /dev - this might take a while..."
80 cd /dev && MAKEDEV generic
85 # install additional packages {{{
87 if [ "$PACKAGES" = 'yes' ] ; then
88 if ! [ -r /etc/debootstrap/packages ] ; then
89 echo "Error: /etc/debootstrap/packages not found, exiting."
93 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(cat /etc/debootstrap/packages) $GRMLPACKAGES
99 # install extra packages {{{
101 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
102 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
103 if [ -n "$PACKAGELIST" ]; then
105 # run apt again to resolve any deps
106 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
112 # sarge specific stuff: mkinitrd {{{
114 if [ "$RELEASE" = 'sarge' ] ; then
115 sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
120 # install kernel packages {{{
122 # do not override $KERNEL if set via config file
123 if [ -z "$KERNEL" ] ; then
124 if [ "$ARCH" = 'i386' ] ; then
126 elif [ "$ARCH" = 'amd64' ] ; then
131 if [ -n "$KERNEL" ] ; then
133 if [ "$RELEASE" = 'sarge' ] ; then
134 KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
136 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
138 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
143 # reconfigure packages {{{
145 if [ -n "$RECONFIGURE" ] ; then
146 for package in $RECONFIGURE ; do
147 dpkg --list $package 1>/dev/null 2>/dev/null && \
148 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
149 echo "Warning: $package does not exist, can not reconfigure it."
155 # set password of user root {{{
157 # Set a password, via chpasswd.
158 # Use perl rather than echo, to avoid the password
159 # showing in the process table. (However, this is normally
160 # only called when first booting the system, when root has no
161 # password at all, so that should be an unnecessary precaution).
163 # Pass in three arguments: the user, the password, and 'true' if the
164 # password has been pre-crypted (by preseeding).
166 # Taken from /var/lib/dpkg/info/passwd.config
170 # This is very annoying. chpasswd cannot handle generating md5
171 # passwords as it is not PAM-aware. Thus, I have to work around
172 # that by crypting the password myself if md5 is used.
176 if [ "$3" = true ]; then
182 LC_ALL=C LANGUAGE=C LANG=C perl -e '
183 sub CreateCryptSalt {
186 my @valid = split(//, "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
189 my $cryptsaltlen = ($md5 ? 8 : 2);
191 open (F, "</dev/urandom") || die "No /dev/urandom found!";
192 foreach (1..$cryptsaltlen) {
194 $out .= $valid[ord($in) % ($#valid + 1)];
197 return ($md5 ? "\$1\$$out\$" : $out);
200 open(P,"| chpasswd -e");
201 if ($ENV{PRECRYPTED}) {
202 print P shift().":$ENV{SETPASSWD_PW}\n";
205 crypt($ENV{SETPASSWD_PW}, CreateCryptSalt($ENV{USE_MD5})).
216 echo "Activating shadow passwords."
219 if [ -n "$ROOTPASSWORD" ] ; then
220 setpassword root "$ROOTPASSWD" false
223 echo "Setting password for user root:"
224 set +e # do not exit if passwd returns error due to missmatching passwords
227 set -e # restore default behaviour again
232 # set up /etc/hosts {{{
234 if ! [ -f /etc/hosts ] ; then
235 echo "Setting up /etc/hosts"
236 echo "127.0.0.1 localhost $HOSTNAME" > /etc/hosts
241 # set up /etc/network/interfaces {{{
243 if ! [ -f /etc/network/interfaces ] ; then
244 echo "Setting up /etc/network/interfaces"
245 cat >> /etc/network/interfaces << EOF
246 iface lo inet loopback
255 # adjust timezone {{{
257 if [ -n "$TIMEZONE" ] ; then
258 echo "Adjusting /etc/localtime"
259 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
264 # helper function for fstab() {{{
266 echo "Setting up /etc/fstab"
267 cat > /etc/fstab << EOF
268 $TARGET / auto defaults,errors=remount-ro 0 1
269 /sys /sys sysfs rw,nosuid,nodev,noexec 0 0
270 proc /proc proc defaults 0 0
271 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
272 # some other examples:
273 # /dev/sda2 none swap sw 0 0
274 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
275 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
276 # linux:/pub /beer nfs defaults 0 0
277 # tmpfs /tmp tmpfs size=300M 0 0
282 # generate /etc/fstab {{{
284 # set up /etc/fstab if file is not present (cdebootstrap)
285 if [ ! -f /etc/fstab ] ; then
289 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
290 if grep -q UNCONFIGURED /etc/fstab ; then
296 # set up hostname {{{
298 if [ -n "$HOSTNAME" ] ; then
299 echo "Setting hostname to ${HOSTNAME}."
300 echo "$HOSTNAME" > /etc/hostname
305 # generate initrd/initramfs {{{
307 # assume the first available kernel as our main kernel
308 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
309 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
312 if [ -n "$INITRD" ] ; then
313 if [ "$RELEASE" = 'sarge' ] ; then
314 echo "Release sarge detected, will not create an initrd."
316 echo "Generating initrd."
317 update-initramfs -c -t -k $KERNELVER
318 if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
319 GRUBINITRD="initrd /boot/initrd.img-$KERNELVER"
320 LILOINITRD=" initrd=/boot/initrd.img-$KERNELVER"
327 # grub configuration/installation {{{
329 if [ -z "$GROOT" ] ; then
330 echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
332 echo "Adjusting grub configuration for use on ${GROOT}."
334 # copy stage-files to /boot/grub/
335 [ -d /boot/grub/ ] || mkdir /boot/grub
337 [ -d /usr/lib/grub/i386-pc ] && cp /usr/lib/grub/i386-pc/* /boot/grub/
339 [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
340 # sarge ships grub files in another directory
341 [ "$RELEASE" = 'sarge' ] && cp /lib/grub/i386-pc/* /boot/grub/
343 # finally install grub
344 if [ -x /usr/sbin/update-grub ] ; then
345 UPDATEGRUB='/usr/sbin/update-grub'
347 UPDATEGRUB='/sbin/update-grub'
350 if [ -f /boot/grub/menu.lst ] ; then
351 sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
352 sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
353 # not sure why savedefault does not work for me; any ideas?
354 sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
361 # make sure we don't have any running processes left {{{
363 for service in ssh mdadm mdadm-raid ; do
364 [ -x "/etc/init.d/$service" ] && "/etc/init.d/$service" stop
369 # unmount all filesystems in chroot, make sure nothing is left {{{
371 # make sure we don't leave any sensible data
372 rm -f /etc/debootstrap/variables
373 umount -a 1>/dev/null 2>/dev/null || true
374 umount /proc 1>/dev/null 2>/dev/null || true
375 umount /proc 1>/dev/null 2>/dev/null || true
376 umount -a 1>/dev/null 2>/dev/null || true
380 # execute the functions {{{
381 stage chrootmirror && chrootmirror && stage chrootmirror done
382 stage grmlrepos && grmlrepos && stage grmlrepos done
383 stage kernelimg_conf && kernelimg_conf && stage kernelimg_conf done
384 stage makedev && makedev && stage makedev done
385 stage packages && packages && stage packages done
386 stage extrapackages && extrapackages && stage extrapackages done
387 stage mkinitrd && mkinitrd && stage mkinitrd done
388 stage kernel && kernel && stage kernel done
389 stage reconfigure && reconfigure && stage reconfigure done
390 stage hosts && hosts && stage hosts done
391 stage interfaces && interfaces && stage interfaces done
392 stage timezone && timezone && stage timezone done
393 stage fstab && fstab && stage fstab done
394 stage hostname && hostname && stage hostname done
395 stage initrd && initrd && stage initrd done
396 stage grub && grub && stage grub done
397 stage passwords && passwords && stage passwords done
398 stage services && services && stage services done
399 stage finalize && finalize && stage finalize done
402 # finally exit the chroot {{{
403 echo "Finished chroot installation, exiting."
407 ## END OF FILE #################################################################
408 # vim: ai tw=80 expandtab foldmethod=marker