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