source: projects/specs/trunk/a/apt/remove-old-kernels.lua @ 2109

Revision 2109, 3.2 KB checked in by daisuke, 14 years ago (diff)

apt: update remove-old-kernels.lua

  • check /boot/vmlinuz and latest kernel
  • Property svn:executable set to *
Line 
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
12
13if confget("RPM::Remove-Old-Kernels/b", "true") == "false" then
14  return
15end
16
17-- remove old kernel and kernel-devel
18knames = {'kernel', 'kernel%-pae', 'kernel%-smp', 'kernel%-devel', 'kernel%-pae%-devel', 'kernel%-smp%-devel'}
19
20-- get current running kernel version
21function get_running_kernel_ver()
22    local inp = io.popen("uname -r")
23    local currentver = nil
24    for line in inp.lines(inp) do
25        currentver = string.gsub(line,"pae$","")
26    end
27    if currentver then
28        return currentver
29    else
30        return 0
31    end
32end
33
34-- get kernel version of vmlinuz.
35function get_current_kernel_ver()
36    local inp = io.popen("readlink /boot/vmlinuz")
37    local oldver = nil
38    for line in inp.lines(inp) do
39        oldver = string.gsub(string.gsub(line,"pae$",""),"^vmlinuz%-","")
40    end
41    if oldver then
42        return oldver
43    else
44        return 0
45    end
46end
47
48-- get kernel version of vmlinuz.old
49function get_old_kernel_ver()
50    local inp = io.popen("readlink /boot/vmlinuz.old")
51    local oldver = nil
52    for line in inp.lines(inp) do
53        oldver = string.gsub(string.gsub(line,"pae$",""),"^vmlinuz%-","")
54    end
55    if oldver then
56        return oldver
57    else
58        return 0
59    end
60end
61
62function get_latest(kname)
63    pkgs = pkglist()
64    latest = "0"
65    for i, pkg in ipairs(pkgs) do
66        if not pkgisvirtual(pkg) and string.find(pkgname(pkg), kname.."#") then
67            ver = pkgvercur(pkg)
68            if not ver then
69                ver = pkgvercand(pkg)
70            end
71            if ver and verstrcmp(verstr(ver), latest) > 0 then
72                latest = verstr(ver)
73                latestpkg = pkg
74            end
75        end
76    end
77    return latestpkg
78end
79
80function get_latest_kernel_ver()
81    latestver = nil
82    latestver = verstr(pkgvercur(get_latest("kernel")))
83    if latestver then
84        return latestver
85    else
86        return 0
87    end
88end
89
90-- remove all kernels older than current or vmlinuz.old
91function remove_old_kernels()
92    rkver = get_running_kernel_ver()
93    ckver = get_current_kernel_ver()
94    lkver = get_latest_kernel_ver()
95    okver = get_old_kernel_ver()
96    pkgs = pkglist()
97    for i, pkg in ipairs(pkgs) do
98        ver = pkgvercur(pkg)
99        if not pkgisvirtual(pkg) and ver then
100            for j, kname in ipairs(knames) do
101                if string.find(pkgname(pkg), kname..'#') then
102                    if ( verstrcmp(verstr(ver), rkver) < 0 and
103                         verstrcmp(verstr(ver), ckver) < 0 and
104                         verstrcmp(verstr(ver), lkver) < 0 and
105                         verstrcmp(verstr(ver), okver) < 0 ) then
106                        markremove(pkg)
107                    end
108                end
109            end
110        end
111    end
112end
113
114--
115if script_slot == "Scripts::AptGet::DistUpgrade" or
116   script_slot == "Scripts::AptGet::Upgrade" or
117   script_slot == "Scripts::Synaptic::DistUpgrade" or
118   script_slot == "Scripts::Synaptic::Upgrade" then
119    remove_old_kernels()
120end
121
122-- vim:ts=4:sw=4:et
Note: See TracBrowser for help on using the repository browser.