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