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