Coder Social home page Coder Social logo

kthresher's Introduction

logo0

kthresher

Github release Build Master Status Build Development Status License Twitter

Tool to remove unused kernels that were installed automatically in Debian/Ubuntu.

This tool removes those kernel packages marked as candidate for autoremoval. Those packages are generally installed via Unattended upgrade or meta-packages.

By default, on apt 1.0 and below, the booted kernel, the latest-installed kernel and the latest kernel are set to "NeverAutoRemove". Or, for apt 1.2 and above, the booted kernel, the latest-installed kernel, the latest kernel and the second-latest kernel are set to "NeverAutoRemove".

The official documentation has multiple suggestions on how to remove kernels:
https://help.ubuntu.com/community/RemoveOldKernels

A great recommendation is to make use of unattended-upgrades u-u (debian, ubuntu). Since version 1.0 (debian, ubuntu) u-u removes unused kernel packages by default (Remove-Unused-Kernel-Packages). Some distribution versions may not make reference or may comment out that setting in its configuration, but unless it is explicitly disabled, u-u will attempt to remove unused kernels. More info here.

For scenarios where u-u is not available or older than 1.0, kthresher is still a good option.

thresher - A device that first separates the head of a stalk of grain from the straw, and then further separates the kernel from the rest of the head.


Latest Version PyPi Downloads Versions License Github Stars


Table of Contents


How a package is marked for autoremoval?

Whenever a package is auto-installed and there is no other dependency for it, the package is marked as a candidate for autoremoval, there is an exception if the APT configuration does have the package marked as "NeverAutoRemove".

How the kernel image is added into the "APT::NeverAutoRemove::" config?

When a kernel image is installed the postinstall script will issue the run-parts on /etc/kernel/postinst.d/ and /etc/kernel/postinst.d/${version} if any exist. The run-parts script will run each one of the scripts located in that directory, e.g.

# ls -1 /etc/kernel/postinst.d/
apt-auto-removal
initramfs-tools
update-notifier
x-grub-legacy-ec2
zz-update-grub

All the scripts found by run-parts are executed on post install of the kernel package and the output of apt-get install/upgrade/dist-upgrade will show them, e.g.

run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 3.13.0-96-generic /boot/vmlinuz-3.13.0-96-generic
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 3.13.0-96-generic /boot/vmlinuz-3.13.0-96-generic
run-parts: executing /etc/kernel/postinst.d/update-notifier 3.13.0-96-generic /boot/vmlinuz-3.13.0-96-generic
run-parts: executing /etc/kernel/postinst.d/x-grub-legacy-ec2 3.13.0-96-generic /boot/vmlinuz-3.13.0-96-generic
run-parts: executing /etc/kernel/postinst.d/zz-update-grub 3.13.0-96-generic /boot/vmlinuz-3.13.0-96-generic

The first script "apt-auto-removal" takes care of adding a configuration in /etc/apt/apt.conf.d/01autoremove-kernels this script generates that list based on the logic described above, it means that the NeverAutoRemove may have anything between two to three kernels listed.

Supported Operating Systems

Installation

script

wget -O kthresher https://raw.githubusercontent.com/rackerlabs/kthresher/master/kthresher.py
chmod u+x kthresher

pip

pip install kthresher

or

pip install git+https://github.com/rackerlabs/kthresher.git

Github

git clone https://github.com/rackerlabs/kthresher.git
cd kthresher && python setup.py install

Usage

$ kthresher -h
usage: kthresher [-h] [-c FILE] [-d] [-H] [-k [N]] [-p] [-s] [-v] [-V]

Purge Unused Kernels.

optional arguments:
  -h, --help            show this help message and exit
  -c FILE, --config FILE
                        Config file, default is /etc/kthresher.conf
  -d, --dry-run         List unused kernel images available to purge(dry run).
                        Is always verbose.
  -H, --headers         Include the search for kernel headers.
  -k [N], --keep [N]    Number of kernels to keep, default 1.
  -p, --purge           Purge Unused Kernels.
  -s, --show-autoremoval
                        Show kernel packages available for autoremoval.
  -v, --verbose         Be verbose.
  -V, --version         Print version.

Examples

List which kernel images and its dependencies would remove(dry run)

