3 # Check if the running kernel has the same version string as the on-disk
6 # Copyright 2008,2009 Peter Palfrader
7 # Copyright 2009 Stephen Gran
8 # Copyright 2010 Uli Martens
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
18 # The above copyright notice and this permission notice shall be
19 # included in all copies or substantial portions of the Software.
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 $i = index(<>, "'"$needle"'");
45 print $i,"\n"' < "$file"
49 # This is wrong, but leaves room for when we have to care for machines running
50 # myfirstunix-image-0.1-dsa-arm
53 local kervers=$(uname -r)
57 # DSA uses kernel versions of the form 2.6.29.3-dsa-dl380-oldxeon, where
58 # Debian uses versions of the form 2.6.29-2-amd64
59 if [ "${kervers//dsa}" != "$kervers" ]; then
60 metavers=$(echo $kervers | sed -r -e 's/^2\.(4|6)\.[0-9]+([\.0-9]+?)-(.*)/2.\1-\3/')
62 metavers=$(echo $kervers | sed -r -e 's/^2\.(4|6)\.[0-9]+-[A-Za-z0-9\.]+-(.*)/2.\1-\2/')
65 # Attempt to track back to a metapackage failed. bail
66 if [ "$metavers" = "$kervers" ]; then
70 # We're just going to give up if we can't find a matching metapackage
71 # I tried being strict once, and it just caused a lot of headaches. We'll see how
72 # being lax does for us
74 local output=$(apt-cache policy ${prefix}-image-${metavers} 2>/dev/null)
75 local metaavailvers=$(echo "$output" | grep '^ Candidate:' | awk '{print $2}')
76 local metainstavers=$(echo "$output" | grep '^ Installed:' | awk '{print $2}')
78 if [ -z "$metaavailvers" ] || [ "$metaavailvers" = '(none)' ]; then
81 if [ -z "$metainstavers" ] || [ "$metainstavers" = '(none)' ]; then
85 if [ "$metaavailvers" != "$metainstavers" ] ; then
86 echo "${prefix}-image-${metavers} $metaavailvers available but $metainstavers installed"
91 # --no-all-versions show shows only the candidate
92 for vers in $(apt-cache --no-all-versions show ${prefix}-image-${metavers} | sed -n 's/^Depends: //p' | tr ',' '\n' | tr -d ' ' | grep ${prefix}-image | awk '{print $1}' | sort -u); do
93 if dpkg --compare-versions $vers gt $imagename; then
98 if [ -z "$imagename" ] || [ "$imagename" = 0 ]; then
102 if [ "$imagename" != "${prefix}-image-${kervers}" ]; then
103 if dpkg --compare-versions "$imagename" lt "${prefix}-image-${kervers}"; then
106 echo "$imagename" != "${prefix}-image-${kervers}"
110 local availvrs=$(apt-cache policy ${imagename} 2>/dev/null | grep '^ Candidate' | awk '{print $2}')
111 local kernelversion=$(apt-cache policy ${prefix}-image-${kervers} 2>/dev/null | grep '^ Installed:' | awk '{print $2}')
113 if [ "$availvrs" = "$kernelversion" ]; then
117 echo "$kernelversion != $availvrs"
122 local image GZHDR1 GZHDR2 LZHDR off
126 GZHDR1="\x1f\x8b\x08\x00"
127 GZHDR2="\x1f\x8b\x08\x08"
128 LZHDR="\x00\x00\x00\x02\xff"
130 off=`get_offset "$image" $GZHDR1`
131 [ "$?" != "0" ] && off="-1"
132 if [ "$off" -eq "-1" ]; then
133 off=`get_offset "$image" $GZHDR2`
134 [ "$?" != "0" ] && off="-1"
136 if [ "$off" -eq "0" ]; then
139 elif [ "$off" -ne "-1" ]; then
140 (dd ibs="$off" skip=1 count=0 && dd bs=512k) < "$image" 2>/dev/null | zcat 2>/dev/null
144 off=`get_offset "$image" $LZHDR`
145 [ "$?" != "0" ] && off="-1"
146 if [ "$off" -ne "-1" ]; then
147 (dd ibs="$[off-1]" skip=1 count=0 && dd bs=512k) < "$image" 2>/dev/null | lzcat 2>/dev/null
151 echo "ERROR: Unable to extract kernel image." 2>&1
157 "/boot/vmlinuz-`uname -r`"\
158 "/boot/vmlinux-`uname -r`"\
159 "/boot/kfreebsd-`uname -r`.gz"; do
161 if [ -e "$on_disk" ]; then
162 if [ ! -x "$(which strings)" ]; then
163 echo "UNKNOWN: 'strings' command missing, perhaps install binutils?"
166 if [ "${on_disk/vmlinu}" != "$on_disk" ]; then
167 on_disk_version="`get_image_linux "$on_disk" | strings | grep 'Linux version' | head -n1`"
168 [ -z "$on_disk_version" ] || break
169 on_disk_version="`cat "$on_disk" | strings | grep 'Linux version' | head -n1`"
170 [ -z "$on_disk_version" ] || break
172 echo "UNKNOWN: Failed to get a version string from image $on_disk"
175 on_disk_version="$(zcat $on_disk | strings | grep Debian | head -n 1 | sed -e 's/Debian [[:alnum:]]\+ (\(.*\))/\1/')"
178 searched="$searched $on_disk"
181 if ! [ -e "$on_disk" ]; then
182 echo "WARNING: Did not find a kernel image (checked$searched) - I have no idea which kernel I am running"
186 if [ "$(uname -s)" = "Linux" ]; then
187 running_version="`cat /proc/version`"
188 if [ -z "$running_version" ] ; then
189 echo "UNKNOWN: Failed to get a version string from running system"
193 if [ "$running_version" != "$on_disk_version" ]; then
194 echo "WARNING: Running kernel does not match on-disk kernel image: [$running_version != $on_disk_version]"
198 ret="$(get_avail_linux)"
200 echo "WARNING: Kernel needs upgrade [$ret]"
204 echo "No support for FreeBSD yet"
208 echo "OK: Running kernel matches on disk image: [$running_version]"