当前位置: 首页 > 工具软件 > LinuxBoot > 使用案例 >

linux boot分区空间不足问题

宇文飞羽
2023-12-01

写这篇文章,一定是因为按照常规没能解决问题。
本文介绍了如何解决 /boot 分区空间不足以及因 /boot 空间不足导致的卸载旧内核失败的问题

按照常规方法未能卸载的,直接跳到第三节:

一、系统:Ubuntu12.10 (GNU/Linux 3.5.0-25-generic x86_64)

最近登录开发服务器时总是看到这样一句提示(红色部分):

Welcome to Ubuntu 12.10 (GNU/Linux3.5.0-25-generic x86_64)

*Documentation:  https://help.ubuntu.com/

  Systeminformation disabled due to load higher than 4.0

System load:  1.5                Processes:           128

Usage of /:   8.2% of452.47GB   Users logged in:     0

Memory usage: 78%               IP address for p5p1: 172.16.5.2

Swap usage:   34%

=> /boot is using 96.5% of 228MB

Graph this data and manage this system athttps://landscape.canonical.com/

New release '13.04' available.

Run 'do-release-upgrade' to upgrade to it.

*** System restart required ***

You have new mail.

Last login: Wed Jul 31 10:17:37 2013 from172.16.7.6

devel@devServer:~$

今天终于忍不住想解决一下,先看一下分区使用情况吧:

devel@devServer:~$ df -lh

Filesystem                  Size  Used Avail Use% Mounted on

/dev/mapper/devServer-root  453G  41G  389G  10% /

udev                        1.9G   12K 1.9G   1% /dev

tmpfs                       776M  7.0M 770M   1% /run

none                        5.0M     0 5.0M   0% /run/lock

none                        1.9G     0 1.9G   0% /run/shm

none                        100M     0 100M   0% /run/user

/dev/sda2                  229M  221M     0 100% /boot

/dev/sda1                   190M  126K 190M   1% /boot/efi

devel@devServer:~$

好吧,我和我的小伙伴们都惊呆了!,/boot分区100%了(不要怪我,当初进公司时还没有服务器,那天搭建环境装系统时,老总图省事,一路下一步,就成这个样子了),Linux默认分区时,boot分区就200M,按理说也不小,足够了(实际也就几十M),但是内核经常性的升级,而且自己又不自动卸载,于是该目录下旧的内核文件越积越多,最后就满了。

对于boot分区,不像LV里面的分区,可大可小,它一般是挂在单独的物理分区上的,所以就不考虑怎么扩容了,还是先看看怎么把浪费的空间回收吧,去网上学习了一下,取回两条命令:

查看系统所有内核:dpkg --get-selections |grep linux-image

卸载内核:sudo apt-get remove linux-image-3.2.0-17-generic

注意:实际运行时,系统提示要使用autoremove,经查,autoremoveremove的区别是,它可以连同当初自动安装的依赖(或推荐)包(同时未被其他包依赖)也一同卸载,我们将采用这个命令。

先在备份服务器上操作一下看看效果,再搞开发服务器(万一宕掉。。。)

这台是我分的区,boot分区分的比默认的大一倍(看来还是有好处的):

二、系统:Ubuntu12.04 LTS (GNU/Linux 3.2.0-39-generic x86_64)

devel@devDBServer:~$ df -lh

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/devDBServer-root   94G 2.9G   87G   4% /

udev                          1.9G  4.0K 1.9G   1% /dev

tmpfs                         776M  324K 776M   1% /run

none                          5.0M     0 5.0M   0% /run/lock

none                          1.9G  4.0K 1.9G   1% /run/shm

/dev/sda1                     458M 232M 202M  54% /boot

/dev/mapper/devDBServer-home   46G 181M   44G   1% /home

/dev/mapper/devDBServer-opt   178G 467M  168G   1% /opt

/dev/mapper/devDBServer-var    46G 2.1G   42G   5% /var

