跳转至

删除旧内核并释放 /boot 磁盘空间

文档时效性说明

本文为早期笔记,可能存在版本过时、命令失效、链接失效、最佳实践变化等问题。请以官方最新文档为准。

原英文标题:Way to delete old kernel and release disk space in /boot #

1. 场景:/boot 分区 100% 已满,apt 无法正常工作

当我在 Ubuntu 14.04 服务器上尝试安装 Nginx 时,发现无论使用 apt install 安装任何包,都会出现类似下面的错误信息。

1
2
3
4
5
6
7
8
The following packages have unmet dependencies: 
    linux-image-generic : 
        Depends: linux-image-4.4.0-150-generic but it is not installed or
                 linux-image-unsigned-4.4.0-150-generic but it is not installed             
        Recommends: thermald but it is not installed
    linux-modules-extra-4.4.0-150-generic : 
        Depends: linux-image-4.4.0-150-generic but it is not installed or
             linux-image-unsigned-4.4.0-150-generic but it is not installed

此外,我尝试使用 teddysun 提供的脚本更新 Linux 内核,但失败了。

我还尝试使用 apt -f install nginx 命令安装 Nginx,结果输出与上面相同。apt autoremove 也无效。

2. 解决方案

2.1 检测 /boot 分区是否已满

运行 df -lh

root@localhost:~# df -lh
Filesystem      Size  Used Avail Use% Mounted on
udev            217M     0  217M   0% /dev
tmpfs            50M  9.7M   40M  20% /run
/dev/sda2       9.5G  3.3G  5.7G  37% /
tmpfs           247M     0  247M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           247M     0  247M   0% /sys/fs/cgroup
/dev/sda1       361M  361M    0M 100% /boot
tmpfs            50M     0   50M   0% /run/user/0

看起来 /boot 分区已满,因此新内核无法安装。

2.2 列出所有内核

sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r`

输出可能类似下面这样:

linux-image-3.19.0-25-generic
linux-image-3.19.0-56-generic
linux-image-3.19.0-58-generic
linux-image-3.19.0-59-generic
linux-image-3.19.0-61-generic
linux-image-3.19.0-65-generic
linux-image-extra-3.19.0-25-generic
linux-image-extra-3.19.0-56-generic
linux-image-extra-3.19.0-58-generic
linux-image-extra-3.19.0-59-generic
linux-image-extra-3.19.0-61-generic

2.3 强制移除旧内核

使用 dpkg -P --force-depends 强制移除内核文件。apt purge 对我不起作用。

例如:

sudo  dpkg -P --force-depends linux-image-4.4.0-134-generic

3. 原文(English)

4. Case: /boot partition is 100% used and apt malfunctions

When I tired to install Nginx in Ubuntu 14.04 server, I noticed that whatever package I install by using apt install, error massage like this emerges.

1
2
3
4
5
6
7
8
The following packages have unmet dependencies: 
    linux-image-generic : 
        Depends: linux-image-4.4.0-150-generic but it is not installed or
                 linux-image-unsigned-4.4.0-150-generic but it is not installed             
        Recommends: thermald but it is not installed
    linux-modules-extra-4.4.0-150-generic : 
        Depends: linux-image-4.4.0-150-generic but it is not installed or
             linux-image-unsigned-4.4.0-150-generic but it is not installed

Also, I've attempted to update the Linux kernel by using the script provided by teddysun, but failed.

I also tried installing Nginx by using the command apt -f install nginx, it turned out to be the same output as above. And apt autoremove does not take effect.

5. Solution to this problem

5.1 Detect if /boot partition is fully occupied

Run df -lh

root@localhost:~# df -lh
Filesystem      Size  Used Avail Use% Mounted on
udev            217M     0  217M   0% /dev
tmpfs            50M  9.7M   40M  20% /run
/dev/sda2       9.5G  3.3G  5.7G  37% /
tmpfs           247M     0  247M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           247M     0  247M   0% /sys/fs/cgroup
/dev/sda1       361M  361M    0M 100% /boot
tmpfs            50M     0   50M   0% /run/user/0

It seems that the /boot partition is full. So that new kernel cannot be installed.

5.2 List all the kernels

sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r`

The output may be something like below

linux-image-3.19.0-25-generic
linux-image-3.19.0-56-generic
linux-image-3.19.0-58-generic
linux-image-3.19.0-59-generic
linux-image-3.19.0-61-generic
linux-image-3.19.0-65-generic
linux-image-extra-3.19.0-25-generic
linux-image-extra-3.19.0-56-generic
linux-image-extra-3.19.0-58-generic
linux-image-extra-3.19.0-59-generic
linux-image-extra-3.19.0-61-generic

5.3 Force remove old kernels

Use dpkg -P --force-depends to force remove the kernel file. apt purge did not work for me.

For example:

sudo  dpkg -P --force-depends linux-image-4.4.0-134-generic