#!/bin/bash # Check if the running kernel has the same version string as the on-disk # kernel image. # Copyright 2008 Peter Palfrader # Copyright 2009 Stephen Gran # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. OK=0; WARNING=1; CRITICAL=2; UNKNOWN=3; get_offset() { local file needle file="$1" needle="$2" perl -e ' undef $/; $i = index(<>, "'"$needle"'"); if ($i < 0) { exit 1; }; print $i,"\n"' < "$file" } get_avail() { # This is wrong, but leaves room for when we have to care for machines running # myfirstunix-image-0.1-dsa-arm local prefix=linux local kervers=$(uname -r) local kernelversion=$(apt-cache policy ${prefix}-image-${kervers} 2>/dev/null | grep '^ Installed:' | awk '{print $2}') local metavers='' # DSA uses kernel versions of the form 2.6.29.3-dsa-dl380-oldxeon, where # Debian uses versions of the form 2.6.29-2-amd64 if [ "${kervers//dsa}" != "$kervers" ]; then metavers=$(echo $kervers | sed -r -e 's/^2\.(4|6)\.[0-9]+([\.0-9]+?)-(.*)/2.\1-\3/') else metavers=$(echo $kervers | sed -r -e 's/^2\.(4|6)\.[0-9]+-[A-Za-z0-9\.]+-(.*)/2.\1-\2/') fi # We're just going to give up if we can't find a matching metapackage # I tried being strict once, and it just caused a lot of headaches. We'll see how # being lax does for us local output=$(apt-cache policy ${prefix}-image-${metavers} 2>/dev/null) local metaavailvers=$(echo "$output" | grep '^ Candidate:' | awk '{print $2}') local metainstavers=$(echo "$output" | grep '^ Installed:' | awk '{print $2}') if [ -z "$metaavailvers" ] || [ "$metaavailvers" = '(none)' ]; then return 2 fi if [ -z "$metainstavers" ] || [ "$metainstavers" = '(none)' ]; then return 2 fi local last=0 for vers in $(apt-cache --no-all-versions depends ${prefix}-image-${metavers} | grep Depends | awk '{print $2}' | sort -u); do if dpkg --compare-versions $vers gt $last; then last=$vers fi done local imagename=$last if [ -z "$imagename" ] || [ "$imagename" = 0 ]; then return 2 fi local availvrs=$(apt-cache policy ${imagename} 2>/dev/null | grep '^ Candidate' | awk '{print $2}') if [ "$availvrs" = "$kernelversion" ]; then return 0 fi echo "$kernelversion != $availvrs" return 1 } get_image() { local image GZHDR1 GZHDR2 off image="$1" GZHDR1="\x1f\x8b\x08\x00" GZHDR2="\x1f\x8b\x08\x08" off=`get_offset "$image" $GZHDR1` [ "$?" != "0" ] && off="-1" if [ "$off" -eq "-1" ]; then off=`get_offset "$image" $GZHDR2` [ "$?" != "0" ] && off="-1" fi if [ "$off" -eq "0" ]; then zcat < "$image" return elif [ "$off" -ne "-1" ]; then (dd ibs="$off" skip=1 count=0 && dd bs=512k) < "$image" 2>/dev/null | zcat 2>/dev/null return fi echo "ERROR: Unable to extract kernel image." 2>&1 exit 1 } searched="" for on_disk in \ "/boot/vmlinuz-`uname -r`"\ "/boot/vmlinux-`uname -r`"; do if [ -e "$on_disk" ]; then on_disk_version="`get_image "$on_disk" | strings | grep 'Linux version' | head -n1`" [ -z "$on_disk_version" ] || break on_disk_version="`cat "$on_disk" | strings | grep 'Linux version' | head -n1`" [ -z "$on_disk_version" ] || break echo "UNKNOWN: Failed to get a version string from image $on_disk" exit $UNKNOWN fi searched="$searched $on_disk" done if ! [ -e "$on_disk" ]; then echo "WARNING: Did not find a kernel image (checked$searched) - I have no idea which kernel I am running" exit $WARNING fi running_version="`cat /proc/version`" if [ -z "$running_version" ] ; then echo "UNKNOWN: Failed to get a version string from running system" exit $UNKNOWN fi if [ "$running_version" != "$on_disk_version" ]; then echo "WARNING: Running kernel does not match on-disk kernel image: [$running_version != $on_disk_version]" exit $WARNING fi ret="$(get_avail)" if [ $? = 1 ]; then echo "WARNING: Kernel needs upgrade [$ret]" exit $WARNING fi echo "OK: Running kernel matches on disk image: [$running_version]" exit $OK