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