Clean up that full ubuntu /boot drive

Without any kind of monitoring or alerting enabled, you may run into your /boot drive becoming full, and resulting in errors such as:

root@trex:/boot# apt-get install -f
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following packages were automatically installed and are no longer required:
  linux-image-extra-4.4.0-92-generic linux-image-extra-4.4.0-93-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  linux-image-4.4.0-98-generic
Suggested packages:
  fdutils linux-doc-4.4.0 | linux-source-4.4.0 linux-tools
The following NEW packages will be installed:
  linux-image-4.4.0-98-generic
0 upgraded, 1 newly installed, 0 to remove and 301 not upgraded.
7 not fully installed or removed.
Need to get 0 B/21.9 MB of archives.
After this operation, 67.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
(Reading database ... 691458 files and directories currently installed.)
Preparing to unpack .../linux-image-4.4.0-98-generic_4.4.0-98.121_amd64.deb ...
Done.
Unpacking linux-image-4.4.0-98-generic (4.4.0-98.121) ...
dpkg: error processing archive /var/cache/apt/archives/linux-image-4.4.0-98-generic_4.4.0-98.121_amd64.deb (--unpack):
 cannot copy extracted data for './boot/abi-4.4.0-98-generic' to '/boot/abi-4.4.0-98-generic.dpkg-new': failed to write (No space left on device)
No apport report written because the error message 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 4.4.0-98-generic /boot/vmlinuz-4.4.0-98-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 4.4.0-98-generic /boot/vmlinuz-4.4.0-98-generic
Errors were encountered while processing:
 /var/cache/apt/archives/linux-image-4.4.0-98-generic_4.4.0-98.121_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
root@trex:/boot# df -h
Filesystem                      Size  Used Avail Use% Mounted on
udev                            7.8G     0  7.8G   0% /dev
tmpfs                           1.6G  9.2M  1.6G   1% /run
/dev/mapper/trex--vg-root       453G   32G  399G   8% /
tmpfs                           7.8G     0  7.8G   0% /dev/shm
tmpfs                           5.0M     0  5.0M   0% /run/lock
tmpfs                           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/nvme0n1p2                  473M  473M     0 100% /boot
/dev/nvme0n1p1                  511M  3.4M  508M   1% /boot/efi

When your disk is at 100% full, it’s difficult to take the safe route and remove old kernels with apt-get. So the blunt way of fixing this is with rm -f.

The following 1 liner can help achieve this.

for x in $(sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r` | grep -o -P '\d.\d.\d-\d\d' ); do sudo  rm -f *$x* ; done; sudo apt-get autoremove

This command was derived from this gist: https://gist.github.com/ipbastola/2760cfc28be62a5ee10036851c654600  that does a great job reviewing the different checks that should be done, and how to achieve the same manually.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: