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