remove-old-kernels.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. -- This script will clean up all kernels older than current version.
  2. --
  3. -- This script must be plugged in the following slots:
  4. --
  5. -- Scripts::AptGet::Upgrade
  6. -- Scripts::AptGet::DistUpgrade
  7. -- Scripts::Synaptic::Upgrade
  8. -- Scripts::Synaptic::DistUpgrade
  9. --
  10. -- Author: Daisuke SUZUKI <daiuske@linux.or.jp>
  11. if confget("RPM::Remove-Old-Kernels/b", "true") == "false" then
  12. return
  13. end
  14. -- remove old kernel and kernel-devel
  15. knames = {'kernel', 'kernel%-pae', 'kernel%-smp', 'kernel%-devel', 'kernel%-pae%-devel', 'kernel%-smp%-devel'}
  16. -- get current running kernel version
  17. function get_running_kernel_ver()
  18. local inp = io.popen("uname -r")
  19. local currentver = nil
  20. for line in inp.lines(inp) do
  21. currentver = string.gsub(line,"pae$","")
  22. end
  23. if currentver then
  24. return currentver
  25. else
  26. return 0
  27. end
  28. end
  29. -- get kernel version of vmlinuz.
  30. function get_current_kernel_ver()
  31. local inp = io.popen("readlink /boot/vmlinuz")
  32. local oldver = nil
  33. for line in inp.lines(inp) do
  34. oldver = string.gsub(string.gsub(line,"pae$",""),"^vmlinuz%-","")
  35. end
  36. if oldver then
  37. return oldver
  38. else
  39. return 0
  40. end
  41. end
  42. -- get kernel version of vmlinuz.old
  43. function get_old_kernel_ver()
  44. local inp = io.popen("readlink /boot/vmlinuz.old")
  45. local oldver = nil
  46. for line in inp.lines(inp) do
  47. oldver = string.gsub(string.gsub(line,"pae$",""),"^vmlinuz%-","")
  48. end
  49. if oldver then
  50. return oldver
  51. else
  52. return 0
  53. end
  54. end
  55. function get_latest(kname)
  56. pkgs = pkglist()
  57. latest = "0"
  58. for i, pkg in ipairs(pkgs) do
  59. if not pkgisvirtual(pkg) and string.find(pkgname(pkg), kname.."#") then
  60. ver = pkgvercur(pkg)
  61. if not ver then
  62. ver = pkgvercand(pkg)
  63. end
  64. if ver and verstrcmp(verstr(ver), latest) > 0 then
  65. latest = verstr(ver)
  66. latestpkg = pkg
  67. end
  68. end
  69. end
  70. return latestpkg
  71. end
  72. function get_latest_kernel_ver()
  73. latestver = nil
  74. latestver = verstr(pkgvercur(get_latest("kernel")))
  75. if latestver then
  76. return latestver
  77. else
  78. return 0
  79. end
  80. end
  81. -- remove all kernels older than current or vmlinuz.old
  82. function remove_old_kernels()
  83. rkver = get_running_kernel_ver()
  84. ckver = get_current_kernel_ver()
  85. lkver = get_latest_kernel_ver()
  86. okver = get_old_kernel_ver()
  87. pkgs = pkglist()
  88. for i, pkg in ipairs(pkgs) do
  89. ver = pkgvercur(pkg)
  90. if not pkgisvirtual(pkg) and ver then
  91. for j, kname in ipairs(knames) do
  92. if string.find(pkgname(pkg), kname..'#') then
  93. if ( verstrcmp(verstr(ver), rkver) < 0 and
  94. verstrcmp(verstr(ver), ckver) < 0 and
  95. verstrcmp(verstr(ver), lkver) < 0 and
  96. verstrcmp(verstr(ver), okver) < 0 ) then
  97. markremove(pkg)
  98. end
  99. end
  100. end
  101. end
  102. end
  103. end
  104. --
  105. if script_slot == "Scripts::AptGet::DistUpgrade" or
  106. script_slot == "Scripts::AptGet::Upgrade" or
  107. script_slot == "Scripts::Synaptic::DistUpgrade" or
  108. script_slot == "Scripts::Synaptic::Upgrade" then
  109. remove_old_kernels()
  110. end
  111. -- vim:ts=4:sw=4:et