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