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