如何使用内核名(不是当前内核)获取内核版本

2024-03-29 05:48:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想清理旧内核的模块。它是“uname-r”,但我需要为所有使用Python的内核获取这样的信息(我已经知道它们的名称,并且可以清理内核文件、initramfs和系统地图). 如果可能的话。。。在

谢谢。在


Tags: 模块文件名称信息系统地图内核uname
3条回答

正如@utdemir所指出的,没有每个人都必须遵循的内核路径,但是有很多发行版遵循的Fileystem Hierarchy Standard。根据FSH,内核文件应该位于/boot,称为vmlinux/vmlinuz(uncompressed/compressed)。在

uname命令报告正在运行的内核,因此它不会帮助您。但是这些模块都存储在/lib/modules下。下面的程序可以清除它们。在

#!/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))

可能有点粗糙,但您可以尝试在/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

这些是安装在我机器上的内核。在

或者,在Debian类型的发行版(例如Ubuntu)上,您可以运行:

^{pr2}$

基于RPM的发行版(如RedHat)需要以下内容:

$ 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

相关问题 更多 >