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