如何在/ boot中释放更多空间?

我的/boot分区几乎已满,每次重启系统时都会收到警告。 我已经删除了旧的内核包(linux-headers …),实际上我这样做是为了安装自动更新附带的新内核版本。

安装新版本后,分区几乎已满。 那我还能删除什么呢? 是否有其他文件与旧内核映像相关联?

这是我/boot分区上的文件列表:

 :~$ ls /boot/ abi-2.6.31-21-generic lost+found abi-2.6.32-25-generic memtest86+.bin abi-2.6.38-10-generic memtest86+_multiboot.bin abi-2.6.38-11-generic System.map-2.6.31-21-generic abi-2.6.38-12-generic System.map-2.6.32-25-generic abi-2.6.38-8-generic System.map-2.6.38-10-generic abi-3.0.0-12-generic System.map-2.6.38-11-generic abi-3.0.0-13-generic System.map-2.6.38-12-generic abi-3.0.0-14-generic System.map-2.6.38-8-generic boot System.map-3.0.0-12-generic config-2.6.31-21-generic System.map-3.0.0-13-generic config-2.6.32-25-generic System.map-3.0.0-14-generic config-2.6.38-10-generic vmcoreinfo-2.6.31-21-generic config-2.6.38-11-generic vmcoreinfo-2.6.32-25-generic config-2.6.38-12-generic vmcoreinfo-2.6.38-10-generic config-2.6.38-8-generic vmcoreinfo-2.6.38-11-generic config-3.0.0-12-generic vmcoreinfo-2.6.38-12-generic config-3.0.0-13-generic vmcoreinfo-2.6.38-8-generic config-3.0.0-14-generic vmcoreinfo-3.0.0-12-generic extlinux vmcoreinfo-3.0.0-13-generic grub vmcoreinfo-3.0.0-14-generic initrd.img-2.6.31-21-generic vmlinuz-2.6.31-21-generic initrd.img-2.6.32-25-generic vmlinuz-2.6.32-25-generic initrd.img-2.6.38-10-generic vmlinuz-2.6.38-10-generic initrd.img-2.6.38-11-generic vmlinuz-2.6.38-11-generic initrd.img-2.6.38-12-generic vmlinuz-2.6.38-12-generic initrd.img-2.6.38-8-generic vmlinuz-2.6.38-8-generic initrd.img-3.0.0-12-generic vmlinuz-3.0.0-12-generic initrd.img-3.0.0-13-generic vmlinuz-3.0.0-13-generic initrd.img-3.0.0-14-generic vmlinuz-3.0.0-14-generic 

目前,我正在使用3.0.0-14-generic内核。

你有很多未使用的内核。 删除除最后一个内核以外的所有内核:

 sudo apt-get purge linux-image-{3.0.0-12,2.6.3{1-21,2-25,8-{1[012],8}}} 

这是以下的简写:

 sudo apt-get purge linux-image-3.0.0-12 linux-image-2.6.31-21 linux-image-2.6.32-25 linux-image-2.6.38-10 linux-image-2.6.38-11 linux-image-2.6.38-12 linux-image-2.6.38-8 

删除linux-image-xxx-x包也将删除linux-image-xxx-x-generic

标头安装在/usr/src ,用于构建out-tree内核模块(如专有的nvidia驱动程序和virtualbox)。 如果未安装匹配的内核包( linux-image-*linux-image-*大多数用户应删除这些头包。

要列出所有已安装的内核,请运行:

 dpkg -l linux-image-\* | grep ^ii 

一个命令显示可以删除的所有内核和标头,不包括当前运行的内核:

 kernelver=$(uname -r | sed -r 's/-[az]+//') dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve $kernelver 

它选择以linux-headers-linux-image-开头的所有包,打印已安装包的包名,然后排除当前加载/运行的内核 (不一定是最新的内核!)。 这符合在删除较旧的,已知工作内核之前测试较新内核的建议。

因此,在升级内核并重新启动以测试它之后,您可以删除所有其他内核:

 sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[az]+//')") 

您的启动分区已满。 由于这是内核更新,因此这些文件将被复制到启动分区,因此您需要清除。 这是一篇博客文章 ,将向您展示如何使用一个命令清除旧内核映像。 我将给出该方法的基本概要。 使用此命令打印出当前版本的内核:

 uname -r 

然后使用此命令打印出您安装的不是最新内核的所有内核:

 dpkg -l linux-{image,headers}-"[0-9]*" | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e '[0-9]' 

确保您当前的内核不在该列表中。 注意这是最终命令的大部分(下面是)。 要卸载和删除这些旧内核,您需要将这些参数传递给:

 sudo apt-get -y purge 

现在我们可以通过将最后两个命令组合到这个不圣洁的混乱中来做我们想要的一切:

 dpkg -l linux-{image,headers}-"[0-9]*" | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e '[0-9]' | xargs sudo apt-get -y purge 

而那一个命令将为您处理一切。 我将确认这确实有效,但从不信任互联网上的任何人。 :)有关详细信息,博客文章非常好地解释了命令的每个部分如何通过它阅读,以便您满意它做了您想要它做的事情。

 sudo apt-get autoremove 

此命令自动执行该任务。

Synaptic Package Manager可用于轻松选择和删除旧内核映像。 Synaptic Package Manager的屏幕截图

如果您还没有安装Synaptic:

 sudo apt-get install synaptic 

启动应用程序并选择显示的选项。

根据/ boot文件夹中的文件,您应该能够突出显示版本为“2.6.x”的所有“linux-”软件包,其中x介于31到38之间。

右键单击每个linux软件包,然后选择“标记为完全删除”选项。 最后单击“应用”按钮。 这将删除所有文件和任何相关文件。 您的/boot文件夹现在应该有点整洁。

感谢您提供有关问题的详细信息,这让我朝着正确的方向前进。 虽然保留以前的内核文件很有用,但您可以一次性删除所有内核文件,请查看以下post:
如何删除所有未使用的Linux内核标头,图像和模块

通过命令行完成。 如果您通过远程使用类似WINSCP的东西来打开终端会话并将其粘贴,那么效果非常好。

这里是从文章链接复制的,我建议你阅读完整的文章:

 dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge 

这是一个旧问题的新答案,但清理这个东西(以及更多)的简单方法是安装Ubuntu Tweak 。 要安装它 :

 sudo add-apt-repository ppa:tualatrix/ppa sudo apt-get update sudo apt-get install ubuntu-tweak 

然后你可以运行Ubuntu Tweak,转到“janitor”标签,从这里只需点击三下:

Ubuntu调整旧内核的门卫

不过,最好留下最后一个内核(你永远不知道)或一个众所周知的工作内核来保证安全; 但这很容易定制。

您可以使用相同的工具来清理很多东西 – 只需记住,如果您清理缩略图缓存或TB缓存,则系统必须在需要时重建它们。

我能够通过使用dpkg直接删除包来解决问题。 尽管这些包仍然在dpkg -l列出,但是文件将从/boot中删除,从而释放空间。

 phrogz@planar:/boot$ sudo dpkg --remove linux-image-2.6.32-30-server (Reading database ... 145199 files and directories currently installed.) Removing linux-image-2.6.32-30-server ... Running postrm hook script /usr/sbin/update-grub. Generating grub.cfg ... Found linux image: /boot/vmlinuz-2.6.32-35-server Found linux image: /boot/vmlinuz-2.6.32-34-server Found initrd image: /boot/initrd.img-2.6.32-34-server Found linux image: /boot/vmlinuz-2.6.32-33-server Found initrd image: /boot/initrd.img-2.6.32-33-server Found linux image: /boot/vmlinuz-2.6.32-32-server Found initrd image: /boot/initrd.img-2.6.32-32-server Found linux image: /boot/vmlinuz-2.6.32-31-server Found initrd image: /boot/initrd.img-2.6.32-31-server Found memtest86+ image: /memtest86+.bin done # Repeat for kernels -31 and -32 and -33, up to (but not including) # the version listed by `uname -a` 

在此之后, apt-get -f install修复了我的依赖性问题,并且一切都很好。

但是,我不会接受我的这个答案,因为我仍然需要知道我是否应该增加/boot大小或做其他事情。

您可以停止使用单独的/ boot分区,那么您将没有那么有限的空间。 要执行此操作,请卸载分区,然后将其安装在其他位置并将其中的所有文件复制到根分区中的/ boot目录,然后从/ etc / fstab中删除该条目并重新安装grub。 例如(您将需要使用正确的分区):

 sudo -s umount /boot mount /dev/sda2 /mnt cp -a /mnt/* /boot/ umount /mnt gedit /etc/fstab grub-install /dev/sda 

然后,您可以使用gparted删除旧的/ boot分区,并可能扩展根分区以使用该空间。 要扩展根分区,您需要从livecd启动,并且可用空间需要立即在右侧。 如果/ boot分区当前位于根分区的左侧,那么您需要先将根分区向左移动,然后将其扩展,但这可能需要非常长的时间,因此可能不值得麻烦。

对我来说, apt-get purgedpkg --remove了。 所以我不得不使用rm -f从/ boot中删除了几个较旧的内核映像。

从上面的答案中获得最好的结果,我的尝试和方法是:

  • uname -a找到正在运行的内核。
  • dpkg -l linux-{headers,image}-\* | grep ^ii dpkg -l linux-{headers,image}-\* | grep ^ii列出所有当前安装的内核相关软件包。 这将包括正在运行的内核。
  • sudo apt-get purge linux-{image,headers}-3.16.0-{xx,yy,zz}删除旧内核。 将xx,yy,zz替换为要删除的内核构建列表 – 这些是上一个命令列出的所有构建版本,它们比当前运行的内核旧。 确保不删除当前运行的内核 – 您的系统将无法启动 。 您可能还需要将内核版本从3.16.0更改为系统上安装的任何内容。
  • (可选)执行额外的sudo apt-get autoremove将删除当前不再需要的旧内核的剩余依赖项,从而释放更多空间。

您可能没有安装匹配的内核版本的头文件,反之亦然 – 只需在命令中包含所有这些版本。 APT会抱怨某些软件包因为未安装而无法删除,但这不会造成任何损害。

如果出现问题……

  • 如果apt命令由于设备错误没有剩余空间而失败,请使用相应的dpkg命令: sudo dpkg --purge linux-{image,headers}-3.16.0-{xx,yy,zz}
  • 如果dpkg命令仍然不起作用,因为同一版本的linux-headers包(你也想卸载)需要linux-image包,dpkg可能无法解决这个问题。 重新检查dpkg -l的输出并在命令行上指定任何包名称后缀。 例如,在我的一个系统上,最终工作的命令是sudo dpkg --purge linux-{image,headers}-3.16.0-{xx,yy,zz}-generic-pae
  • 如果您遇到任何其他错误,请寻求帮助 – 您可能遇到的问题与您想象的不同。

我已经删除了旧的内核包(linux-headers …)

linux-headers-*不是内核。 内核包是名为linux-image-* 。 名为linux-headers-*是用于编译内核模块的开发包:它们不存在于/ boot目录中,并且对于系统的日常使用不是必需的。

您在/ boot中列出的文件包含几个旧内核映像( vmlinuz* )和这些内核的已编译initrd映像( initrd.img* ),这表明您仍然安装了许多旧内核软件包。

您应该能够列出已安装的内核

 aptitude search ~ilinux-image 

(请注意,这可能会返回非内核的包)。

通常不需要安装两个以上的内核 – 当前正在使用的内核和前一个内核(作为后备)。 所以你可以逐个删除旧版本,如下所示:

 sudo apt-get autoremove linux-image-3.2.0-23-generic 

确保将“3.2.0-23-generic”替换为要删除的实际内核版本! 另外,不要删除linux-image-generic等软件包。 你必须非常小心,不要删除当前正在运行的内核,否则你将无法启动(Ubuntu可能会或可能不会警告你这样做)。

您可以找到当前运行的内核:

 uname -r 

我有这个问题,因为我从/ boot手动删除了一些initrd-img-xxx文件,我遇到了这些旧版本生成并填入/boot文件夹的问题。 要修复它我遵循以下内容:

  • 我手动删除了生成的旧内核版本,以释放空间。
  • 您将以超级用户身份编辑文本配置文件,因此将以下内容粘贴到终端中:

     sudo gedit /etc/initramfs-tools/update-initramfs.conf 
  • 找到update_initramfs=yes并将其更改为update_initramfs=no 。 保存并退出该文件,然后运行:

     sudo dpkg --configure -a 

这解决了我的问题。 这是基于这个博客

希望在重新启动时一切都很好,之后您可以尝试在update-initramfs.conf中将no更改为yes

列出所有内核:

 dpkg --list 'linux-image*' 

显示当前内核:

 uname -r 

列出所有内核除了当前的一个:

 dpkg -l linux-{image,headers}-"[0-9]*" | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e '[0-9]' 

确保您当前的内核不在该列表中。

删除所有内核除了当前内核:

 dpkg -l linux-{image,headers}-"[0-9]*" | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e '[0-9]' | xargs sudo apt-get -y purge 

清除其他东西:

 sudo apt-get autoremove 

如果仍然为安装/卸载文件提供启动磁盘空间错误,则直接从/ boot目录中删除一个OLD映像,

 ls -lh /boot/*-3.13.0-119*; rm /boot/*-3.13.0-119*; 

注意:请在删除任何图像之前再次查看当前内核映像。

超级有用的实用程序 ,将清除您的启动分区

 git clone https://github.com/erichs/bootnukem.git cd bootnukem sudo ./install.sh 

使用风险自负,但它对我有用:

 sudo bootnukem 

在aptitude或synaptic中有一个“旧的或手动安装的包装”部分。 那里应该有旧的linux包。

我在下面写的一个bash脚本使得这个过程更加用户友好。

YMMV – 它是为薄荷14制作的。还在学习BASH,所以它可能有点笨重。 使用风险自负,但它对我有用!

 #!/bin/bash endCol='\e[0m' bold_red='\e[1;31m' bold_green='\e[1;32m' bold_yellow='\e[1;33m' title_color='\e[0;30;47m' function show_kernel_info { clear current_kernel=$(uname -r) echo "Current ACTIVE kernel is:" echo -e " "$bold_yellow$current_kernel$endCol echo "This kernel will be TOTALLY EXCLUDED from all actions in this script." echo "Also, one fallback non-active kernel will be always left untouched." echo "" echo "These are the non-active kernels stored in /boot:" count_of_old_kernels_in_boot=$(ls -o /boot/initrd* | grep -c -v "$current_kernel") if [ $count_of_old_kernels_in_boot = 0 ]; then echo " * No non-active kernels found! *" else ls -o /boot/initrd* | grep -v "$current_kernel" fi echo "" list_of_old_kernels=$(dpkg --list | grep linux-image | awk -F' ' '{ print $2 }' | grep -v "$current_kernel" | grep -v "linux-image-generic") current_old_kernel=$(dpkg --list | grep linux-image | awk -F' ' '{ print $2 }' | grep -v "$current_kernel" | grep -v "linux-image-generic" | head -n 1) count_of_old_kernels_installed=$(dpkg --list | grep linux-image | awk -F' ' '{ print $2 }' | grep -v "$current_kernel" | grep -c -v "linux-image-generic") echo "Listing of all unused kernels still installed in the system (these may not exist in /boot):" if [ $count_of_old_kernels_installed = 0 ]; then echo " * No unused kernel installs found! *" else dpkg --list | grep linux-image | awk -F' ' '{ print $2 }' | grep -v "$current_kernel" | grep -v "linux-image-generic" fi echo "" } function exit_script { free_space_after=$(df -BM /boot | tail -n 1 | awk -F' ' '{ print $4 }' | tr -d M) let freed_space=$free_space_after-$free_space_before echo "" echo "Results (in MB)" echo "---------------" echo "Free space in /boot before script was run: "$free_space_before echo "Free space now: "$free_space_after echo "" echo "Amount of space freed up = "$freed_space echo "" echo "Press any key to exit." read -s -n 1 echo "" exit } # Main code echo "" echo -e $title_color" --------------------------- "$endCol echo -e $title_color" - Kernel Cleanup v1.0 - "$endCol echo -e $title_color" --------------------------- "$endCol echo "" echo "Maximise this window for readability." echo "Press any key to continue." read -s -n 1 echo "" echo "This script will remove old unused kernels, but it will prompt you before removing each one." echo "It will never remove the current running kernel, and will also leave one fallback kernel." echo "It can also remove source files from /usr/src for each kernel removed." echo "This is normally safe to do and will free up lots more space." echo "" echo "Do you want that done as well? (y/n, enter=yes)" valid_input=0 while [ "$valid_input" = "0" ]; do read -s -n 1 YesNo_input if [ "$YesNo_input" = "" ]; then YesNo_input="y" fi case $YesNo_input in y) RemoveSource="y" valid_input=1 ;; Y) RemoveSource="y" valid_input=1 ;; n) RemoveSource="n" valid_input=1 ;; N) RemoveSource="N" valid_input=1 ;; esac done free_space_before=$(df -h /boot | tail -n 1 | awk -F' ' '{ print $4 }' | tr -d M) show_kernel_info while [ $count_of_old_kernels_in_boot -gt 1 ]; do # failsafe check if somehow the current kernel is about to be removed! if [ "$current_old_kernel" = "$current_kernel" ]; then echo -e $bold_red"ERROR!"$endCol" Somehow the current kernel has crept into the removal process!" echo "I refuse to do that! Aborting script." exit_script fi # failsafe check if somehow a linux-image-generic entry is about to be removed if [ "$current_old_kernel" = "linux-image-generic" ]; then echo -e $bold_red"ERROR!"$endCol" Somehow one of the linux-image-generic entries has crept into the removal process!" echo "I refuse to do that! Aborting script." exit_script fi echo "Command about to be executed is:" echo " $ sudo apt-get purge \"$current_old_kernel\"" check_in_boot=$(echo $current_old_kernel | sed 's/linux-image/initrd.img/g') if [ -e /boot/$check_in_boot ]; then echo -e $bold_yellow"Note:"$endCol" This kernel exists in /boot but it NON-active, so it's OK to remove." else echo -e $bold_green"Totally safe to remove:"$endCol" This kernel does NOT exist in /boot." fi echo "" echo "Are you sure you want to remove this kernel?" echo "(*upper case* Y=yes / any other key will exit the script)" read -s -n 1 yes_no echo "" # Only entering a single upper case Y will work! if [ "$yes_no" != "Y" ]; then echo "Aborting script." exit_script fi echo "Removing kernel "$current_old_kernel"..." sleep 1 sudo apt-get -y purge $current_old_kernel if [ "$RemoveSource" = "y" ]; then current_old_source=$(echo $current_old_kernel | sed 's/linux-image/linux-headers/g') current_old_source=$(echo $current_old_source | sed 's/-generic//g') current_old_source=$(echo $current_old_source | sed 's/-pae//g') sudo apt-get -y purge $current_old_source fi show_kernel_info done if [ $count_of_old_kernels_in_boot = 0 ]; then echo -e $bold_red"There are no NON-active kernels to remove!"$endCol else echo -e $bold_red"There is only one NON-active kernel left in /boot!"$endCol echo "This script will not remove the last non-active kernel so that you have at least one backup kernel." fi echo "Aborting script." exit_script 

[作为AskUbuntu noob,我不能发表评论,直到声望= 50,所以不要因此而投票。]

我的服务器也是这样做的。 从字面上看,这里没有任何预期的答案,因为这些需要一些工作室/ boot才能完成。 如果启动分区已满,它将突然结束而不删除任何图像。

对我来说唯一有用的是查看当前图像,然后手动sudo rm filename以查找最旧的图像文件(每个文件名都有-3.16.0-30 )。 一旦完成,那么sudo apt-get autoremove就有了完成工作所需的摆动空间。 它确实突出了与该版本相关的一些错误,例如:“depmod:FATAL:无法加载/boot/System.map-3.16.0-30-generic:没有这样的文件或目录”,但这是预期的。

完成后, df返回42%用于/ boot以表明它再次健康。

I wrote this bash script to selective purge old kernels all at once:

rm kernels real size

All the bash code and instructions are included in the link.

I show that still in 2017 this question has new comments and answers but missing one answer that I think is very useful here:

Ubuntu as a desktop OS of simple everyday usage has no reason to be installed with separate \boot partition and something like this will NOT offer something to a “common user”… So a solution is a fresh install without \boot partition and this way you will never have such a problem

PS: My answer can be deleted or added in the accepted answer… (I think it will help some people this way)

Save this script as /usr/local/bin/remove_kernels.sh (remember to give execute permissions sudo chmod 755 /usr/local/bin/remove_kernels.sh ):

 #!/bin/sh if test $(id -u) != 0; then echo Error: You must be root to run this script! exit 1 fi apt purge $( dpkg --list | grep -P -o "linux-image-\d\S+" | grep -v $(uname -r | grep -P -o ".+\d") ) update-grub 

Then to remove all old kernels just type: sudo remove_kernels.sh

If you cannot remove more unused files and if you have other partition with/or free space on same device, you can resize of /boot partition by parted / gparted . (It is included on installation media, too.)

Warning : Resizing of partition is dangerous operation, save your important data on other media before doing it!