/home/devel/.Private           46G 181M   44G   1% /home/devel

devel@devDBServer:~$

我们看到,这台的boot分区也占了54%了,OK,开始,先看一下今年都自动更新了多少内核了:

devel@devDBServer:~$ dpkg --get-selections|grep linux-image

linux-image-3.2.0-23-generic                     install

linux-image-3.2.0-39-generic                     install

linux-image-3.2.0-40-generic                     install

linux-image-3.2.0-41-generic                     install

linux-image-3.2.0-43-generic                     install

linux-image-3.2.0-44-generic                     install

linux-image-3.2.0-45-generic                     install

linux-image-3.2.0-48-generic                     install

linux-image-3.2.0-51-generic                     install

linux-image-server

是不少了,查看一下当前启动的内核是哪个版本:

devel@devDBServer:~$ uname -a

Linux devDBServer 3.2.0-39-generic

说明自上一个版本以来,机器一直没被重启过啊,OK现在重启一下,让它启动进入最新内核:

devel@devDBServer:~$ sudo reboot

Broadcast message from devel@devDBServer

         (/dev/pts/0)at 11:12 ...

The system is going down for reboot NOW!

devel@devDBServer:~$

同时打开Windows命令提示符,输入ping命令以监控远程系统的启动

C:\Users\Think>ping 172.16.5.32 -t

正在 Ping 172.16.5.32 具有 32 字节的数据:

来自 172.16.5.32 的回复字节=32 时间=2ms TTL=63

来自 172.16.5.32 的回复字节=32 时间=55ms TTL=63

来自 172.16.5.32 的回复字节=32 时间=4ms TTL=63

来自 172.16.5.32 的回复字节=32 时间=1ms TTL=63

请求超时。

请求超时。

请求超时。

请求超时。

请求超时。

请求超时。

请求超时。

请求超时。

请求超时。

请求超时。

请求超时。

来自 172.16.5.32 的回复字节=32 时间=1ms TTL=63

来自 172.16.5.32 的回复字节=32 时间=134ms TTL=63

来自 172.16.5.32 的回复字节=32 时间=4ms TTL=63

来自 172.16.5.32 的回复字节=32 时间=2ms TTL=63

OK,开始返回数据包,说明远程服务器已经重启完毕,重新登录系统,查看内核版本:

devel@devDBServer:~$ uname -a

Linux devDBServer 3.2.0-51-generic#77-Ubuntu SMP Wed Jul 24 20:18:19 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

现在开始卸载不用的内核:

sudo apt-get autoremovelinux-image-3.2.0-23-generic linux-image-3.2.0-39-generic linux-image-3.2.0-40-genericlinux-image-3.2.0-41-generic linux-image-3.2.0-43-generic linux-image-3.2.0-44-genericlinux-image-3.2.0-45-generic

稍后根据提示Do you want to continue [Y/n]?

输入y确定。

接着就开始卸载了,卸载完毕会自动重建grub

这样,我们只保留最近的两个内核(48,51)(防止最新的不稳定)

查看剩余内核:

devel@devDBServer:~$ dpkg --get-selections|grep linux-image

linux-image-3.2.0-23-generic                     deinstall

linux-image-3.2.0-39-generic                     deinstall

linux-image-3.2.0-40-generic                     deinstall

linux-image-3.2.0-41-generic                     deinstall

linux-image-3.2.0-43-generic                     deinstall

linux-image-3.2.0-44-generic                     deinstall

linux-image-3.2.0-45-generic                     deinstall

linux-image-3.2.0-48-generic                     install

linux-image-3.2.0-51-generic                     install

linux-image-server                              install

devel@devDBServer:~$

deinstall表示已经卸载。

再查看一下现在的分区使用情况:

devel@devDBServer:~$ df -lh

Filesystem                    Size  Used Avail Use% Mounted on

