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