开发者

How to get kernel version by using kernel name (not current kernel)

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 t开发者_如何学Pythonhat is possible ...

Thank you.


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))


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


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