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