Skip to content

Instantly share code, notes, and snippets.

@ipcjk
Last active October 10, 2019 09:40
Show Gist options
  • Save ipcjk/b83b64437d2ec8966452eeaa6eb2a007 to your computer and use it in GitHub Desktop.
Save ipcjk/b83b64437d2ec8966452eeaa6eb2a007 to your computer and use it in GitHub Desktop.
All mighty kernel cleanup script for Debian based distributions
#!/usr/bin/perl -w
use strict;
# Some vars..
my @KPKGS;
my $myKernel = `uname -r`;
chomp $myKernel;
my $linuxVersion = `hostnamectl | grep -i Operating`;
my $linuxFile = -f '/etc/centos-release';
if($linuxVersion =~ /Debian|Ubuntu/) {
$linuxVersion = "Debian";
} elsif($linuxVersion =~ /CentOS|RedHat/ || $linuxFile) {
$linuxVersion = "CentOS";
} else { exit(3); }
# Read installed kernel files
open(KERNELPKG, "find /boot/vml* -printf '%T+\t%p\n' | sort -r|");
while(<KERNELPKG>) {
chomp;
push(@KPKGS, $_);
}
# too less to do ...
if (scalar(@KPKGS) <= 2) {
print "KernelCleanup: Too less to do, exit\n";
exit 0;
}
# We ignore the first element (thats the latest kernel
# we dont want to uninstall anyway
for(my $i = 1; $i<scalar(@KPKGS); $i++) {
if($KPKGS[$i] =~ /$myKernel$/) { next; }
$KPKGS[$i] =~ /.*(\/boot\/.*)/;
if($1 && $linuxVersion eq 'Debian') {
`dpkg -S $1 | cut -f 1 -d : | DEBIAN_FRONTEND=noninteractive xargs -n1 apt-get remove -y `;
} elsif($1 && $linuxVersion eq 'CentOS') {
system("rpm -qf $1 | xargs -n1 yum -y remove ");
}
}
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment