当前位置: 首页>>技术问答>>正文


是否为所有已安装的内核重建所有DKMS模块的命令?

, , ,

问题描述

偶尔,我的系统会进入某些内核缺少一两个模块的状态,因为DKMS不知何故忘记为该内核编译这些模块。如果有一个命令我可以运行woudl只是为每个已安装的内核重建每个dkms-controlled模块,而不是花时间诊断问题。有这样的命令吗?

最佳解决思路

我想出了一个shell one-liner来做到这一点:

ls /var/lib/initramfs-tools | \
    sudo xargs -n1 /usr/lib/dkms/dkms_autoinstaller start

这是因为/var/lib/initramfs-tools中目录的名称正是您需要传递给dkms_autoinstaller的内核版本名称,以告诉它重建这些内核版本的所有模块。请注意,如果您已经卸载了一些旧内核,它们的目录可能仍然存在,并导致报告一些错误,但这不是问题,因为dkms_autoinstaller将对那些未安装的内核版本不执行任何操作。

次佳解决思路

看起来不像dkms命令允许您这样做。我创建了一个小的Python脚本,可以做你想要的。您可以在~/.bashrc中添加别名

alias dkms-buildall='sudo ./wherever/your/script/is'

当然,您需要先将其设为可执行文件。这是代码:

#!/bin/env python
#
# NOTE: This assumes that all modules and versions are built for at
#       least one kernel. If that's not the case, adapt parsing as needed.
import os
import subprocess

# Permission check.
if os.geteuid() != 0:
    print "You need to be root to run this script."
    exit(1)

# Get DKMS status output.
cmd = ['dkms', 'status']
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
dkms_status = process.communicate()[0].strip('\n').split('\n')
dkms_status = [x.split(', ') for x in dkms_status]

# Get kernel versions (probably crap).
cmd = ['ls', '/var/lib/initramfs-tools/']
# Alternative (for use with Arch Linux for example)
# cmd = ['ls', '/usr/lib/modules/']
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
kernels = process.communicate()[0].strip('\n').split('\n')

# Parse output, 'modules' will contain all modules pointing to a set
# of versions.
modules = {}

for entry in dkms_status:
   module = entry[0]
   version = entry[1].split(': ')[0]
   try:
      modules[module].add(version)
   except KeyError:
      # We don't have that module, add it.
      modules[module] = set([version])

# For each module, build all versions for all kernels.
for module in modules:
   for version in modules[module]:
      for kernel in kernels:
         cmd = ['dkms', 'build', '-m', module, '-v', version, '-k', kernel]
         ret = subprocess.call(cmd)

在这里测试过,似乎工作得很好:

$ dkms status
nvidia-current, 275.09.07, 3.0.0-5-generic, x86_64: installed
virtualbox, 4.0.10, 3.0.0-5-generic, x86_64: installed

$ sudo python dkms.py
...

$ dkms status
nvidia-current, 275.09.07, 3.0.0-5-generic, x86_64: installed
nvidia-current, 275.09.07, 3.0-2-generic, x86_64: built
nvidia-current, 275.09.07, 3.0-3-generic, x86_64: built
virtualbox, 4.0.10, 3.0.0-5-generic, x86_64: installed
virtualbox, 4.0.10, 3.0-2-generic, x86_64: built
virtualbox, 4.0.10, 3.0-3-generic, x86_64: built

如果您还想安装模块,请在最后一行替换build with install。

第三种解决思路

结合@htorque和@Ryan Thompson的答案,这是我的(作为根)one-liner:

dkms status | sed s/,//g | awk '{print "-m",$1,"-v",$2}' | while read line; do ls /var/lib/initramfs-tools | xargs -n 1 dkms install $line -k; done

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/2611.html,未经允许,请勿转载。