Purge Old Linux Headers/Images with aptitude

If there is limited space on /boot, this becomes a necessity. Identify what packages are installed and what is current with the following commands:

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

Sample output

ii  linux-image-3.2.0-38-generic     3.2.0-38.61                         Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-39-generic     3.2.0-39.62                         Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-40-generic     3.2.0-40.64                         Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-43-generic     3.2.0-43.68                         Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-server               3.2.0.43.51                         Linux kernel image on Server Equipment.


Linux ub4 3.2.0-43-generic #68-Ubuntu SMP Wed May 15 03:33:33 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

Considering that 3.2.0-43 is installed we can remove 3.2.0-38,39,40. To remove 40 (run one at a time):

PACKAGE="linux-image-3.2.0-40-generic"
aptitude search $PACKAGE
aptitude remove $PACKAGE
aptitude -y purge $PACKAGE

Outputs

# aptitude search $PACKAGE
i A linux-image-3.2.0-40-generic                        - Linux kernel image for version 3.2.0 on 64 bit x86 SMP
p   linux-image-3.2.0-40-generic:i386                   - Linux kernel image for version 3.2.0 on 32 bit x86 SMP
p   linux-image-3.2.0-40-generic-pae:i386               - Linux kernel image for version 3.2.0 on 32 bit x86 SMP
# aptitude search $PACKAGE
i A linux-image-3.2.0-40-generic                        - Linux kernel image for version 3.2.0 on 64 bit x86 SMP
p   linux-image-3.2.0-40-generic:i386                   - Linux kernel image for version 3.2.0 on 32 bit x86 SMP
p   linux-image-3.2.0-40-generic-pae:i386               - Linux kernel image for version 3.2.0 on 32 bit x86 SMP
[07:49:46 AM][root@ub4]:/etc/cron.d[0]# aptitude remove $PACKAGE
The following packages will be REMOVED:
  linux-image-3.2.0-40-generic
0 packages upgraded, 0 newly installed, 1 to remove and 78 not upgraded.
Need to get 0 B of archives. After unpacking 150 MB will be freed.
(Reading database ... 111084 files and directories currently installed.)
Removing linux-image-3.2.0-40-generic ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.2.0-40-generic /boot/vmlinuz-3.2.0-40-generic
update-initramfs: Deleting /boot/initrd.img-3.2.0-40-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.2.0-40-generic /boot/vmlinuz-3.2.0-40-generic
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-3.2.0-57-generic
Found initrd image: /boot/initrd.img-3.2.0-57-generic
Found linux image: /boot/vmlinuz-3.2.0-54-generic
Found initrd image: /boot/initrd.img-3.2.0-54-generic
Found linux image: /boot/vmlinuz-3.2.0-52-generic
Found initrd image: /boot/initrd.img-3.2.0-52-generic
Found memtest86+ image: /memtest86+.bin
done
# aptitude -y purge $PACKAGE
The following packages will be REMOVED:
  linux-image-3.2.0-40-generic{p}
0 packages upgraded, 0 newly installed, 1 to remove and 78 not upgraded.
Need to get 0 B of archives. After unpacking 0 B will be used.
(Reading database ... 106946 files and directories currently installed.)
Removing linux-image-3.2.0-40-generic ...
Purging configuration files for linux-image-3.2.0-40-generic ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.2.0-40-generic /boot/vmlinuz-3.2.0-40-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.2.0-40-generic /boot/vmlinuz-3.2.0-40-generic

Repeat for 39 and 38 as well. I usually leave a few versions behind and not remove all of them.

Update to Latest Kernel

aptitude update
aptitude install linux-headers-generic

Script

Create the list of existing images

dpkg -l linux-image-\* | grep ^ii | awk '{print "/tmp/pimage " $2}' > /tmp/pimage_list
chmod 755 /tmp/pimage_list

DELETE the last few lines before running to avoid deleting recent/current image!!!

Create file /tmp/pimage as:

#!/bin/bash
#
PACKAGE=$1
aptitude -y remove $PACKAGE
aptitude -y purge $PACKAGE
#
exit

Change permissions on /tmp/pimage

chmod 755 /tmp/pimage

References


QR Code
QR Code tech:linux:purge_linux_images (generated for current page)