remove local (file://) mirrors from sources.list
[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 # GRML_CHROOT_SCRIPT_MARKER - do not remove this line unless you want to keep
9 # this script as /bin/chroot-script on your new installed system
10 ################################################################################
11
12 . /etc/debootstrap/config    || exit 1
13 . /etc/debootstrap/variables || exit 1
14
15 [ -r /proc/1 ] || mount -t proc none /proc
16
17 # variable checks {{{
18
19 # use aptitude only if it's available
20 if [ -x /usr/bin/aptitude ] ; then
21    APTUPDATE='aptitude update'
22    # Debian ISOs do not contain signed Release files
23    if [ -n "$ISO" ] ; then
24       APTINSTALL="aptitude -y --allow-untrusted --without-recommends install $DPKG_OPTIONS"
25       APTUPGRADE='aptitude -y --allow-untrusted safe-upgrade'
26    else
27       APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
28       APTUPGRADE='aptitude -y safe-upgrade'
29    fi
30 else
31    APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
32    APTUPDATE='apt-get update'
33    APTUPGRADE='apt-get --force-yes -y upgrade'
34 fi
35
36 if [ -z "$STAGES" ] ; then
37    STAGES='/etc/debootstrap/stages'
38    [ -d "$STAGES" ] || mkdir -p "$STAGES"
39 fi
40 # }}}
41
42 # helper functions {{{
43 stage() {
44   if [ -n "$2" ] ; then
45      echo "$2" > "$STAGES/$1"
46      return 0
47   elif grep -q done "$STAGES/$1" 2>/dev/null ; then
48      echo "   [*] Notice: stage $1 has been executed already, skipping execution therefore.">&2
49      return 1
50   fi
51   echo "   Executing stage ${1}"
52   return 0
53 }
54
55 askpass() {
56   # read -s emulation for dash. result is in $resp.
57   set -o noglob
58   stty -echo
59   read resp
60   stty echo
61   set +o noglob
62 }
63 # }}}
64
65 # define chroot mirror {{{
66 chrootmirror() {
67   if [ -n "$KEEP_SRC_LIST" ] ; then
68     echo "KEEP_SRC_LIST has been set, skipping chrootmirror stage."
69     return
70   fi
71
72   if [ -z "$COMPONENTS" ] ; then
73     COMPONENTS='main contrib non-free'
74   fi
75   echo "Using repository components $COMPONENTS"
76
77   if [ -n "$ISO" ] ; then
78     echo "Adjusting sources.list for ISO (${ISO})."
79     echo "deb $ISO $RELEASE $COMPONENTS" > /etc/apt/sources.list
80     echo "Adding mirror entry (${MIRROR}) to sources.list."
81     [ -n "$MIRROR" ] && echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list || true
82   else
83     if [ -n "$MIRROR" ] ; then
84       echo "Adjusting sources.list for mirror (${MIRROR})."
85       echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
86     fi
87   fi
88
89   # add security.debian.org:
90   case "$RELEASE" in
91     unstable|sid) ;;  # no security pool available
92     *)
93       echo "Adding security.debian.org to sources.list."
94       echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
95       ;;
96   esac
97 }
98 # }}}
99
100 # remove local chroot mirror {{{
101 remove_chrootmirror() {
102   if [ -n "$KEEP_SRC_LIST" ] ; then
103     echo "KEEP_SRC_LIST has been set, skipping remove_chrootmirror stage."
104     return
105   fi
106
107   if [ -n "$ISO" ] ; then
108     echo "Removing ISO (${ISO}) from sources.list."
109     TMP_ISO=$(echo "$ISO" |sed 's#/#\\/#g')
110     sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
111   else
112     if [ -n "$MIRROR" -a -n "$(echo $MIRROR|grep file:)" ] ; then
113       echo "Removing local mirror (${MIRROR}) from sources.list."
114       TMP_MIRROR=$(echo "$MIRROR" |sed 's#/#\\/#g')
115       sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
116     fi
117   fi
118 }
119 # }}}
120
121 # set up grml repository {{{
122 grmlrepos() {
123   if [ -n "$GRMLREPOS" ] ; then
124      # user might have provided their own apt sources.list
125      if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
126         cat >> /etc/apt/sources.list.d/grml.list << EOF
127 # grml: stable repository:
128   deb     http://deb.grml.org/ grml-stable  main
129   deb-src http://deb.grml.org/ grml-stable  main
130
131 # grml: testing/development repository:
132   deb     http://deb.grml.org/ grml-testing main
133   deb-src http://deb.grml.org/ grml-testing main
134 EOF
135      fi
136
137      if apt-get update ; then
138        apt-get -y --allow-unauthenticated install grml-debian-keyring
139        apt-get update
140      else
141        # make sure we have the keys available for aptitude
142        gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
143        gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
144        # why it's necessary, sometimes we get an error even though it works [mika]
145      fi
146
147      # make sure we install packages from Grml's pool only if not available
148      # from Debian!
149      if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
150         cat >> /etc/apt/preferences << EOF
151 // debian pool (default):
152 Package: *
153 Pin: release o=Debian
154 Pin-Priority: 996
155
156 // main grml-repository:
157 Package: *
158 Pin: origin deb.grml.org
159 Pin-Priority: 991
160 EOF
161      fi
162   fi
163 }
164 # }}}
165
166 # set up kernel-img.conf {{{
167 kernelimg_conf() {
168   if ! [ -r /etc/kernel-img.conf ] ; then
169      echo "Setting up /etc/kernel-img.conf"
170      cat > /etc/kernel-img.conf << EOF
171 # Kernel Image management overrides
172 # See kernel-img.conf(5) for details
173 do_initrd = Yes
174 do_symlinks = Yes
175 EOF
176   fi
177 }
178 # }}}
179
180 # make sure services do not start up {{{
181 install_policy_rcd() {
182   if ! [ -r /usr/sbin/policy-rc.d ] ; then
183      export POLICYRCD=1
184      cat > /usr/sbin/policy-rc.d << EOF
185 #!/bin/sh
186 exit 101
187 EOF
188      chmod 775 /usr/sbin/policy-rc.d
189   fi
190 }
191 # }}}
192
193 # make sure we have an up2date system {{{
194 upgrade_system() {
195   if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
196     echo "Running update + upgrade"
197     $APTUPDATE
198     $APTUPGRADE
199   else
200     echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
201   fi
202 }
203
204 # }}}
205 # remove now useless apt cache {{{
206 remove_apt_cache() {
207   if [ "$RM_APTCACHE" = 'yes' ] ; then
208     echo "Cleaning apt cache."
209     apt-get clean
210   else
211     echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
212   fi
213 }
214 # }}}
215
216 # install additional packages {{{
217 packages() {
218   # Pre-seed the debconf database with answers. Each question will be marked
219   # as seen to prevent debconf from asking the question interactively.
220   [ -f /etc/debootstrap/debconf-selections ] && {
221     echo "Preseeding the debconf database, some lines might be skipped..."
222     cat /etc/debootstrap/debconf-selections | debconf-set-selections
223   }
224
225   if [ "$PACKAGES" = 'yes' ] ; then
226      if ! [ -r /etc/debootstrap/packages ] ; then
227        echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
228        exit 1
229      else
230        $APTUPDATE
231        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
232      fi
233   fi
234 }
235 # }}}
236
237 # install extra packages {{{
238 extrapackages() {
239     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
240         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
241         if [ -n "$PACKAGELIST" ]; then
242             dpkg -i $PACKAGELIST
243             # run apt again to resolve any deps
244             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
245         fi
246     fi
247 }
248 # }}}
249
250 # install kernel packages {{{
251 kernel() {
252   # do not override $KERNEL if set via config file
253   if [ -z "$KERNEL" ] ; then
254      if [ "$ARCH" = 'i386' ] ; then
255         KERNEL='2.6-686'
256      elif [ "$ARCH" = 'amd64' ] ; then
257         KERNEL='2.6-amd64'
258      fi
259   fi
260
261   if [ -n "$KERNEL" ] ; then
262      $APTUPDATE
263      # note: install busybox to be able to debug initramfs
264      KERNELPACKAGES="linux-image-$KERNEL linux-headers-$KERNEL busybox firmware-linux"
265      DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
266   fi
267 }
268 # }}}
269
270 # reconfigure packages {{{
271 reconfigure() {
272   if [ -n "$RECONFIGURE" ] ; then
273      for package in $RECONFIGURE ; do
274          if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
275            DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
276            echo "Warning: $package does not exist, can not reconfigure it."
277          fi
278      done
279   fi
280 }
281 # }}}
282
283 # set password of user root {{{
284 passwords()
285 {
286   if [ -n "$NOPASSWORD" ] ; then
287     echo "Skip setting root password as requested."
288     return 0
289   fi
290
291   echo "Activating shadow passwords."
292   shadowconfig on
293
294   CHPASSWD_OPTION=
295   if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
296      CHPASSWD_OPTION='-m'
297   fi
298
299   if [ -n "$ROOTPASSWORD" ] ; then
300      echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
301      export ROOTPASSWORD=''
302   else
303     a='1'
304     b='2'
305      echo "Setting password for user root:"
306      while [ "$a" != "$b" ] ; do
307        echo -n "Enter new UNIX password for user root: "
308        askpass
309        a="$resp"
310        unset resp
311        echo
312        echo -n "Retype new UNIX password for user root: "
313        askpass
314        b="$resp"
315        unset resp
316        echo
317        if [ "$a" != "$b" ] ; then
318          echo "Sorry, passwords do not match. Retry."
319          a='1'
320          b='2'
321        else
322          echo root:"$a" | chpasswd $CHPASSWD_OPTION
323          unset a
324          unset b
325        fi
326      done
327   fi
328 }
329 # }}}
330
331 # set up /etc/hosts {{{
332 hosts() {
333   if [ -f /etc/hosts ] ; then
334      sed -i "s#127.0.0.1 .*#127.0.0.1       localhost  $HOSTNAME#" /etc/hosts
335      [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
336   else
337      cat > /etc/hosts << EOF
338 127.0.0.1       localhost $HOSTNAME
339
340 #127.0.0.1       localhost
341 #127.0.1.1       $HOSTNAME.example.org $HOSTNAME
342
343 # The following lines are desirable for IPv6 capable hosts
344 #::1     ip6-localhost ip6-loopback $HOSTNAME
345 ::1     ip6-localhost ip6-loopback
346 fe00::0 ip6-localnet
347 ff00::0 ip6-mcastprefix
348 ff02::1 ip6-allnodes
349 ff02::2 ip6-allrouters
350 ff02::3 ip6-allhosts
351 EOF
352   fi
353 }
354 # }}}
355
356 # set up /etc/network/interfaces {{{
357 interfaces() {
358   if ! [ -r /etc/network/interfaces ] || ! grep -q "auto lo" /etc/network/interfaces ; then
359      echo "Setting up /etc/network/interfaces"
360      cat >> /etc/network/interfaces << EOF
361
362 # loopback device:
363 iface lo inet loopback
364 auto lo
365
366 # eth0:
367 # iface eth0 inet dhcp
368 # auto eth0
369
370 EOF
371   fi
372 }
373 # }}}
374
375 # adjust timezone {{{
376 timezone() {
377   if [ -n "$TIMEZONE" ] ; then
378      echo "Adjusting /etc/localtime"
379      ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
380   fi
381 }
382 # }}}
383
384 # helper function for fstab() {{{
385 createfstab(){
386      echo "Setting up /etc/fstab"
387 if [ -n "$TARGET_UUID" ] ; then
388    echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
389 else
390    echo "${TARGET} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
391 fi
392
393 cat >> /etc/fstab << EOF
394 proc           /proc        proc    defaults                      0   0
395 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
396 # some other examples:
397 # /dev/sda2       none         swap    sw,pri=0             0   0
398 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
399 # //1.2.3.4/pub   /smb/pub     smbfs   defaults,user,noauto,uid=grml,gid=grml 0 0
400 # linux:/pub      /beer        nfs     defaults             0  0
401 # tmpfs           /tmp         tmpfs   size=300M            0  0
402 # /dev/sda5       none         swap    sw                   0  0
403 EOF
404 }
405 # }}}
406
407 # generate /etc/fstab {{{
408 fstab() {
409   # set up /etc/fstab if file is not present (cdebootstrap)
410   if [ ! -f /etc/fstab  ] ; then
411      createfstab
412   fi
413
414   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
415   if grep -q UNCONFIGURED /etc/fstab ; then
416      createfstab
417   fi
418 }
419 # }}}
420
421 # set up hostname {{{
422 hostname() {
423   if [ -n "$HOSTNAME" ] ; then
424      echo "Setting hostname to ${HOSTNAME}."
425      echo "$HOSTNAME" > /etc/hostname
426
427      # adjust postfix configuration
428      if [ -r /etc/postfix/main.cf ] ; then
429         # adjust hostname related options:
430         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
431
432         # listen on loopback interface only:
433         sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
434         grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
435      fi
436   fi
437 }
438 # }}}
439
440 # generate initrd/initramfs {{{
441 initrd() {
442   # assume the first available kernel as our main kernel
443   KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
444   if [ -z "$KERNELIMG" ] ; then
445      echo 'No kernel image found, skipping initrd stuff.'>&2
446      return
447   fi
448
449   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
450
451   # generate initrd
452   if [ -n "$INITRD" ] ; then
453      echo "Generating initrd."
454      update-initramfs -c -t -k $KERNELVER
455   fi
456 }
457 # }}}
458
459 # grub configuration/installation {{{
460 grub_install() {
461
462   if [ -z "$GRUB" ] ; then
463     echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
464     return 0
465   fi
466
467   if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
468     echo "Notice: grub option set but no grub-pc package, installing it therefore."
469     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
470   fi
471
472   if ! [ -x "$(which grub-install)" ] ; then
473      echo "Error: grub-install not available. (Error while installing grub package?)" >&2
474      return 1
475   fi
476
477   if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
478      for device in $SELECTED_PARTITIONS ; do
479         GRUB="${device%%[0-9]}"
480         echo "Installing grub on ${GRUB}:"
481         grub-install --no-floppy "$GRUB"
482      done
483   else
484      echo "Installing grub on ${GRUB}:"
485      grub-install --no-floppy "$GRUB"
486   fi
487
488   echo "Adjusting grub configuration for use on ${GRUB}."
489
490   # finally install grub
491   if [ -x /usr/sbin/update-grub ] ; then
492      UPDATEGRUB='/usr/sbin/update-grub'
493   elif [ -x /sbin/update-grub ] ; then
494      UPDATEGRUB='/sbin/update-grub'
495   else
496     echo "Error: update-grub not available, can not execute it." >&2
497     return 1
498   fi
499
500   $UPDATEGRUB
501 }
502 # }}}
503
504 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
505 custom_scripts() {
506   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
507
508   for script in /etc/debootstrap/chroot-scripts/* ; do
509       echo "Executing script $script"
510       $script && echo "done" || echo "failed"
511   done
512 }
513 # }}}
514
515 # make sure we don't have any running processes left {{{
516 services() {
517   for service in ssh mdadm mdadm-raid ; do
518     if [ -x /etc/init.d/"$service" ] ; then
519        /etc/init.d/"$service" stop || true
520     fi
521   done
522 }
523 # }}}
524
525 # unmount /proc and make sure nothing is left {{{
526 finalize() {
527   # make sure we don't leave any sensible data
528   rm -f /etc/debootstrap/variables
529
530   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
531
532   umount /proc >/dev/null 2>/dev/null || true
533 }
534 # }}}
535
536 # signal handler {{{
537 signal_handler() {
538   finalize
539   [ -n "$1" ] && EXIT="$1" || EXIT="1"
540   exit "$EXIT"
541 }
542 # }}}
543
544 # set signal handler {{{
545 trap signal_handler HUP INT QUIT TERM
546 # }}}
547
548 # execute the functions {{{
549
550  # always execute install_policy_rcd
551  install_policy_rcd
552
553  for i in chrootmirror grmlrepos kernelimg_conf \
554      kernel packages extrapackages  reconfigure hosts interfaces \
555      timezone fstab hostname initrd grub_install passwords        \
556      custom_scripts upgrade_system remove_apt_cache services \
557      remove_chrootmirror; do
558      if stage $i ; then
559        $i && stage $i done || exit 1
560      fi
561   done
562   # always execute the finalize stage:
563   finalize
564 # }}}
565
566 # finally exit the chroot {{{
567   echo "Finished chroot installation, exiting."
568   exit 0
569 # }}}
570
571 ## END OF FILE #################################################################
572 # vim: ai tw=80 expandtab foldmethod=marker