# kthresher -d
INFO: Attempting to read /etc/kthresher.conf.
INFO: Config file /etc/kthresher.conf is empty or does not exist, ignoring.
INFO: Options: {'purge': False, 'verbose': False, 'dry_run': True, 'keep': 1}
INFO: ----- DRY RUN -----
INFO: Running kernel is linux-image-3.13.0-83-generic v[3.13.0-83.127]
INFO: Attempting to keep 1 kernel package(s)
INFO: Found 4 kernel image(s) installed and available for autoremoval
INFO: Pre-sorting: ['3.16.0-60.80~14.04.1', '3.13.0-77.121', '3.13.0-63.103', '3.16.0-33.44~14.04.1']
INFO: Post-sorting: ['3.13.0-63.103', '3.13.0-77.121', '3.16.0-33.44~14.04.1', '3.16.0-60.80~14.04.1']
INFO:   Purging packages from version: 3.13.0-63.103
INFO:           Purging: linux-image-extra-3.13.0-63-generic
INFO:           Purging: linux-image-3.13.0-63-generic
INFO:   Purging packages from version: 3.13.0-77.121
INFO:           Purging: linux-image-3.13.0-77-generic
INFO:           Purging: linux-image-extra-3.13.0-77-generic
INFO:   Purging packages from version: 3.16.0-33.44~14.04.1
INFO:           Purging: linux-image-3.16.0-33-generic

Show all kernel packages available for autoremoval

# kthresher -s
List of kernel packages available for autoremoval:
               Version Package
          3.13.0.83.89 linux-generic
          3.13.0-51.84 linux-headers-3.13.0-51
          3.13.0-51.84 linux-headers-3.13.0-51-generic
         3.13.0-71.114 linux-headers-3.13.0-71
         3.13.0-71.114 linux-headers-3.13.0-71-generic
         3.13.0-77.121 linux-headers-3.13.0-77
         3.13.0-77.121 linux-headers-3.13.0-77-generic
         3.13.0-79.123 linux-headers-3.13.0-79
         3.13.0-79.123 linux-headers-3.13.0-79-generic
         3.13.0-63.103 linux-image-3.13.0-63-generic
         3.13.0-77.121 linux-image-3.13.0-77-generic
  3.16.0-33.44~14.04.1 linux-image-3.16.0-33-generic
  3.16.0-60.80~14.04.1 linux-image-3.16.0-60-generic
         3.13.0-63.103 linux-image-extra-3.13.0-63-generic
         3.13.0-77.121 linux-image-extra-3.13.0-77-generic
          3.13.0.83.89 linux-image-generic

Purge Unused Kernels, keep 3 kernels and be verbose

# kthresher -p -k3 -v
INFO: Attempting to read /etc/kthresher.conf.
INFO: Config file /etc/kthresher.conf is empty or does not exist, ignoring.
INFO: Options: {'purge': True, 'verbose': True, 'dry_run': False, 'keep': 3}
INFO: Running kernel is linux-image-3.13.0-83-generic v[3.13.0-83.127]
INFO: Attempting to keep 3 kernel package(s)
INFO: Found 4 kernel image(s) installed and available for autoremoval
INFO: Pre-sorting: ['3.16.0-60.80~14.04.1', '3.13.0-77.121', '3.13.0-63.103', '3.16.0-33.44~14.04.1']
INFO: Post-sorting: ['3.13.0-63.103', '3.13.0-77.121', '3.16.0-33.44~14.04.1', '3.16.0-60.80~14.04.1']
INFO:   Purging packages from version: 3.13.0-63.103
INFO:           Purging: linux-image-extra-3.13.0-63-generic
INFO:           Purging: linux-image-3.13.0-63-generic
Fetched 0 B in 0s (0 B/s)
(Reading database ... 169514 files and directories currently installed.)
Removing linux-image-extra-3.13.0-63-generic (3.13.0-63.103) ...
run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 3.13.0-63-generic /boot/vmlinuz-3.13.0-63-generic
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 3.13.0-63-generic /boot/vmlinuz-3.13.0-63-generic
update-initramfs: Generating /boot/initrd.img-3.13.0-63-generic
run-parts: executing /etc/kernel/postinst.d/zz-update-grub 3.13.0-63-generic /boot/vmlinuz-3.13.0-63-generic
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.16.0-60-generic
Found initrd image: /boot/initrd.img-3.16.0-60-generic
Found linux image: /boot/vmlinuz-3.16.0-33-generic
Found initrd image: /boot/initrd.img-3.16.0-33-generic
Found linux image: /boot/vmlinuz-3.13.0-83-generic
Found initrd image: /boot/initrd.img-3.13.0-83-generic
Found linux image: /boot/vmlinuz-3.13.0-77-generic
Found initrd image: /boot/initrd.img-3.13.0-77-generic
Found linux image: /boot/vmlinuz-3.13.0-63-generic
Found initrd image: /boot/initrd.img-3.13.0-63-generic
done
Purging configuration files for linux-image-extra-3.13.0-63-generic (3.13.0-63.103) ...
Removing linux-image-3.13.0-63-generic (3.13.0-63.103) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-63-generic /boot/vmlinuz-3.13.0-63-generic
update-initramfs: Deleting /boot/initrd.img-3.13.0-63-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-63-generic /boot/vmlinuz-3.13.0-63-generic
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.16.0-60-generic
Found initrd image: /boot/initrd.img-3.16.0-60-generic
Found linux image: /boot/vmlinuz-3.16.0-33-generic
Found initrd image: /boot/initrd.img-3.16.0-33-generic
Found linux image: /boot/vmlinuz-3.13.0-83-generic
Found initrd image: /boot/initrd.img-3.13.0-83-generic
Found linux image: /boot/vmlinuz-3.13.0-77-generic
Found initrd image: /boot/initrd.img-3.13.0-77-generic
done
Purging configuration files for linux-image-3.13.0-63-generic (3.13.0-63.103) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-63-generic /boot/vmlinuz-3.13.0-63-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-63-generic /boot/vmlinuz-3.13.0-63-generic

