Always execute finalize(), based on patch by Tong Sun <suntong@cpan.org>
[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 '
21    APTUPDATE='aptitude update'
22 else
23    APTINSTALL='apt-get --force-yes -y install'
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
47 # define chroot mirror {{{
48 chrootmirror() {
49   [ -n "$KEEP_SRC_LIST" ] && return
50   if [ -n "$ISO" ] ; then
51      echo "deb $ISO $RELEASE main contrib" > /etc/apt/sources.list
52      [ -n "$CHROOTMIRROR" ] && echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" >> /etc/apt/sources.list
53   else
54     if [ -n "$CHROOTMIRROR" ] ; then
55        echo "deb $CHROOTMIRROR $RELEASE main contrib non-free" > /etc/apt/sources.list
56     fi
57   fi
58 }
59 # }}}
60
61 # set up grml repository {{{
62 grmlrepos() {
63   if [ -n "$GRMLREPOS" ] ; then
64      # user might have provided their own apt sources.list
65      if ! grep -q grml /etc/apt/sources.list 2>/dev/null ; then
66         cat >> /etc/apt/sources.list << EOF
67
68 # grml: stable repository:
69   deb     http://deb.grml.org/ grml-stable  main
70   deb-src http://deb.grml.org/ grml-stable  main
71
72 # grml: testing/development repository:
73   deb     http://deb.grml.org/ grml-testing main
74   deb-src http://deb.grml.org/ grml-testing main
75
76 EOF
77      fi
78
79      # make sure we have the keys available for aptitude
80      gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
81      gpg --export F61E2E7CECDEA787 | apt-key add - || /bin/true # not yet sure
82      # why it's necessary, sometimes we get an error even though it works [mika]
83
84      # make sure we install packages from grml's pool only if not available
85      # from Debian!
86      if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
87         cat >> /etc/apt/preferences << EOF
88 // debian pool (default):
89 Package: *
90 Pin: release o=Debian
91 Pin-Priority: 996
92
93 // main grml-repository:
94 Package: *
95 Pin: origin deb.grml.org
96 Pin-Priority: 991
97 EOF
98      fi
99   fi
100 }
101 # }}}
102
103 # set up kernel-img.conf {{{
104 kernelimg_conf() {
105   if ! [ -r /etc/kernel-img.conf ] ; then
106      echo "Setting up /etc/kernel-img.conf"
107      cat > /etc/kernel-img.conf << EOF
108 # Kernel Image management overrides
109 # See kernel-img.conf(5) for details
110 do_initrd = Yes
111 do_symlinks = Yes
112 EOF
113   fi
114 }
115 # }}}
116
117 # create default devices {{{
118 makedev() {
119   if ! [ -r /dev/hda20 ] ; then
120      echo "Creating generic devices in /dev - this might take a while..."
121      cd /dev && MAKEDEV generic
122   fi
123 }
124 # }}}
125
126 # make sure services do not start up {{{
127 install_policy_rcd() {
128   if ! [ -r /usr/sbin/policy-rc.d ] ; then
129      export POLICYRCD=1
130      cat > /usr/sbin/policy-rc.d << EOF
131 #!/bin/sh
132 exit 101
133 EOF
134      chmod 775 /usr/sbin/policy-rc.d
135   fi
136 }
137 # }}}
138
139 # install additional packages {{{
140 packages() {
141   # Pre-seed the debconf database with answers. Each question will be marked
142   # as seen to prevent debconf from asking the question interactively.
143   [ -f /etc/debootstrap/debconf-selections ] && {
144     echo "Preseeding the debconf database, some lines might be skipped..."
145     cat /etc/debootstrap/debconf-selections | debconf-set-selections
146   }
147
148   if [ "$PACKAGES" = 'yes' ] ; then
149      if ! [ -r /etc/debootstrap/packages ] ; then
150        echo "Error: /etc/debootstrap/packages not found, exiting."
151        exit 1
152      else
153        $APTUPDATE
154        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
155      fi
156   fi
157 }
158 # }}}
159
160 # install extra packages {{{
161 extrapackages() {
162     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
163         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
164         if [ -n "$PACKAGELIST" ]; then
165             dpkg -i $PACKAGELIST
166             # run apt again to resolve any deps
167             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
168         fi
169     fi
170 }
171 # }}}
172
173 #  sarge specific stuff: mkinitrd {{{
174 mkinitrd() {
175   if [ "$RELEASE" = 'sarge' ] ; then
176      sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
177   fi
178 }
179 # }}}
180
181 # install kernel packages {{{
182 kernel() {
183   # do not override $KERNEL if set via config file
184   if [ -z "$KERNEL" ] ; then
185      if [ "$ARCH" = 'i386' ] ; then
186         KERNEL='2.6-686'
187      elif [ "$ARCH" = 'amd64' ] ; then
188         KERNEL='2.6-amd64'
189      fi
190   fi
191
192   if [ -n "$KERNEL" ] ; then
193      $APTUPDATE
194      if [ "$RELEASE" = 'sarge' ] ; then
195         KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
196      else
197         KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL"
198      fi
199       DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
200   fi
201 }
202 # }}}
203
204 # reconfigure packages {{{
205 reconfigure() {
206   if [ -n "$RECONFIGURE" ] ; then
207      for package in $RECONFIGURE ; do
208          dpkg --list $package 1>/dev/null 2>/dev/null && \
209          DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
210          echo "Warning: $package does not exist, can not reconfigure it."
211      done
212   fi
213 }
214 # }}}
215
216 # set password of user root {{{
217 passwords()
218 {
219   echo "Activating shadow passwords."
220   shadowconfig on
221
222   if [ -n "$ROOTPASSWORD" ] ; then
223      echo root:"$ROOTPASSWORD" | chpasswd -m
224      export ROOTPASSWORD=''
225   else
226     a='1'
227     b='2'
228      echo "Setting password for user root:"
229      while [ "$a" != "$b" ] ; do
230        echo -n "Enter new UNIX password for user root: "
231        read -s a
232        echo
233        echo -n "Retype new UNIX password for user root: "
234        read -s b
235        echo
236        if [ "$a" != "$b" ] ; then
237          echo "Sorry, passwords do not match. Retry."
238          a='1'
239          b='2'
240        else
241          echo root:"$a" | chpasswd -m
242          unset a
243          unset b
244        fi
245      done
246   fi
247 }
248 # }}}
249
250 # set up /etc/hosts {{{
251 hosts() {
252   if ! [ -f /etc/hosts ] ; then
253      echo "Setting up /etc/hosts"
254      echo "127.0.0.1       localhost  $HOSTNAME" > /etc/hosts
255   fi
256 }
257 # }}}
258
259 # set up /etc/network/interfaces {{{
260 interfaces() {
261   if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
262      echo "Setting up /etc/network/interfaces"
263      cat >> /etc/network/interfaces << EOF
264
265 # loopback device:
266 iface lo inet loopback
267 auto lo
268
269 # eth0:
270 # iface eth0 inet dhcp
271 # auto eth0
272
273 EOF
274   fi
275 }
276 # }}}
277
278 # adjust timezone {{{
279 timezone() {
280   if [ -n "$TIMEZONE" ] ; then
281      echo "Adjusting /etc/localtime"
282      ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
283   fi
284 }
285 # }}}
286
287 # helper function for fstab() {{{
288 createfstab(){
289      echo "Setting up /etc/fstab"
290 if [ -n "$TARGET_UUID" ] ; then
291    echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
292 else
293    echo "${TARGET} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
294 fi
295
296 cat >> /etc/fstab << EOF
297 proc           /proc        proc    defaults                      0   0
298 /sys           /sys         sysfs   noauto,rw,nosuid,nodev,noexec 0   0
299 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
300 # some other examples:
301 # /dev/sda2       none         swap    sw                   0   0
302 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
303 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
304 # linux:/pub      /beer        nfs     defaults             0  0
305 # tmpfs           /tmp         tmpfs   size=300M            0  0
306 EOF
307 }
308 # }}}
309
310 # generate /etc/fstab {{{
311 fstab() {
312   # set up /etc/fstab if file is not present (cdebootstrap)
313   if [ ! -f /etc/fstab  ] ; then
314      createfstab
315   fi
316
317   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
318   if grep -q UNCONFIGURED /etc/fstab ; then
319      createfstab
320   fi
321 }
322 # }}}
323
324 # set up hostname {{{
325 hostname() {
326   if [ -n "$HOSTNAME" ] ; then
327      echo "Setting hostname to ${HOSTNAME}."
328      echo "$HOSTNAME" > /etc/hostname
329
330      # adjust postfix configuration
331      if [ -r /etc/postfix/main.cf ] ; then
332         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
333      fi
334   fi
335 }
336 # }}}
337
338 # generate initrd/initramfs {{{
339 initrd() {
340   # assume the first available kernel as our main kernel
341   KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
342   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
343
344   # generate initrd
345   if [ -n "$INITRD" ] ; then
346      if [ "$RELEASE" = 'sarge' ] ; then
347         echo "Release sarge detected, will not create an initrd."
348      else
349         echo "Generating initrd."
350         update-initramfs -c -t -k $KERNELVER
351         if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
352            GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
353            LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
354         fi
355      fi
356   fi
357 }
358 # }}}
359
360 # grub configuration/installation {{{
361 grub() {
362   if [ -z "$GROOT" ] ; then
363      echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
364   else
365      echo "Adjusting grub configuration for use on ${GROOT}."
366
367      # copy stage-files to /boot/grub/
368      [ -d /boot/grub/ ] || mkdir /boot/grub
369      # i386 specific:
370      [ -d /usr/lib/grub/i386-pc ]   && cp /usr/lib/grub/i386-pc/* /boot/grub/
371      # amd64 specific:
372      [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
373      # sarge ships grub files in another directory
374      [ "$RELEASE" = 'sarge' ]       && cp /lib/grub/i386-pc/* /boot/grub/
375
376      # finally install grub
377      if [ -x /usr/sbin/update-grub ] ; then
378         UPDATEGRUB='/usr/sbin/update-grub'
379      else
380         UPDATEGRUB='/sbin/update-grub'
381      fi
382      $UPDATEGRUB -y
383      if [ -f /boot/grub/menu.lst ] ; then
384         sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
385         if [ -n "$TARGET_UUID" ] ; then
386            sed -i "s|^# kopt=root=.*|# kopt=root=UUID=${TARGET_UUID} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
387         else
388            sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
389         fi
390         # not sure why savedefault does not work for me; any ideas?
391         sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
392         $UPDATEGRUB -y
393      fi
394   fi
395 }
396 # }}}
397
398 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
399 custom_scripts() {
400   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
401
402   for script in /etc/debootstrap/chroot-scripts/* ; do
403       echo "Executing script $script"
404       $script && echo "done" || echo "failed"
405   done
406 }
407 # }}}
408
409 # make sure we don't have any running processes left {{{
410 services() {
411   for service in ssh mdadm mdadm-raid ; do
412     if [ -x /etc/init.d/"$service" ] ; then
413        /etc/init.d/"$service" stop || /bin/true
414     fi
415   done
416 }
417 # }}}
418
419 # unmount all filesystems in chroot, make sure nothing is left {{{
420 finalize() {
421   # make sure we don't leave any sensible data
422   rm -f /etc/debootstrap/variables
423   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
424   umount -a    1>/dev/null 2>/dev/null || true
425   umount /proc 1>/dev/null 2>/dev/null || true
426   umount /proc 1>/dev/null 2>/dev/null || true
427   umount -a    1>/dev/null 2>/dev/null || true
428 }
429 # }}}
430
431 # execute the functions {{{
432  for i in chrootmirror grmlrepos kernelimg_conf makedev install_policy_rcd \
433      packages extrapackages mkinitrd kernel reconfigure hosts interfaces   \
434      timezone fstab hostname initrd grub passwords custom_scripts services ; do
435      if stage $i ; then
436        $i && stage $i done || exit 1
437      fi
438   done
439   # always execute the finalize stage:
440   finalize
441 # }}}
442
443 # finally exit the chroot {{{
444   echo "Finished chroot installation, exiting."
445   exit 0
446 # }}}
447
448 ## END OF FILE #################################################################
449 # vim: ai tw=80 expandtab foldmethod=marker