Cleanup old Ubuntu packages after upgrade
- Published:
- categories: linux
- tags: ubuntu, upgrade, apt-get
During the process of doing an upgrade I got an error that I did not have enough room in my /boot
directory for the upgrade to go through. I had not cleaned up my old kernels for a bit, so I did a quick search to find an easier way to do this generically so I could run this occasionally. I run most of my machines headless so I am not looking at the various gui tools that can help here.
First helper here is from an askubuntu post, for how to cleanup old kernels:
sgwilbur@kablamo:~$ sudo apt-get remove --purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^
]*\).*/\1/;/[0-9]/!d')
[sudo] password for sgwilbur:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
linux-image-3.11.0-24-generic* linux-image-extra-3.11.0-24-generic*
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 185 MB disk space will be freed.
Do you want to continue? [Y/n] Y
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = "en_AU.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
(Reading database ... 170301 files and directories currently installed.)
Removing linux-image-extra-3.11.0-24-generic (3.11.0-24.41) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.11.0-24-generic /boot/vmlinuz-3.11.0-24-generic
update-initramfs: Deleting /boot/initrd.img-3.11.0-24-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.11.0-24-generic /boot/vmlinuz-3.11.0-24-generic
Generating grub configuration file ...
Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.
Found linux image: /boot/vmlinuz-3.13.0-30-generic
Found initrd image: /boot/initrd.img-3.13.0-30-generic
Found linux image: /boot/vmlinuz-3.11.0-24-generic
Found linux image: /boot/vmlinuz-3.1.10-1.29-desktop
Found initrd image: /boot/initrd-3.1.10-1.29-desktop
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
done
The link /initrd.img.old is a damaged link
Removing symbolic link initrd.img.old
you may need to re-run your boot loader[grub]
Purging configuration files for linux-image-extra-3.11.0-24-generic (3.11.0-24.41) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.11.0-24-generic /boot/vmlinuz-3.11.0-24-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.11.0-24-generic /boot/vmlinuz-3.11.0-24-generic
Removing linux-image-3.11.0-24-generic (3.11.0-24.41) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.11.0-24-generic /boot/vmlinuz-3.11.0-24-generic
update-initramfs: Deleting /boot/initrd.img-3.11.0-24-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.11.0-24-generic /boot/vmlinuz-3.11.0-24-generic
Generating grub configuration file ...
Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.
Found linux image: /boot/vmlinuz-3.13.0-30-generic
Found initrd image: /boot/initrd.img-3.13.0-30-generic
Found linux image: /boot/vmlinuz-3.1.10-1.29-desktop
Found initrd image: /boot/initrd-3.1.10-1.29-desktop
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
done
The link /vmlinuz.old is a damaged link
Removing symbolic link vmlinuz.old
you may need to re-run your boot loader[grub]
Purging configuration files for linux-image-3.11.0-24-generic (3.11.0-24.41) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.11.0-24-generic /boot/vmlinuz-3.11.0-24-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.11.0-24-generic /boot/vmlinuz-3.11.0-24-generic
sgwilbur@kablamo:~$
Some other basic ones to try that are non-destructive are the apt-get clean
and apt-get autoclean
, this will remove your local copies, I run a local apt-cacher-ng server so I don’t worry too much about having to download them again so for you keep this in mind as YMMV. Additionally you can run apt-get autoremove
to remove oboslete packages.
The other item is to remove left-over configurations from old packages dpkg -l | grep '^rc' | awk '{print $2}' | xargs dpkg --purge
this one if from the Ubuntu wiki and is very helpful especially after an upgrade or if you remove
packages often without using the purge
options. Below is the output from my system after doing a 12.04 to 13.10 to 14.04 upgrade on my
sgwilbur@kablamo:~$ dpkg -l | grep '^rc' | awk '{print $2}' | xargs sudo dpkg --purge
(Reading database ... 165512 files and directories currently installed.)
Removing appmenu-gtk:amd64 (12.10.2-0ubuntu1.3) ...
Purging configuration files for appmenu-gtk:amd64 (12.10.2-0ubuntu1.3) ...
Removing appmenu-gtk3:amd64 (12.10.2-0ubuntu1.3) ...
Purging configuration files for appmenu-gtk3:amd64 (12.10.2-0ubuntu1.3) ...
... < REMOVING LOTS OF LINES OF SIMILAR ENTRIES >
sgwilbur@kablamo:~$
Bringing it together as a simple helper you can run via script, but this is not really something you want to run via cron because it requires that you are ready to remove all kernels but the currently working one and can cause problems if you are not careful, for instance if you install a new kernel and run this before restarting.
#!/bin/bash
echo "apt-get: Run update"
sudo apt-get update
echo "apt-get: Run clean"
sudo apt-get clean
echo "apt-get: Run autoclean"
sudo apt-get autoclean
echo "apt-get: Run autoremove"
sudo apt-get autoremove
echo "clean configuration: Removing old configurations from packages that have been removed"
old_packages=`dpkg -l | grep '^rc' | awk '{print $2}'`
#echo ${old_packages}
if [ -n ${old_packages} ]
then
dpkg -l | grep '^rc' | awk '{print $2}' | xargs sudo dpkg --purge
fi
# Cleanup old kernels
echo "cleanup kernels: Remove old linux kernels that are installed."
sudo apt-get remove --purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')
Will be another tool I will put in my script toolbox and may be able to help you as well.
Reference:
- manpages.ubuntu.com - apt-get
- help.ubuntu.com Aptget/Howto
- aksubuntu.com - Delete old packages from local repository
- aksubuntu.com - How do I free up more space in /boot?
- aksubuntu.com - How do I remove or hide old kernel versions to clean up the boot menu?
- markmcb.com - Cleanup Unused Linux Kernels in Ubuntu