7495e80604cdf27d01b8c968f4df9625a2733607
[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 #  sarge specific stuff: mkinitrd {{{
176 mkinitrd() {
177   if [ "$RELEASE" = 'sarge' ] ; then
178      sed -i "s#ROOT=probe#ROOT=$TARGET#" /etc/mkinitrd/mkinitrd.conf
179   fi
180 }
181 # }}}
182
183 # install kernel packages {{{
184 kernel() {
185   # do not override $KERNEL if set via config file
186   if [ -z "$KERNEL" ] ; then
187      if [ "$ARCH" = 'i386' ] ; then
188         KERNEL='2.6-686'
189      elif [ "$ARCH" = 'amd64' ] ; then
190         KERNEL='2.6-amd64'
191      fi
192   fi
193
194   if [ -n "$KERNEL" ] ; then
195      $APTUPDATE
196      if [ "$RELEASE" = 'sarge' ] ; then
197         KERNELPACKAGES="kernel-image-$KERNEL kernel-headers-$KERNEL"
198      else
199         # note: install busybox to be able to debug initramfs
200         KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox"
201      fi
202       DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
203   fi
204 }
205 # }}}
206
207 # reconfigure packages {{{
208 reconfigure() {
209   if [ -n "$RECONFIGURE" ] ; then
210      for package in $RECONFIGURE ; do
211          dpkg --list $package 1>/dev/null 2>/dev/null && \
212          DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
213          echo "Warning: $package does not exist, can not reconfigure it."
214      done
215   fi
216 }
217 # }}}
218
219 # set password of user root {{{
220 passwords()
221 {
222   echo "Activating shadow passwords."
223   shadowconfig on
224
225   CHPASSWD_OPTION=
226   if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
227      CHPASSWD_OPTION='-m'
228   fi
229
230
231   if [ -n "$ROOTPASSWORD" ] ; then
232      echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
233      export ROOTPASSWORD=''
234   else
235     a='1'
236     b='2'
237      echo "Setting password for user root:"
238      while [ "$a" != "$b" ] ; do
239        echo -n "Enter new UNIX password for user root: "
240        read -s a
241        echo
242        echo -n "Retype new UNIX password for user root: "
243        read -s b
244        echo
245        if [ "$a" != "$b" ] ; then
246          echo "Sorry, passwords do not match. Retry."
247          a='1'
248          b='2'
249        else
250          echo root:"$a" | chpasswd $CHPASSWD_OPTION
251          unset a
252          unset b
253        fi
254      done
255   fi
256 }
257 # }}}
258
259 # set up /etc/hosts {{{
260 hosts() {
261   if [ -f /etc/hosts ] ; then
262      sed -i "s#127.0.0.1 .*#127.0.0.1       localhost  $HOSTNAME#" /etc/hosts
263      [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
264   else
265      cat > /etc/hosts << EOF
266 127.0.0.1       localhost $HOSTNAME
267
268 #127.0.0.1       localhost
269 #127.0.1.1       $HOSTNAME.example.org $HOSTNAME
270
271 # The following lines are desirable for IPv6 capable hosts
272 #::1     ip6-localhost ip6-loopback $HOSTNAME
273 ::1     ip6-localhost ip6-loopback
274 fe00::0 ip6-localnet
275 ff00::0 ip6-mcastprefix
276 ff02::1 ip6-allnodes
277 ff02::2 ip6-allrouters
278 ff02::3 ip6-allhosts
279 EOF
280   fi
281 }
282 # }}}
283
284 # set up /etc/network/interfaces {{{
285 interfaces() {
286   if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
287      echo "Setting up /etc/network/interfaces"
288      cat >> /etc/network/interfaces << EOF
289
290 # loopback device:
291 iface lo inet loopback
292 auto lo
293
294 # eth0:
295 # iface eth0 inet dhcp
296 # auto eth0
297
298 EOF
299   fi
300 }
301 # }}}
302
303 # adjust timezone {{{
304 timezone() {
305   if [ -n "$TIMEZONE" ] ; then
306      echo "Adjusting /etc/localtime"
307      ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
308   fi
309 }
310 # }}}
311
312 # helper function for fstab() {{{
313 createfstab(){
314      echo "Setting up /etc/fstab"
315 if [ -n "$TARGET_UUID" ] ; then
316    echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
317 else
318    echo "${TARGET} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
319 fi
320
321 cat >> /etc/fstab << EOF
322 proc           /proc        proc    defaults                      0   0
323 /sys           /sys         sysfs   noauto,rw,nosuid,nodev,noexec 0   0
324 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
325 # some other examples:
326 # /dev/sda2       none         swap    sw,pri=0             0   0
327 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
328 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
329 # linux:/pub      /beer        nfs     defaults             0  0
330 # tmpfs           /tmp         tmpfs   size=300M            0  0
331 # /dev/sda5       none         swap    sw                   0  0
332 EOF
333 }
334 # }}}
335
336 # generate /etc/fstab {{{
337 fstab() {
338   # set up /etc/fstab if file is not present (cdebootstrap)
339   if [ ! -f /etc/fstab  ] ; then
340      createfstab
341   fi
342
343   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
344   if grep -q UNCONFIGURED /etc/fstab ; then
345      createfstab
346   fi
347 }
348 # }}}
349
350 # set up hostname {{{
351 hostname() {
352   if [ -n "$HOSTNAME" ] ; then
353      echo "Setting hostname to ${HOSTNAME}."
354      echo "$HOSTNAME" > /etc/hostname
355
356      # adjust postfix configuration
357      if [ -r /etc/postfix/main.cf ] ; then
358         # adjust hostname related options:
359         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
360
361         # listen on loopback interface only:
362         sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
363         grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
364      fi
365   fi
366 }
367 # }}}
368
369 # generate initrd/initramfs {{{
370 initrd() {
371   # assume the first available kernel as our main kernel
372   KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
373   if [ -z "$KERNELIMG" ] ; then
374      echo 'No kernel image found, skipping initrd stuff.'>&2
375      return
376   fi
377
378   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
379
380   # generate initrd
381   if [ -n "$INITRD" ] ; then
382      if [ "$RELEASE" = 'sarge' ] ; then
383         echo "Release sarge detected, will not create an initrd."
384      else
385         echo "Generating initrd."
386         update-initramfs -c -t -k $KERNELVER
387         if [ -f "/boot/initrd.img-$KERNELVER" ] ; then
388            GRUBINITRD="initrd          /boot/initrd.img-$KERNELVER"
389            LILOINITRD="        initrd=/boot/initrd.img-$KERNELVER"
390         fi
391      fi
392   fi
393 }
394 # }}}
395
396 # grub configuration/installation {{{
397 grub() {
398   if [ -z "$GROOT" ] ; then
399      echo "Warning: \$GROOT is not defined, will not adjust grub configuration therefore."
400   else
401      echo "Adjusting grub configuration for use on ${GROOT}."
402
403      # copy stage-files to /boot/grub/
404      [ -d /boot/grub/ ] || mkdir /boot/grub
405      # i386 specific:
406      [ -d /usr/lib/grub/i386-pc ]   && cp /usr/lib/grub/i386-pc/* /boot/grub/
407      # amd64 specific:
408      [ -d /usr/lib/grub/x86_64-pc ] && cp /usr/lib/grub/x86_64-pc/* /boot/grub/
409      # sarge ships grub files in another directory
410      [ "$RELEASE" = 'sarge' ]       && cp /lib/grub/i386-pc/* /boot/grub/
411
412      # finally install grub
413      if [ -x /usr/sbin/update-grub ] ; then
414         UPDATEGRUB='/usr/sbin/update-grub'
415      else
416         UPDATEGRUB='/sbin/update-grub'
417      fi
418      $UPDATEGRUB -y
419
420      # grub1:
421      if [ -f /boot/grub/menu.lst ] ; then
422         sed -i "s/^# groot=.*/# groot=(${GROOT})/g" /boot/grub/menu.lst
423         case "$TARGET" in
424              # do NOT use uuid with SW-RAID
425              /dev/md*)
426                sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
427                ;;
428              *)
429                if [ -n "$TARGET_UUID" ] ; then
430                  sed -i "s|^# kopt=root=.*|# kopt=root=UUID=${TARGET_UUID} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
431                else
432                  sed -i "s|^# kopt=root=.*|# kopt=root=${TARGET} ro ${BOOT_APPEND}|g" /boot/grub/menu.lst
433                fi
434                ;;
435         esac
436
437         # not sure why savedefault does not work for me; any ideas?
438         sed -i "s/^savedefault.*/# &/g" /boot/grub/menu.lst
439         $UPDATEGRUB -y
440      fi
441
442      # grub2:
443      if [ -f /etc/default/grub ] ; then
444         # create a copy of the original:
445         cp /etc/default/grub /etc/default/grub.orig
446
447         case "$TARGET" in
448              # do NOT use uuid with SW-RAID
449              /dev/md*)
450                GRUB_CMDLINE="root=${TARGET} ro ${BOOT_APPEND}"
451                ;;
452              *)
453                if [ -n "$TARGET_UUID" ] ; then
454                  GRUB_CMDLINE="root=UUID=${TARGET_UUID} ro ${BOOT_APPEND}"
455                else
456                  GRUB_CMDLINE="root=${TARGET} ro ${BOOT_APPEND}"
457                fi
458                ;;
459         esac
460
461         cat > /etc/default/grub << EOF
462 # Note: this file has been generated by chroot-script of grml-debootstrap.
463 # This configuration file is a workaround being necessary as the device.map
464 # generated by the live system might not correspond with your harddisk
465 # installation.
466 #
467 # Please restore the original Debian configuration executing the following
468 # commands when running your harddisk installation:
469 #
470 #   grub-mkdevicemap
471 #   mv /etc/default/grub.orig /etc/default/grub
472 #   update-grub
473 #
474 GRUB_DEFAULT=0
475 GRUB_TIMEOUT=5
476 GRUB_DISTRIBUTOR=Debian
477 # GRUB_DISABLE_LINUX_UUID=true
478 GRUB_CMDLINE_LINUX="$GRUB_CMDLINE"
479 EOF
480
481         [ -n "$INSTALL_NOTES" ] && echo "
482
483 The grub2 configuration has been adjusted so you should
484 be able to boot into your Debian installation now.
485 After rebooting into your Debian installation please execute:
486
487   grub-mkdevicemap
488   mv /etc/default/grub.orig /etc/default/grub
489   update-grub
490
491 to finalise configuration. This instructions are available
492 in the header of /etc/default/grub as well.
493 " >> $INSTALL_NOTES
494
495         $UPDATEGRUB
496      fi
497   fi
498 }
499 # }}}
500
501 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
502 custom_scripts() {
503   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
504
505   for script in /etc/debootstrap/chroot-scripts/* ; do
506       echo "Executing script $script"
507       $script && echo "done" || echo "failed"
508   done
509 }
510 # }}}
511
512 # make sure we don't have any running processes left {{{
513 services() {
514   for service in ssh mdadm mdadm-raid ; do
515     if [ -x /etc/init.d/"$service" ] ; then
516        /etc/init.d/"$service" stop || /bin/true
517     fi
518   done
519 }
520 # }}}
521
522 # unmount all filesystems in chroot, make sure nothing is left {{{
523 finalize() {
524   # make sure we don't leave any sensible data
525   rm -f /etc/debootstrap/variables
526   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
527   umount -a    1>/dev/null 2>/dev/null || true
528   umount /proc 1>/dev/null 2>/dev/null || true
529   umount /proc 1>/dev/null 2>/dev/null || true
530   umount -a    1>/dev/null 2>/dev/null || true
531 }
532 # }}}
533
534 # execute the functions {{{
535  for i in chrootmirror grmlrepos kernelimg_conf makedev install_policy_rcd \
536      kernel packages extrapackages mkinitrd reconfigure hosts interfaces   \
537      timezone fstab hostname initrd grub passwords custom_scripts services ; do
538      if stage $i ; then
539        $i && stage $i done || exit 1
540      fi
541   done
542   # always execute the finalize stage:
543   finalize
544 # }}}
545
546 # finally exit the chroot {{{
547   echo "Finished chroot installation, exiting."
548   exit 0
549 # }}}
550
551 ## END OF FILE #################################################################
552 # vim: ai tw=80 expandtab foldmethod=marker