purge-old-kernels: an attempt at a successor

Forum rules
Before you post please read how to get help
User avatar
Pjotr
Level 18
Level 18
Posts: 8423
Joined: Mon Mar 07, 2011 10:18 am
Location: The Netherlands (Holland)
Contact:

purge-old-kernels: an attempt at a successor

Postby Pjotr » Sat Aug 19, 2017 7:17 am

The nifty Ubuntu script purge-old-kernels (part of the byobu package) has been deprecated for Ubuntu versions after 16.04, because apt autoremove should do the same job.

For those of us who want to keep this precision instrument, I've forked it here:
https://github.com/Pjotr123/purge-old-kernels-2

Any improvement suggestions are welcome, both at GitHub and here. :)

Note: as you can still install purge-old-kernels in Mint 18.x (by installing byobu), this'll only become useful in Mint 19. Well, maybe now already, if you don't want to install byobu....

---Edit: I've tried to make it even easier to use, with this how-to:
https://sites.google.com/site/easylinux ... one-stroke
(item 6.1, right column)
Tip: 10 things to do after installing Linux Mint 18.2 Sonya
Keep your Linux Mint healthy: Avoid these 10 fatal mistakes
Twitter: twitter.com/easylinuxtips
All in all, horse sense simply makes sense.

User avatar
Termy
Level 1
Level 1
Posts: 23
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: purge-old-kernels: an attempt at a successor

Postby Termy » Thu Sep 07, 2017 5:19 pm

Here are some things I'd like to suggest, and would do myself:

- Add support for linux-image-extra-*.
- Rework the logic so awk isn't needed, to avoid an additional dependency.
- Add dep testing and test for supported system with $OSTYPE and lsb_release.
- Use apt-get by default (more features) but offer flag for apt use.
- Maybe add add --type=x, where x is the type of kernel. Disregard type by default.
- Provide short option(s).
- Provide --help or --usage information.
- Provide some verbosity; error messages and such.
- Use direct paths as a security measure.
- Maybe use bash, so you could use extglob, brace expansion, and bash variables, such as UID.

I'd love to see any changes you make to the script.
Here to help.

User avatar
Pjotr
Level 18
Level 18
Posts: 8423
Joined: Mon Mar 07, 2011 10:18 am
Location: The Netherlands (Holland)
Contact:

Re: purge-old-kernels: an attempt at a successor

Postby Pjotr » Fri Sep 08, 2017 4:57 am

Thanks for responding! :)

Unfortunately, my coding skills are very small. In Linux, I'm purely a layman with some teaching skills (hence my focus on improving the how-to). So I can only try to address a few of your suggestions. Here goes:

Termy wrote:Here are some things I'd like to suggest, and would do myself:

- Add support for linux-image-extra-*.

Please bear in mind that I'm only a layman, but isn't that already covered by this: {print "linux-image-" $0 " linux-headers-" $0}?

Or do we need to add the wildcard *, so it becomes {print "linux-image-*" $0 " linux-headers-" $0}? Or even {print "linux-image-*" $0 " linux-headers-*" $0}?

- Rework the logic so awk isn't needed, to avoid an additional dependency.

AFAIK, awk is part of the default Linux Mint installation. If I'm right in that assumption, this means that this particular dependency is probably not cumbersome....

The other things: could you perhaps fork the script on GitHub into e.g. purge-old-kernels-3, and showcase your suggestions?
Tip: 10 things to do after installing Linux Mint 18.2 Sonya
Keep your Linux Mint healthy: Avoid these 10 fatal mistakes
Twitter: twitter.com/easylinuxtips
All in all, horse sense simply makes sense.

User avatar
Termy
Level 1
Level 1
Posts: 23
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: purge-old-kernels: an attempt at a successor

Postby Termy » Fri Sep 08, 2017 9:02 am

I don't have an account on GitHub, but I'll set one up, with a contact E-Mail address I'll use on all my projects. I would have had to use it eventually, as I've got projects piling up just itching to be shared. lol

You can find what I've called roks (I've mentioned what prompted me to write it :) ) here:

https://github.com/terminalforlife/roks

I'm completely new to GitHub (at least as far as having an account), so if I've done something silly, please let me know!

Moments later...

I forgot to address your questions. I guess I was more thinking of roks as I intend it to be viable on more than just Mint. I'm a big fan of dep checking and using as few non-essentials as possible. If you can do it with a builtin, it has a chance to be faster, and it's more portable. This has at least been my experience and what I've learned from others. As for linux-image-extras, I'm looking into that now, but I'm pretty sure it would be best to specify all, or at least provide the option to. I'm a big fan of options too; giving the user the power to do what they want; I always love that in a program. :)

By the way, I forgot last night that apt in Mint is different to apt in other places, so, since apparently apt in Mint is a python wrapper for apt-get, apt-cache, etc, that would mean my using apt-get by default isn't necessarily the better move, at least in Mint, but I can't see it being harmful.

I forked it, thinking it with something else, but I totally did the wrong thing. I hate GitHub >.< but I'm sure I'll figure it all out eventually. I have no idea how to suggest changes to what you have. I thought committing was posting a change, which can then be revised and possibly put forward by the person(s) behind the repository. Sorry if I caused any confusion. :oops:

Oh, oh, and and (lol) I believe you're right about awk being part of Mint by default; I've yet to see a Mint setup without it, but then the user can just remove it, or have some other sort of custom setup; this is why I don't like to assume too often.
Here to help.

User avatar
Termy
Level 1
Level 1
Posts: 23
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: purge-old-kernels: an attempt at a successor

Postby Termy » Mon Sep 11, 2017 8:16 pm

Finally finished roks. Took a while because I ran into an issue that took a while to figure out. In the end, since the original code on which I based a part of my own approach was faulty, I had to come up with something myself, while stubbornly trying to ensure it's portability, speed, and efficiency; it's now gone 1am. lol

Can't remember if I posted the link, but here it is: https://github.com/terminalforlife/roks

For details:

The issue with what you've forked is that the whole head approach is broken. I tested the heck out of it, my approach and the original, and could not get it to work properly. I installed 3 more kernels to test it (4 total) and it just refused to process, for example, the last 2 kernels, if I went with the default of 2. If I chose 3, it wouldn't remove the remaining 1. It was very unreliable.

I didn't have particularly bizarre kernels, just 4 *-generic ones:

Code: Select all

➤  sudo sh -c "ls -tr /boot/vmlinuz-*"
/boot/vmlinuz-4.10.0-30-generic  /boot/vmlinuz-4.4.0-92-generic
/boot/vmlinuz-4.11.0-14-generic  /boot/vmlinuz-4.4.0-93-generic

Now I'll use roks to remove the old kernels, and optionally apply --purge and/or --autoremove. It's more flexible than using --autoremove to remove kernels, I think.

I wonder if the behavior of head or something else used in the original logic was changed with some update which messed it up. In any case, I've got it sorted in roks, so I'm sure a similar or same approach can be done in your purge-old-kernels-2.

If you or someone else could test roks in your systems (Mint 18.2 especially, but any would do) that would be cool. Use the -s flag to have it simulate, if you're unsure.
Here to help.


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 2 guests