Ubuntu上删除boot分区中的无用kernel image

一直使用VPS,今天心血来潮,想升级一下VPS上的ubuntu和kernel版本,但在升级过程中,遇到一个情况:VPS的boot分区太小,剩余空间不足,造成kernel upgrade时失败了。

The upgrade has aborted. The upgrade needs a total of 207 M free
space on disk '/boot'. Please free at least an additional 69.2 M of
disk space on '/boot'. You can remove old kernels using 'sudo apt
autoremove' and you could also set COMPRESS=xz in
/etc/initramfs-tools/initramfs.conf to reduce the size of your
initramfs.

按照上面提示,尝试把压缩选项从lz4改成了xz,但空间依旧不够。仔细检查了下boot分区内的文件,发现以前老旧的kernel image占用了很大空间,遂尝试了下面的命令进行了删除,删除之后就可以正常升级kernel了。

  • 查看当前kernel版本,当前版本为5.15
root@valuable-idea-1:~# uname -a
Linux valuable-idea-1.localdomain 5.15.0-138-generic #148-Ubuntu SMP Fri Mar 14 19:05:48 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
  • 删除无用kernel版本5.4
root@valuable-idea-1:~# apt remove linux-image-5.
linux-image-5.15.0-138-generic linux-image-5.4.0-214-generic
root@valuable-idea-1:~# apt remove linux-image-5.4.0-214-generic
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
 accountsservice alsa-topology-conf alsa-ucm-conf at bsdmainutils crda cryptsetup-run
 dconf-gsettings-backend dconf-service gcc-10-base glib-networking glib-networking-common
 glib-networking-services gsettings-desktop-schemas language-selector-common libaccountsservice0
 libasn1-8-heimdal libasound2 libasound2-data libboost-program-options1.71.0 libcanberra0 libcbor0.6
 libdconf1 libdns-export1109 libevent-2.1-7 libffi7 libfl2 libfuse2 libgssapi3-heimdal libhavege1
 libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhogweed5 libhx509-5-heimdal libicu66
 libjson-c4 libkrb5-26-heimdal libldap-2.4-2 libltdl7 libmpdec2 libnettle7 libntfs-3g883 libogg0
 libperl5.30 libproxy1v5 libpython3.8 libpython3.8-minimal libpython3.8-stdlib libreadline5
 libroken18-heimdal libsoup2.4-1 libsoup2.4-common libtdb1 liburcu6 libvorbis0a libvorbisfile3 libwebp6
 libwind0-heimdal ltrace lz4 ncal perl-modules-5.30 popularity-contest python3-entrypoints python3-future
 python3-mock python3-pbr python3-requests-unixsocket python3-simplejson python3.8 python3.8-minimal
 session-migration sound-theme-freedesktop
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
 linux-image-5.4.0-214-generic linux-modules-extra-5.4.0-214-generic
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 215 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 116717 files and directories currently installed.)
Removing linux-modules-extra-5.4.0-214-generic (5.4.0-214.234) ...
Removing linux-image-5.4.0-214-generic (5.4.0-214.234) ...
I: /boot/vmlinuz.old is now a symlink to vmlinuz-5.15.0-138-generic
I: /boot/initrd.img.old is now a symlink to initrd.img-5.15.0-138-generic
/etc/kernel/postrm.d/initramfs-tools:
update-initramfs: Deleting /boot/initrd.img-5.4.0-214-generic
/etc/kernel/postrm.d/zz-update-grub:
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.15.0-138-generic
Found initrd image: /boot/initrd.img-5.15.0-138-generic
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
done
  • 按照提示,把相关无用的packages也一并删除
root@valuable-idea-1:~# apt autoremove --purge
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
 accountsservice* alsa-topology-conf* alsa-ucm-conf* at* bsdmainutils* crda* cryptsetup-run*
 dconf-gsettings-backend* dconf-service* gcc-10-base* glib-networking* glib-networking-common*
 glib-networking-services* gsettings-desktop-schemas* language-selector-common* libaccountsservice0*
 libasn1-8-heimdal* libasound2* libasound2-data* libboost-program-options1.71.0* libcanberra0* libcbor0.6*
 libdconf1* libdns-export1109* libevent-2.1-7* libffi7* libfl2* libfuse2* libgssapi3-heimdal* libhavege1*
 libhcrypto4-heimdal* libheimbase1-heimdal* libheimntlm0-heimdal* libhogweed5* libhx509-5-heimdal*
 libicu66* libjson-c4* libkrb5-26-heimdal* libldap-2.4-2* libltdl7* libmpdec2* libnettle7* libntfs-3g883*
 libogg0* libperl5.30* libproxy1v5* libpython3.8* libpython3.8-minimal* libpython3.8-stdlib* libreadline5*
 libroken18-heimdal* libsoup2.4-1* libsoup2.4-common* libssl1.1* libtdb1* liburcu6* libvorbis0a*
 libvorbisfile3* libwebp6* libwind0-heimdal* linux-headers-5.4.0-214* linux-headers-5.4.0-214-generic*
 linux-modules-5.4.0-214-generic* ltrace* lz4* ncal* perl-modules-5.30* popularity-contest*
 python3-entrypoints* python3-future* python3-mock* python3-pbr* python3-requests-unixsocket*
 python3-simplejson* python3.8* python3.8-minimal* session-migration* sound-theme-freedesktop*
