#!/usr/bin/python -tt # skvidal@linux.duke.edu # process kernel and kernel-smp packages and hand back n-v-r info on # the oldest ones barring: # - current running one and the newest 2 import rpm import sys import os def get_running_kernel_package_info(): ver = os.uname()[2] kernfile = '/boot/vmlinuz-%s' % ver ts = rpm.TransactionSet() mi = ts.dbMatch('basenames', kernfile) num = mi.count() if num == 0: return None else: hdr = mi.next() return (hdr['name'], hdr['version'], hdr['release']) def sorthdrs(hdr1, hdr2): rc = rpm.versionCompare(hdr1, hdr2) if rc < 0: return -1 elif rc == 0: return 0 elif rc > 0: return 1 def main(): runname, runver, runrel = get_running_kernel_package_info() runstring = '%s-%s-%s' % (runname, runver, runrel) ts = rpm.TransactionSet() oldlist = [] for pkgname in ['kernel', 'kernel-smp', 'kernel-enterprise' 'kernel-hugmem', 'kernel-bigmem']: mi = ts.dbMatch('name', pkgname) thislist = [] if mi.count() > 1: kernlist = [] for hdr in mi: thislist.append(hdr) thislist.sort(sorthdrs) for hdr in thislist[:-2]: kstring = '%s-%s-%s' % (hdr['name'], hdr['version'], hdr['release']) if kstring != runstring: oldlist.append(kstring) # get the list of all the older kernels # iterate them and remove our running kernel # return n-v-r on the line for handing off to yum for item in oldlist: print ' %s' % item if __name__ == "__main__": main()