Add console-common to /etc/debootstrap/packages, update todo
[grml-debootstrap.git] / chroot-script
1 #!/bin/sh
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 ################################################################################
9
10 set -e # exit on any error
11
12 . /etc/debootstrap/config    || exit 1
13 . /etc/debootstrap/variables || exit 1
14
15 [ -r /proc/1 ] || mount -t proc   none /proc
16
17 # variable checks {{{
18
19 # use aptitude only if it's available
20 if [ -x /usr/bin/aptitude ] ; then
21    APTINSTALL='aptitude -y install '
22    APTUPDATE='aptitude update'
23 else
24    APTINSTALL='apt-get --force-yes -y install'
25    APTUPDATE='apt-get update'
26 fi
27
28 if [ -z "$STAGES" ] ; then
29    STAGES='/etc/debootstrap/stages'
30    [ -d "$STAGES" ] || mkdir -p "$STAGES"
31 fi
32 # }}}
33
34 # helper functions {{{
35 stage() {
36   if [ -n "$2" ] ; then
37      echo "$2" > "$STAGES/$1"
38      return 0
39   elif grep -q done "$STAGES/$1" 2>/dev/null ; then
40      echo "[*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
41      return 1
42   fi
43 }
44 # }}}
45
46 # define chroot mirror {{{
47 chrootmirror() {
48   if [ -n "$CHROOTMIRROR" ] ; then
49      echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
50   fi
51 }
52 # }}}
53
54 # set up grml repository {{{
55 grmlrepos() {
56   if [ -n "$GRMLREPOS" ] ; then
57      cat >> /etc/apt/sources.list << EOF
58
59 # grml: stable repository:
60   deb     http://deb.grml.org/ grml-stable  main
61   deb-src http://deb.grml.org/ grml-stable  main
62
63 # grml: testing/development repository:
64   deb     http://deb.grml.org/ grml-testing main
65   deb-src http://deb.grml.org/ grml-testing main
66
67 EOF
68      # make sure we have the keys available for aptitude
69      gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
70      gpg --export F61E2E7CECDEA787 | apt-key add - || /bin/true # not yet sure
71      # why it's necessary, sometimes we get an error even though it works [mika]
72
73      # make sure we install packages from grml's pool only if not available
74      # from Debian!
75      if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
76         cat >> /etc/apt/preferences << EOF
77 // debian pool (default):
78 Package: *
79 Pin: release o=Debian
80 Pin-Priority: 996
81
82 // main grml-repository:
83 Package: *
84 Pin: origin deb.grml.org
85 Pin-Priority: 991
86 EOF
87      fi
88   fi
89 }
90 # }}}
91
92 # set up kernel-img.conf {{{
93 kernelimg_conf() {
94   if ! [ -r /etc/kernel-img.conf ] ; then
95      echo "Setting up /etc/kernel-img.conf"
96      cat > /etc/kernel-img.conf << EOF
97 # Kernel Image management overrides
98 # See kernel-img.conf(5) for details
99 do_initrd = Yes
100 do_symlinks = Yes
101 EOF
102   fi
103 }
104 # }}}
105
106 # create default devices {{{
107 makedev() {
108   if ! [ -r /dev/hda20 ] ; then
109      echo "Creating generic devices in /dev - this might take a while..."
110      cd /dev && MAKEDEV generic
111   fi
112 }
113 # }}}
114
115 # install additional packages {{{
116 packages() {
117   if [ "$PACKAGES" = 'yes' ] ; then
118      if ! [ -r /etc/debootstrap/packages ] ; then
119        echo "Error: /etc/debootstrap/packages not found, exiting."
120        exit 1
121      else
122        $APTUPDATE
123        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(cat /etc/debootstrap/packages) $GRMLPACKAGES
124      fi
125   fi
126 }
127 # }}}
128
129 # install extra packages {{{
130 extrapackages() {
131     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
132         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
133         if [ -n "$PACKAGELIST" ]; then
134             dpkg -i $PACKAGELIST
135             # run apt again to resolve any deps
136             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
137         fi
138     fi
139 }
140 # }}}
141
142 #  sarge specific stuff: mkinitrd {{{
143 mkinitrd() {
144   if [ "$RELEASE" = 'sarge' ] ; then
145      sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
146   fi
147 }
148 # }}}
149
150 # install kernel packages {{{
151 kernel() {
152   # do not override $KERNEL if set via config file
153   if [ -z "$KERNEL" ] ; then
154      if [ "$ARCH" = 'i386' ] ; then
155         KERNEL='2.6-686'
156      elif [ "$ARCH" = 'amd64' ] ; then
157         KERNEL='2.6-amd64'
158      fi
159   fi
160
161   if [ -n "$KERNEL" ] ; then
162      $APTUPDATE
163      if [ "$RELEASE" = 'sarge' ] ; then
164         KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
165      else
166         KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
167      fi
168       DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
169   fi
170 }
171 # }}}
172
173 # reconfigure packages {{{
174 reconfigure() {
175   if [ -n "$RECONFIGURE" ] ; then
176      for package in $RECONFIGURE ; do
177          dpkg --list $package 1>/dev/null 2>/dev/null && \
178          DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
179          echo "Warning: $package does not exist, can not reconfigure it."
180      done
181   fi
182 }
183 # }}}
184
185 # set password of user root {{{
186 setpassword() {
187 # Set a password, via chpasswd.
188 # Use perl rather than echo, to avoid the password
189 # showing in the process table. (However, this is normally
190 # only called when first booting the system, when root has no
191 # password at all, so that should be an unnecessary precaution).
192 #
193 # Pass in three arguments: the user, the password, and 'true' if the
194 # password has been pre-crypted (by preseeding).
195 #
196 # Taken from /var/lib/dpkg/info/passwd.config
197         SETPASSWD_PW="$2"
198         export SETPASSWD_PW
199
200         # This is very annoying. chpasswd cannot handle generating md5
201         # passwords as it is not PAM-aware. Thus, I have to work around
202         # that by crypting the password myself if md5 is used.
203         USE_MD5=1
204         export USE_MD5
205
206         if [ "$3" = true ]; then
207                 PRECRYPTED=1
208         else
209                 PRECRYPTED=''
210         fi
211         export PRECRYPTED
212         LC_ALL=C LANGUAGE=C LANG=C perl -e '
213                 sub CreateCryptSalt {
214                         my $md5 = shift;
215
216                         my @valid = split(//, "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
217                         my ($in, $out);
218
219                         my $cryptsaltlen = ($md5 ? 8 : 2);
220
221                         open (F, "</dev/urandom") || die "No /dev/urandom found!";
222                         foreach (1..$cryptsaltlen) {
223                                 read(F, $in, 1);
224                                 $out .= $valid[ord($in) % ($#valid + 1)];
225                         }
226                         close F;
227                         return ($md5 ? "\$1\$$out\$" : $out);
228                 }
229
230                 open(P,"| chpasswd -e");
231                 if ($ENV{PRECRYPTED}) {
232                         print P shift().":$ENV{SETPASSWD_PW}\n";
233                 } else {
234                         print P shift().":".
235                                 crypt($ENV{SETPASSWD_PW}, CreateCryptSalt($ENV{USE_MD5})).
236                                 "\n";
237                 }
238                 close P;
239         ' "$1"
240         SETPASSWD_PW=''
241         USE_MD5=''
242         PRECRYPTED=''
243 }
244
245 passwords() {
246   echo "Activating shadow passwords."
247   shadowconfig on
248
249   if [ -n "$ROOTPASSWORD" ] ; then
250      setpassword root "$ROOTPASSWD" false
251      export ROOTPASSWD=''
252   else
253      echo "Setting password for user root:"
254      set +e # do not exit if passwd returns error due to missmatching passwords
255      passwd
256      echo ""
257      set -e # restore default behaviour again
258   fi
259 }
260 # }}}
261
262 # set up /etc/hosts {{{
263 hosts() {
264   if ! [ -f /etc/hosts ] ; then
265      echo "Setting up /etc/hosts"
266      echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
267   fi
268 }
269 # }}}
270
271 # set up /etc/network/interfaces {{{
272 interfaces() {
273   touch /etc/network/interfaces
274   # make sure we add the entries only once
275   if ! grep -q eth0 /etc/network/interfaces ; then
276      echo "Setting up /etc/network/interfaces"
277      cat >> /etc/network/interfaces << EOF
278
279 # loopback device:
280 iface lo inet loopback
281 auto lo
282
283 # eth0:
284 # iface eth0 inet dhcp
285 # auto eth0
286
287 EOF
288   fi
289 }
290 # }}}
291
292 # adjust timezone {{{
293 timezone() {
294   if [ -n "$TIMEZONE" ] ; then
295      echo "Adjusting /etc/localtime"
296      ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
297   fi
298 }
299 # }}}
300
301 # helper function for fstab() {{{
302 createfstab(){
303      echo "Setting up /etc/fstab"
304 cat > /etc/fstab << EOF
305 $TARGET      /            auto    defaults,errors=remount-ro 0   1
306 /sys           /sys         sysfs   rw,nosuid,nodev,noexec     0   0
307 proc           /proc        proc    defaults                   0   0
308 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto             0   0
309 # some other examples:
310 # /dev/sda2       none         swap    sw                   0   0
311 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
312 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
313 # linux:/pub      /beer        nfs     defaults             0  0
314 # tmpfs           /tmp         tmpfs   size=300M            0  0
315 EOF
316 }
317 # }}}
318
319 # generate /etc/fstab {{{
320 fstab() {
321   # set up /etc/fstab if file is not present (cdebootstrap)
322   if [ ! -f /etc/fstab  ] ; then
323      createfstab
324   fi
325
326   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
327   if grep -q UNCONFIGURED /etc/fstab ; then
328      createfstab
329   fi
330 }
331 # }}}
332
333 # set up hostname {{{
334 hostname() {
335   if [ -n "$HOSTNAME" ] ; then
336      echo "Setting hostname to ${HOSTNAME}."
337      echo "$HOSTNAME" > /etc/hostname
338   fi
339 }
340 # }}}
341
342 # generate initrd/initramfs {{{
343 initrd() {
344   # assume the first available kernel as our main kernel
345   KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
346   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
347
348   # generate initrd
349   if [ -n "$INITRD" ] ; then
350      if [ "$RELEASE" = 'sarge' ] ; then
351         echo "Release sarge detected, will not create an initrd."
352      else
353         echo "Generating initrd."
354         update-initramfs -c -t -k $KERNELVER
355         if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
356            GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
357            LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
358         fi
359      fi
360   fi
361 }
362 # }}}
363
364 # grub configuration/installation {{{
365 grub() {
366   if [ -z "$GROOT" ] ; then
367      echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
368   else
369      echo "Adjusting grub configuration for use on ${GROOT}."
370
371      # copy stage-files to /boot/grub/
372      [ -d /boot/grub/ ] || mkdir /boot/grub
373      # i386 specific:
374      [ -d /usr/lib/grub/i386-pc ]   && cp /usr/lib/grub/i386-pc/* /boot/grub/
375      # amd64 specific:
376      [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
377      # sarge ships grub files in another directory
378      [ "$RELEASE" = 'sarge' ]       && cp /lib/grub/i386-pc/* /boot/grub/
379
380      # finally install grub
381      if [ -x /usr/sbin/update-grub ] ; then
382         UPDATEGRUB='/usr/sbin/update-grub'
383      else
384         UPDATEGRUB='/sbin/update-grub'
385      fi
386      $UPDATEGRUB -y
387      if [ -f /boot/grub/menu.lst ] ; then
388         sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
389         sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
390         # not sure why savedefault does not work for me; any ideas?
391         sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
392         $UPDATEGRUB -y
393      fi
394   fi
395 }
396 # }}}
397
398 # make sure we don't have any running processes left {{{
399 services() {
400   for service in ssh mdadm mdadm-raid ; do
401     if [ -x /etc/init.d/"$service" ] ; then
402        /etc/init.d/"$service" stop || /bin/true
403     fi
404   done
405 }
406 # }}}
407
408 # unmount all filesystems in chroot, make sure nothing is left {{{
409 finalize() {
410   # make sure we don't leave any sensible data
411   rm -f /etc/debootstrap/variables
412   umount -a    1>/dev/null 2>/dev/null || true
413   umount /proc 1>/dev/null 2>/dev/null || true
414   umount /proc 1>/dev/null 2>/dev/null || true
415   umount -a    1>/dev/null 2>/dev/null || true
416 }
417 # }}}
418
419 # execute the functions {{{
420  for i in chrootmirror grmlrepos kernelimg_conf makedev packages extrapackages \
421      mkinitrd kernel reconfigure hosts interfaces timezone fstab hostname \
422      initrd grub passwords services finalize ; do
423     if stage $i ; then
424        $i && stage $i done || exit 1
425     fi
426   done
427 # }}}
428
429 # finally exit the chroot {{{
430   echo "Finished chroot installation, exiting."
431   exit 0
432 # }}}
433
434 ## END OF FILE #################################################################
435 # vim: ai tw=80 expandtab foldmethod=marker