Use UUIDs in /boot/grub/grub.cfg for VM builds rather than hardcoding /dev/sda
[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 $DPKG_OPTIONS"
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 $DPKG_OPTIONS"
26    else
27       APTINSTALL="aptitude -y --without-recommends install $DPKG_OPTIONS"
28       APTUPGRADE="aptitude -y safe-upgrade $DPKG_OPTIONS"
29    fi
30 else
31    APTINSTALL="apt-get --force-yes -y --no-install-recommends install $DPKG_OPTIONS"
32    APTUPDATE="apt-get update $DPKG_OPTIONS"
33    APTUPGRADE="apt-get --force-yes -y upgrade $DPKG_OPTIONS"
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   [ -t 0 ] && stty -echo
59   read resp
60   [ -t 0 ] && 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
81     if [ -n "$MIRROR" ] ; then
82       echo "Adding mirror entry (${MIRROR}) to sources.list."
83       echo "deb $MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
84     fi
85   else
86     if [ -n "$MIRROR" ] ; then
87       echo "Adjusting sources.list for mirror (${MIRROR})."
88       echo "deb $MIRROR $RELEASE $COMPONENTS" > /etc/apt/sources.list
89     fi
90   fi
91
92   # LTS support
93   case "$RELEASE" in
94     squeeze)
95       if [ -n "$MIRROR" ] ; then
96         echo "Release matching $RELEASE - enabling LTS support in sources.list"
97         echo "deb $MIRROR ${RELEASE}-lts $COMPONENTS" >> /etc/apt/sources.list
98       fi
99       ;;
100   esac
101
102   # add security.debian.org:
103   case "$RELEASE" in
104     unstable|sid|lenny) ;;  # no security pool available
105     *)
106       echo "Adding security.debian.org to sources.list."
107       echo "deb http://security.debian.org ${RELEASE}/updates $COMPONENTS" >> /etc/apt/sources.list
108       ;;
109   esac
110 }
111 # }}}
112
113 # remove local chroot mirror {{{
114 remove_chrootmirror() {
115   if [ -n "$KEEP_SRC_LIST" ] ; then
116     echo "KEEP_SRC_LIST has been set, skipping remove_chrootmirror stage."
117     return
118   fi
119
120   if [ -n "$ISO" ] ; then
121     echo "Removing ISO (${ISO}) from sources.list."
122     TMP_ISO=$(echo "$ISO" |sed 's#/#\\/#g')
123     sed -i "/deb $TMP_ISO $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
124   else
125     if [ -n "$MIRROR" ] && echo "$MIRROR" | grep -q 'file:' ; then
126       echo "Removing local mirror (${MIRROR}) from sources.list."
127       TMP_MIRROR=$(echo "$MIRROR" |sed 's#/#\\/#g')
128       sed -i "/deb $TMP_MIRROR $RELEASE $COMPONENTS/ D" /etc/apt/sources.list
129       echo "Adding fallback mirror entry (${FALLBACK_MIRROR}) to sources.list instead."
130       echo "deb $FALLBACK_MIRROR $RELEASE $COMPONENTS" >> /etc/apt/sources.list
131     fi
132   fi
133 }
134 # }}}
135
136 # set up grml repository {{{
137 grmlrepos() {
138   if [ -n "$GRMLREPOS" ] ; then
139      # user might have provided their own apt sources.list
140      if ! grep -q grml /etc/apt/sources.list.d/grml.list 2>/dev/null ; then
141         cat >> /etc/apt/sources.list.d/grml.list << EOF
142 # grml: stable repository:
143   deb     http://deb.grml.org/ grml-stable  main
144   deb-src http://deb.grml.org/ grml-stable  main
145
146 # grml: testing/development repository:
147   deb     http://deb.grml.org/ grml-testing main
148   deb-src http://deb.grml.org/ grml-testing main
149 EOF
150      fi
151
152      if apt-get update $DPKG_OPTIONS; then
153        apt-get -y --allow-unauthenticated install grml-debian-keyring $DPKG_OPTIONS
154        apt-get update $DPKG_OPTIONS
155      else
156        # make sure we have the keys available for aptitude
157        gpg --keyserver subkeys.pgp.net --recv-keys F61E2E7CECDEA787
158        gpg --export F61E2E7CECDEA787 | apt-key add - || true # not yet sure
159        # why it's necessary, sometimes we get an error even though it works [mika]
160      fi
161
162      # make sure we install packages from Grml's pool only if not available
163      # from Debian!
164      if ! grep -q grml /etc/apt/preferences 2>/dev/null ; then
165         cat >> /etc/apt/preferences << EOF
166 // debian pool (default):
167 Package: *
168 Pin: release o=Debian
169 Pin-Priority: 996
170
171 // main grml-repository:
172 Package: *
173 Pin: origin deb.grml.org
174 Pin-Priority: 991
175 EOF
176      fi
177   fi
178 }
179 # }}}
180
181 # check available backports release version {{{
182 checkbackports() {
183   wget -q -O/dev/null "http://backports.debian.org/debian-backports/dists/${1}-backports/Release"
184 }
185 # }}}
186
187 # feature to provide Debian backports repos {{{
188 backportrepos() {
189     if [ -n "$BACKPORTREPOS" ] ; then
190         if ! checkbackports "$RELEASE" ; then
191             echo "Backports for ${RELEASE} are not available." >&2
192             exit 1
193         else
194             # user might have provided their own apt sources.list
195             if ! grep -q backports /etc/apt/sources.list.d/backports.list 2>/dev/null ; then
196                 cat >> /etc/apt/sources.list.d/backports.list << EOF
197 # debian backports: ${RELEASE}-backports repository:
198 deb     http://backports.debian.org/debian-backports ${RELEASE}-backports main
199 deb-src http://backports.debian.org/debian-backports ${RELEASE}-backports main
200 EOF
201             fi
202         fi
203     fi
204 }
205 # }}}
206
207 # set up kernel-img.conf {{{
208 kernelimg_conf() {
209   if ! [ -r /etc/kernel-img.conf ] ; then
210      echo "Setting up /etc/kernel-img.conf"
211      cat > /etc/kernel-img.conf << EOF
212 # Kernel Image management overrides
213 # See kernel-img.conf(5) for details
214 do_initrd = Yes
215 do_symlinks = Yes
216 EOF
217   fi
218 }
219 # }}}
220
221 # make sure services do not start up {{{
222 install_policy_rcd() {
223   if ! [ -r /usr/sbin/policy-rc.d ] ; then
224      export POLICYRCD=1
225      cat > /usr/sbin/policy-rc.d << EOF
226 #!/bin/sh
227 exit 101
228 EOF
229      chmod 775 /usr/sbin/policy-rc.d
230   fi
231 }
232 # }}}
233
234 # make sure we have an up2date system {{{
235 upgrade_system() {
236   if [ "$UPGRADE_SYSTEM" = "yes" ] ; then
237     echo "Running update + upgrade"
238     $APTUPDATE
239     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTUPGRADE
240   else
241     echo "Not running update + upgrade as \$UPDATE_AND_UPGRADE is not set to 'yes'."
242   fi
243 }
244
245 # }}}
246 # remove now useless apt cache {{{
247 remove_apt_cache() {
248   if [ "$RM_APTCACHE" = 'yes' ] ; then
249     echo "Cleaning apt cache."
250     apt-get clean $DPKG_OPTIONS
251   else
252     echo "Not cleaning apt cache as \$RM_APTCACHE is unset."
253   fi
254 }
255 # }}}
256
257 # install additional packages {{{
258 packages() {
259   # Pre-seed the debconf database with answers. Each question will be marked
260   # as seen to prevent debconf from asking the question interactively.
261   [ -f /etc/debootstrap/debconf-selections ] && {
262     echo "Preseeding the debconf database, some lines might be skipped..."
263     debconf-set-selections < /etc/debootstrap/debconf-selections
264   }
265
266   if [ "$PACKAGES" = 'yes' ] ; then
267      if ! [ -r /etc/debootstrap/packages ] ; then
268        echo "Error: /etc/debootstrap/packages (inside chroot) not found, exiting." >&2
269        exit 1
270      else
271        $APTUPDATE
272        DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $(grep -v '^#' /etc/debootstrap/packages) $GRMLPACKAGES
273      fi
274   fi
275 }
276 # }}}
277
278 # install extra packages {{{
279 extrapackages() {
280     if [ "$EXTRAPACKAGES" = 'yes' ] ; then
281         PACKAGELIST=$(find /etc/debootstrap/extrapackages -type f -name '*.deb')
282         if [ -n "$PACKAGELIST" ]; then
283             dpkg -i $PACKAGELIST
284             # run apt again to resolve any deps
285             DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL
286         fi
287     fi
288 }
289 # }}}
290
291 # check if the specified Debian package exists
292 package_exists() {
293   output=$(apt-cache show "$1" 2>/dev/null)
294   [ -n "$output" ]
295   return $?
296 }
297
298
299 # determine the kernel version postfix
300 get_kernel_version() {
301   # do not override $KERNEL if set via config file
302   if [ -n "$KERNEL" ] ; then
303     echo "$KERNEL"
304     return 0
305   fi
306
307   case $ARCH in
308     i386)   KARCH=686   ;;
309     amd64)  KARCH=amd64 ;;
310     *)
311       echo "Only i386 and amd64 are currently supported" >&2
312       return 1
313   esac
314
315   for KPREFIX in "" "2.6-" ; do  # iterate through the kernel prefixes,
316                                  # currently "" and "2.6-"
317     if package_exists linux-image-${KPREFIX}${KARCH} ; then
318       echo ${KPREFIX}${KARCH}
319       return 0
320     fi
321
322   done
323 }
324
325 # install kernel packages {{{
326 kernel() {
327   if [ -n "$NOKERNEL" ] ; then
328     echo "Skipping installation of kernel packages as requested via --nokernel"
329     return 0
330   fi
331
332   $APTUPDATE
333   KVER=$(get_kernel_version)
334   if [ -n "$KVER" ] ; then
335      # note: install busybox to be able to debug initramfs
336      KERNELPACKAGES="linux-image-$KVER linux-headers-$KVER busybox firmware-linux-free firmware-linux"
337      DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL $KERNELPACKAGES
338   else
339      echo "Warning: Could not find a kernel for your system. Your system won't be able to boot itself!"
340   fi
341 }
342 # }}}
343
344 # reconfigure packages {{{
345 reconfigure() {
346   if [ -n "$RECONFIGURE" ] ; then
347      for package in $RECONFIGURE ; do
348          if dpkg --list $package >/dev/null 2>&1 | grep -q '^ii' ; then
349            DEBIAN_FRONTEND=$DEBIAN_FRONTEND dpkg-reconfigure $package || \
350            echo "Warning: $package does not exist, can not reconfigure it."
351          fi
352      done
353   fi
354 }
355 # }}}
356
357 # set password of user root {{{
358 passwords()
359 {
360   if [ -n "$NOPASSWORD" ] ; then
361     echo "Skip setting root password as requested."
362     return 0
363   fi
364
365   echo "Activating shadow passwords."
366   shadowconfig on
367
368   CHPASSWD_OPTION=
369   if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
370      CHPASSWD_OPTION='-m'
371   fi
372
373   if [ -n "$ROOTPASSWORD" ] ; then
374      echo root:"$ROOTPASSWORD" | chpasswd $CHPASSWD_OPTION
375      export ROOTPASSWORD=''
376   else
377     a='1'
378     b='2'
379      echo "Setting password for user root:"
380      while [ "$a" != "$b" ] ; do
381        printf "Enter new UNIX password for user root: "
382        askpass
383        a="$resp"
384        unset resp
385        echo
386        printf "Retype new UNIX password for user root: "
387        askpass
388        b="$resp"
389        unset resp
390        echo
391        if [ "$a" != "$b" ] ; then
392          echo "Sorry, passwords do not match. Retry."
393          a='1'
394          b='2'
395        else
396          echo root:"$a" | chpasswd $CHPASSWD_OPTION
397          unset a
398          unset b
399        fi
400      done
401   fi
402 }
403 # }}}
404
405 # set up /etc/hosts {{{
406 hosts() {
407   if [ -f /etc/hosts ] ; then
408      sed -i "s#127.0.0.1 .*#127.0.0.1       localhost  $HOSTNAME#" /etc/hosts
409      [ -n "$HOSTNAME" ] && sed -i "s/grml/$HOSTNAME/g" /etc/hosts
410   else
411      cat > /etc/hosts << EOF
412 127.0.0.1       localhost $HOSTNAME
413
414 #127.0.0.1       localhost
415 #127.0.1.1       $HOSTNAME.example.org $HOSTNAME
416
417 # The following lines are desirable for IPv6 capable hosts
418 #::1     ip6-localhost ip6-loopback $HOSTNAME
419 ::1     ip6-localhost ip6-loopback
420 fe00::0 ip6-localnet
421 ff00::0 ip6-mcastprefix
422 ff02::1 ip6-allnodes
423 ff02::2 ip6-allrouters
424 ff02::3 ip6-allhosts
425 EOF
426   fi
427 }
428 # }}}
429
430 # set default locales {{{
431 default_locales() {
432   if [ -n "$DEFAULT_LOCALES" ] ; then
433     if ! [ -x /usr/sbin/update-locale ] ; then
434       echo "Warning: update-locale executable not available (no locales package installed?)"
435       echo "Ignoring request to run update-locale for $DEFAULT_LOCALES therefore"
436       return 0
437     fi
438
439     /usr/sbin/update-locale LANGUAGE="$DEFAULT_LANGUAGE" LANG="$DEFAULT_LOCALES"
440   fi
441 }
442 # }}}
443
444 # adjust timezone {{{
445 timezone() {
446   if [ -n "$TIMEZONE" ] ; then
447      echo "Adjusting /etc/localtime"
448      ln -sf "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime
449   fi
450 }
451 # }}}
452
453 # helper function for fstab() {{{
454 createfstab(){
455      echo "Setting up /etc/fstab"
456 if [ -n "$TARGET_UUID" ] ; then
457    echo "/dev/disk/by-uuid/${TARGET_UUID} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
458 else
459    echo "${TARGET} /  auto    defaults,errors=remount-ro 0   1" > /etc/fstab
460 fi
461
462 cat >> /etc/fstab << EOF
463 proc           /proc        proc    defaults                      0   0
464 /dev/cdrom     /mnt/cdrom0  iso9660 ro,user,noauto                0   0
465 # some other examples:
466 # /dev/sda2       none         swap    sw,pri=0             0   0
467 # /dev/hda1       /Grml        ext3    dev,suid,user,noauto 0  2
468 # //1.2.3.4/pub   /smb/pub     cifs    user,noauto,uid=grml,gid=grml 0 0
469 # linux:/pub      /beer        nfs     defaults             0  0
470 # tmpfs           /tmp         tmpfs   size=300M            0  0
471 # /dev/sda5       none         swap    sw                   0  0
472 EOF
473 }
474 # }}}
475
476 # generate /etc/fstab {{{
477 fstab() {
478   # set up /etc/fstab if file is not present (cdebootstrap)
479   if [ ! -f /etc/fstab  ] ; then
480      createfstab
481   fi
482
483   # set up /etc/fstab if file is UNCONFIGURED (debootstrap)
484   if grep -q UNCONFIGURED /etc/fstab ; then
485      createfstab
486   fi
487 }
488 # }}}
489
490 # set up hostname {{{
491 hostname() {
492   if [ -n "$HOSTNAME" ] ; then
493      echo "Setting hostname to ${HOSTNAME}."
494      echo "$HOSTNAME" > /etc/hostname
495
496      # adjust postfix configuration
497      if [ -r /etc/postfix/main.cf ] ; then
498         # adjust hostname related options:
499         sed -i "s/grml/$HOSTNAME/g" /etc/postfix/main.cf
500
501         # listen on loopback interface only:
502         sed -i "s/^inet_interfaces = .*/inet_interfaces = loopback-only/" /etc/postfix/main.cf
503         grep -q inet_interfaces /etc/postfix/main.cf || echo 'inet_interfaces = loopback-only' >> /etc/postfix/main.cf
504      fi
505      if [ -r /etc/mailname ] ; then
506         # adjust /etc/mailname
507         local etc_mail_domain=$(/bin/dnsdomainname 2>/dev/null || echo localdomain)
508         case "$HOSTNAME" in
509           *.*)
510             local mailname="$HOSTNAME"
511             ;;
512           *)
513             local mailname="${HOSTNAME}.${etc_mail_domain}"
514             ;;
515         esac
516         echo "Setting mailname to ${mailname}"
517         echo "$mailname" > /etc/mailname
518      fi
519   fi
520 }
521 # }}}
522
523 # generate initrd/initramfs {{{
524 initrd() {
525   # assume the first available kernel as our main kernel
526   KERNELIMG=$(ls -1 /boot/vmlinuz-* 2>/dev/null | head -1)
527   if [ -z "$KERNELIMG" ] ; then
528      echo 'No kernel image found, skipping initrd stuff.'>&2
529      return
530   fi
531
532   KERNELVER=${KERNELIMG#/boot/vmlinuz-}
533
534   # generate initrd
535   if [ -n "$INITRD" ] ; then
536      echo "Generating initrd."
537      update-initramfs -c -t -k "$KERNELVER"
538   fi
539 }
540 # }}}
541
542 # grub configuration/installation {{{
543 grub_install() {
544
545   if [ -z "$GRUB" ] ; then
546     echo "Notice: \$GRUB not defined, will not install grub inside chroot at this stage."
547     return 0
548   fi
549
550   if ! dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then
551     echo "Notice: grub option set but no grub-pc package, installing it therefore."
552     DEBIAN_FRONTEND=$DEBIAN_FRONTEND $APTINSTALL grub-pc
553   fi
554
555   if ! [ -x "$(which grub-install)" ] ; then
556      echo "Error: grub-install not available. (Error while installing grub package?)" >&2
557      return 1
558   fi
559
560   if [ -n "$SELECTED_PARTITIONS" ] ; then # using sw-raid
561      for device in $SELECTED_PARTITIONS ; do
562         GRUB="${device%%[0-9]}"
563         echo "Installing grub on ${GRUB}:"
564         grub-install --no-floppy "$GRUB"
565      done
566   else
567      echo "Installing grub on ${GRUB}:"
568      grub-install --no-floppy "$GRUB"
569   fi
570
571   echo "Adjusting grub configuration for use on ${GRUB}."
572
573   # finally install grub
574   if [ -x /usr/sbin/update-grub ] ; then
575      UPDATEGRUB='/usr/sbin/update-grub'
576   elif [ -x /sbin/update-grub ] ; then
577      UPDATEGRUB='/sbin/update-grub'
578   else
579     echo "Error: update-grub not available, can not execute it." >&2
580     return 1
581   fi
582
583   $UPDATEGRUB
584 }
585 # }}}
586
587 # execute all scripts present in /etc/debootstrap/chroot-scripts/ {{{
588 custom_scripts() {
589   [ -d /etc/debootstrap/chroot-scripts/ ] || return 0
590
591   for script in /etc/debootstrap/chroot-scripts/* ; do
592       echo "Executing script $script"
593       $script && echo "done" || echo "failed"
594   done
595 }
596 # }}}
597
598 # make sure we don't have any running processes left {{{
599 services() {
600   for service in ssh mdadm mdadm-raid ; do
601     if [ -x /etc/init.d/"$service" ] ; then
602        /etc/init.d/"$service" stop || true
603     fi
604   done
605 }
606 # }}}
607
608 # unmount /proc and make sure nothing is left {{{
609 finalize() {
610   # make sure we don't leave any sensible data
611   rm -f /etc/debootstrap/variables
612
613   [ -n "$POLICYRCD" ] && rm -f /usr/sbin/policy-rc.d
614
615   umount /proc >/dev/null 2>/dev/null || true
616 }
617 # }}}
618
619 # signal handler {{{
620 signal_handler() {
621   finalize
622   [ -n "$1" ] && EXIT="$1" || EXIT="1"
623   exit "$EXIT"
624 }
625 # }}}
626
627 # set signal handler {{{
628 trap signal_handler HUP INT QUIT TERM
629 # }}}
630
631 # execute the functions {{{
632
633  # always execute install_policy_rcd
634  install_policy_rcd
635
636  for i in chrootmirror grmlrepos backportrepos kernelimg_conf \
637      kernel packages extrapackages reconfigure hosts \
638      default_locales timezone fstab hostname initrd grub_install passwords \
639      custom_scripts upgrade_system remove_apt_cache services \
640      remove_chrootmirror; do
641      if stage $i ; then
642        $i && stage $i 'done' || exit 1
643      fi
644   done
645   # always execute the finalize stage:
646   finalize
647 # }}}
648
649 # finally exit the chroot {{{
650   echo "Finished chroot installation, exiting."
651   exit 0
652 # }}}
653
654 ## END OF FILE #################################################################
655 # vim: ai tw=80 expandtab foldmethod=marker