0e93220ad2d70fa5ab1de25f480d2fa2cbcd31fd
[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      sed -i "s#127.0.0.1 .*#127.0.0.1       localhost  $HOSTNAME#" /etc/hosts
254      [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
255   else
256      cat > /etc/hosts << EOF
257 127.0.0.1       localhost $HOSTNAME
258
259 #127.0.0.1       localhost
260 #127.0.1.1       $HOSTNAME.example.org $HOSTNAME
261
262 # The following lines are desirable for IPv6 capable hosts
263 #::1     ip6-localhost ip6-loopback $HOSTNAME
264 ::1     ip6-localhost ip6-loopback
265 fe00::0 ip6-localnet
266 ff00::0 ip6-mcastprefix
267 ff02::1 ip6-allnodes
268 ff02::2 ip6-allrouters
269 ff02::3 ip6-allhosts
270 EOF
271   fi
272 }
273 # }}}
274
275 # set up /etc/network/interfaces {{{
276 interfaces() {
277   if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
278      echo "Setting up /etc/network/interfaces"
279      cat >> /etc/network/interfaces << EOF
280
281 # loopback device:
282 iface lo inet loopback
283 auto lo
284
285 # eth0:
286 # iface eth0 inet dhcp
287 # auto eth0
288
289 EOF
290   fi
291 }
292 # }}}
293
294 # adjust timezone {{{
295 timezone() {
296   if [ -n "$TIMEZONE" ] ; then
297      echo "Adjusting /etc/localtime"
298      ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
299   fi
300 }
301 # }}}
302
303 # helper function for fstab() {{{
304 createfstab(){
305      echo "Setting up /etc/fstab"
306 if [ -n "$TARGET_UUID" ] ; then
307    echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
308 else
309    echo "${TARGET} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
310 fi
311
312 cat >> /etc/fstab << EOF
313 proc           /proc        proc    defaults                      0   0
314 /sys           /sys         sysfs   noauto,rw,nosuid,nodev,noexec 0   0
315 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
316 # some other examples:
317 # /dev/sda2       none         swap    sw                   0   0
318 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
319 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
320 # linux:/pub      /beer        nfs     defaults             0  0
321 # tmpfs           /tmp         tmpfs   size=300M            0  0
322 EOF
323 }
324 # }}}
325
326 # generate /etc/fstab {{{
327 fstab() {
328   # set up /etc/fstab if file is not present (cdebootstrap)
329   if [ ! -f /etc/fstab  ] ; then
330      createfstab
331   fi
332
333   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
334   if grep -q UNCONFIGURED /etc/fstab ; then
335      createfstab
336   fi
337 }
338 # }}}
339
340 # set up hostname {{{
341 hostname() {
342   if [ -n "$HOSTNAME" ] ; then
343      echo "Setting hostname to ${HOSTNAME}."
344      echo "$HOSTNAME" > /etc/hostname
345
346      # adjust postfix configuration
347      if [ -r /etc/postfix/main.cf ] ; then
348         # adjust hostname related options:
349         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
350         sed -i "s/^myhostname = .*/myhostname = $HOSTNAME/" /etc/postfix/main.cf
351         sed -i "s/^mydestination = .*/mydestination = localdomain, localhost, localhost.localdomain, $HOSTNAME/" /etc/postfix/main.cf
352
353         # listen on loopback interface only:
354         sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
355         grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
356      fi
357   fi
358 }
359 # }}}
360
361 # generate initrd/initramfs {{{
362 initrd() {
363   # assume the first available kernel as our main kernel
364   KERNELIMG=$(ls -1 /boot/vmlinuz-* | head -1)
365   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
366
367   # generate initrd
368   if [ -n "$INITRD" ] ; then
369      if [ "$RELEASE" = 'sarge' ] ; then
370         echo "Release sarge detected, will not create an initrd."
371      else
372         echo "Generating initrd."
373         update-initramfs -c -t -k $KERNELVER
374         if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
375            GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
376            LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
377         fi
378      fi
379   fi
380 }
381 # }}}
382
383 # grub configuration/installation {{{
384 grub() {
385   if [ -z "$GROOT" ] ; then
386      echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
387   else
388      echo "Adjusting grub configuration for use on ${GROOT}."
389
390      # copy stage-files to /boot/grub/
391      [ -d /boot/grub/ ] || mkdir /boot/grub
392      # i386 specific:
393      [ -d /usr/lib/grub/i386-pc ]   && cp /usr/lib/grub/i386-pc/* /boot/grub/
394      # amd64 specific:
395      [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
396      # sarge ships grub files in another directory
397      [ "$RELEASE" = 'sarge' ]       && cp /lib/grub/i386-pc/* /boot/grub/
398
399      # finally install grub
400      if [ -x /usr/sbin/update-grub ] ; then
401         UPDATEGRUB='/usr/sbin/update-grub'
402      else
403         UPDATEGRUB='/sbin/update-grub'
404      fi
405      $UPDATEGRUB -y
406      if [ -f /boot/grub/menu.lst ] ; then
407         sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
408         if [ -n "$TARGET_UUID" ] ; then
409            sed -i "s|^# kopt=root=.*|# kopt=root=UUID=${TARGET_UUID} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
410         else
411            sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
412         fi
413         # not sure why savedefault does not work for me; any ideas?
414         sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
415         $UPDATEGRUB -y
416      fi
417   fi
418 }
419 # }}}
420
421 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
422 custom_scripts() {
423   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
424
425   for script in /etc/debootstrap/chroot-scripts/* ; do
426       echo "Executing script $script"
427       $script && echo "done" || echo "failed"
428   done
429 }
430 # }}}
431
432 # make sure we don't have any running processes left {{{
433 services() {
434   for service in ssh mdadm mdadm-raid ; do
435     if [ -x /etc/init.d/"$service" ] ; then
436        /etc/init.d/"$service" stop || /bin/true
437     fi
438   done
439 }
440 # }}}
441
442 # unmount all filesystems in chroot, make sure nothing is left {{{
443 finalize() {
444   # make sure we don't leave any sensible data
445   rm -f /etc/debootstrap/variables
446   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
447   umount -a    1>/dev/null 2>/dev/null || true
448   umount /proc 1>/dev/null 2>/dev/null || true
449   umount /proc 1>/dev/null 2>/dev/null || true
450   umount -a    1>/dev/null 2>/dev/null || true
451 }
452 # }}}
453
454 # execute the functions {{{
455  for i in chrootmirror grmlrepos kernelimg_conf makedev install_policy_rcd \
456      packages extrapackages mkinitrd kernel reconfigure hosts interfaces   \
457      timezone fstab hostname initrd grub passwords custom_scripts services ; do
458      if stage $i ; then
459        $i && stage $i done || exit 1
460      fi
461   done
462   # always execute the finalize stage:
463   finalize
464 # }}}
465
466 # finally exit the chroot {{{
467   echo "Finished chroot installation, exiting."
468   exit 0
469 # }}}
470
471 ## END OF FILE #################################################################
472 # vim: ai tw=80 expandtab foldmethod=marker