Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Scripting - remove oldest kernel cruft
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
BoneKracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1499
Location: U.S.A.

PostPosted: Thu May 03, 2012 6:24 am    Post subject: Scripting - remove oldest kernel cruft Reply with quote

The Idea is to purge the system of the files belonging to the oldest (i.e. lowest version) kernel on the system.

This is independent of whether the package has been "unmerged" in Portage; the point is to remove the files left behind by Portage which are associated with a kernel no longer being used and no longer needed as a fall-back.

How can I improve this? I have a list below of "nice to have" improvement ideas, but I am more interested in fundamentals. Is there already a tool for this I don't know about? What I have screwed up, done stupidly, done inefficiently, etc.
Code:
#!/bin/sh

## configurable
src="/usr/src"

## testing
cmd="echo"
#cmd="rm -rf"

## kernel name: e.g., "linux-3.3.4-hardened"
krn=$(ls -1 --hide linux $src | sort -V | head -n 1)

$cmd ${src}/${krn}

## kernel version: e.g., "3.3.4-hardened"
ver=${krn#linux-}

$cmd /lib/modules/${ver}

$cmd /boot/*-${ver}

"Nice to have" (not the more basic "what did I do wrong" input I am mainly looking for):
  • Take as a command line argument the version (release number) to be purged or the keyword "list", which would allow the user to select one from a list (in something like 'eselect' fashion). If no argument is given, invoke the current default of purging the oldest kernel.
  • Protect the current kernel. Abort if version matches running kernel (via 'uname -r'). Erase self and emit surly message from energyman76b; user is too stupid to have such tools.
  • If system has separate /boot device, mount it if necessary and then unmount it when done.
  • Preserve versions likely be referenced by bootloader config files. I.e., exit with error if version is same as target of any links in /boot (softlinks should be easy; haven't thought through most efficient way to versions for hardlinks, which I do happen to use).
  • Maybe: Protect any slotted kernels. Abort if version matches any kernel packages reported by Portage as being installed (via 'equery list'?).
  • Maybe: "About to have unprotected sex -- are you sure? (y/n)" type of validation, unless -q option given.

I have no idea what if anything an initrd user would add, because I don't use one.
_________________
Oldthinkers unbellyfeel INGSOC.
-- Headline of a document on Winston Smith's terminal in his cubicle at the Ministry of Truth, seen briefly in the background in one scene of the movie rendition of Nineteen Eighty-Four.
Back to top
View user's profile Send private message
grooveharder
n00b
n00b


Joined: 18 Feb 2007
Posts: 43

PostPosted: Thu May 03, 2012 8:16 am    Post subject: Reply with quote

Have you looked at app-portage/eclean-kernel? :)
Back to top
View user's profile Send private message
BoneKracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1499
Location: U.S.A.

PostPosted: Thu May 03, 2012 8:59 am    Post subject: Reply with quote

grooveharder wrote:
Have you looked at app-portage/eclean-kernel? :)

No. So I wasn't aware of it.

Excellent. I'd rather not reinvent the wheel if this does all the same things.

Thanks. :)

Edit: after a quick look, it looks like it very much does. :lol:
_________________
Oldthinkers unbellyfeel INGSOC.
-- Headline of a document on Winston Smith's terminal in his cubicle at the Ministry of Truth, seen briefly in the background in one scene of the movie rendition of Nineteen Eighty-Four.


Last edited by BoneKracker on Thu May 03, 2012 9:53 am; edited 1 time in total
Back to top
View user's profile Send private message
notageek
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jun 2008
Posts: 81
Location: Bangalore, India

PostPosted: Thu May 03, 2012 9:13 am    Post subject: Reply with quote

/facepalm
_________________
What looks like a cat, flies like a bat, brays like a donkey, and plays like a monkey?
Back to top
View user's profile Send private message
BoneKracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1499
Location: U.S.A.

PostPosted: Thu May 03, 2012 9:53 am    Post subject: Reply with quote

notageek wrote:
/facepalm

Eat me. I suppose you knew about this tool. At least I was smart enough to ask. :lol:
_________________
Oldthinkers unbellyfeel INGSOC.
-- Headline of a document on Winston Smith's terminal in his cubicle at the Ministry of Truth, seen briefly in the background in one scene of the movie rendition of Nineteen Eighty-Four.


Last edited by BoneKracker on Thu May 03, 2012 9:54 am; edited 1 time in total
Back to top
View user's profile Send private message
BoneKracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1499
Location: U.S.A.

PostPosted: Thu May 03, 2012 10:03 am    Post subject: Reply with quote

So, you deleted your "of course" reply. Does that mean you realized this is not part of eclean? Doh!? Or did you actually know about this?
_________________
Oldthinkers unbellyfeel INGSOC.
-- Headline of a document on Winston Smith's terminal in his cubicle at the Ministry of Truth, seen briefly in the background in one scene of the movie rendition of Nineteen Eighty-Four.
Back to top
View user's profile Send private message
notageek
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jun 2008
Posts: 81
Location: Bangalore, India

PostPosted: Thu May 03, 2012 10:09 am    Post subject: Reply with quote

I removed my reply because you had edited your post and my reply made it look like I agreed with you that you're smart.
_________________
What looks like a cat, flies like a bat, brays like a donkey, and plays like a monkey?
Back to top
View user's profile Send private message
BoneKracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1499
Location: U.S.A.

PostPosted: Thu May 03, 2012 10:20 am    Post subject: Reply with quote

Oh, I see. :?

Hmm... it's quite lacking in documentation and code comments. Git entries prior to the current version we have indicate capability was added to automatically mount /boot (and unmount it), but it doesn't seem to work. It kind of shits the bed if /boot isn't mounted.
Code:
~ # eclean-kernel
No outdated kernels found.
~ # umount /boot
~ # eclean-kernel
Traceback (most recent call last):
  File "/usr/bin/eclean-kernel-2.7", line 26, in <module>
    sys.exit(main(sys.argv))
  File "/usr/lib/python2.7/site-packages/ecleankernel/cli.py", line 118, in main
    debug = debug)
  File "/usr/lib/python2.7/site-packages/ecleankernel/process.py", line 33, in get_removal_list
    raise SystemError('No vmlinuz found. This seems ridiculous, aborting.')
SystemError: No vmlinuz found. This seems ridiculous, aborting.

_________________
Oldthinkers unbellyfeel INGSOC.
-- Headline of a document on Winston Smith's terminal in his cubicle at the Ministry of Truth, seen briefly in the background in one scene of the movie rendition of Nineteen Eighty-Four.
Back to top
View user's profile Send private message
BoneKracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1499
Location: U.S.A.

PostPosted: Wed May 09, 2012 9:22 pm    Post subject: Reply with quote

Decided to use my own script. It's faster, its interpreter (BASH) is always loaded, it takes care of mounting if needed, and it's only 341 bytes in size. It meets my needs, and it's one less package. Here it is with the mount / unmount logic added.
Code:
#!/bin/bash

krn=$(ls -1 --hide linux /usr/src |sort -V |head -n 1)

ver=${krn#linux-}

if ! mountpoint -q /boot; then
    mount /boot
    mnt=true
fi

if ! find /boot -lname "vmlinu*" -execdir readlink {} \; |grep -q $ver; then
    rm -rf /usr/src/${krn}
    rm -rf /lib/modules/${ver}
    rm -f /boot/*-${ver}
fi

[ $mnt ] && umount /boot

_________________
Oldthinkers unbellyfeel INGSOC.
-- Headline of a document on Winston Smith's terminal in his cubicle at the Ministry of Truth, seen briefly in the background in one scene of the movie rendition of Nineteen Eighty-Four.
Back to top
View user's profile Send private message
Prenj
n00b
n00b


Joined: 20 Nov 2011
Posts: 8
Location: Mostar, BiH

PostPosted: Wed May 09, 2012 10:01 pm    Post subject: Reply with quote

My grandad used to say that those who ask for directions don't get lost. :lol:
Back to top
View user's profile Send private message
Akkara
Administrator
Administrator


Joined: 28 Mar 2006
Posts: 4366
Location: &akkara

PostPosted: Wed May 09, 2012 10:35 pm    Post subject: Reply with quote

notageek wrote:
/facepalm

This isn't necessary. It was a legitimate question and BK was wise to ask and save time instead of quietly re-inventing a wheel. Not to mention, this is directly on-topic with what these forums exist for. Unlike 99.9999% of the posts here. And while BK certainly doesn't need anyone to help defend him, I felt this needed to be said, if only to encourage (or not discourage) others who aren't sure about something, to ask.
Back to top
View user's profile Send private message
BoneKracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1499
Location: U.S.A.

PostPosted: Wed May 09, 2012 11:41 pm    Post subject: Reply with quote

Yeah. So there, haterz.
_________________
Oldthinkers unbellyfeel INGSOC.
-- Headline of a document on Winston Smith's terminal in his cubicle at the Ministry of Truth, seen briefly in the background in one scene of the movie rendition of Nineteen Eighty-Four.
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 16033
Location: Colorado

PostPosted: Fri May 11, 2012 3:45 am    Post subject: Reply with quote

I started writing the equivalent of the eselect tool to manage which kernel was selected. Mostly done too, then found the eselect version, which I don't have to maintain.

I was going to bring that system up to see if I was using the portage tool mentioned in the thread, but I think it has an overheating / dead fan problem, so don't have the time to correct that. Oh well. I'll probably forget by the time I get around to fixing it.
_________________
lolgov. 'cause where we're going, you don't have civil liberties.

In Loving Memory
1787 - 2008
Back to top
View user's profile Send private message
notageek
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jun 2008
Posts: 81
Location: Bangalore, India

PostPosted: Fri May 11, 2012 4:44 am    Post subject: Reply with quote

Akkara wrote:
notageek wrote:
/facepalm

This isn't necessary. It was a legitimate question and BK was wise to ask and save time instead of quietly re-inventing a wheel. Not to mention, this is directly on-topic with what these forums exist for. Unlike 99.9999% of the posts here. And while BK certainly doesn't need anyone to help defend him, I felt this needed to be said, if only to encourage (or not discourage) others who aren't sure about something, to ask.
Wow, I must be the first person in the history of OTW to have been lectured on being unhelpful. On OTW.

...and all I got was this lousy T-shirt.
_________________
What looks like a cat, flies like a bat, brays like a donkey, and plays like a monkey?
Back to top
View user's profile Send private message
BoneKracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1499
Location: U.S.A.

PostPosted: Fri May 11, 2012 2:47 pm    Post subject: Reply with quote

notageek wrote:
Akkara wrote:
notageek wrote:
/facepalm

This isn't necessary. It was a legitimate question and BK was wise to ask and save time instead of quietly re-inventing a wheel. Not to mention, this is directly on-topic with what these forums exist for. Unlike 99.9999% of the posts here. And while BK certainly doesn't need anyone to help defend him, I felt this needed to be said, if only to encourage (or not discourage) others who aren't sure about something, to ask.
Wow, I must be the first person in the history of OTW to have been lectured on being unhelpful. On OTW.

...and all I got was this lousy T-shirt.

Look at your lame ass with 77 posts. Don't you ever help anybody? Go answer some unanswered questions. :x
_________________
Oldthinkers unbellyfeel INGSOC.
-- Headline of a document on Winston Smith's terminal in his cubicle at the Ministry of Truth, seen briefly in the background in one scene of the movie rendition of Nineteen Eighty-Four.
Back to top
View user's profile Send private message
notageek
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jun 2008
Posts: 81
Location: Bangalore, India

PostPosted: Fri May 11, 2012 3:02 pm    Post subject: Reply with quote

Will not. By principle. People who use Gentoo shouldn't need help. You need help? Use Ubuntu, Fedora.
_________________
What looks like a cat, flies like a bat, brays like a donkey, and plays like a monkey?
Back to top
View user's profile Send private message
BoneKracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1499
Location: U.S.A.

PostPosted: Fri May 11, 2012 8:16 pm    Post subject: Reply with quote

notageek wrote:
Will not. By principle. People who use Gentoo shouldn't need help. You need help? Use Ubuntu, Fedora.

Says the guy who rides a motorcycle into cows.
_________________
Oldthinkers unbellyfeel INGSOC.
-- Headline of a document on Winston Smith's terminal in his cubicle at the Ministry of Truth, seen briefly in the background in one scene of the movie rendition of Nineteen Eighty-Four.
Back to top
View user's profile Send private message
Dr.Willy
Apprentice
Apprentice


Joined: 15 Jul 2007
Posts: 296
Location: NRW, Germany

PostPosted: Fri May 11, 2012 8:23 pm    Post subject: Reply with quote

BoneKracker wrote:
Says the guy who rides a motorcycle into cows.

Are you sure it's not the other way around?
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 16033
Location: Colorado

PostPosted: Fri May 11, 2012 11:32 pm    Post subject: Reply with quote

notageek wrote:
Will not. By principle. People who use Gentoo shouldn't need help. You need help? Use Ubuntu, Fedora.
Hopefully that happens to you the next time you need help (with anything).
_________________
lolgov. 'cause where we're going, you don't have civil liberties.

In Loving Memory
1787 - 2008
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum