make sure grub-pc is installed when grub option is used
[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 ################################################################################
8
9 . /etc/debootstrap/config    || exit 1
10 . /etc/debootstrap/variables || exit 1
11
12 [ -r /proc/1 ] || mount -t proc none /proc
13
14 # variable checks {{{
15
16 # use aptitude only if it's available
17 if [ -x /usr/bin/aptitude ] ; then
18    APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
19    APTUPDATE='aptitude update'
20 else
21    APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
22    APTUPDATE='apt-get update'
23 fi
24
25 if [ -z "$STAGES" ] ; then
26    STAGES='/etc/debootstrap/stages'
27    [ -d "$STAGES" ] || mkdir -p "$STAGES"
28 fi
29 # }}}
30
31 # helper functions {{{
32 stage() {
33   if [ -n "$2" ] ; then
34      echo "$2" > "$STAGES/$1"
35      return 0
36   elif grep -q done "$STAGES/$1" 2>/dev/null ; then
37      echo "   [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
38      return 1
39   fi
40   echo "   Executing stage ${1}"
41   return 0
42 }
43
44 askpass() {
45   # read -s emulation for dash. result is in $resp.
46   set -o noglob
47   stty -echo
48   read resp
49   stty echo
50   set +o noglob
51 }
52 # }}}
53
54 # define chroot mirror {{{
55 chrootmirror() {
56   if [ -n "$KEEP_SRC_LIST" ] ; then
57     echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
58     return
59   fi
60
61   if [ -z "$COMPONENTS" ] ; then
62     COMPONENTS='main contrib non-free'
63   fi
64   echo "Using repository components $COMPONENTS"
65
66   if [ -n "$ISO" ] ; then
67     echo "Adjusting sources.list for ISO (${ISO})."
68     echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
69     echo "Adding mirror entry (${MIRROR}) to sources.list."
70     [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
71   else
72     if [ -n "$MIRROR" ] ; then
73       echo "Adjusting sources.list for mirror (${MIRROR})."
74       echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
75     fi
76   fi
77
78   # add security.debian.org:
79   case "$RELEASE" in
80     unstable|sid) ;;  # no security pool available
81     *)
82       echo "Adding security.debian.org to sources.list."
83       echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
84       ;;
85   esac
86 }
87 # }}}
88
89 # set up grml repository {{{
90 grmlrepos() {
91   if [ -n "$GRMLREPOS" ] ; then
92      # user might have provided their own apt sources.list
93      if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
94         cat >> /etc/apt/sources.list.d/grml.list << EOF
95 # grml: stable repository:
96   deb     http://deb.grml.org/ grml-stable  main
97   deb-src http://deb.grml.org/ grml-stable  main
98
99 # grml: testing/development repository:
100   deb     http://deb.grml.org/ grml-testing main
101   deb-src http://deb.grml.org/ grml-testing main
102 EOF
103      fi
104
105      if apt-get update ; then
106        apt-get -y --allow-unauthenticated install grml-debian-keyring
107        apt-get update
108      else
109        # make sure we have the keys available for aptitude
110        gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
111        gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
112        # why it's necessary, sometimes we get an error even though it works [mika]
113      fi
114
115      # make sure we install packages from Grml's pool only if not available
116      # from Debian!
117      if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
118         cat >> /etc/apt/preferences << EOF
119 // debian pool (default):
120 Package: *
121 Pin: release o=Debian
122 Pin-Priority: 996
123
124 // main grml-repository:
125 Package: *
126 Pin: origin deb.grml.org
127 Pin-Priority: 991
128 EOF
129      fi
130   fi
131 }
132 # }}}
133
134 # set up kernel-img.conf {{{
135 kernelimg_conf() {
136   if ! [ -r /etc/kernel-img.conf ] ; then
137      echo "Setting up /etc/kernel-img.conf"
138      cat > /etc/kernel-img.conf << EOF
139 # Kernel Image management overrides
140 # See kernel-img.conf(5) for details
141 do_initrd = Yes
142 do_symlinks = Yes
143 EOF
144   fi
145 }
146 # }}}
147
148 # make sure services do not start up {{{
149 install_policy_rcd() {
150   if ! [ -r /usr/sbin/policy-rc.d ] ; then
151      export POLICYRCD=1
152      cat > /usr/sbin/policy-rc.d << EOF
153 #!/bin/sh
154 exit 101
155 EOF
156      chmod 775 /usr/sbin/policy-rc.d
157   fi
158 }
159 # }}}
160
161 # remove now useless apt cache {{{
162 remove_apt_cache() {
163   if [ "$RM_APTCACHE" = 'yes' ] ; then
164     echo "Cleaning apt cache."
165     apt-get clean
166   else
167     echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
168   fi
169 }
170 # }}}
171
172 # install additional packages {{{
173 packages() {
174   # Pre-seed the debconf database with answers. Each question will be marked
175   # as seen to prevent debconf from asking the question interactively.
176   [ -f /etc/debootstrap/debconf-selections ] && {
177     echo "Preseeding the debconf database, some lines might be skipped..."
178     cat /etc/debootstrap/debconf-selections | debconf-set-selections
179   }
180
181   if [ "$PACKAGES" = 'yes' ] ; then
182      if ! [ -r /etc/debootstrap/packages ] ; then
183        echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
184        exit 1
185      else
186        $APTUPDATE
187        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
188      fi
189   fi
190 }
191 # }}}
192
193 # install extra packages {{{
194 extrapackages() {
195     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
196         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
197         if [ -n "$PACKAGELIST" ]; then
198             dpkg -i $PACKAGELIST
199             # run apt again to resolve any deps
200             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
201         fi
202     fi
203 }
204 # }}}
205
206 # install kernel packages {{{
207 kernel() {
208   # do not override $KERNEL if set via config file
209   if [ -z "$KERNEL" ] ; then
210      if [ "$ARCH" = 'i386' ] ; then
211         KERNEL='2.6-686'
212      elif [ "$ARCH" = 'amd64' ] ; then
213         KERNEL='2.6-amd64'
214      fi
215   fi
216
217   if [ -n "$KERNEL" ] ; then
218      $APTUPDATE
219      # note: install busybox to be able to debug initramfs
220      KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox firmware-linux"
221      DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
222   fi
223 }
224 # }}}
225
226 # reconfigure packages {{{
227 reconfigure() {
228   if [ -n "$RECONFIGURE" ] ; then
229      for package in $RECONFIGURE ; do
230          if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
231            DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
232            echo "Warning: $package does not exist, can not reconfigure it."
233          fi
234      done
235   fi
236 }
237 # }}}
238
239 # set password of user root {{{
240 passwords()
241 {
242   if [ -n "$NOPASSWORD" ] ; then
243     echo "Skip setting root password as requested."
244     return 0
245   fi
246
247   echo "Activating shadow passwords."
248   shadowconfig on
249
250   CHPASSWD_OPTION=
251   if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
252      CHPASSWD_OPTION='-m'
253   fi
254
255   if [ -n "$ROOTPASSWORD" ] ; then
256      echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
257      export ROOTPASSWORD=''
258   else
259     a='1'
260     b='2'
261      echo "Setting password for user root:"
262      while [ "$a" != "$b" ] ; do
263        echo -n "Enter new UNIX password for user root: "
264        askpass
265        a="$resp"
266        unset resp
267        echo
268        echo -n "Retype new UNIX password for user root: "
269        askpass
270        b="$resp"
271        unset resp
272        echo
273        if [ "$a" != "$b" ] ; then
274          echo "Sorry, passwords do not match. Retry."
275          a='1'
276          b='2'
277        else
278          echo root:"$a" | chpasswd $CHPASSWD_OPTION
279          unset a
280          unset b
281        fi
282      done
283   fi
284 }
285 # }}}
286
287 # set up /etc/hosts {{{
288 hosts() {
289   if [ -f /etc/hosts ] ; then
290      sed -i "s#127.0.0.1 .*#127.0.0.1       localhost  $HOSTNAME#" /etc/hosts
291      [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
292   else
293      cat > /etc/hosts << EOF
294 127.0.0.1       localhost $HOSTNAME
295
296 #127.0.0.1       localhost
297 #127.0.1.1       $HOSTNAME.example.org $HOSTNAME
298
299 # The following lines are desirable for IPv6 capable hosts
300 #::1     ip6-localhost ip6-loopback $HOSTNAME
301 ::1     ip6-localhost ip6-loopback
302 fe00::0 ip6-localnet
303 ff00::0 ip6-mcastprefix
304 ff02::1 ip6-allnodes
305 ff02::2 ip6-allrouters
306 ff02::3 ip6-allhosts
307 EOF
308   fi
309 }
310 # }}}
311
312 # set up /etc/network/interfaces {{{
313 interfaces() {
314   if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
315      echo "Setting up /etc/network/interfaces"
316      cat >> /etc/network/interfaces << EOF
317
318 # loopback device:
319 iface lo inet loopback
320 auto lo
321
322 # eth0:
323 # iface eth0 inet dhcp
324 # auto eth0
325
326 EOF
327   fi
328 }
329 # }}}
330
331 # adjust timezone {{{
332 timezone() {
333   if [ -n "$TIMEZONE" ] ; then
334      echo "Adjusting /etc/localtime"
335      ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
336   fi
337 }
338 # }}}
339
340 # helper function for fstab() {{{
341 createfstab(){
342      echo "Setting up /etc/fstab"
343 if [ -n "$TARGET_UUID" ] ; then
344    echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
345 else
346    echo "${TARGET} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
347 fi
348
349 cat >> /etc/fstab << EOF
350 proc           /proc        proc    defaults                      0   0
351 /sys           /sys         sysfs   noauto,rw,nosuid,nodev,noexec 0   0
352 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
353 # some other examples:
354 # /dev/sda2       none         swap    sw,pri=0             0   0
355 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
356 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
357 # linux:/pub      /beer        nfs     defaults             0  0
358 # tmpfs           /tmp         tmpfs   size=300M            0  0
359 # /dev/sda5       none         swap    sw                   0  0
360 EOF
361 }
362 # }}}
363
364 # generate /etc/fstab {{{
365 fstab() {
366   # set up /etc/fstab if file is not present (cdebootstrap)
367   if [ ! -f /etc/fstab  ] ; then
368      createfstab
369   fi
370
371   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
372   if grep -q UNCONFIGURED /etc/fstab ; then
373      createfstab
374   fi
375 }
376 # }}}
377
378 # set up hostname {{{
379 hostname() {
380   if [ -n "$HOSTNAME" ] ; then
381      echo "Setting hostname to ${HOSTNAME}."
382      echo "$HOSTNAME" > /etc/hostname
383
384      # adjust postfix configuration
385      if [ -r /etc/postfix/main.cf ] ; then
386         # adjust hostname related options:
387         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
388
389         # listen on loopback interface only:
390         sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
391         grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
392      fi
393   fi
394 }
395 # }}}
396
397 # generate initrd/initramfs {{{
398 initrd() {
399   # assume the first available kernel as our main kernel
400   KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
401   if [ -z "$KERNELIMG" ] ; then
402      echo 'No kernel image found, skipping initrd stuff.'>&2
403      return
404   fi
405
406   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
407
408   # generate initrd
409   if [ -n "$INITRD" ] ; then
410      echo "Generating initrd."
411      update-initramfs -c -t -k $KERNELVER
412   fi
413 }
414 # }}}
415
416 # grub configuration/installation {{{
417 grub_install() {
418
419   if [ -z "$GRUB" ] ; then
420      echo "Notice: \$GRUB not defined, will not install grub therefore."
421      return 0
422   fi
423
424   if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
425     echo "Notice: grub option set but no grub-pc package, installing it therefore."
426     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
427   fi
428
429   if ! [ -x "$(which grub-install)" ] ; then
430      echo "Error: grub-install not available. (Error while installing grub package?)" >&2
431      return 1
432   fi
433
434   if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
435      for device in $SELECTED_PARTITIONS ; do
436         GRUB="${device%%[0-9]}"
437         echo "Installing grub on ${GRUB}:"
438         grub-install --no-floppy "$GRUB"
439      done
440   else
441      echo "Installing grub on ${GRUB}:"
442      grub-install --no-floppy "$GRUB"
443   fi
444
445   echo "Adjusting grub configuration for use on ${GRUB}."
446
447   # finally install grub
448   if [ -x /usr/sbin/update-grub ] ; then
449      UPDATEGRUB='/usr/sbin/update-grub'
450   elif [ -x /sbin/update-grub ] ; then
451      UPDATEGRUB='/sbin/update-grub'
452   else
453     echo "Error: update-grub not available, can not execute it." >&2
454     return 1
455   fi
456
457   $UPDATEGRUB
458 }
459 # }}}
460
461 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
462 custom_scripts() {
463   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
464
465   for script in /etc/debootstrap/chroot-scripts/* ; do
466       echo "Executing script $script"
467       $script && echo "done" || echo "failed"
468   done
469 }
470 # }}}
471
472 # make sure we don't have any running processes left {{{
473 services() {
474   for service in ssh mdadm mdadm-raid ; do
475     if [ -x /etc/init.d/"$service" ] ; then
476        /etc/init.d/"$service" stop || true
477     fi
478   done
479 }
480 # }}}
481
482 # unmount /proc and make sure nothing is left {{{
483 finalize() {
484   # make sure we don't leave any sensible data
485   rm -f /etc/debootstrap/variables
486
487   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
488
489   umount /proc >/dev/null 2>/dev/null || true
490 }
491 # }}}
492
493 # signal handler {{{
494 signal_handler() {
495   finalize
496   [ -n "$1" ] && EXIT="$1" || EXIT="1"
497   exit "$EXIT"
498 }
499 # }}}
500
501 # set signal handler {{{
502 trap signal_handler HUP INT QUIT TERM
503 # }}}
504
505 # execute the functions {{{
506
507  # always execute install_policy_rcd
508  install_policy_rcd
509
510  for i in chrootmirror grmlrepos kernelimg_conf \
511      kernel packages extrapackages  reconfigure hosts interfaces \
512      timezone fstab hostname initrd grub_install passwords        \
513      custom_scripts remove_apt_cache services ; do
514      if stage $i ; then
515        $i && stage $i done || exit 1
516      fi
517   done
518   # always execute the finalize stage:
519   finalize
520 # }}}
521
522 # finally exit the chroot {{{
523   echo "Finished chroot installation, exiting."
524   exit 0
525 # }}}
526
527 ## END OF FILE #################################################################
528 # vim: ai tw=80 expandtab foldmethod=marker