1

Simply I want to clean older kernel's modules. It's "uname -r" but I need to get such information for all kernels with Python (I already know their names and can clean kernel files, initramfs and System.map). if that is possible ...

Thank you.

cnd
  • 32,616
  • 62
  • 183
  • 313
  • I don't think getting info of all kernels is possible. There is no "standart kernel path" or something. – utdemir Jul 19 '11 at 11:54
  • @utdemir I can get info about kernels, I want to get info about their versions. – cnd Jul 19 '11 at 11:58
  • The proper way to clean out old kernels will depend on your distribution. You should use the package management system, e.g. apt, rpm, etc. Some have a specific tool to remove all but the current kernel and the last one successful booted, or something like that. – nealmcb Feb 22 '12 at 15:31

4 Answers4

3

The uname command reports on the running kernel, so it won't help you. But the modules are all stored under /lib/modules. The following program can clean them all out.

#!/usr/bin/python2

import os
import shutil

moddirs = os.listdir("/lib/modules")
moddirs.remove(os.uname()[2])

for d in moddirs:
    shutil.rmtree(os.path.join("/lib/modules", d))
Keith
  • 42,110
  • 11
  • 57
  • 76
  • I need to stay modules of default kernel, but I know only names, I need to get version, that is what the question about :) – cnd Jul 19 '11 at 12:05
  • The directory entry names are the same as `uname -r` for all the kernels. There is not way to find it other than using the file names or the package manager of your distro. The script above will keep the modules of the currently running kernel. – Keith Jul 19 '11 at 12:08
  • I have an idea... I can check if moddir(dir name) (kernel version) is part of my kernel name - this way I can map kernel names and modules folders, what do you think ? :) – cnd Jul 19 '11 at 12:15
  • can you give me another tip :) I'm new with Python - is there some python function alike "is a part of b" ? – cnd Jul 19 '11 at 12:22
  • @nCdy let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1637/discussion-between-keith-and-bjorky) – Keith Jul 19 '11 at 21:16
  • thank you, I found solution, it's "if a in b:" pretty easy syntax – cnd Jul 20 '11 at 04:18
  • @Keith let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1643/discussion-between-ncdy-and-keith) – cnd Jul 20 '11 at 04:18
0

Perhaps slightly crude, but you could try looking in /boot:

aix@aix:~$ ls -al /boot/vmlinu[xz]-*
-rw-r--r-- 1 root root 4050848 2010-09-16 19:24 /boot/vmlinuz-2.6.32-24-generic
-rw-r--r-- 1 root root 4050592 2010-10-16 21:37 /boot/vmlinuz-2.6.32-25-generic
-rw-r--r-- 1 root root 4050080 2010-11-24 10:58 /boot/vmlinuz-2.6.32-26-generic
-rw-r--r-- 1 root root 4049888 2010-12-02 04:42 /boot/vmlinuz-2.6.32-27-generic
-rw-r--r-- 1 root root 4052512 2011-01-11 00:27 /boot/vmlinuz-2.6.32-28-generic
-rw-r--r-- 1 root root 4053280 2011-02-11 21:37 /boot/vmlinuz-2.6.32-29-generic
-rw-r--r-- 1 root root 4055488 2011-03-02 01:24 /boot/vmlinuz-2.6.32-30-generic
-rw-r--r-- 1 root root 4055840 2011-04-08 23:26 /boot/vmlinuz-2.6.32-31-generic
-rw-r--r-- 1 root root 4049376 2011-04-20 23:38 /boot/vmlinuz-2.6.32-32-generic
-rw-r--r-- 1 root root 4050464 2011-07-08 02:00 /boot/vmlinuz-2.6.32-33-generic

These are the kernels that are installed on my machine.

Alternatively, on Debian-type distros (e.g. Ubuntu), you could run:

aix@aix:~$ dpkg --list | grep linux-image
ii  linux-image-2.6.32-24-generic                               2.6.32-24.43                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-25-generic                               2.6.32-25.45                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-26-generic                               2.6.32-26.48                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-27-generic                               2.6.32-27.49                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-28-generic                               2.6.32-28.55                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-29-generic                               2.6.32-29.58                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-30-generic                               2.6.32-30.59                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-31-generic                               2.6.32-31.61                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-32-generic                               2.6.32-32.62                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-33-generic                               2.6.32-33.70                                    Linux kernel image for version 2.6.32 on x86
ii  linux-image-generic                                         2.6.32.33.39                                    Generic Linux kernel image

RPM-based distros (e.g. RedHat) would need something like:

$ rpm -qa kernel
kernel-2.6.18-128.7.1.el5
kernel-2.6.18-128.2.1.el5
kernel-2.6.18-194.17.4.el5
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

As @utdemir pointed out, there is no kernel path everybody has to conform to, but there is the Fileystem Hierarchy Standard that many distributions follow. According to the FSH, kernel files are supposed to be located in /boot and are called vmlinux/vmlinuz (uncompressed/compressed).

Jacob
  • 41,721
  • 6
  • 79
  • 81
0

Another possibility would be to try some different commands till you find one that returns sensible results. "rpm -qa kernel" should work for RedHat and some others while "dpkg --list | grep linux-image" should do the trick for ubuntu. Probably not much better than searching the filesystem directly and you have to parse the result anyhow.

Voo
  • 29,040
  • 11
  • 82
  • 156