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