Implement dpkg-divert handling to address failing update-grub in openvz environments.
[grml-live.git] / etc / grml / fai / config / hooks / instsoft.GRMLBASE
1 #!/bin/bash
2 # Filename:      ${GRML_FAI_CONFIG}/hooks/instsoft.GRMLBASE
3 # Purpose:       Grml specific software installation in the chroot, executed after updatebase
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 or any later version.
7 ################################################################################
8
9 set -u
10 set -e
11
12 GRML_SOURCES_LIST="$target/etc/apt/sources.list.d/grml.list"
13 DEBIAN_SOURCES_LIST="$target/etc/apt/sources.list.d/debian.list"
14
15 # if hooks/updatebase.GRMLBASE fails for whatever reason
16 # and can't skip instsoft.GRMLBASE we have to make sure
17 # we exit here as well
18 if [ -n "$BUILD_ONLY" ] ; then
19    "Exiting hooks/instsoft.GRMLBASE as BUILD_ONLY environment is set."
20    exit 0
21 fi
22
23 if [ "$FAI_ACTION" = "softupdate" ] ; then
24    echo "Action $FAI_ACTION of FAI (hooks/instsoft.GRMLBASE) via grml-live running"
25
26    if [ -r /etc/resolv.conf ] ; then
27      if [ -d $target/etc/resolvconf/run ] ; then # resolvconf without /run
28        # sanity check to avoid "input file is output file",
29        # see http://bts.grml.org/grml/issue984
30        if ! [ -L $target/etc/resolvconf/run/resolv.conf ] ; then
31          cat /etc/resolv.conf >> $target/etc/resolvconf/run/resolv.conf
32        else
33          echo "Error: /etc/resolvconf/run/resolv.conf in chroot should not be a symlink." >&2
34          echo "       Looks like something is wrong, please fix manually." >&2
35          exit 1
36        fi
37      elif [ -L $target/etc/resolvconf/run ] ; then # resolvconf with /run
38        # /etc/resolvconf/run symlinks to /run/resolvconf
39        mkdir -p $target/run/resolvconf
40        cat /etc/resolv.conf >> $target/run/resolvconf/resolv.conf
41      else # no resolvconf installed in chroot
42        if ! [ -L $target/etc/resolv.conf ] ; then
43          cat /etc/resolv.conf >> $target/etc/resolv.conf
44        else
45          echo "Error: /etc/resolv.conf in chroot should not be a symlink (resolvconf not installed)." >&2
46          exit 1
47        fi
48      fi
49    fi
50
51    if [ -r $target/etc/policy-rc.d.conf ] ; then
52       sed -i "s/EXITSTATUS=.*/EXITSTATUS='101'/" $target/etc/policy-rc.d.conf
53    fi
54
55    # we definitely don't want to fail running fai sofupdate just
56    # because of some well known bugs:
57    [ -d $target/etc/apt/apt.conf.d ] || mkdir $target/etc/apt/apt.conf.d
58    cat > $target/etc/apt/apt.conf.d/10apt-listbugs << EOF
59 // Check all packages whether they has critical bugs before they are installed.
60 // If you don't like it, comment it out.
61 //DPkg::Pre-Install-Pkgs {"/usr/sbin/apt-listbugs apt || exit 10"};
62 //DPkg::Tools::Options::/usr/sbin/apt-listbugs "";
63 //DPkg::Tools::Options::/usr/sbin/apt-listbugs::Version "2";
64 EOF
65
66    # FAI softupdate executes upgrade only with the sources.list being
67    # present in the chroot - so let's do it on our own:
68    fcopy /etc/apt/sources.list
69
70    # when re-running grml-live with the -u option we do not want to use the
71    # sources.list config of the live system but grml-live's config instead,
72    # therefore temporarly move known files and restore them again later on
73    # through /etc/grml/fai/config/scripts/GRMLBASE/33-aptsetup:
74    if [ -r "$GRML_SOURCES_LIST" ] ; then
75       mv "$GRML_SOURCES_LIST" "${GRML_SOURCES_LIST}.unused"
76    fi
77
78    if [ -r "$DEBIAN_SOURCES_LIST" ] ; then
79       mv "$DEBIAN_SOURCES_LIST" "${DEBIAN_SOURCES_LIST}.unused"
80    fi
81
82    # run it on our own, as updatebase is used at the wrong stage for our needs,
83    # see ${GRML_FAI_CONFIG}/hooks/updatebase.GRMLBASE
84    # the apt-get update might return an error if there's for example
85    # a hashsum mismatch on Debian mirror sources, we might want to continue
86    # but should warn the user
87    if ! $ROOTCMD apt-get update ; then
88       echo "Warning: there was an error executing apt-get update, continuing anyway."
89    fi
90
91    # work around /etc/kernel/postinst.d/zz-update-grub failing
92    # inside openvz environment, see #597084
93    if ! $ROOTCMD dpkg-divert --list | grep -q '/usr/sbin/update-grub' ; then
94      echo "Diverting update-grub executable"
95      $ROOTCMD dpkg-divert --rename --add /usr/sbin/update-grub
96      $ROOTCMD ln -s /bin/true /usr/sbin/update-grub
97    fi
98
99   # newer aptitude versions won't remove essential packages using
100   # 'aptitude -f -y install file-rc' anymore, therefore force it:
101   $ROOTCMD aptitude -o Aptitude::ProblemResolver::Keep-All-Tier=60000 -f -y install file-rc
102
103    if ! [ -x $target/usr/bin/aptitude ] ; then
104       $ROOTCMD apt-get -y install aptitude
105    fi
106
107    if [ -x $target/usr/bin/aptitude ] ; then
108       if $ROOTCMD aptitude --help | grep -q safe-upgrade ; then
109          APT_LISTCHANGES_FRONTEND=none APT_LISTBUGS_FRONTEND=none $ROOTCMD aptitude -y safe-upgrade
110       else
111          APT_LISTCHANGES_FRONTEND=none APT_LISTBUGS_FRONTEND=none $ROOTCMD aptitude -y upgrade
112       fi
113    else
114       APT_LISTCHANGES_FRONTEND=none APT_LISTBUGS_FRONTEND=none $ROOTCMD apt-get -y upgrade
115    fi
116
117    exit # make sure we don't continue behind the following "fi"
118 fi
119
120 # no softupdate but fresh installation
121 echo "Action $FAI_ACTION of FAI (hooks/instsoft.GRMLBASE) via grml-live running"
122
123 # make sure we have file-rc available before package_config/GRML* is being executed {{{
124 # the apt-get update might return an error if there's for example
125 # a hashsum mismatch on Debian mirror sources, we might want to continue
126 # but should warn the user
127 if ! $ROOTCMD apt-get update ; then
128    echo "Warning: there was an error executing apt-get update, continuing anyway."
129    echo "Warning: there was an error executing apt-get update, continuing anyway." >&2
130 fi
131
132 # newer aptitude versions won't remove essential packages using
133 # 'aptitude -f -y install file-rc' anymore, therefore force it via:
134 $ROOTCMD aptitude -o Aptitude::ProblemResolver::Keep-All-Tier=60000 -f -y install file-rc
135 # }}}
136
137 # get rid of insserv:
138 if $ROOTCMD dpkg --list insserv 2>/dev/null | grep -q '^ii' ; then
139    $ROOTCMD apt-get -y --purge remove insserv
140 fi
141
142 # we definitely don't want to fail running fai dirinstall just
143 # because of some well known bugs:
144 [ -d $target/etc/apt/apt.conf.d ] || mkdir $target/etc/apt/apt.conf.d
145 cat > $target/etc/apt/apt.conf.d/10apt-listbugs << EOF
146 // Check all packages whether they has critical bugs before they are installed.
147 // If you don't like it, comment it out.
148 //DPkg::Pre-Install-Pkgs {"/usr/sbin/apt-listbugs apt || exit 10"};
149 //DPkg::Tools::Options::/usr/sbin/apt-listbugs "";
150 //DPkg::Tools::Options::/usr/sbin/apt-listbugs::Version "2";
151 EOF
152
153 # make sure /dev/MAKEDEV is available:
154 if [ -x "$target"/sbin/MAKEDEV ] && ! [ -r "$target"/dev/MAKEDEV ] ; then
155    ln -s /sbin/MAKEDEV "$target"/dev/MAKEDEV
156 fi
157
158 # we don't need the invoke-rc.d.d diversion (we have grml-policyrcd :)):
159 if [ -L "$target"/usr/sbin/invoke-rc.d ] ; then
160    rm -f "$target"/usr/sbin/invoke-rc.d
161    $ROOTCMD dpkg-divert --package fai --rename --remove /usr/sbin/invoke-rc.d
162 fi
163
164 ## END OF FILE #################################################################
165 # vim:ft=sh expandtab ai tw=80 tabstop=4 shiftwidth=2