/dev/mapper/devDBServer-root   94G 1.4G   88G   2% /

udev                          1.9G  4.0K 1.9G   1% /dev

tmpfs                         776M  324K 776M   1% /run

none                          5.0M     0 5.0M   0% /run/lock

none                          1.9G  4.0K 1.9G   1% /run/shm

/dev/sda1                    458M   54M  380M 13% /boot

/dev/mapper/devDBServer-home   46G 181M   44G   1% /home

/dev/mapper/devDBServer-opt   178G 467M  168G   1% /opt

/dev/mapper/devDBServer-var    46G 2.1G   42G   5% /var

/home/devel/.Private           46G 181M   44G   1% /home/devel

devel@devDBServer:~$

呵呵,顿时感觉系统轻松多了。

三、接着整开发服务器~

按照上边的经验,查看所有内核:

devel@devServer:~$ dpkg --get-selections|grep linux-image

linux-image-3.5.0-17-generic                     install

linux-image-3.5.0-21-generic                     install

linux-image-3.5.0-22-generic                     install

linux-image-3.5.0-23-generic                     install

linux-image-3.5.0-25-generic                     install

linux-image-3.5.0-26-generic                     install

linux-image-3.5.0-27-generic                     install

linux-image-3.5.0-28-generic                     install

linux-image-3.5.0-30-generic                     install

linux-image-extra-3.5.0-17-generic                   install

linux-image-extra-3.5.0-21-generic                   install

linux-image-extra-3.5.0-22-generic                   install

linux-image-extra-3.5.0-23-generic                   install

linux-image-extra-3.5.0-25-generic                   install

linux-image-extra-3.5.0-26-generic                   install

linux-image-extra-3.5.0-27-generic                   install

linux-image-extra-3.5.0-28-generic                   install

linux-image-extra-3.5.0-30-generic                   install

linux-image-extra-3.5.0-31-generic                   install

linux-image-generic                                      install

devel@devServer:~$

这个版本有变化啊,

系统当前内核是25那个,然后试着卸载第一个linux-image-3.5.0-17-generic,结果报错,然后以为是要先卸载那个extra的,结果还是报错,然后挨个试,结果都是报错:

devel@devServer:~$ sudo apt-get autoremovelinux-image-3.5.0-17-generic

[sudo] password for devel:

Reading package lists... Done

Building dependency tree      

Reading state information... Done

You might want to run 'apt-get -f install'to correct these:

The following packages have unmetdependencies:

linux-image-extra-3.5.0-17-generic : Depends:linux-image-3.5.0-17-generic but it is not going to be installed

linux-image-extra-3.5.0-31-generic : Depends:linux-image-3.5.0-31-generic but it is not going to be installed

linux-image-extra-3.5.0-37-generic : Depends:linux-image-3.5.0-37-generic but it is not going to be installed

linux-image-generic : Depends:linux-image-3.5.0-31-generic but it is not going to be installed

E: Unmet dependencies. Try 'apt-get -finstall' with no packages (or specify a solution).

devel@devServer:~$

怪不得之前查看所有内核时,31那个只有extra的一个,其他都两个。

根据提示,运行sudo apt-get -f install试试吧,这回开始下载东西了,又更新了3137的内核,

然后运行到最后又报错了:

Unpacking linux-image-3.5.0-31-generic(from .../linux-image-3.5.0-31-generic_3.5.0-31.52_amd64.deb) ...

Done.

dpkg: error processing/var/cache/apt/archives/linux-image-3.5.0-31-generic_3.5.0-31.52_amd64.deb(--unpack):

cannot copy extracted data for'./boot/vmlinuz-3.5.0-31-generic' to '/boot/vmlinuz-3.5.0-31-generic.dpkg-new':failed to write (No space left on device)

No apport report written because the errormessage indicates a disk full error

                                                                             dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)

Examining /etc/kernel/postrm.d .

run-parts: executing/etc/kernel/postrm.d/initramfs-tools 3.5.0-31-generic/boot/vmlinuz-3.5.0-31-generic

run-parts: executing/etc/kernel/postrm.d/zz-update-grub 3.5.0-31-generic/boot/vmlinuz-3.5.0-31-generic

Unpacking linux-image-3.5.0-37-generic(from .../linux-image-3.5.0-37-generic_3.5.0-37.58_amd64.deb) ...

Done.

dpkg: error processing /var/cache/apt/archives/linux-image-3.5.0-37-generic_3.5.0-37.58_amd64.deb(--unpack):

cannot copy extracted data for'./boot/System.map-3.5.0-37-generic' to'/boot/System.map-3.5.0-37-generic.dpkg-new': failed to write (No space left ondevice)

No apport report written because the errormessage indicates a disk full error

                                                                             dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)

Examining /etc/kernel/postrm.d .

run-parts: executing /etc/kernel/postrm.d/initramfs-tools3.5.0-37-generic /boot/vmlinuz-3.5.0-37-generic

run-parts: executing/etc/kernel/postrm.d/zz-update-grub 3.5.0-37-generic/boot/vmlinuz-3.5.0-37-generic

Errors were encountered while processing:

/var/cache/apt/archives/linux-image-3.5.0-31-generic_3.5.0-31.52_amd64.deb

/var/cache/apt/archives/linux-image-3.5.0-37-generic_3.5.0-37.58_amd64.deb

E: Sub-process /usr/bin/dpkg returned anerror code (1)

devel@devServer:~$

好吧,根据错误提示信息,这回明白了,这不是陷入死循环了吗,想卸载,没解决完依赖关系;想更新,没有足够的空间,卧槽。

又查了下资料,未果。
只有自己尝试了,既然那些是旧的内核,而且当前系统启动又没有用到,先搬出去应该没问题,腾出空间来先更新完再说,
OK,试试,新建备份文件夹:

devel@devServer:~$ sudo mkdir /opt/kernel

[sudo] password for devel:

devel@devServer:~$

挑选/boot下体积较大的几个文件备份出来,其中,以vmlinuzinitrd.img开头的文件较大,然后将21~27的内核中以这两个开头的全部备份至刚才新建的文件夹,最后直接删除(sudo rm –f),再查看一下,这回腾出好几十M的空间了,然后再执行之前的那个命令(sudo apt-get -f install),这回顺利更新完毕!然后再去尝试卸载那些旧的内核,所有的都能正常卸载了!

看看最后的效果:

System load:  0.12               Processes:           130

Usage of /:   7.9% of452.47GB   Users logged in:     1

Memory usage: 18%               IP address for p5p1: 172.16.5.2

Swap usage:  0%

devel@devServer:~$ df -lh

Filesystem                  Size  Used Avail Use% Mounted on

/dev/mapper/devServer-root  453G  36G  394G   9% /

udev                        1.9G   12K 1.9G   1% /dev

tmpfs                       776M  1.6M 775M   1% /run

none                        5.0M     0 5.0M   0% /run/lock

none                        1.9G     0 1.9G   0% /run/shm

none                        100M     0 100M   0% /run/user

/dev/sda2                   229M   54M 164M  25% /boot

/dev/sda1                   190M 126K  190M   1% /boot/efi

devel@devServer:~$

注意,之前备份出去的那些文件可以不用再挪回来,但是还要按照正常方式运行卸载命令去卸载,我们对比之前的分区使用情况发现,不只是boot分区空间变大了,根分区也变大了,所以不能简单的将boot分区的文件直接删除,应该按照正常方式卸载。
再次提醒大家,分区的时候记得一定要手动分区,在这个GB都快要过时的年代,那么吝惜磁盘干什么,合理预留一定空间一定会带来很多方便的 ,其实生活中也是这样,有时候该精确,有时候该宽裕。

 类似资料: