Pul*_*all 5 package-management apt kernel software-uninstall 18.04
我想通过命令行安装正在运行的内核。
举例来说,我已经安装了一个自定义内核,linux-image-4.0-1-xyz并且我已经使用这个内核启动了(当我这样做时uname -r,我得到了高于内核版本。
现在,我想删除这个内核而不切换到通用内核。
我试过了apt purge -y linux-image-4.0-1-xyz,这运行了,但我收到一个Package Configuration对话框,询问
Do you want to abort removal now?
Run Code Online (Sandbox Code Playgroud)
有2个选项
<Yes> <No>
Run Code Online (Sandbox Code Playgroud)
如何<No>通过命令行选择?
这个问题的实际答案比我预期的要烦人一些。是的,是的,这很危险,而维护者正是因为这个原因才让这样做变得异常困难。但是,如果您尝试自动化基础架构来执行类似的操作,我相信您实际上知道自己在做什么。就我而言,这是因为我试图清除另一个脚本中的(当前正在运行的)内核,该脚本验证确实有更新的内核可用。
通过检查 的输出debconf-get-selections,确实有一个选项可以为您预先选择该值:
$ sudo debconf-get-selections | grep -B1 linux-base
# Abort kernel removal?
linux-base linux-base/removing-running-kernel boolean true
Run Code Online (Sandbox Code Playgroud)
但是,如果使用 将其设置为“false” debconf-set-selections,则不会发生任何变化。系统仍会提示您。
这让一些人深入了解这到底是什么。事实证明,至少在 Debian 和可能的 Ubuntu 的相对当前 (Buster) 版本上,有一个名为linux-check-removalwho 的单独 Perl 脚本,其目的似乎是忽略、重置,然后提示此警告。
http://manpages.ubuntu.com/manpages/cosmic/man1/linux-check-removal.1.html
该脚本是从prerm dpkg给定内核的脚本中调用的,例如
$ cat /var/lib/dpkg/info/linux-image-4.19.0-13-amd64.prerm
#!/bin/sh -e
version=4.19.0-13-amd64
image_path=/boot/vmlinuz-$version
if [ "$1" != remove ]; then
exit 0
fi
linux-check-removal $version
if [ -d /etc/kernel/prerm.d ]; then
DEB_MAINT_PARAMS="$*" run-parts --report --exit-on-error --arg=$version \
--arg=$image_path /etc/kernel/prerm.d
fi
exit 0
Run Code Online (Sandbox Code Playgroud)
并验证其运行情况:
$ sudo linux-check-removal $(uname -r)
# "Yes" selected
E: Aborting removal of the running kernel
$ echo $?
1
$ sudo linux-check-removal $(uname -r)
# "No" selected
W: Removing the running kernel
$ echo $?
0
Run Code Online (Sandbox Code Playgroud)
因此,实际的解决方案是用仅返回 0 的内容覆盖此命令。
但是,尽管该命令使用相对调用,但调用$PATH该脚本时,其内部不包含目录。因此,正如您所期望的那样,对 中的脚本进行简单的覆盖是行不通的。为了演示,我修改了打印和值,然后调用命令:dpkg/usr/local/usr/local/binprerm$PATHwhichapt remove
Removing linux-image-4.19.0-13-amd64 (4.19.160-2) ...
PATH: /usr/sbin:/usr/bin:/sbin:/bin
WHICH: /usr/bin/linux-check-removal
W: Removing the running kernel
Run Code Online (Sandbox Code Playgroud)
因此,我们被迫暂时将真实的内容移开/usr/bin/linux-check-removal,并安装一个仅返回 0 的虚拟占位符脚本。我能想到的最简单的方法是:
$ sudo mv /usr/bin/linux-check-removal /usr/bin/linux-check-removal.orig
$ echo -e '#!/bin/sh\necho "Overriding default linux-check-removal script!"\nexit 0' | sudo tee /usr/bin/linux-check-removal
$ sudo chmod +x /usr/bin/linux-check-removal
Run Code Online (Sandbox Code Playgroud)
如果您愿意,您可以删除该echo行,但我将其添加到演示中。
现在您可以以非交互方式成功清除正在运行的内核版本:
$ sudo apt purge -y linux-image-4.19.0-13-amd64
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-4.19.0-12-amd64 linux-headers-4.19.0-12-common linux-image-4.19.0-12-amd64
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
linux-image-4.19.0-13-amd64*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 270 MB disk space will be freed.
(Reading database ... 104395 files and directories currently installed.)
Removing linux-image-4.19.0-13-amd64 (4.19.160-2) ...
Overriding default linux-check-removal script!
I: /vmlinuz.old is now a symlink to boot/vmlinuz-4.19.0-12-amd64
I: /initrd.img.old is now a symlink to boot/initrd.img-4.19.0-12-amd64
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-4.19.0-13-amd64
/etc/kernel/postrm.d/zz-update-grub:
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.19.0-14-amd64
Found initrd image: /boot/initrd.img-4.19.0-14-amd64
Found linux image: /boot/vmlinuz-4.19.0-12-amd64
Found initrd image: /boot/initrd.img-4.19.0-12-amd64
done
(Reading database ... 99993 files and directories currently installed.)
Purging configuration files for linux-image-4.19.0-13-amd64 (4.19.160-2) ...
Run Code Online (Sandbox Code Playgroud)
然后恢复原来的linux-check-removal:
$ sudo mv /usr/bin/linux-check-removal.orig /usr/bin/linux-check-removal
Run Code Online (Sandbox Code Playgroud)
访客请注意:虽然可以,但强烈不建议删除正在运行的内核。如果该内核是唯一安装的内核,并且您在卸载或删除它后重新启动或断电,您的系统将无法启动,您可能必须重新安装。恢复模式将不起作用。如果发生这种情况,您可以从实时系统启动来挽救数据,并可能将内核文件复制到正确的位置。通常没有理由卸载正在运行的内核。所以你可能不应该遵循这个答案:)
您在评论中指出,您希望避免在使用包管理工具删除正在运行的内核时出现的对话框。我能想到的实现此目的的唯一方法是删除属于正在运行的内核的所有文件。
我们将用来locate查找内核的文件。首先更新其数据库。
sudo updatedb
Run Code Online (Sandbox Code Playgroud)
如果由于某种原因您不想删除dpkg内核的数据库记录,请运行此命令而不是上面的命令:
sudo updatedb --prunepaths=/var/lib/dpkg
Run Code Online (Sandbox Code Playgroud)
找到正在运行的内核及其模块等:
locate -be $(uname -r)
Run Code Online (Sandbox Code Playgroud)
检查清单。要删除属于内核的所有文件,甚至是 中的许多小文件/usr/src,请-b省略 中的标志locate。当您获得所需的列表时,将结果通过管道传递到xargs以删除文件:
locate -be $(uname -r) -0 | xargs -0 sudo rm -r
Run Code Online (Sandbox Code Playgroud)
要进行xargs交互,您可以添加该-p标志,它会提示您确认是否确实要rm在列表中的每个文件上运行。如果省略-bfromlocate加上-pto xargs,就会确认很久。
实际上没有必要使用-0标志来获取空分隔的输出,因为所有这些文件都将具有正常的名称,除非您或其他人对它们进行了修改。但最好使用 空分隔xargs,因为如果任何文件名有空格,就会出错。
为了防止 GRUB 尝试引导或让您选择引导不存在的内核,请更新配置:
sudo update-grub
Run Code Online (Sandbox Code Playgroud)
我刚刚对此进行了测试,现在,我正在从已以这种方式删除正在运行的内核的系统中输入此内容(我从较旧的 Ubuntu 内核启动来执行此操作,因为我通常使用修补后的内核,该内核dpkg不会我不知道,我不想删除它并再次编译)。
内核位于 RAM 中。它在启动时加载到 RAM 中,因此现在不需要它的文件。但是,我现在将使用以下命令重新安装内核:
sudo apt update
sudo apt install --reinstall linux-image-$(uname -r) \
linux-modules-extra-$(uname -r) \
linux-headers-$(uname -r) \
linux-modules-$(uname -r)
Run Code Online (Sandbox Code Playgroud)
由于您似乎对非交互式过程感兴趣,因此我会提到,如果您希望将任何输出捕获到文件中以供以后使用,则可以apt-get优先使用。apt
| 归档时间: |
|
| 查看次数: |
2422 次 |
| 最近记录: |