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