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