2

How can I clean /boot or what can I remove considering it's almost full and I keep getting a message that says there is only 5.1 MB left. Because if I go to disk usage analyzer, like it says to there is no open to clean.

Here are the content of the partition:

username@hostname:~$ ls -l /boot/
total 423041
-rw-r--r-- 1 root root  1443719 Aug  9 07:08 abi-4.10.0-32-generic
-rw-r--r-- 1 root root  1443962 Aug 11 13:59 abi-4.10.0-33-generic
-rw-r--r-- 1 root root  1443962 Sep 13 07:16 abi-4.10.0-35-generic
-rw-r--r-- 1 root root  1407843 Feb  5  2017 abi-4.8.0-36-generic
-rw-r--r-- 1 root root  1408375 Mar 31 14:01 abi-4.8.0-46-generic
-rw-r--r-- 1 root root  1408671 Apr 20 09:07 abi-4.8.0-49-generic
-rw-r--r-- 1 root root  1408813 Jun 14 11:17 abi-4.8.0-56-generic
-rw-r--r-- 1 root root  1408813 Jun 26 17:31 abi-4.8.0-58-generic
-rw-r--r-- 1 root root   204970 Aug  9 07:08 config-4.10.0-32-generic
-rw-r--r-- 1 root root   204970 Aug 11 13:59 config-4.10.0-33-generic
-rw-r--r-- 1 root root   204970 Sep 13 07:16 config-4.10.0-35-generic
-rw-r--r-- 1 root root   199575 Feb  5  2017 config-4.8.0-36-generic
-rw-r--r-- 1 root root   199564 Mar 31 14:01 config-4.8.0-46-generic
-rw-r--r-- 1 root root   199588 Apr 20 09:07 config-4.8.0-49-generic
-rw-r--r-- 1 root root   199564 Jun 14 11:17 config-4.8.0-56-generic
-rw-r--r-- 1 root root   199564 Jun 26 17:31 config-4.8.0-58-generic
drwxr-xr-x 3 root root     1024 Apr  9 12:00 efi
drwxr-xr-x 5 root root     1024 Sep 20 05:03 grub
-rw-r--r-- 1 root root 42058341 Aug 28 10:57 initrd.img-4.10.0-32-generic
-rw-r--r-- 1 root root 42056847 Aug 29 02:58 initrd.img-4.10.0-33-generic
-rw-r--r-- 1 root root 42056820 Sep 20 05:03 initrd.img-4.10.0-35-generic
-rw-r--r-- 1 root root 40647942 Aug 28 10:56 initrd.img-4.8.0-36-generic
-rw-r--r-- 1 root root 40638652 Aug 28 10:55 initrd.img-4.8.0-46-generic
-rw-r--r-- 1 root root 40637975 Aug 28 10:55 initrd.img-4.8.0-49-generic
-rw-r--r-- 1 root root 40644972 Aug 28 10:55 initrd.img-4.8.0-56-generic
-rw-r--r-- 1 root root 40643997 Aug 28 10:55 initrd.img-4.8.0-58-generic
drwx------ 2 root root    12288 Apr  9 12:00 lost+found
-rw-r--r-- 1 root root   182704 Jan 28  2016 memtest86+.bin
-rw-r--r-- 1 root root   184380 Jan 28  2016 memtest86+.elf
-rw-r--r-- 1 root root   184840 Jan 28  2016 memtest86+_multiboot.bin
-rw------- 1 root root  3718993 Aug  9 07:08 System.map-4.10.0-32-generic
-rw------- 1 root root  3721812 Aug 11 13:59 System.map-4.10.0-33-generic
-rw------- 1 root root  3721891 Sep 13 07:16 System.map-4.10.0-35-generic
-rw------- 1 root root  4060748 Feb  5  2017 System.map-4.8.0-36-generic
-rw------- 1 root root  4066103 Mar 31 14:01 System.map-4.8.0-46-generic
-rw------- 1 root root  4066773 Apr 20 09:07 System.map-4.8.0-49-generic
-rw------- 1 root root  4068016 Jun 14 11:17 System.map-4.8.0-56-generic
-rw------- 1 root root  4068043 Jun 26 17:32 System.map-4.8.0-58-generic
-rw------- 1 root root  7398816 Aug  9 07:08 vmlinuz-4.10.0-32-generic
-rw------- 1 root root  7404416 Aug 11 13:59 vmlinuz-4.10.0-33-generic
-rw------- 1 root root  7405152 Sep 13 07:16 vmlinuz-4.10.0-35-generic
-rw-r--r-- 1 root root  7297312 Apr  9 12:08 vmlinuz-4.8.0-36-generic
-rw------- 1 root root  7305856 Mar 31 14:01 vmlinuz-4.8.0-46-generic
-rw------- 1 root root  7305856 Apr 20 09:07 vmlinuz-4.8.0-49-generic
-rw------- 1 root root  7309536 Jun 14 11:17 vmlinuz-4.8.0-56-generic
-rw------- 1 root root  7310848 Jun 26 17:31 vmlinuz-4.8.0-58-generic
username@hostname:~$ 
1
  • Just do sudo apt autoremove
    – user692175
    Sep 22 '17 at 4:57
1

Here is a script that does it automatically.

The easiest way to clear up space in /boot is to get rid of old and unused kernels. I't important when deleting kernels to never delete the kernel you are currently using (check with uname -r) and you don't want to delete the newest kernel otherwise you will download it again next time you upgrade.

Each kernel and the disk resources related to it takes up about 50MB of space. You also want to delete kernels using only apt purge because it removes dependencies and updates grub at the end. You can see a list of installed kernels using dpkg -l | grep linux-image.

Autoremove does not always get all the old kernels.

#!/bin/bash

# create the clean-boot command line utility, if it doesn't exist
if [ ! -f /usr/sbin/clean-boot ]; then
    cp "$0" /usr/sbin/clean-boot
    chown root.root /usr/sbin/clean-boot
    chmod 0770 /usr/sbin/clean-boot
fi

# add clean-boot cron job to root's crontab, if it doesn't exist
if [[  `crontab -l | grep "/usr/sbin/clean-boot" | wc -l` -eq 0 ]]; then    
    (crontab -l 2>/dev/null; echo "0 10 * * 3,4 /usr/sbin/clean-boot") | crontab -
fi


# array of kernels installed on the system
kernels=($(dpkg --list | grep 'linux-image-[0-9]' | awk '{ print $2 }'))

# kernel currently being used by the system 
current_kernel=linux-image-`uname -r`

# newest kernel installed (currently being initialized to current_kernel)
newest_kernel=$current_kernel


# for loop below used to find the newest insitalled kernel
for i in "${kernels[@]}"
do

    # since newest is initialized to current, there is no need to parse through kernels same version as the one currently loaded
    if [[ $i != $current_kernel*  ]]; then

        # each iteration of the for loop is for each version number (kernel version, major, minor, incidental numbers) example 4.8.0-56-generic
        for counter in 1 2 3 4
        do

            # if statement extracts the version numbers
            if [[ $counter -lt 3 ]]; then

                compare_number=$(echo ${i#"linux-image-"} | cut -d'.' -f$counter)
                newest_number=$(echo  ${newest_kernel#"linux-image-"} | cut -d'.' -f$counter)

            elif [[ $counter -eq 3 ]]; then

                compare_number=$(echo ${i#"linux-image-"} | cut -d'.' -f$counter)
                newest_number=$(echo  ${newest_kernel#"linux-image-"} | cut -d'.' -f$counter)

                compare_number=$(echo ${compare_number} | cut -d'-' -f1)
                newest_number=$(echo  ${newest_number} | cut -d'-' -f1)             

            else

                compare_number=$(echo ${i#"linux-image-"} | cut -d'-' -f2)
                newest_number=$(echo  ${newest_kernel#"linux-image-"} | cut -d'-' -f2)

            fi

            # this if statement does the comparison
            if [[ $compare_number -eq $newest_number ]]; then
                continue
            elif [[ $compare_number -lt $newest_number ]]; then
                break
            else
                newest_kernel=$i
                break
            fi

        done

    fi

done


# for every kernel
for i in "${kernels[@]}"
do

    # delete the kernel if the kernel is not the one in use and is not the newest one installed
    if [[ $i != $current_kernel* && $i != $newest_kernel* ]]; then

        apt-get purge "$i" -y

    fi 

done
3
  • I tried it as follows but it said the following: username@hostname:~$ sudo /home/username/cleanboot.bash no crontab for root /home/username/cleanboot.bash: line 81: syntax error: unexpected end of file username@hostname:~$ Sep 25 '17 at 23:29
  • Nevermind for now because it appears to be working. Sep 25 '17 at 23:34
  • I think this is causing issues with the copy command. Sep 28 '17 at 1:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.