Announcement

Collapse
No announcement yet.

remove old Kernel in Kubuntu

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    remove old Kernel in Kubuntu

    Hello,
    Are the old kernels automatically removed in Kubuntu? If not, I found a script on the following page and wanted to ask if it was still up to date?

    https://bluelupo.info/post/enfernen-von-kerneln/

    Here is the script

    Code:
    #!/bin/bash
    #
    #######################
    #  Variablenbelegung
    #######################
    #
    TIME_STAMP=$(date '+%Y%m%d-%H%M%S')
    USER_TMP_DIR="/home/$USER/tmp"
    LOGFILE_PURGE="$USER_TMP_DIR/run_kernel_remover.log"
    LOGFILE_KERNEL_LIST="$USER_TMP_DIR/kernel_list.txt"
    KERNEL_ACTUAL=$(uname -r|awk -F"-" '{print $1"-"$2}')
    KERNEL_MAJOR_VERSION=$(uname -r|awk -F"." '{print $1"."$2}')
    #
    #
    ################
    #  Funktionen
    ################
    #
    #
    show_passive_popup_button_push()
    {
        # KDE Benachrichtigung anzeigen nach dem Bestätigungsdialog
        if [ "$1" = "0" ]
            then
            echo "[$(date '+%H:%M:%S')] DEL_DIALOG YES=$1" >> $LOGFILE_PURGE
        elif [ "$1" = "1" ]
            then
            echo "[$(date '+%H:%M:%S')] DEL_DIALOG NO=$1" >> $LOGFILE_PURGE
            kdialog --passivepopup "[Kernel Remover]\nKernel $KERNEL_SELECTION konnte nicht gelöscht werden, da Button NEIN gedrückt wurde!" 5
            close_progressbar
            del_log
            exit 3
        elif [ "$1" = "2" ]
            then
            echo "[$(date '+%H:%M:%S')] DEL_DIALOG CANCEL=$1" >> $LOGFILE_PURGE
            kdialog --passivepopup "[Kernel Remover]\nKernel $KERNEL_SELECTION konnte nicht gelöscht werden, da Button ABBRECHEN gedrückt wurde!" 5
            close_progressbar
            del_log
            exit 4
        fi
    }
    
    
    show_passive_popup_kernel_del()
    {
        # KDE Benachrichtigung anzeigen nach dem Löschen des Kernels
        if [ "$1" = "0" ]
            then
            echo "[$(date '+%H:%M:%S')] PURGE RC="$1 >> $LOGFILE_PURGE
            kdialog --passivepopup "[Kernel Remover]\nKernel $KERNEL_SELECTION konnte erfolgreich gelöscht werden" 5
        elif [ "$1" != "0" ]
            then
            echo "[$(date '+%H:%M:%S')] PURGE RC="$1 >> $LOGFILE_PURGE
            kdialog --passivepopup "[Kernel Remover]\nAchtung, Kernel $KERNEL_SELECTION konnte nicht gelöscht werden!" 5
        fi
    }
    
    
    show_passive_popup_pwd()
    {
        # KDE Benachrichtigung anzeigen nach dem Passwortdialog bei falschen Passwort oder drücken das Abbrechen-Buttons
        if [ "$PASSWD_DIALOG_CHECK" = "1" ]
            then
            kdialog --passivepopup "[Kernel Remover]\nAchtung, Abbruch da im Passwortdialog der Button ABBRECHEN gedrückt wurde!" 5
        elif [ "$PWD_CHECK" = "1" ]
            then
            kdialog --passivepopup "[Kernel Remover]\nAchtung, Abbruch da das Passwort (sudo) falsch eingegeben wurde!" 5
        fi
    }
    
    
    init_progressbar()
    {
        # initieren der Fortschrittsanzeige
        progress=$(kdialog --title "Kernel-Remover" --progressbar "Ausgewählte alte Kernelpakete löschen" 4)
        qdbus $progress showCancelButton false
    }
    
    
    run_progressbar()
    {
        # Fortschrittsanzeige aktualisieren bei jedem Schritt
        VALUE="$1"
        TEXT="$2"
        qdbus $progress Set "" value $VALUE > /dev/null
        qdbus $progress setLabelText "$VALUE. Schritt:   $TEXT" > /dev/null
    }
    
    
    close_progressbar()
    {
        # Fortschrittsanzeige schließen
        qdbus $progress close > /dev/null
    }
    
    
    print_timestamp()
    {
        # Timestamp (Start und Ende) ins Logfile schreiben
        TS_VALUE=$1
        if [ "$TS_VALUE" = "start" ]
            then
            echo "[$(date '+%H:%M:%S')] ----------------------------------------------- Start -----------------------------------------------" >> $LOGFILE_PURGE
        elif [ "$TS_VALUE" = "end" ]
            then
            echo "[$(date '+%H:%M:%S')] ----------------------------------------------- Ende ------------------------------------------------" >> $LOGFILE_PURGE
        fi
    }
    
    
    del_log()
    {
     # Löschen des Logfiles mit der Kernelliste
     if [ -e "$LOGFILE_KERNEL_LIST" ]
        then
            echo "[$(date '+%H:%M:%S')] DELETE $LOGFILE_KERNEL_LIST" >> $LOGFILE_PURGE
            rm "$LOGFILE_KERNEL_LIST"
        fi
        echo "[$(date '+%H:%M:%S')] cp $LOGFILE_PURGE $LOGFILE_PURGE.$TIME_STAMP" >> $LOGFILE_PURGE
        print_timestamp end
        # Kopie des aktuellen Logfiles erzeugen
        cp $LOGFILE_PURGE $LOGFILE_PURGE"."$TIME_STAMP
    }
    
    
    create_log()
    {
        # ggf. temporäres tmp Verzeichnis im $HOME erstellen
        if [ ! -d "$USER_TMP_DIR" ]
            then
            mkdir "$USER_TMP_DIR"
        fi
        # Altes Logfile löschen und neues erstellen
        if [ -e "$LOGFILE_PURGE" ]
            then
            rm $LOGFILE_PURGE
            touch $LOGFILE_PURGE
            touch $LOGFILE_KERNEL_LIST
            print_timestamp start
            echo "[$(date '+%H:%M:%S')] Logfile $LOGFILE_INSTALL von User $USER am $(date '+%d.%m.%Y') angelegt (1)" >> $LOGFILE_PURGE
        else
            touch $LOGFILE_PURGE
            touch $LOGFILE_KERNEL_LIST
            print_timestamp start
            echo "[$(date '+%H:%M:%S')] Logfile $LOGFILE_INSTALL von User $USER am $(date '+%d.%m.%Y') angelegt (2)" >> $LOGFILE_PURGE
        fi
    }
    
    ##################
    #  Hauptprogramm
    ##################
    #
    #
    # Logfile anlegen
    create_log
    
    # Aktuelle Kernel-Majorversion und Kernelversion ins Logfile schreiben
    echo "[$(date '+%H:%M:%S')] KERNEL_MAJOR_VERSION="$KERNEL_MAJOR_VERSION >> $LOGFILE_PURGE
    echo "[$(date '+%H:%M:%S')] KERNEL_ACTUAL="$KERNEL_ACTUAL >> $LOGFILE_PURGE
    
    # Alle installierten Kernel ermitteln und in Datei schreiben (LOGFILE_KERNEL_LIST)
    dpkg -l | egrep linux-image-[0-9] | awk -F'-' '{printf("%s%s%s\n", $3,"-",$4)}' >> $LOGFILE_KERNEL_LIST
    
    # Aktuell laufenden Kernel aus oben erzeugter Datei wieder entfernen
    sed -i '/'$KERNEL_ACTUAL'/d' $LOGFILE_KERNEL_LIST
    
    # löschbare Kernel ins Logfile schreiben
    echo "[$(date '+%H:%M:%S')] BEGIN DELETABLE KERNEL" >> $LOGFILE_PURGE
    cat $LOGFILE_KERNEL_LIST >> $LOGFILE_PURGE
    echo "[$(date '+%H:%M:%S')] END DELETABLE KERNEL" >> $LOGFILE_PURGE
    
    # Dialogbox anzeigen mit allen installierten Kernel außer dem aktuell laufenden
    KERNEL_SELECTION=$(kdialog --geometry 400x300 --title "Kernel-Remover" --menu "\nAktuell laufende und damit nicht entfernbare Kernelversion:<h3>$KERNEL_ACTUAL</h3>Bitte die Kernelversion für die Deinstallation auswählen:<br>" $(while read ROW; do MP=$ROW; echo $ROW; echo $MP; done < $LOGFILE_KERNEL_LIST) "9999")
    KERNEL_SELECTION_CHECK=$?
    
    # Auf Tastendrücke von Dialogbox KERNEL_SELECTION reagieren
    if [ "$KERNEL_SELECTION_CHECK" -eq 0 ]
        then
        echo "[$(date '+%H:%M:%S')] KERNEL_SELECTION_CHECK="$KERNEL_SELECTION >> $LOGFILE_PURGE
    elif [ "$KERNEL_SELECTION_CHECK" -eq 1 ]
        then
        echo "[$(date '+%H:%M:%S')] KERNEL_SELECTION_CHECK="$KERNEL_SELECTION >> $LOGFILE_PURGE
        del_log
        exit 1
    fi
    
    # Dialogbox zur Passwortabfrage, da sudo Rechte benötigt werden
    PASSWD_DIALOG=$(kdialog --title "Kernel-Remover (Passwort)" --password "Bitte das Passwort (sudo) eingeben")
    PASSWD_DIALOG_CHECK=$?
    
    # Funktion zum Initialisieren der Fortschrittsanzeige
    init_progressbar
    run_progressbar "1" "Vorbereitungen zum Löschen des Kernels"
    sleep 1
    
    # Test ob Passwort korrekt ist via simulierten "sudo -S apt purge"
    echo "$PASSWD_DIALOG" | sudo -S apt purge --simulate `apt-cache search 'linux-(image(-extra)?|hwe-'$KERNEL_MAJOR_VERSION'-headers|headers|modules|modules-extra)-'$KERNEL_SELECTION'(-generic){0,1}' | cut -d" " -f1 | awk '{printf("%s ",$0)}END {print "\n"}'`
    PWD_CHECK=$?
    
    # Auswertung der Tastendrücke des Passwortdialogs und des Passwortchecks
    if [ "$PASSWD_DIALOG_CHECK" = "0" -a "$PWD_CHECK" = "0" ]
        then
        echo "[$(date '+%H:%M:%S')] PASSWD_DIALOG RC="$PASSWD_DIALOG_CHECK >> $LOGFILE_PURGE
        echo "[$(date '+%H:%M:%S')] PWD_CHECK="$PWD_CHECK >> $LOGFILE_PURGE
    elif [ "$PASSWD_DIALOG_CHECK" = "1" -o "$PWD_CHECK" = "1" ]
        then
        echo "[$(date '+%H:%M:%S')] PASSWD_DIALOG RC="$RC >> $LOGFILE_PURGE
        echo "[$(date '+%H:%M:%S')] PWD_CHECK="$PWD_CHECK >> $LOGFILE_PURGE
        close_progressbar
        show_passive_popup_pwd
        del_log
        exit 2
    fi
    
    # Zu löschende Kernelpakete errmitteln für Bestätigungsdialog
    KERNEL_PACKAGES=$(dpkg -l | egrep 'linux-(image(-extra)?|hwe-'$KERNEL_MAJOR_VERSION'-headers|headers|modules|modules-extra)-'$KERNEL_SELECTION'(-generic){0,1}'| awk '{print $2}')
    
    # Dialogbox mit Bestätigungsdialog ob Kernel wirklich gelöscht werden soll
    DEL_DIALOG=$(kdialog --title "Kernel-Remover (Bestätigung)" --warningyesnocancel "Kernelversion $KERNEL_SELECTION mit den angegebenen Paketen wirklich löschen?\n\n$KERNEL_PACKAGES\n\nLogdatei unter $LOGFILE_PURGE\n\n")
    DEL_DIALOG_CHECK=$?
    
    # KDE Benachrichtigung anzeigen
    show_passive_popup_button_push $DEL_DIALOG_CHECK
    
    # Fortschrittsanzeige aktualisieren
    run_progressbar "2" "Ausgewählter Kernel wird jetzt gelöscht"
    
    # Löschen der Kernelpakete
    # Zum testen den Optionsparameter "--yes" (Pakete löschen ohne Nachfrage) von "apt purge" durch "--simulate" (Ausführung des Kommandos nur simulieren) ersetzen
    echo "$PASSWD_DIALOG" | sudo -S apt purge --yes `apt-cache search 'linux-(image(-extra)?|hwe-'$KERNEL_MAJOR_VERSION'-headers|headers|modules|modules-extra)-'$KERNEL_SELECTION'(-generic){0,1}' | cut -d" " -f1 | awk '{printf("%s ",$0)}END {print "\n"}'` >> $LOGFILE_PURGE
    PURGE=$?
    
    # Testsimmulation (ggf. aktivieren zu Testzwecken)
    #echo "$PASSWD_DIALOG" | sudo -S apt purge --simulate `apt-cache search 'linux-(image(-extra)?|hwe-'$KERNEL_MAJOR_VERSION'-headers|headers|modules|modules-extra)-'$KERNEL_SELECTION'(-generic){0,1}' | cut -d" " -f1 | awk '{printf("%s ",$0)}END {print "\n"}'` >> $LOGFILE_PURGE
    #PURGE=$?
    
    # Fortschrittsanzeige aktualisieren
    run_progressbar "3" "Temporäre Log-Dateien löschen"
    sleep 1
    
    # Anzeige der KDE Beanchrichtigung
    show_passive_popup_kernel_del $PURGE
    
    # zu Testzwecken auf der Kommandozeile nachfolgende Zeile entkommentieren (#-Zeichen entfernen), damit das Logfile sofort angezeigt wird
    #cat $LOGFILE_PURGE
    
    # Aufruf der Funktion zum Löschen des Logfiles
    del_log
    
    # Aufruf der Funktion zum Beenden der Fortschrittsanzeige
    close_progressbar
    #EOF
    ​

    #2
    Follow the instructions for adding the ucaresystem-core ppa here. It's a superior script. I have been using it for years.
    Windows no longer obstructs my view.
    Using Kubuntu Linux since March 23, 2007.
    "It is a capital mistake to theorize before one has data." - Sherlock Holmes

    Comment


      #3
      Thanks for the quick reply.
      I installed it and tried it out. Everything went through without errors. The other script still tells me that I have installed the old kernel 5.6.0.9.
      what is right now?​

      Is it possible that the PPA script still leaves an old kernel by default?
      Last edited by gorzka; Jan 09, 2024, 12:57 PM.

      Comment


        #4
        'Possible'. You might still need to remove the old kernel 'stuff' manually, but once that is done (if required), this script will keep your system nice and tidy.
        Windows no longer obstructs my view.
        Using Kubuntu Linux since March 23, 2007.
        "It is a capital mistake to theorize before one has data." - Sherlock Holmes

        Comment


          #5
          Old kernels and their associated packages (modules and headers) are normally removed, but in the past some slipped through the cracks, perhaps in release upgrades. The change from kernel versions 5.* to 6.* may have been bad for this. There have been reports here on KFN of users finding a pile of old stuff. I found a whole lot once, some years ago, and just now checking in muon I've just found linux-headers-5.19.0-26.27, from 22.04 I think.
          Regards, John Little

          Comment


            #6
            I'll keep an eye on it.

            Thanks for the help​

            Comment


              #7
              Originally posted by Snowhog View Post
              Follow the instructions for adding the ucaresystem-core ppa here. It's a superior script. I have been using it for years.
              Thanks for the tip

              '@gorzka you may also want to try Ubuntu Cleaner found here It will remove old kernels. https://www.omgubuntu.co.uk/2016/12/free-space-ubuntu-cleaner-janitor-app

              In my install it will issue a crash notice but it works fine so just ignore it. Have been using this tool for a longtime and has always worked for me.
              Last edited by kc1di; Jan 13, 2024, 04:41 AM.
              Dave Kubuntu 20.04 Registered Linux User #462608

              Wireless Script: http://ubuntuforums.org/showthread.p...5#post12350385

              Comment


                #8
                Is there margin for conflicts by leaving the old kernels in place?

                Comment


                  #9
                  Originally posted by Montmorency View Post
                  Is there margin for conflicts by leaving the old kernels in place?
                  No because only one kernel can be booted at a time so any old ones will just be dormant until it booted to. I usually keep the newest one and the one it replaces for a bit just to be sure everything works with the new kernel.
                  Dave Kubuntu 20.04 Registered Linux User #462608

                  Wireless Script: http://ubuntuforums.org/showthread.p...5#post12350385

                  Comment


                    #10
                    Originally posted by Montmorency View Post
                    Is there margin for conflicts by leaving the old kernels in place?
                    No, apt usually keeps three kernels installed, but there won't be any sort of conflicts
                    An issue can occur with having too many, if someone has manually created a separate /boot partition, and it isn't big enough to hold all the files.

                    Comment

                    Working...
                    X