34

I know this question is asked many times before, however with me it is just a bit different I guess.

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       224G  5.2G  208G   3% /
udev            1.9G  4.0K  1.9G   1% /dev
tmpfs           777M  260K  777M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.9G     0  1.9G   0% /run/shm
/dev/sda2        90M   88M     0 100% /boot
/dev/sda6       1.9G  514M  1.3G  29% /tmp

My boot partition is full.

Current Kernel:

# uname -r
3.2.0-35-generic

All Kernels:

# dpkg --list | grep linux-image
ii  linux-image-3.2.0-32-generic    3.2.0-32.51                         Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-34-generic    3.2.0-34.53                         Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii  linux-image-3.2.0-35-generic    3.2.0-35.55                         Linux kernel image for version 3.2.0 on 64 bit x86 SMP
iF  linux-image-3.2.0-37-generic    3.2.0-37.58                         Linux kernel image for version 3.2.0 on 64 bit x86 SMP
iF  linux-image-3.2.0-38-generic    3.2.0-38.60                         Linux kernel image for version 3.2.0 on 64 bit x86 SMP
iU  linux-image-generic             3.2.0.37.45                         Generic Linux kernel image

So I thought of removing the 3.2.0.32-generic kernel with:

# sudo apt-get purge linux-image-3.2.0-32-generic
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 unmet dependencies:
 linux-generic : Depends: linux-headers-generic (= 3.2.0.37.45) but 3.2.0.38.46 is to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

No success.

When I try apt-get -f install it also fails:

# apt-get -f install
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-headers-3.2.0-34 linux-headers-3.2.0-35 linux-headers-3.2.0-34-generic linux-headers-3.2.0-35-generic
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  linux-generic linux-image-generic
The following packages will be upgraded:
  linux-generic linux-image-generic
2 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
5 not fully installed or removed.
Need to get 0 B/4,334 B of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue [Y/n]? y
Setting up initramfs-tools (0.99ubuntu13.1) ...
update-initramfs: deferring update (trigger activated)
Setting up linux-image-3.2.0-37-generic (3.2.0-37.58) ...
Running depmod.
update-initramfs: deferring update (hook will be called later)
The link /initrd.img is a dangling linkto /boot/initrd.img-3.2.0-38-generic
Examining /etc/kernel/postinst.d.
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 3.2.0-37-generic /boot/vmlinuz-3.2.0-37-generic
update-initramfs: Generating /boot/initrd.img-3.2.0-37-generic

gzip: stdout: No space left on device
E: mkinitramfs failure cpio 141 gzip 1
update-initramfs: failed for /boot/initrd.img-3.2.0-37-generic with 1.
run-parts: /etc/kernel/postinst.d/initramfs-tools exited with return code 1
Failed to process /etc/kernel/postinst.d at /var/lib/dpkg/info/linux-image-3.2.0-37-generic.postinst line 1010.
dpkg: error processing linux-image-3.2.0-37-generic (--configure):
 subprocess installed post-installation script returned error exit status 2
Setting up linux-image-3.2.0-38-generic (3.2.0-38.60) ...
Running depmod.
update-initramfs: deferring update (hook will be called later)
The link /initrd.img is a dangling linkto /boot/initrd.img-3.2.0-37-generic
Examining /etc/kernel/postinst.d.
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 3.2.0-38-generic /boot/vmlinuz-3.2.0-38-generic
update-initramfs: Generating /boot/initrd.img-3.2.0-38-generic

gzip: stdout: No space left on device
E: mkinitramfs failure cpio 141 gzip 1
update-initramfs: failed for /boot/initrd.img-3.2.0-38-generic with 1.
run-parts: /etc/kernel/postinst.d/initramfs-tools exited with return code 1
Failed to process /etc/kernel/postinst.d at /var/lib/dpkg/info/linux-image-3.2.0-38-generic.postinst line 1010.
dpkg: error processing linux-image-3.2.0-38-generic (--configure):
 subprocess installed post-installation script returned error exit status 2
dpkg: dependency problems prevent configuration of linux-image-generic:
 linux-image-generic depends on linux-image-3.2.0-37-generic; however:
  Package linux-image-3.2.0-37-generic is not configured yet.
dpkg: error processing linux-image-generic (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of linux-generic:
 linux-generic depends on linux-image-generic (= 3.2.0.37.45); however:
  Package linux-image-generic is not configured yet.
 linux-generic depends on linux-headers-generic (= 3.2.0.37.45); however:
  Version of linux-headers-generic on system is 3.2.0.38.46.
dpkg: error processing linux-generic (--configure):
 dependency problems - leaving unconfigured
Processing triggers for initramfs-tools ...
No apport report written because the error message indicates its a followup error from a previous failure.
No apport report written because MaxReports is reached already
  update-initramfs: Generating /boot/initrd.img-3.2.0-35-generic

gzip: stdout: No space left on device
E: mkinitramfs failure cpio 141 gzip 1
update-initramfs: failed for /boot/initrd.img-3.2.0-35-generic with 1.
dpkg: error processing initramfs-tools (--configure):
 subprocess installed post-installation script returned error exit status 1
No apport report written because MaxReports is reached already
Errors were encountered while processing:
 linux-image-3.2.0-37-generic
 linux-image-3.2.0-38-generic
 linux-image-generic
 linux-generic
 initramfs-tools
E: Sub-process /usr/bin/dpkg returned an error code (1)

Any help would really be appreciated.

Update: I did:

sudo rm /boot/*-3.2.0-32-generic /boot/*-3.2.0-34-generic

After that the following problem with apt-get -f install:

root@localhost:/# apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following extra packages will be installed:
  linux-generic
The following packages will be upgraded:
  linux-generic
1 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
1 not fully installed or removed.
Need to get 0 B/1,722 B of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue [Y/n]? y
dpkg: dependency problems prevent configuration of linux-generic:
 linux-generic depends on linux-image-generic (= 3.2.0.37.45); however:
  Version of linux-image-generic on system is 3.2.0.38.46.
 linux-generic depends on linux-headers-generic (= 3.2.0.37.45); however:
  Version of linux-headers-generic on system is 3.2.0.38.46.
dpkg: error processing linux-generic (--configure):
 dependency problems - leaving unconfigured
No apport report written because the error message indicates its a followup error     from a previous failure.
                      Errors were encountered while processing:
 linux-generic
E: Sub-process /usr/bin/dpkg returned an error code (1)
2

7 Answers 7

24

You cannot use apt-get when your kernel is outdated.

cd /boot
sudo rm *-2.6.35-6.9-generic
sudo rm *-3.16.0-23-generic

use ls to see what is in the folder remove the oldest one or two then

sudo apt-get -f install

Then you cen either apt-get purge as in

sudo apt-get purge linux-image-3.2.0-32-generic linux-image-3.2.0-34-generic

or just continue with rm.

you should also sudo update-grub when you are finsihed

2
  • The install command reinstalled many old kernels, resulting in a full boot disk. The purge command solved this for me.
    – SPRBRN
    Feb 10, 2017 at 9:25
  • I followed this answer up to sudo apt-get -f install, then restarted to make sure all is good and removed the rest with apt autoremove as suggested in previous commands output.
    – Bart C
    Sep 20, 2017 at 9:44
10

Apparently, this works:

sudo apt-get autoremove 

According to apt-get man-page:

autoremove is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed.

3.16.0-39 is the one I tried to install, but failed because /boot was full. 3.16.0-38 is my current version. You can see that -38 was skipped.

server:~$ uname -r
3.16.0-38-generic
server:~$ sudo apt-get autoremove 
[sudo] password for mikeb: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-3.16.0-30 linux-headers-3.16.0-30-generic
  linux-headers-3.16.0-31 linux-headers-3.16.0-31-generic
  linux-headers-3.16.0-33 linux-headers-3.16.0-33-generic
  linux-headers-3.16.0-34 linux-headers-3.16.0-34-generic
  linux-headers-3.16.0-36 linux-headers-3.16.0-36-generic
  linux-headers-3.16.0-39 linux-headers-3.16.0-39-generic
  linux-image-3.16.0-30-generic linux-image-3.16.0-31-generic
  linux-image-3.16.0-33-generic linux-image-3.16.0-34-generic
  linux-image-3.16.0-36-generic linux-image-3.16.0-39-generic
  linux-image-extra-3.16.0-30-generic linux-image-extra-3.16.0-31-generic
  linux-image-extra-3.16.0-33-generic linux-image-extra-3.16.0-34-generic
  linux-image-extra-3.16.0-36-generic linux-image-extra-3.16.0-39-generic
0 upgraded, 0 newly installed, 24 to remove and 0 not upgraded.
3
  • I like this answer but it misses some manual to know exactly what is autoremove for.
    – s3m3n
    Oct 8, 2015 at 13:44
  • 1
    autoremove only works if any previously-queued apt actions properly complete. Autoremove won't work if you are already getting a 'no space left on device' error - those previously queued actions will fail, and autoremove won't run. In other words, autoremove works...until it doesn't.
    – user535733
    Dec 31, 2015 at 17:59
  • @user535733 you are correct. See my answer for when autoremove doesn't work due to lack of space.
    – null
    Aug 8, 2019 at 12:43
8

In a terminal, execute the following command:

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

This command lists all currently installed Linux kernels, then selects all of the ones that are currently not used and removes them.

5
  • 2
    Perhaps you would like to explain what this command does exactly. It looks rather complicated: in general, people should be advised away from running commands like this they don't understand - especially with something vital to their system.
    – Marcus
    Jan 7, 2014 at 1:22
  • @mtdevans: I have added that the above command lists all currently installed Linux kernels, then selects all of the ones that are currently not used, and then removes all the kernels that are currently not used.
    – Kristian
    Nov 5, 2014 at 16:05
  • 4
    The command is useful, just run the part before "| xargs" first to check what you are going to delete. Also, | grep -v may be needed to preserve the latest kernel, which may not be in use yet, but wanted.
    – Roman Susi
    May 5, 2015 at 6:10
  • 4
    this is dangerous as it also matches linux-libc-dev:amd64 Aug 31, 2015 at 11:07
  • Since the command is complicated, I run this command with the purge and therefore it listed all the matched packages. Then I carefully chose which ones I want to purge and purged them one by one.
    – scarface
    Jan 24, 2022 at 13:43
2
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

If running the above command without the "sudo apt-get -y purge" matches your linux-libc-dev:amd64 you can add a semicolon to the regex to not include it:

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

Now just make sure it looks good and add the "sudo apt-get -y purge"

1

For Ubuntu 12.04 try this sudo apt-get purge linux-image-[kernel version]

This will delete any kernel version. If your boot folder has many kernels don't forget updating grub and rebooting your system

ex. sudo apt-get purge linux-image-3.11.6*

2
  • 2
    The problem is that this doesn't work anymore when one has a point where there are dependency problems, like the one ahead. Oct 8, 2013 at 11:00
  • This works for me!
    – Daniel
    Oct 16, 2013 at 13:52
0

Many answers say to use "apt autoremove" but that may not work when /boot is full because apt tries to add kernels before removing. Next apt autoremove won't remove any kernels mentioned in /etc/apt/apt.conf.d/01autoremove-kernels so we need to fix that. Here is the process that worked for me.

Remove unwanted kernel config files.

sudo dpkg --purge $(COLUMNS=200 dpkg -l | grep "linux-image-extra" | grep "^rc" | tr -s ' ' | cut -d ' ' -f 2)

Update /etc/apt/apt.conf.d/01autoremove-kernels by running ...

sudo /etc/kernel/postinst.d/apt-auto-removal

Hopefully apt autoremove will work.

sudo apt autoremove --purge

If not you should be able to manually remove very old and unused kernels from /boot.

First find out what kernel you are running.

uname -r 

Don't remove you current kernel, or previous kernel.

sudo rm /boot/initrd.img-V.XX.Y-ZZ-generic

Now you should have enough room to try the apt autoremove again. If not, remove another very old kernel. Of course, keep current and next oldest.

-2

I hope this can help:

i usually delete old kernels using ubuntu-tweak janitor, nice GUI and easy :)

click on 'system' and there will be list of old kernels, just click it and clean

2
  • 1
    it's an ubuntu server and i am connected through putty. (But i also have the availability to Webmin on the server)
    – Marcel
    Mar 3, 2013 at 13:50
  • How can I install this ubuntu-tweak janitor please? Dec 22, 2016 at 16:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .