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