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