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