Verbose run using a non-default config file

# kthresher -c myconf.conf
INFO: Attempting to read myconf.conf.
INFO: Options found: ['keep', 'dry_run'].
INFO: Valid setting found "keep"
INFO:   keep = 1
INFO: Valid setting found "dry_run"
INFO:   dry_run = True
INFO: Options: {'purge': False, 'verbose': True, 'dry_run': True, 'keep': 1}
INFO: ----- DRY RUN -----
INFO: Running kernel is linux-image-3.13.0-83-generic v[3.13.0-83.127]
INFO: Attempting to keep 1 kernel package(s)
INFO: Found 2 kernel image(s) installed and available for autoremoval
INFO: Pre-sorting: ['3.16.0-60.80~14.04.1', '3.16.0-33.44~14.04.1']
INFO: Post-sorting: ['3.16.0-33.44~14.04.1', '3.16.0-60.80~14.04.1']
INFO:   Purging packages from version: 3.16.0-33.44~14.04.1
INFO:           Purging: linux-image-3.16.0-33-generic

Content of myconf.conf is: .. code-block:

[main]
keep    = 1
dry_run = yes
#purge = yes

Dry run including headers

# kthresher -v -d -H
INFO: Attempting to read /etc/kthresher.conf.
INFO: Options found: ['keep', 'dry_run', 'purge', 'verbose'].
INFO: Valid setting found "keep"
INFO:   keep = 2
INFO: Valid setting found "dry_run"
INFO:   dry_run = False
INFO: Valid setting found "purge"
INFO:   purge = True
INFO: Valid setting found "verbose"
INFO:   verbose = True
INFO: Options: {'verbose': True, 'dry_run': True, 'keep': 2, 'purge': True, 'headers': True}
INFO: ----- DRY RUN -----
INFO: Running kernel is linux-image-3.13.0-83-generic v[3.13.0-83.127]
INFO: Attempting to keep 2 kernel package(s)
INFO: Found 4 kernel image(s) installed and available for autoremoval
INFO: Pre-sorting: ['3.16.0-60.80~14.04.1', '3.16.0-33.44~14.04.1', '3.13.0-85.129', '3.13.0-79.123']
INFO: Post-sorting: ['3.13.0-79.123', '3.13.0-85.129', '3.16.0-33.44~14.04.1', '3.16.0-60.80~14.04.1']
INFO:   Purging packages from version: 3.13.0-79.123
INFO:           Purging: linux-image-3.13.0-79-generic
INFO:           Purging: linux-headers-3.13.0-79-generic
INFO:           Purging: linux-headers-3.13.0-79
INFO:   Purging packages from version: 3.13.0-85.129
INFO:           Purging: linux-image-3.13.0-85-generic
INFO:           Purging: linux-headers-3.13.0-85
INFO:           Purging: linux-headers-3.13.0-85-generic