0 upgraded, 0 newly installed, 78 to remove and 0 not upgraded.
After this operation, 298 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 111359 files and directories currently installed.)
Removing language-selector-common (0.219.1) ...
Removing accountsservice (22.07.5-2ubuntu1.5) ...
Removing alsa-topology-conf (1.2.5.1-2) ...
Removing alsa-ucm-conf (1.2.6.3-1ubuntu1.12) ...
Removing at (3.2.5-1ubuntu1) ...
Removing bsdmainutils (12.1.7+nmu3ubuntu2) ...
Removing crda (3.18-1build1) ...
Removing cryptsetup-run (2:2.4.3-1ubuntu1.3) ...
Removing libsoup2.4-1:amd64 (2.74.2-3ubuntu0.2) ...
Removing glib-networking:amd64 (2.72.0-1) ...
Removing gsettings-desktop-schemas (42.0-1ubuntu1) ...
Removing dconf-gsettings-backend:amd64 (0.40.0-3) ...
Removing dconf-service (0.40.0-3) ...
Removing gcc-10-base:amd64 (10.5.0-1ubuntu1~22.04) ...
Removing glib-networking-services (2.72.0-1) ...
Removing glib-networking-common (2.72.0-1) ...
Removing libaccountsservice0:amd64 (22.07.5-2ubuntu1.5) ...
Removing libldap-2.4-2:amd64 (2.4.49+dfsg-2ubuntu1.10) ...
Removing libgssapi3-heimdal:amd64 (7.7.0+dfsg-3ubuntu1) ...
Removing libheimntlm0-heimdal:amd64 (7.7.0+dfsg-3ubuntu1) ...
Removing libkrb5-26-heimdal:amd64 (7.7.0+dfsg-3ubuntu1) ...
Removing libhx509-5-heimdal:amd64 (7.7.0+dfsg-3ubuntu1) ...
Removing libcanberra0:amd64 (0.30-10ubuntu1.22.04.1) ...
Removing libasound2:amd64 (1.2.6.1-1ubuntu1) ...
Removing libasound2-data (1.2.6.1-1ubuntu1) ...
Removing libboost-program-options1.71.0:amd64 (1.71.0-6ubuntu6) ...
Removing libcbor0.6:amd64 (0.6.0-0ubuntu1) ...
Removing libdconf1:amd64 (0.40.0-3) ...
Removing libdns-export1109 (1:9.11.16+dfsg-3~ubuntu1) ...
Removing libevent-2.1-7:amd64 (2.1.12-stable-1build3) ...
Removing python3.8 (3.8.10-0ubuntu1~20.04.18) ...
Removing libpython3.8:amd64 (3.8.10-0ubuntu1~20.04.18) ...
Removing libpython3.8-stdlib:amd64 (3.8.10-0ubuntu1~20.04.18) ...
Removing libffi7:amd64 (3.3-5ubuntu1) ...
Removing libfl2:amd64 (2.6.4-8build2) ...
Removing libfuse2:amd64 (2.9.9-5ubuntu3) ...
Removing libhavege1:amd64 (1.9.1-6ubuntu1) ...
Removing libhcrypto4-heimdal:amd64 (7.7.0+dfsg-3ubuntu1) ...
Removing libheimbase1-heimdal:amd64 (7.7.0+dfsg-3ubuntu1) ...
Removing libhogweed5:amd64 (3.5.1+really3.5.1-2ubuntu0.2) ...
Removing libicu66:amd64 (66.1-2ubuntu2.1) ...
Removing libjson-c4:amd64 (0.13.1+dfsg-7ubuntu0.3) ...
Removing libltdl7:amd64 (2.4.6-15build2) ...
Removing libmpdec2:amd64 (2.4.2-3) ...
Removing libnettle7:amd64 (3.5.1+really3.5.1-2ubuntu0.2) ...
Removing libntfs-3g883 (1:2017.3.23AR.3-3ubuntu1.3) ...
Removing libvorbisfile3:amd64 (1.3.7-1build2) ...
Removing libvorbis0a:amd64 (1.3.7-1build2) ...
Removing libogg0:amd64 (1.3.5-0ubuntu3) ...
Removing libperl5.30:amd64 (5.30.0-9ubuntu0.5) ...
Removing libproxy1v5:amd64 (0.4.17-2) ...
Removing python3.8-minimal (3.8.10-0ubuntu1~20.04.18) ...
Unlinking and removing bytecode for runtime python3.8
Removing libpython3.8-minimal:amd64 (3.8.10-0ubuntu1~20.04.18) ...
Removing libreadline5:amd64 (5.2+dfsg-3build3) ...
Removing libwind0-heimdal:amd64 (7.7.0+dfsg-3ubuntu1) ...
Removing libsoup2.4-common (2.74.2-3ubuntu0.2) ...
Removing linux-headers-5.4.0-214-generic (5.4.0-214.234) ...
Removing libssl1.1:amd64 (1.1.1f-1ubuntu2.24) ...
Removing libtdb1:amd64 (1.4.5-2build1) ...
Removing liburcu6:amd64 (0.11.1-2) ...
Removing libwebp6:amd64 (0.6.1-2ubuntu0.20.04.3) ...
Removing linux-headers-5.4.0-214 (5.4.0-214.234) ...
Removing linux-modules-5.4.0-214-generic (5.4.0-214.234) ...
Removing ltrace (0.7.3-6.1ubuntu6.22.04.1) ...
Removing lz4 (1.9.3-2build2) ...
Removing ncal (12.1.7+nmu3ubuntu2) ...
Removing perl-modules-5.30 (5.30.0-9ubuntu0.5) ...
Removing popularity-contest (1.71ubuntu3) ...
Removing python3-entrypoints (0.4-1) ...
Removing python3-future (0.18.2-5ubuntu0.1) ...
Removing python3-mock (4.0.3-3) ...
Removing python3-pbr (5.8.0-0ubuntu1) ...
update-alternatives: warning: alternative /usr/bin/python3-pbr (part of link group pbr) doesn't exist; removing from list of alternatives
update-alternatives: warning: /etc/alternatives/pbr is dangling; it will be updated with best choice
Removing python3-requests-unixsocket (0.2.0-2) ...
Removing python3-simplejson (3.17.6-1build1) ...
Removing session-migration (0.3.6) ...
Removing sound-theme-freedesktop (0.8-2ubuntu1) ...
Removing libasn1-8-heimdal:amd64 (7.7.0+dfsg-3ubuntu1) ...
Removing libroken18-heimdal:amd64 (7.7.0+dfsg-3ubuntu1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.9) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...
Processing triggers for libglib2.0-0:amd64 (2.72.4-0ubuntu2.4) ...
No schema files found: removed existing output file.
(Reading database ... 76078 files and directories currently installed.)
Purging configuration files for session-migration (0.3.6) ...
Purging configuration files for libpython3.8-minimal:amd64 (3.8.10-0ubuntu1~20.04.18) ...
Purging configuration files for at (3.2.5-1ubuntu1) ...
Purging configuration files for python3-pbr (5.8.0-0ubuntu1) ...
Purging configuration files for libssl1.1:amd64 (1.1.1f-1ubuntu2.24) ...
Purging configuration files for popularity-contest (1.71ubuntu3) ...
Purging configuration files for linux-modules-5.4.0-214-generic (5.4.0-214.234) ...
dpkg: warning: while removing linux-modules-5.4.0-214-generic, directory '/lib/modules/5.4.0-214-generic' not empty so not removed
Purging configuration files for ltrace (0.7.3-6.1ubuntu6.22.04.1) ...
Purging configuration files for bsdmainutils (12.1.7+nmu3ubuntu2) ...
Purging configuration files for python3.8-minimal (3.8.10-0ubuntu1~20.04.18) ...
Purging configuration files for language-selector-common (0.219.1) ...
Purging configuration files for crda (3.18-1build1) ...
Purging configuration files for accountsservice (22.07.5-2ubuntu1.5) ...
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
  • 更新下GRUB配置,之前apt remove时已经更新过,这步其实可以省略
root@valuable-idea-1:~# update-grub
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.15.0-138-generic
Found initrd image: /boot/initrd.img-5.15.0-138-generic
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
done

 

作者:低调的LEO原文地址:https://www.cnblogs.com/randyjang/p/18850392

%s 个评论

要回复文章请先登录注册