| 1 |
#!/usr/bin/env python |
| 2 |
from __future__ import print_function |
| 3 |
|
| 4 |
|
| 5 |
# Copyright 2015 Tony Garcia |
| 6 |
# |
| 7 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
# you may not use this file except in compliance with the License. |
| 9 |
# You may obtain a copy of the License at |
| 10 |
# |
| 11 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
# |
| 13 |
# Unless required by applicable law or agreed to in writing, software |
| 14 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
# See the License for the specific language governing permissions and |
| 17 |
# limitations under the License. |
| 18 |
|
| 19 |
"""Tool to Purge Unused Kernels |
| 20 |
Removes those kernel packages marked as candidate for autoremoval. |
| 21 |
Those packages are generally installed via Unattended upgrade or meta-packages |
| 22 |
By default the latest kernel and manual installations are marked to Never |
| 23 |
Auto Remove. |
| 24 |
|
| 25 |
Ubuntu has multiple suggestions on how to remove kernels: |
| 26 |
https://help.ubuntu.com/community/RemoveOldKernels |
| 27 |
|
| 28 |
A great recommendation is to make use of unattended-upgrades (u-u). Since |
| 29 |
version 1.0 u-u is capable of removing unused kernel packages. |
| 30 |
|
| 31 |
thresher - A device that first separates the head of a stalk of grain from |
| 32 |
the straw, and then further separates the kernel from the rest of the head. |
| 33 |
""" |
| 34 |
|
| 35 |
import re |
| 36 |
import sys |
| 37 |
import logging |
| 38 |
import argparse |
| 39 |
from glob import iglob |
| 40 |
from os import path |
| 41 |
from platform import uname |
| 42 |
from logging.handlers import SysLogHandler |
| 43 |
from distutils.version import LooseVersion |
| 44 |
|
| 45 |
try: |
| 46 |
import configparser |
| 47 |
except ImportError: |
| 48 |
import ConfigParser as configparser |
| 49 |
|
| 50 |
try: |
| 51 |
Parser = configparser.ConfigParser |
| 52 |
except AttributeError: |
| 53 |
Parser = configparser.SafeConfigParser |
| 54 |
|
| 55 |
try: |
| 56 |
import apt |
| 57 |
except ImportError: |
| 58 |
try: |
| 59 |
import distro |
| 60 |
except ImportError: |
| 61 |
from platform import dist |
| 62 |
|
| 63 |
DISTRO = dist()[0] |
| 64 |
else: |
| 65 |
DISTRO = distro.linux_distribution(False)[0] |
| 66 |
|
| 67 |
if DISTRO == "debian" or DISTRO == "Ubuntu": |
| 68 |
print( |
| 69 |
"Error: python apt library was not found\n" |
| 70 |
"python-apt and/or python3-apt packages provide it.", |
| 71 |
file=sys.stderr, |
| 72 |
) |
| 73 |
else: |
| 74 |
print("Error: {0} distro not supported".format(DISTRO), file=sys.stderr) |
| 75 |
sys.exit(1) |
| 76 |
|
| 77 |
|
| 78 |
__version__ = "1.4.1" |
| 79 |
|
| 80 |
|
| 81 |
# Loggers |
| 82 |
logger = logging.getLogger("kthresher") |
| 83 |
|
| 84 |
|
| 85 |
def cmp_to_key(mycmp): |
| 86 |
"""Convert a cmp= function into a key= function |
| 87 |
Wrapper to leverage the use of apt_pkg.version_compare. |
| 88 |
As documented in: |
| 89 |
https://docs.python.org/3/howto/sorting.html#the-old-way-using-the-cmp-parameter |
| 90 |
""" |
| 91 |
|
| 92 |
class K: |
| 93 |
def __init__(self, obj, *args): |
| 94 |
self.obj = obj |
| 95 |
|
| 96 |
def __lt__(self, other): |
| 97 |
return mycmp(self.obj, other.obj) < 0 |
| 98 |
|
| 99 |
def __gt__(self, other): |
| 100 |
return mycmp(self.obj, other.obj) > 0 |
| 101 |
|
| 102 |
def __eq__(self, other): |
| 103 |
return mycmp(self.obj, other.obj) == 0 |
| 104 |
|
| 105 |
def __le__(self, other): |
| 106 |
return mycmp(self.obj, other.obj) <= 0 |
| 107 |
|
| 108 |
def __ge__(self, other): |
| 109 |
return mycmp(self.obj, other.obj) >= 0 |
| 110 |
|
| 111 |
def __ne__(self, other): |
| 112 |
return mycmp(self.obj, other.obj) != 0 |
| 113 |
|
| 114 |
return K |
| 115 |
|
| 116 |
|
| 117 |
def get_configs(conf_file, section): |
| 118 |
"""Obtains the configs from a file. |
| 119 |
Config file format: INI |
| 120 |
Valid sections: main |
| 121 |
Valid options: headers, include, keep, purge, verbose |
| 122 |
Example: |
| 123 |
[main] |
| 124 |
headers=(yes|on|true|no|off|false) |
| 125 |
include=/path/to/dir/ |
| 126 |
keep=[0-9] |
| 127 |
purge=(yes|on|true|no|off|false) |
| 128 |
verbose=(yes|on|true|no|off|false) |
| 129 |
""" |
| 130 |
valid_configs = { |
| 131 |
"headers": "boolean", |
| 132 |
"include": "str", |
| 133 |
"keep": "int", |
| 134 |
"purge": "boolean", |
| 135 |
"verbose": "boolean", |
| 136 |
} |
| 137 |
configs = {} |
| 138 |
def_conf = Parser() |
| 139 |
logger.info("Attempting to read {0}.".format(conf_file)) |
| 140 |
try: |
| 141 |
def_conf.read(conf_file) |
| 142 |
except configparser.ParsingError: |
| 143 |
logger.error("Config file contains errors: {0}".format(conf_file)) |
| 144 |
sys.exit(1) |
| 145 |
if not def_conf.read(conf_file): |
| 146 |
logger.info( |
| 147 |
"Config file {0} is empty or does not exist, ignoring.".format(conf_file) |
| 148 |
) |
| 149 |
return configs |
| 150 |
if not def_conf.has_section(section): |
| 151 |
logger.info("Unable to find section [{0}].".format(section)) |
| 152 |
return configs |
| 153 |
if len(def_conf.options(section)) < 1: |
| 154 |
logger.info("No options found in section [{0}].".format(section)) |
| 155 |
return configs |
| 156 |
logger.info("Options found: {0}.".format(def_conf.options(section))) |
| 157 |
# Validation of the options found |
| 158 |
for option in def_conf.options(section): |
| 159 |
if option not in valid_configs.keys(): |
| 160 |
logger.info('Invalid setting "{0}", ignoring'.format(option)) |
| 161 |
else: |
| 162 |
logger.info('Valid setting found "{0}"'.format(option)) |
| 163 |
if valid_configs[option] == "int": |
| 164 |
try: |
| 165 |
configs[option] = def_conf.getint(section, option) |
| 166 |
except configparser.NoOptionError: |
| 167 |
logger.error('Unable to get value from "{0}".'.format(option)) |
| 168 |
sys.exit(1) |
| 169 |
except ValueError: |
| 170 |
logger.error( |
| 171 |
'Invalid "{0}" value, an integer is required.'.format(option) |
| 172 |
) |
| 173 |
sys.exit(1) |
| 174 |
if option == "keep": |
| 175 |
if configs[option] > 9: |
| 176 |
logger.error("keep value should be between 0-9.") |
| 177 |
sys.exit(1) |
| 178 |
elif valid_configs[option] == "boolean": |
| 179 |
try: |
| 180 |
configs[option] = def_conf.getboolean(section, option) |
| 181 |
except configparser.NoOptionError: |
| 182 |
logger.error('Unable to get value from "{0}".'.format(option)) |
| 183 |
sys.exit(1) |
| 184 |
except ValueError: |
| 185 |
logger.error( |
| 186 |
'Invalid "{0}" value, a boolean is required.'.format(option) |
| 187 |
) |
| 188 |
sys.exit(1) |
| 189 |
elif valid_configs[option] == "str": |
| 190 |
try: |
| 191 |
configs[option] = def_conf.get(section, option) |
| 192 |
except configparser.NoOptionError: |
| 193 |
logger.error('Unable to get value from "{0}".'.format(option)) |
| 194 |
logger.info("\t{0} = {1}".format(option, configs[option])) |
| 195 |
if "include" in configs.keys(): |
| 196 |
# Obtain the configs on each nested config file. |
| 197 |
for nested_file in sorted(iglob(configs["include"])): |
| 198 |
# Aborting if importing the same config file. |
| 199 |
# Won't prevent indirect loops. |
| 200 |
if nested_file == conf_file: |
| 201 |
logger.error("Looping config files, aborting...") |
| 202 |
sys.exit(1) |
| 203 |
nested_configs = get_configs(nested_file, section) |
| 204 |
# Override any option coming from the nested configs. |
| 205 |
for nested_config in nested_configs: |
| 206 |
configs[nested_config] = nested_configs[nested_config] |
| 207 |
return configs |
| 208 |
|
| 209 |
|
| 210 |
def show_autoremovable_pkgs(): |
| 211 |
"""List all the kernel related packages available for autoremoval. |
| 212 |
""" |
| 213 |
packages = {} |
| 214 |
ver_max_len = 0 |
| 215 |
try: |
| 216 |
apt_cache = apt.Cache() |
| 217 |
except SystemError: |
| 218 |
logger.error("Unable to obtain the cache!") |
| 219 |
sys.exit(1) |
| 220 |
for pkg_name in apt_cache.keys(): |
| 221 |
pkg = apt_cache[pkg_name] |
| 222 |
if (pkg.is_installed and pkg.is_auto_removable) and re.match( |
| 223 |
r"^linux-(image|(\w+-)?headers)-.*$", pkg_name |
| 224 |
): |
| 225 |
packages[pkg_name] = pkg.installed.version |
| 226 |
if ver_max_len < len(pkg.installed.version): |
| 227 |
ver_max_len = len(pkg.installed.version) |
| 228 |
if packages: |
| 229 |
logger.info("List of kernel packages available for autoremoval:") |
| 230 |
logger.info( |
| 231 |
"{0:>{width}} {1:<{width}}".format( |
| 232 |
"Version", "Package", width=ver_max_len + 2 |
| 233 |
) |
| 234 |
) |
| 235 |
for package in sorted(packages.keys()): |
| 236 |
logger.info( |
| 237 |
"{0:>{width}} {1:<{width}}".format( |
| 238 |
packages[package], package, width=ver_max_len + 2 |
| 239 |
) |
| 240 |
) |
| 241 |
logger.log( |
| 242 |
42, |
| 243 |
"kernel packages available for autoremoval: {0}".format( |
| 244 |
sorted(packages.keys()) |
| 245 |
), |
| 246 |
) |
| 247 |
else: |
| 248 |
logger.log(42, "No kernel packages available for autoremoval.") |
| 249 |
|
| 250 |
|
| 251 |
def kthreshing(purge=None, headers=None, keep=1): |
| 252 |
"""Purge or list the unused kernels. |
| 253 |
By default keeps 1. |
| 254 |
The running kernel, the kernels marked as NeverAutoRemove and |
| 255 |
Manually installed kernels are nevertouched by kthresher. |
| 256 |
""" |
| 257 |
kernels = {} |
| 258 |
ver_max_len = 0 |
| 259 |
kernel_image_regex = r"^linux-image-.*$" |
| 260 |
kernel_header_regex = r"^linux-(\w+-)?headers-.*$" |
| 261 |
try: |
| 262 |
apt_cache = apt.Cache() |
| 263 |
except SystemError: |
| 264 |
logger.error("Unable to obtain the cache!") |
| 265 |
sys.exit(1) |
| 266 |
current_kernel_ver = uname()[2] |
| 267 |
kernel_pkg = apt_cache["linux-image-%s" % current_kernel_ver] |
| 268 |
logger.info( |
| 269 |
"Running kernel is {0} v[{1}]".format( |
| 270 |
kernel_pkg.name, kernel_pkg.installed.version |
| 271 |
) |
| 272 |
) |
| 273 |
for pkg_name in apt_cache.keys(): |
| 274 |
pkg = apt_cache[pkg_name] |
| 275 |
section = pkg.candidate.section or '' |
| 276 |
if (pkg.is_installed and pkg.is_auto_removable) and ( |
| 277 |
"kernel" in section and re.match(kernel_image_regex, pkg_name) |
| 278 |
): |
| 279 |
if ver_max_len < len(pkg.installed.version): |
| 280 |
ver_max_len = len(pkg.installed.version) |
| 281 |
kernels.setdefault(pkg.installed.version, []).append(pkg.name) |
| 282 |
if headers: |
| 283 |
for pkg_name in apt_cache.keys(): |
| 284 |
pkg = apt_cache[pkg_name] |
| 285 |
if (pkg.is_installed and pkg.is_auto_removable) and re.match( |
| 286 |
kernel_header_regex, pkg_name |
| 287 |
): |
| 288 |
if pkg.installed.version in kernels.keys(): |
| 289 |
kernels[pkg.installed.version].append(pkg.name) |
| 290 |
else: |
| 291 |
kernels[pkg.installed.version] = [pkg.name] |
| 292 |
if kernels: |
| 293 |
logger.info("Attempting to keep {0} kernel package(s)".format(keep)) |
| 294 |
kernel_versions = list(kernels.copy().keys()) |
| 295 |
logger.info( |
| 296 |
"Found {0} kernel image(s) installed and available for " |
| 297 |
"autoremoval".format(len(kernel_versions)) |
| 298 |
) |
| 299 |
logger.info("Pre-sorting: {0}".format(kernel_versions)) |
| 300 |
try: |
| 301 |
# Sadly this is broken in python3, https://bugs.python.org/issue14894 |
| 302 |
sorted_kernel_list = sorted(kernel_versions, key=LooseVersion) |
| 303 |
except TypeError: |
| 304 |
# Using apt_pkg.version_compare |
| 305 |
# https://github.com/rackerlabs/kthresher/pull/61 |
| 306 |
sorted_kernel_list = sorted( |
| 307 |
kernel_versions, key=cmp_to_key(apt.apt_pkg.version_compare) |
| 308 |
) |
| 309 |
|
| 310 |
logger.info("Post-sorting: {0}".format(sorted_kernel_list)) |
| 311 |
if keep >= len(kernel_versions): |
| 312 |
logger.log( |
| 313 |
42, |
| 314 |
"Nothing to do, attempting to keep {0} out of {1} " |
| 315 |
"kernel images.".format(keep, len(kernel_versions)), |
| 316 |
) |
| 317 |
sys.exit(0) |
| 318 |
else: |
| 319 |
purged_pkgs = [] |
| 320 |
for index in range(0, len(sorted_kernel_list) - keep): |
| 321 |
kernel_version = sorted_kernel_list[index] |
| 322 |
logger.info( |
| 323 |
"\tPurging packages from version: {0}".format(kernel_version) |
| 324 |
) |
| 325 |
for pkg_name in kernels[kernel_version]: |
| 326 |
logger.info("\t\tPurging: {0}".format(pkg_name)) |
| 327 |
if purge: |
| 328 |
pkg = apt_cache[pkg_name] |
| 329 |
pkg.mark_delete(purge=True) |
| 330 |
purged_pkgs.append(pkg_name) |
| 331 |
if purge: |
| 332 |
try: |
| 333 |
apt_cache.commit( |
| 334 |
fetch_progress=apt.progress.text.AcquireProgress(), |
| 335 |
install_progress=apt.progress.base.InstallProgress(), |
| 336 |
) |
| 337 |
except apt.cache.LockFailedException as lfe: |
| 338 |
logger.error("{}, are you root?".format(lfe)) |
| 339 |
sys.exit(1) |
| 340 |
except SystemError: |
| 341 |
logger.error("Unable to commit the changes") |
| 342 |
sys.exit(1) |
| 343 |
logger.log( |
| 344 |
42, |
| 345 |
"kernel packages purged: {} - {}".format( |
| 346 |
len(purged_pkgs), purged_pkgs |
| 347 |
), |
| 348 |
) |
| 349 |
else: |
| 350 |
logger.info("No packages available for autoremoval.") |
| 351 |
|
| 352 |
|
| 353 |
def main(): |
| 354 |
"""The main function. |
| 355 |
""" |
| 356 |
defaults = { |
| 357 |
"config": {"file": "/etc/kthresher.conf", "section": "main"}, |
| 358 |
"options": { |
| 359 |
"dry_run": False, |
| 360 |
"headers": False, |
| 361 |
"keep": 1, |
| 362 |
"purge": False, |
| 363 |
"verbose": False, |
| 364 |
}, |
| 365 |
} |
| 366 |
options = defaults["options"].copy() |
| 367 |
conf_options = {} |
| 368 |
parser = argparse.ArgumentParser( |
| 369 |
description="Purge Unused Kernels.", prog="kthresher" |
| 370 |
) |
| 371 |
parser.add_argument( |
| 372 |
"-c", |
| 373 |
"--config", |
| 374 |
type=str, |
| 375 |
metavar="FILE", |
| 376 |
help="Config file, default is /etc/kthresher.conf", |
| 377 |
) |
| 378 |
parser.add_argument( |
| 379 |
"-d", |
| 380 |
"--dry-run", |
| 381 |
action="store_true", |
| 382 |
help="List unused kernel images available to purge" |
| 383 |
" (dry run). Is always verbose.", |
| 384 |
) |
| 385 |
parser.add_argument( |
| 386 |
"-H", |
| 387 |
"--headers", |
| 388 |
action="store_true", |
| 389 |
help="Include the search for kernel headers.", |
| 390 |
) |
| 391 |
parser.add_argument( |
| 392 |
"-k", |
| 393 |
"--keep", |
| 394 |
nargs="?", |
| 395 |
type=int, |
| 396 |
const=1, |
| 397 |
metavar="N", |
| 398 |
choices=range(0, 10), |
| 399 |
help="Number of kernels to keep, default 1.", |
| 400 |
) |
| 401 |
parser.add_argument( |
| 402 |
"-p", "--purge", help="Purge Unused Kernels.", action="store_true" |
| 403 |
) |
| 404 |
parser.add_argument( |
| 405 |
"-s", |
| 406 |
"--show-autoremoval", |
| 407 |
action="store_true", |
| 408 |
help="Show kernel packages available for autoremoval. This is the " |
| 409 |
"default action", |
| 410 |
) |
| 411 |
parser.add_argument("-v", "--verbose", action="store_true", help="Be verbose.") |
| 412 |
parser.add_argument( |
| 413 |
"-V", |
| 414 |
"--version", |
| 415 |
action="version", |
| 416 |
version="%(prog)s v{0}".format(__version__), |
| 417 |
help="Print version.", |
| 418 |
) |
| 419 |
args = parser.parse_args() |
| 420 |
|
| 421 |
# Default logger |
| 422 |
logger.setLevel(logging.ERROR) |
| 423 |
|
| 424 |
# Configure console handler |
| 425 |
ch = logging.StreamHandler() |
| 426 |
cf = logging.Formatter("%(levelname)s: %(message)s") |
| 427 |
ch.setFormatter(cf) |
| 428 |
logger.addHandler(ch) |
| 429 |
|
| 430 |
# Console logging override |
| 431 |
if args.verbose or args.dry_run: |
| 432 |
logger.setLevel(logging.INFO) |
| 433 |
|
| 434 |
# Configure syslog handler |
| 435 |
if path.exists("/dev/log"): |
| 436 |
sh = SysLogHandler("/dev/log") |
| 437 |
sf = logging.Formatter("%(name)s[%(process)d]: %(message)s") |
| 438 |
sh.setLevel(logging.ERROR) |
| 439 |
sh.setFormatter(sf) |
| 440 |
logger.addHandler(sh) |
| 441 |
|
| 442 |
# Create new logging level to be used on syslog |
| 443 |
logging.addLevelName(42, "INFO") |
| 444 |
|
| 445 |
# Read config files |
| 446 |
if args.config: |
| 447 |
conf_options = get_configs(args.config, defaults["config"]["section"]) |
| 448 |
else: |
| 449 |
conf_options = get_configs( |
| 450 |
defaults["config"]["file"], defaults["config"]["section"] |
| 451 |
) |
| 452 |
# Overriding options as follows: |
| 453 |
# defaults -> default config file or custom config file -> included config |
| 454 |
# -> cli arguments |
| 455 |
# First overriding default configs from a file if available: |
| 456 |
if conf_options: |
| 457 |
options.update(conf_options) |
| 458 |
# Override the verbosity if set through configuration |
| 459 |
if options["verbose"]: |
| 460 |
logger.setLevel(logging.INFO) |
| 461 |
# Now overriding the result options with cli arguments |
| 462 |
if args.dry_run: |
| 463 |
options["dry_run"] = args.dry_run |
| 464 |
if args.headers: |
| 465 |
options["headers"] = args.headers |
| 466 |
if args.keep is not None: |
| 467 |
options["keep"] = args.keep |
| 468 |
if args.purge: |
| 469 |
options["purge"] = args.purge |
| 470 |
if args.verbose: |
| 471 |
options["verbose"] = args.verbose |
| 472 |
logger.info("Options: {0}".format(options)) |
| 473 |
# Show auto-removable, this is only available via explicit argument |
| 474 |
# Overrides actions defined in the configuration file(s). |
| 475 |
if args.show_autoremoval: |
| 476 |
logger.setLevel(logging.INFO) |
| 477 |
show_autoremovable_pkgs() |
| 478 |
sys.exit(0) |
| 479 |
if options["dry_run"]: |
| 480 |
logger.info("----- DRY RUN -----") |
| 481 |
kthreshing(purge=False, headers=options["headers"], keep=options["keep"]) |
| 482 |
sys.exit(0) |
| 483 |
if options["purge"]: |
| 484 |
kthreshing(purge=True, headers=options["headers"], keep=options["keep"]) |
| 485 |
sys.exit(0) |
| 486 |
if not sys.stdout.isatty(): |
| 487 |
sys.exit(0) |
| 488 |
else: |
| 489 |
# Show auto-remove is also a default option if no other action is |
| 490 |
# defined. |
| 491 |
logger.setLevel(logging.INFO) |
| 492 |
show_autoremovable_pkgs() |
| 493 |
sys.exit(0) |
| 494 |
|
| 495 |
|
| 496 |
if __name__ == "__main__": |
| 497 |
main() |