Testing

The below code can be used to install up to a fixed amount of kernels and headers if available of the form "linux-(image|headers)-[0-9].*-(generic|amd64)" at the end it should end up with two or three kernels in the NeverAutoRemove list, including the latest, the prior to latest and the running kernel.

#!/usr/bin/env python
'''Installs available linux-image-* and linux-headers-*
And set them for autoremoval, so kthresher can be used for testing.
'''

import re
import apt
import sys
from platform import uname

def autorm_install(pkgs):
    '''Install a list of packages and set them autoremovable.
    '''
    latest_kernel = ''
    ac = apt.Cache()
    for pkg in pkgs:
        latest_kernel = pkg
        k = ac[pkg]
        if not k.is_installed:
            k.mark_install(from_user=False)
    try:
        ac.commit(install_progress=None)
    except apt.cache.LockFailedException as lfe:
        print('{}, are you root?'.format(lfe))
        sys.exit(1)
    except SystemError:
        print('Something failed')
        sys.exit(1)

def get_pkg(regex):
    '''Get a list of packages available that match the regex.
    '''
    pkgs = []
    ac = apt.Cache()
    ac.update()
    for pkg in ac:
        if re.match(regex, pkg.name):
            # ignore running kernel
            if pkg.name == 'linux-image-{0}'.format(uname()[2]):
                continue
            pkgs.append(pkg.name)
    return pkgs

def main():
    limit = 5
    if len(sys.argv) > 1:
        try:
            limit = int(sys.argv[1])
        except:
            print("Use an integer as the limit of pkgs to install.")
            sys.exit(1)
    print("Installing {} kernels/headers if available...".format(limit))
    kernel_regex = "^linux-image-\d\..*-(generic|amd64)$"
    header_regex = "^linux-headers-\d\..*-(generic|amd64)$"
    kernels = get_pkg(kernel_regex)
    headers = get_pkg(header_regex)
    pkgs = kernels[0:limit] + headers[0:limit]
    print("Installing {} packages total\n\tkernels: {}\n\theaders: {}"
          .format(len(pkgs), kernels[0:limit], headers[0:limit]))
    autorm_install(pkgs)

if __name__ == "__main__":
    main()

Bugs

Submit Bug reports, feature requests via issues.

Logos

The art was created by Carlos Garcia <hellyeahdesign AT gmail DOT com> and released under CC BY-SA 4.0

logo0 logo1
logo2 logo3
Creative Commons License

kthresher's People

Contributors

disengage00 avatar jkirk avatar komish avatar tenaciousdlg avatar tonyskapunk avatar tryhoehne avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kthresher's Issues

Feature Request: Add --headers flag

It would be useful if this script could have a --headers flag, so when invoked, you can optionally purge the linux-headers-* files associated with the image files.

kthresher assumes there is always a kernel installed

Running inside a container will throw this error:

# kthresher --dry-run --verbose
INFO: Attempting to read /etc/kthresher.conf.
INFO: Options found: ['include'].
INFO: Valid setting found "include"
INFO:   include = /etc/kthresher.d/*.conf
INFO: Options: {'purge': False, 'headers': False, 'verbose': True, 'dry_run': True, 'include': '/etc/kthresher.d/*.conf', 'keep': 1}
INFO: ----- DRY RUN -----
Traceback (most recent call last):
  File "/usr/bin/kthresher", line 9, in <module>
    load_entry_point('kthresher==1.2.6', 'console_scripts', 'kthresher')()
  File "/usr/lib/python2.7/dist-packages/kthresher.py", line 337, in main
    keep=options['keep']
  File "/usr/lib/python2.7/dist-packages/kthresher.py", line 194, in kthreshing
    kernel_pkg = apt_cache["linux-image-%s" % current_kernel_ver]
  File "/usr/lib/python2.7/dist-packages/apt/cache.py", line 200, in __getitem__
    raise KeyError('The cache has no package named %r' % key)
KeyError: "The cache has no package named 'linux-image-3.10.0-514.10.2.el7.x86_64'"

# uname -r
3.10.0-514.10.2.el7.x86_64

# dpkg -l linux-image
dpkg-query: no packages found matching linux-image

An extra validation would be nice

Keep argument does not default always to 1.

Debian package maintainer @jkirk reported this inconsistency with the keep argument:

"--keep defaults to 1". This does not seem true:

% kthresher -k -d
INFO: Attempting to read /etc/kthresher.conf.
INFO: Options found: ['include'].
INFO: Valid setting found "include"
INFO: include = /etc/kthresher.d/.conf
INFO: Options: {'purge': False, 'headers': False, 'verbose': False, 'dry_run': True, 'include': '/etc/kthresher.d/
.conf', 'keep': 0}
INFO: ----- DRY RUN -----
INFO: Running kernel is linux-image-4.18.0-0.bpo.1-amd64 v[4.18.6-1~bpo9+1]
INFO: No packages available for autoremoval.

Without keep:

% kthresher -d
INFO: Attempting to read /etc/kthresher.conf.
INFO: Options found: ['include'].
INFO: Valid setting found "include"
INFO: include = /etc/kthresher.d/.conf
INFO: Options: {'purge': False, 'headers': False, 'verbose': False, 'dry_run': True, 'include': '/etc/kthresher.d/
.conf', 'keep': 1}
INFO: ----- DRY RUN -----
INFO: Running kernel is linux-image-4.18.0-0.bpo.1-amd64 v[4.18.6-1~bpo9+1]
INFO: No packages available for autoremoval.

--keep defaults to 1 if --keep is not given,
but defaults to 0 if no value for --keep is given.

No default action when running kthresher

In https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=919457 it was found that kthresher was printing out the help content through cron when no configuration to take an action was defined. That was tackled with #59 and while that avoids printing the help to the cronjob/timers it does not solve the user experience when running kthresher on its own.

I've been talking about this with @jkirk (Debian package maintainer) and we believe that the -s,--show-autoremoval action is probably a better default action.

It is also some dialog going about the implementation in #59 about "hiding" the output from a cronjob/timer. I personally would like to avoid the noise of a daily email but I do see the importance of notifications. Seems then that #58 provides both of the best options moving out the noisy emails to the logs with a simple entry stating the actions or not from the run.

kthresher assumes there is always a socket for logging

It was reported[1] that when running kthresher in a container the lack of system log causes a failure:

>   root@9a7817d2b17c:/debian# kthresher                                                                                     
>   INFO: No kernel packages available for autoremoval.                                                                      
>   --- Logging error ---                                                                                                    
>   Traceback (most recent call last):                                                                                       
>     File "/usr/lib/python3.7/logging/handlers.py", line 933, in emit                                                       
>       self.socket.send(msg)                                                                                                
>   OSError: [Errno 9] Bad file descriptor                                                                                   
>                                                                                                                            
>   During handling of the above exception, another exception occurred:                                                      
>                                                                                                                            
>   Traceback (most recent call last):                                                                                       
>     File "/usr/lib/python3.7/logging/handlers.py", line 854, in _connect_unixsocket                                        
>       self.socket.connect(address)                                                                                         
>   FileNotFoundError: [Errno 2] No such file or directory   

This is due to the assumption that /dev/log is always available: https://github.com/rackerlabs/kthresher/blob/master/kthresher.py#L433

[1] https://groups.google.com/forum/#!topic/kthresher-dev/XVZkhvtnXfA

kthresher: Uses Package.section, incompatible with python-apt 1.9

Julian Andres Klode reported in Debian-Bug #931024:

According to codesearch.d.n, kthresher contains trhe line:

        "kernel" in pkg.section and re.match(kernel_image_regex, pkg_name)

The use of the section on packages has been deprecated ages ago, as
it can vary between versions.

This causes kthresher to stop working with apt 1.9, so please migrate to
using a version's section after the buster release.

See:

"kernel" in pkg.section and re.match(kernel_image_regex, pkg_name)

The include headers option doesn't work correctly

The -H option doesn't remove headers when the corresponding kernel images have already been removed.

# kthresher -k2 -pvH
INFO: Attempting to read /etc/kthresher.conf.
INFO: Options found: ['include'].
INFO: Valid setting found "include"
INFO: 	include = /etc/kthresher.d/*.conf
INFO: Options: {'purge': True, 'headers': True, 'verbose': True, 'dry_run': False, 'include': '/etc/kthresher.d/*.conf', 'keep': 2}
INFO: Running kernel is linux-image-4.4.0-96-generic v[4.4.0-96.119]
INFO: Attempting to keep 2 kernel package(s)
INFO: Found 2 kernel image(s) installed and available for autoremoval
INFO: Pre-sorting: ['4.4.0-89.112', '4.4.0-91.114']
INFO: Post-sorting: ['4.4.0-89.112', '4.4.0-91.114']
ERROR: Nothing to do, attempting to keep 2 out of 2 kernel images.

The following are the available packages that were detected for autoremoval:

# kthresher -s
List of kernel packages available for autoremoval:
       Version Package       
   4.4.0-53.74 linux-headers-4.4.0-53-generic
   4.4.0-65.86 linux-headers-4.4.0-65-generic
   4.4.0-66.87 linux-headers-4.4.0-66-generic
   4.4.0-70.91 linux-headers-4.4.0-70-generic
   4.4.0-71.92 linux-headers-4.4.0-71-generic
   4.4.0-72.93 linux-headers-4.4.0-72-generic
   4.4.0-78.99 linux-headers-4.4.0-78-generic
  4.4.0-79.100 linux-headers-4.4.0-79-generic
  4.4.0-81.104 linux-headers-4.4.0-81-generic
  4.4.0-83.106 linux-headers-4.4.0-83-generic
  4.4.0-87.110 linux-headers-4.4.0-87-generic
  4.4.0-89.112 linux-headers-4.4.0-89-generic
  4.4.0-91.114 linux-headers-4.4.0-91-generic
  4.4.0-89.112 linux-image-4.4.0-89-generic
  4.4.0-91.114 linux-image-4.4.0-91-generic

This has been corrected in PR #44.

The new expected behaviour is shown below:

# kthresher -d -k2 -pvH
INFO: Attempting to read /etc/kthresher.conf.
INFO: Options found: ['include'].
INFO: Valid setting found "include"
INFO: 	include = /etc/kthresher.d/*.conf
INFO: Options: {'purge': True, 'headers': True, 'verbose': True, 'dry_run': True, 'include': '/etc/kthresher.d/*.conf', 'keep': 2}
INFO: ----- DRY RUN -----
INFO: Running kernel is linux-image-4.4.0-96-generic v[4.4.0-96.119]
INFO: Attempting to keep 2 kernel package(s)
INFO: Found 13 kernel image(s) installed and available for autoremoval
INFO: Pre-sorting: ['4.4.0-65.86', '4.4.0-87.110', '4.4.0-70.91', '4.4.0-81.104', '4.4.0-89.112', '4.4.0-71.92', '4.4.0-91.114', '4.4.0-72.93', '4.4.0-66.87', '4.4.0-83.106', '4.4.0-78.99', '4.4.0-53.74', '4.4.0-79.100']
INFO: Post-sorting: ['4.4.0-53.74', '4.4.0-65.86', '4.4.0-66.87', '4.4.0-70.91', '4.4.0-71.92', '4.4.0-72.93', '4.4.0-78.99', '4.4.0-79.100', '4.4.0-81.104', '4.4.0-83.106', '4.4.0-87.110', '4.4.0-89.112', '4.4.0-91.114']
INFO: 	Purging packages from version: 4.4.0-53.74
INFO: 		Purging: linux-headers-4.4.0-53-generic
INFO: 	Purging packages from version: 4.4.0-65.86
INFO: 		Purging: linux-headers-4.4.0-65-generic
INFO: 	Purging packages from version: 4.4.0-66.87
INFO: 		Purging: linux-headers-4.4.0-66-generic
INFO: 	Purging packages from version: 4.4.0-70.91
INFO: 		Purging: linux-headers-4.4.0-70-generic
INFO: 	Purging packages from version: 4.4.0-71.92
INFO: 		Purging: linux-headers-4.4.0-71-generic
INFO: 	Purging packages from version: 4.4.0-72.93
INFO: 		Purging: linux-headers-4.4.0-72-generic
INFO: 	Purging packages from version: 4.4.0-78.99
INFO: 		Purging: linux-headers-4.4.0-78-generic
INFO: 	Purging packages from version: 4.4.0-79.100
INFO: 		Purging: linux-headers-4.4.0-79-generic
INFO: 	Purging packages from version: 4.4.0-81.104
INFO: 		Purging: linux-headers-4.4.0-81-generic
INFO: 	Purging packages from version: 4.4.0-83.106
INFO: 		Purging: linux-headers-4.4.0-83-generic
INFO: 	Purging packages from version: 4.4.0-87.110
INFO: 		Purging: linux-headers-4.4.0-87-generic

The following confirms that the existing behaviour also works as expected:

# kthresher -d -k0 -pvH
INFO: Attempting to read /etc/kthresher.conf.
INFO: Options found: ['include'].
INFO: Valid setting found "include"
INFO: 	include = /etc/kthresher.d/*.conf
INFO: Options: {'purge': True, 'headers': True, 'verbose': True, 'dry_run': True, 'include': '/etc/kthresher.d/*.conf', 'keep': 0}
INFO: ----- DRY RUN -----
INFO: Running kernel is linux-image-4.4.0-96-generic v[4.4.0-96.119]
INFO: Attempting to keep 0 kernel package(s)
INFO: Found 13 kernel image(s) installed and available for autoremoval
INFO: Pre-sorting: ['4.4.0-65.86', '4.4.0-87.110', '4.4.0-70.91', '4.4.0-81.104', '4.4.0-89.112', '4.4.0-71.92', '4.4.0-91.114', '4.4.0-72.93', '4.4.0-66.87', '4.4.0-83.106', '4.4.0-78.99', '4.4.0-53.74', '4.4.0-79.100']
INFO: Post-sorting: ['4.4.0-53.74', '4.4.0-65.86', '4.4.0-66.87', '4.4.0-70.91', '4.4.0-71.92', '4.4.0-72.93', '4.4.0-78.99', '4.4.0-79.100', '4.4.0-81.104', '4.4.0-83.106', '4.4.0-87.110', '4.4.0-89.112', '4.4.0-91.114']
INFO: 	Purging packages from version: 4.4.0-53.74
INFO: 		Purging: linux-headers-4.4.0-53-generic
INFO: 	Purging packages from version: 4.4.0-65.86
INFO: 		Purging: linux-headers-4.4.0-65-generic
INFO: 	Purging packages from version: 4.4.0-66.87
INFO: 		Purging: linux-headers-4.4.0-66-generic
INFO: 	Purging packages from version: 4.4.0-70.91
INFO: 		Purging: linux-headers-4.4.0-70-generic
INFO: 	Purging packages from version: 4.4.0-71.92
INFO: 		Purging: linux-headers-4.4.0-71-generic
INFO: 	Purging packages from version: 4.4.0-72.93
INFO: 		Purging: linux-headers-4.4.0-72-generic
INFO: 	Purging packages from version: 4.4.0-78.99
INFO: 		Purging: linux-headers-4.4.0-78-generic
INFO: 	Purging packages from version: 4.4.0-79.100
INFO: 		Purging: linux-headers-4.4.0-79-generic
INFO: 	Purging packages from version: 4.4.0-81.104
INFO: 		Purging: linux-headers-4.4.0-81-generic
INFO: 	Purging packages from version: 4.4.0-83.106
INFO: 		Purging: linux-headers-4.4.0-83-generic
INFO: 	Purging packages from version: 4.4.0-87.110
INFO: 		Purging: linux-headers-4.4.0-87-generic
INFO: 	Purging packages from version: 4.4.0-89.112
INFO: 		Purging: linux-image-4.4.0-89-generic
INFO: 		Purging: linux-headers-4.4.0-89-generic
INFO: 	Purging packages from version: 4.4.0-91.114
INFO: 		Purging: linux-image-4.4.0-91-generic
INFO: 		Purging: linux-headers-4.4.0-91-generic

Feature request: implement daily cron job and /etc/default control of it

We would like to implement a cron.daily (sic) script that runs kthresher as part of normal daily cron tasks. The implementation would include an /etc/default/kthresher configuration that has a setting that enables/disables the cron task, with the default set to 0 / no. Something like:

/etc/cron.daily/kthresher
-------------------------
ENABLED=0
if [ -f /etc/default/kthresher ]; then
  . /etc/default/kthresher
fi

/etc/default/kthresher
----------------------
ENABLED=0

...something like that, it's just a sketch. The spirit is that this is a possibly hazardous task to run for a given server that has an edge case not known about, and only after a local systems admin has tested in their configuration would they enable the cron task to run on schedule.

Feature request: allow user configurable number of old kernels to retain

We would like to implement a control in /etc/default/kthresher that allows the user to specify how many kernels older than the currently running one will be kept. This will allow the local systems admin to decide if they wish to retain "fallback" kernels for emergency boot problems (i.e. if rebuilding an initrd/initramfs fails to boot, provide a fallback kernel). Something like:

/etc/default/kthresher
----------------------
NUMTOKEEP=1

This is just a sketch of design of the intended spirit.

Feature: add basic syslog handler to logging for actions taken

Right now the logging object is instantiated with STDOUT only, and by default it's quiet (no output). As this is run from cron nightly, it would be very handy to know at least two basic results:

  1. Ran, no actions needed
  2. Ran, actions taken: XX

...logged into the standard syslog infrastructure, allowing sysadmins to review their machine health daily (or capture it with log monitoring tools). I'm thinking the basic StreamHandler as outlined here:

from logging.handlers import SysLogHandler

logger = logging.getLogger()
logger.addHandler(SysLogHandler('/dev/log'))

Nothing crazy or fancy. Then as a final step, if not in verbose mode issue a the result to get logged. It's possible that this needs to be a handler only triggered by a commandline parameter that would be added to the crontab entry (meaning it would retain the same default silent behaviour on STDOUT, but if.... --syslog it would then add the SysLogHandler discretely.

That way any existing user who has it in their own scripts (not the supplied crontab by the DEB package) would not get affected with new output, but the invocation from cron with --syslog would add the stream? (because I think the idea of "issue a final message" would also end up on STDOUT as well as syslog, dual handlers).

Issue: -virtual Files not affected on Ubuntu 12.04

I noticed that the script did not seem to affect the -virtual files on an ubuntu 12.04 cloud server.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.04.5 LTS
Release: 12.04
Codename: precise

The files weren't being caught due this value not being 'kernel':

$ dpkg -s linux-header-3.2.0-99 | grep Section
Section: devel
$ dpkg -s linux-header-3.2.0-99-virtual | grep Section
Section: devel

So we had to change the 'and' to an 'or':

(pkg.section == 'kernel' or
re.match(...))

non-root handling exceptions

Need to do a better job when attempting to commit changes as non-administrative users won't be able to make those changes:

[tonyskapunk@gnu:~]$ kthresher --verbose --purge --keep 0                                                                        
INFO: Attempting to read /etc/kthresher.conf.                                                           
INFO: Options found: ['include'].                                                                                                
INFO: Valid setting found "include"                                                                                              
INFO:   include = /etc/kthresher.d/*.conf                                                                                        
INFO: Options: {'purge': True, 'headers': False, 'verbose': True, 'dry_run': False, 'include': '/etc/kthresher.d/*.conf', 'keep': 0}
INFO: Running kernel is linux-image-4.9.0-1-amd64 v[4.9.6-3]                                                                     
INFO: Attempting to keep 0 kernel package(s)                                                                                     
INFO: Found 1 kernel image(s) installed and available for autoremoval                                                          
INFO: Pre-sorting: ['4.8.15-2']                                                                                            
INFO: Post-sorting: ['4.8.15-2']                                                                                        
INFO:   Purging packages from version: 4.8.15-2                                                                      
INFO:           Purging: linux-image-4.8.0-2-amd64                                                                     
Traceback (most recent call last):                                                                                             
  File "/usr/local/bin/kthresher", line 11, in <module>                                                               
    sys.exit(main())                                                                                                       
  File "/usr/local/lib/python2.7/dist-packages/kthresher.py", line 344, in main                                        
    keep=options['keep']                                                                                                  
  File "/usr/local/lib/python2.7/dist-packages/kthresher.py", line 244, in kthreshing                                 
    install_progress=apt.progress.base.InstallProgress()                                                                         
  File "/usr/lib/python2.7/dist-packages/apt/cache.py", line 522, in commit                                    
    res = self._fetch_archives(fetcher, pm)                                                                                      
  File "/usr/lib/python2.7/dist-packages/apt/cache.py", line 348, in _fetch_archives                                             
    raise LockFailedException("Failed to lock %s" % lockfile)                                                                    
apt.cache.LockFailedException: Failed to lock /var/cache/apt/archives/lock 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.