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