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: Sam Apr 28 20:19:58 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 "$ISO" ] ; then
49 echo "deb $ISO $RELEASE main contrib" > /etc/apt/sources.list
50 [ -n "$CHROOTMIRROR" ] && echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" >> /etc/apt/sources.list
52 if [ -n "$CHROOTMIRROR" ] ; then
53 echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
59 # set up grml repository {{{
61 if [ -n "$GRMLREPOS" ] ; then
62 cat >> /etc/apt/sources.list << EOF
64 # grml: stable repository:
65 deb http://deb.grml.org/ grml-stable main
66 deb-src http://deb.grml.org/ grml-stable main
68 # grml: testing/development repository:
69 deb http://deb.grml.org/ grml-testing main
70 deb-src http://deb.grml.org/ grml-testing main
73 # make sure we have the keys available for aptitude
74 gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
75 gpg --export F61E2E7CECDEA787 | apt-key add - || /bin/true # not yet sure
76 # why it's necessary, sometimes we get an error even though it works [mika]
78 # make sure we install packages from grml's pool only if not available
80 if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
81 cat >> /etc/apt/preferences << EOF
82 // debian pool (default):
87 // main grml-repository:
89 Pin: origin deb.grml.org
97 # set up kernel-img.conf {{{
99 if ! [ -r /etc/kernel-img.conf ] ; then
100 echo "Setting up /etc/kernel-img.conf"
101 cat > /etc/kernel-img.conf << EOF
102 # Kernel Image management overrides
103 # See kernel-img.conf(5) for details
111 # create default devices {{{
113 if ! [ -r /dev/hda20 ] ; then
114 echo "Creating generic devices in /dev - this might take a while..."
115 cd /dev && MAKEDEV generic
120 # install additional packages {{{
122 if [ "$PACKAGES" = 'yes' ] ; then
123 if ! [ -r /etc/debootstrap/packages ] ; then
124 echo "Error: /etc/debootstrap/packages not found, exiting."
128 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(cat /etc/debootstrap/packages) $GRMLPACKAGES
134 # install extra packages {{{
136 if [ "$EXTRAPACKAGES" = 'yes' ] ; then
137 PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
138 if [ -n "$PACKAGELIST" ]; then
140 # run apt again to resolve any deps
141 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
147 # sarge specific stuff: mkinitrd {{{
149 if [ "$RELEASE" = 'sarge' ] ; then
150 sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
155 # install kernel packages {{{
157 # do not override $KERNEL if set via config file
158 if [ -z "$KERNEL" ] ; then
159 if [ "$ARCH" = 'i386' ] ; then
161 elif [ "$ARCH" = 'amd64' ] ; then
166 if [ -n "$KERNEL" ] ; then
168 if [ "$RELEASE" = 'sarge' ] ; then
169 KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
171 KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
173 DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
178 # reconfigure packages {{{
180 if [ -n "$RECONFIGURE" ] ; then
181 for package in $RECONFIGURE ; do
182 dpkg --list $package 1>/dev/null 2>/dev/null && \
183 DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
184 echo "Warning: $package does not exist, can not reconfigure it."
190 # set password of user root {{{
192 # Set a password, via chpasswd.
193 # Use perl rather than echo, to avoid the password
194 # showing in the process table. (However, this is normally
195 # only called when first booting the system, when root has no
196 # password at all, so that should be an unnecessary precaution).
198 # Pass in three arguments: the user, the password, and 'true' if the
199 # password has been pre-crypted (by preseeding).
201 # Taken from /var/lib/dpkg/info/passwd.config
205 # This is very annoying. chpasswd cannot handle generating md5
206 # passwords as it is not PAM-aware. Thus, I have to work around
207 # that by crypting the password myself if md5 is used.
211 if [ "$3" = true ]; then
217 LC_ALL=C LANGUAGE=C LANG=C perl -e '
218 sub CreateCryptSalt {
221 my @valid = split(//, "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
224 my $cryptsaltlen = ($md5 ? 8 : 2);
226 open (F, "</dev/urandom") || die "No /dev/urandom found!";
227 foreach (1..$cryptsaltlen) {
229 $out .= $valid[ord($in) % ($#valid + 1)];
232 return ($md5 ? "\$1\$$out\$" : $out);
235 open(P,"| chpasswd -e");
236 if ($ENV{PRECRYPTED}) {
237 print P shift().":$ENV{SETPASSWD_PW}\n";
240 crypt($ENV{SETPASSWD_PW}, CreateCryptSalt($ENV{USE_MD5})).
251 echo "Activating shadow passwords."
254 if [ -n "$ROOTPASSWORD" ] ; then
255 setpassword root "$ROOTPASSWD" false
258 echo "Setting password for user root:"
259 set +e # do not exit if passwd returns error due to missmatching passwords
262 set -e # restore default behaviour again
267 # set up /etc/hosts {{{
269 if ! [ -f /etc/hosts ] ; then
270 echo "Setting up /etc/hosts"
271 echo "127.0.0.1 localhost $HOSTNAME" > /etc/hosts
276 # set up /etc/network/interfaces {{{
278 touch /etc/network/interfaces
279 # make sure we add the entries only once
280 if ! grep -q eth0 /etc/network/interfaces ; then
281 echo "Setting up /etc/network/interfaces"
282 cat >> /etc/network/interfaces << EOF
285 iface lo inet loopback
289 # iface eth0 inet dhcp
297 # adjust timezone {{{
299 if [ -n "$TIMEZONE" ] ; then
300 echo "Adjusting /etc/localtime"
301 ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
306 # helper function for fstab() {{{
308 echo "Setting up /etc/fstab"
309 cat > /etc/fstab << EOF
310 $TARGET / auto defaults,errors=remount-ro 0 1
311 /sys /sys sysfs rw,nosuid,nodev,noexec 0 0
312 proc /proc proc defaults 0 0
313 /dev/cdrom /mnt/cdrom0 iso9660 ro,user,noauto 0 0
314 # some other examples:
315 # /dev/sda2 none swap sw 0 0
316 # /dev/hda1 /Grml ext3 dev,suid,user,noauto 0 2
317 # //1.2.3.4/pub /smb/pub smbfs defaults,user,noauto,uid=grml,gid=grml 0 0
318 # linux:/pub /beer nfs defaults 0 0
319 # tmpfs /tmp tmpfs size=300M 0 0
324 # generate /etc/fstab {{{
326 # set up /etc/fstab if file is not present (cdebootstrap)
327 if [ ! -f /etc/fstab ] ; then
331 # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
332 if grep -q UNCONFIGURED /etc/fstab ; then
338 # set up hostname {{{
340 if [ -n "$HOSTNAME" ] ; then
341 echo "Setting hostname to ${HOSTNAME}."
342 echo "$HOSTNAME" > /etc/hostname
347 # generate initrd/initramfs {{{
349 # assume the first available kernel as our main kernel
350 KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
351 KERNELVER=${KERNELIMG#/boot/vmlinuz-}
354 if [ -n "$INITRD" ] ; then
355 if [ "$RELEASE" = 'sarge' ] ; then
356 echo "Release sarge detected, will not create an initrd."
358 echo "Generating initrd."
359 update-initramfs -c -t -k $KERNELVER
360 if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
361 GRUBINITRD="initrd /boot/initrd.img-$KERNELVER"
362 LILOINITRD=" initrd=/boot/initrd.img-$KERNELVER"
369 # grub configuration/installation {{{
371 if [ -z "$GROOT" ] ; then
372 echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
374 echo "Adjusting grub configuration for use on ${GROOT}."
376 # copy stage-files to /boot/grub/
377 [ -d /boot/grub/ ] || mkdir /boot/grub
379 [ -d /usr/lib/grub/i386-pc ] && cp /usr/lib/grub/i386-pc/* /boot/grub/
381 [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
382 # sarge ships grub files in another directory
383 [ "$RELEASE" = 'sarge' ] && cp /lib/grub/i386-pc/* /boot/grub/
385 # finally install grub
386 if [ -x /usr/sbin/update-grub ] ; then
387 UPDATEGRUB='/usr/sbin/update-grub'
389 UPDATEGRUB='/sbin/update-grub'
392 if [ -f /boot/grub/menu.lst ] ; then
393 sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
394 sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
395 # not sure why savedefault does not work for me; any ideas?
396 sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
403 # make sure we don't have any running processes left {{{
405 for service in ssh mdadm mdadm-raid ; do
406 if [ -x /etc/init.d/"$service" ] ; then
407 /etc/init.d/"$service" stop || /bin/true
413 # unmount all filesystems in chroot, make sure nothing is left {{{
415 # make sure we don't leave any sensible data
416 rm -f /etc/debootstrap/variables
417 umount -a 1>/dev/null 2>/dev/null || true
418 umount /proc 1>/dev/null 2>/dev/null || true
419 umount /proc 1>/dev/null 2>/dev/null || true
420 umount -a 1>/dev/null 2>/dev/null || true
424 # execute the functions {{{
425 for i in chrootmirror grmlrepos kernelimg_conf makedev packages extrapackages \
426 mkinitrd kernel reconfigure hosts interfaces timezone fstab hostname \
427 initrd grub passwords services finalize ; do
429 $i && stage $i done || exit 1
434 # finally exit the chroot {{{
435 echo "Finished chroot installation, exiting."
439 ## END OF FILE #################################################################
440 # vim: ai tw=80 expandtab foldmethod=marker