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 # sarge specific stuff: mkinitrd {{{
101 if [ "$RELEASE" = 'sarge' ] ; then
102 sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
107 # install kernel packages {{{
109 # do not override $KERNEL if set via config file
110 if [ -z "$KERNEL" ] ; then
111 if [ "$ARCH" = 'i386' ] ; then
113 elif [ "$ARCH" = 'amd64' ] ; then
118 if [ -n "$KERNEL" ] ; then
120 if [ "$RELEASE" = 'sarge' ] ; then
121 KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
123 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
125 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
130 # reconfigure packages {{{
132 if [ -n "$RECONFIGURE" ] ; then
133 for package in $RECONFIGURE ; do
134 dpkg --list $package 1>/dev/null 2>/dev/null && \
135 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
136 echo "Warning: $package does not exist, can not reconfigure it."
142 # set password of user root {{{
144 # Set a password, via chpasswd.
145 # Use perl rather than echo, to avoid the password
146 # showing in the process table. (However, this is normally
147 # only called when first booting the system, when root has no
148 # password at all, so that should be an unnecessary precaution).
150 # Pass in three arguments: the user, the password, and 'true' if the
151 # password has been pre-crypted (by preseeding).
153 # Taken from /var/lib/dpkg/info/passwd.config
157 # This is very annoying. chpasswd cannot handle generating md5
158 # passwords as it is not PAM-aware. Thus, I have to work around
159 # that by crypting the password myself if md5 is used.
163 if [ "$3" = true ]; then
169 LC_ALL=C LANGUAGE=C LANG=C perl -e '
170 sub CreateCryptSalt {
173 my @valid = split(//, "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
176 my $cryptsaltlen = ($md5 ? 8 : 2);
178 open (F, "</dev/urandom") || die "No /dev/urandom found!";
179 foreach (1..$cryptsaltlen) {
181 $out .= $valid[ord($in) % ($#valid + 1)];
184 return ($md5 ? "\$1\$$out\$" : $out);
187 open(P,"| chpasswd -e");
188 if ($ENV{PRECRYPTED}) {
189 print P shift().":$ENV{SETPASSWD_PW}\n";
192 crypt($ENV{SETPASSWD_PW}, CreateCryptSalt($ENV{USE_MD5})).
203 echo "Activating shadow passwords."
206 if [ -n "$ROOTPASSWORD" ] ; then
207 setpassword root "$ROOTPASSWD" false
210 echo "Setting password for user root:"
211 set +e # do not exit if passwd returns error due to missmatching passwords
214 set -e # restore default behaviour again
219 # set up /etc/hosts {{{
221 if ! [ -f /etc/hosts ] ; then
222 echo "Setting up /etc/hosts"
223 echo "127.0.0.1 localhost $HOSTNAME" > /etc/hosts
228 # set up /etc/network/interfaces {{{
230 if ! [ -f /etc/network/interfaces ] ; then
231 echo "Setting up /etc/network/interfaces"
232 cat >> /etc/network/interfaces << EOF
233 iface lo inet loopback
242 # adjust timezone {{{
244 if [ -n "$TIMEZONE" ] ; then
245 echo "Adjusting /etc/localtime"
246 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
251 # helper function for fstab() {{{
253 echo "Setting up /etc/fstab"
254 cat > /etc/fstab << EOF
255 $TARGET / auto defaults,errors=remount-ro 0 1
256 /sys /sys sysfs rw,nosuid,nodev,noexec 0 0
257 proc /proc proc defaults 0 0
258 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
259 # some other examples:
260 # /dev/sda2 none swap sw 0 0
261 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
262 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
263 # linux:/pub /beer nfs defaults 0 0
264 # tmpfs /tmp tmpfs size=300M 0 0
269 # generate /etc/fstab {{{
271 # set up /etc/fstab if file is not present (cdebootstrap)
272 if [ ! -f /etc/fstab ] ; then
276 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
277 if grep -q UNCONFIGURED /etc/fstab ; then
283 # set up hostname {{{
285 if [ -n "$HOSTNAME" ] ; then
286 echo "Setting hostname to ${HOSTNAME}."
287 echo "$HOSTNAME" > /etc/hostname
292 # generate initrd/initramfs {{{
294 # assume the first available kernel as our main kernel
295 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
296 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
299 if [ -n "$INITRD" ] ; then
300 if [ "$RELEASE" = 'sarge' ] ; then
301 echo "Release sarge detected, will not create an initrd."
303 echo "Generating initrd."
304 update-initramfs -c -t -k $KERNELVER
305 if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
306 GRUBINITRD="initrd /boot/initrd.img-$KERNELVER"
307 LILOINITRD=" initrd=/boot/initrd.img-$KERNELVER"
314 # grub configuration/installation {{{
316 if [ -z "$GROOT" ] ; then
317 echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
319 echo "Adjusting grub configuration for use on ${GROOT}."
321 # copy stage-files to /boot/grub/
322 [ -d /boot/grub/ ] || mkdir /boot/grub
324 [ -d /usr/lib/grub/i386-pc ] && cp /usr/lib/grub/i386-pc/* /boot/grub/
326 [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
327 # sarge ships grub files in another directory
328 [ "$RELEASE" = 'sarge' ] && cp /lib/grub/i386-pc/* /boot/grub/
330 # finally install grub
331 if [ -x /usr/sbin/update-grub ] ; then
332 UPDATEGRUB='/usr/sbin/update-grub'
334 UPDATEGRUB='/sbin/update-grub'
337 if [ -f /boot/grub/menu.lst ] ; then
338 sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
339 sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro|g" /boot/grub/menu.lst
340 # not sure why savedefault does not work for me; any ideas?
341 sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
348 # make sure we don't have any running processes left {{{
350 for service in ssh mdadm mdadm-raid ; do
351 [ -x "/etc/init.d/$service" ] && "/etc/init.d/$service" stop
356 # unmount all filesystems in chroot, make sure nothing is left {{{
358 # make sure we don't leave any sensible data
359 rm -f /etc/debootstrap/variables
360 umount -a 1>/dev/null 2>/dev/null || true
361 umount /proc 1>/dev/null 2>/dev/null || true
362 umount /proc 1>/dev/null 2>/dev/null || true
363 umount -a 1>/dev/null 2>/dev/null || true
367 # execute the functions {{{
368 stage chrootmirror && chrootmirror && stage chrootmirror done
369 stage grmlrepos && grmlrepos && stage grmlrepos done
370 stage kernelimg_conf && kernelimg_conf && stage kernelimg_conf done
371 stage makedev && makedev && stage makedev done
372 stage packages && packages && stage packages done
373 stage mkinitrd && mkinitrd && stage mkinitrd done
374 stage kernel && kernel && stage kernel done
375 stage reconfigure && reconfigure && stage reconfigure done
376 stage hosts && hosts && stage hosts done
377 stage interfaces && interfaces && stage interfaces done
378 stage timezone && timezone && stage timezone done
379 stage fstab && fstab && stage fstab done
380 stage hostname && hostname && stage hostname done
381 stage initrd && initrd && stage initrd done
382 stage grub && grub && stage grub done
383 stage passwords && passwords && stage passwords done
384 stage services && services && stage services done
385 stage finalize && finalize && stage finalize done
388 # finally exit the chroot {{{
389 echo "Finished chroot installation, exiting."
393 ## END OF FILE #################################################################
394 # vim: ai tw=80 expandtab foldmethod=marker