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