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