Suggestion for suppression of pacsave files on mhwd-kernel -r linuxYZ or mhwd-kernel -i linuxYZ rmc

#!/bin/bash

root_check() {
    [[ $EUID != 0 ]] && err "Please run as root."
}

args_check() {
    [[ $1 -lt $2 || $1 -gt $3 ]] && err "Please use the right amount of arguments (use -h for help)."
}

err() {
    printf "\e[31mError:\e[0m $1\n" 1>&2; exit 1
}

kernel_usage() {
    echo "Usage: mhwd-kernel [option]
    -h  --help              Show this help message
    -i  --install           Install a new kernel        [kernel(s)] [optional: rmc = remove current kernel]
    -l  --list              List all available kernels
    -li --listinstalled     List installed kernels
    -r  --remove            Remove a kernel             [kernel(s)]"
}

kernel_install() {
    pkginstall=()
    rmc=0

    for kernel in "$@"; do
        [[ $kernel = "rmc" ]] && rmc=1 && continue
        [[ $kernel != linux[0-9][0-9]?([0-9]) && $kernel != linux[0-9][0-9]?([0-9])-rt && $kernel != "rmc" ]] && err "Invalid argument.\nPlease choose one of the $(kernel_repo)"
        [[ $current = $kernel ]] && err "You can't reinstall your current kernel. Please use 'pacman -Syu' instead to update."
        [[ -z $(pacman -Ssq "^$kernel$") ]] && err "Please make sure if the given kernel(s) exist(s).\n$(kernel_repo)"
        
        for pkg in $(pacman -Qqs "$current"); do
            pkg=${pkg//$current/$kernel}
            [[ -n $(pacman -Ssq "^$pkg$" | grep -E "^$pkg$|^$pkg-\S+$") ]] && pkginstall+=("$pkg")
        done
    done

    pacman -Syy

    outofdate="$(pacman -Qqu | tr '\n' ' ')"
    if [[ -n $outofdate ]]; then
        echo "The following packages are out of date, please update your system first: $outofdate" >&2
        read -p "Do you want to continue anyway? [y/N] " yesno
        [[ $yesno = [Yy]?([Ee][Ss]) ]] || exit 1
    fi

    pacman -S "${pkginstall[@]}"
-  [[ $rmc = 1 && $? = 0 ]] && pacman -R $(pacman -Qqs $current)
+  [[ $rmc = 1 && $? = 0 ]] && pacman -Rn $(pacman -Qqs $current)
    [[ $rmc = 1 && $? != 0 ]] && { echo ""; err "'rmc' aborted because the kernel failed to install or canceled on removal."; }
}

kernel_repo() {
    printf "\e[32mavailable kernels:\e[0m\n"
    pacman -Ssq "^linux[0-9][0-9]?([0-9])$" | while read -r; do echo "   * $REPLY"; done
    pacman -Ssq "^linux[0-9][0-9]?([0-9])-rt$" | while read -r; do echo "   * $REPLY"; done
}

kernel_list() {
    printf "\e[32mCurrently running:\e[0m $(uname -r) (${current})\n"
    echo "The following kernels are installed in your system:"
    pacman -Qqs "^linux[0-9][0-9]?([0-9])$" | while read -r; do echo "   * $REPLY"; done
    pacman -Qqs "^linux[0-9][0-9]?([0-9])-rt$" | while read -r; do echo "   * $REPLY"; done
}

kernel_remove() {
    pkgremove=()

    for kernel in "$@"; do
        [[ -z "$kernel" ]] && err "Invalid argument (use -h for help)."
        [[ $kernel != linux[0-9][0-9]?([0-9]) && $kernel != linux[0-9][0-9]?([0-9])-rt ]] && err "Please enter a valid kernel name.\n$(kernel_list)"
        [[ $current = $kernel ]] && err "You can't remove your current kernel."
        [[ -z $(pacman -Qqs "^$1$") ]] && err "Kernel not installed.\n$(kernel_list)"

        for pkg in $(pacman -Qqs "$kernel" | grep -E "^$kernel$|^$kernel-\S+$"); do
            pkgremove+=("$pkg")
        done
    done

-    pacman -R "${pkgremove[@]}"
+    pacman -Rn "${pkgremove[@]}"
}

IFS=. read -r major minor _ <<< "$(uname -r)"
current="linux$major$minor"

case "$1" in
    -h | --help)            args_check $# 1 1
                            kernel_usage
                            exit 0;;
    -i | --install)         shift
                            root_check
                            kernel_install $@
                            exit 0 ;;
    -l | --list)            args_check $# 1 1
                            kernel_repo
                            exit 0 ;;
    -li| --listinstalled)   args_check $# 1 1
                            kernel_list
                            exit 0 ;;
    -r | --remove)          shift
                            root_check
                            kernel_remove $@
                            exit 0 ;;
    -*)                     err "Invalid argument (use -h for help)." ;;
    *)                      err "No arguments given (use -h for help)." ;;
esac
2 Likes

@pheiduck referring to this:

This is a patch file :wink:

From aaac5c798384075e5d04cec14ee1a16381c457e9 Mon Sep 17 00:00:00 2001
From: Megavolt <max.megavolt@posteo.de>
Date: Sun, 29 May 2022 18:52:27 +0200
Subject: [PATCH] Stop creating pacsave files for mkinitcpio presets

---
 scripts/mhwd-kernel | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/mhwd-kernel b/scripts/mhwd-kernel
index b7c1207..6153080 100755
--- a/scripts/mhwd-kernel
+++ b/scripts/mhwd-kernel
@@ -48,7 +48,7 @@ kernel_install() {
 
     pacman -S "${pkginstall[@]}"
 
-    [[ $rmc = 1 && $? = 0 ]] && pacman -R $(pacman -Qqs $current)
+    [[ $rmc = 1 && $? = 0 ]] && pacman -Rn $(pacman -Qqs $current)
     [[ $rmc = 1 && $? != 0 ]] && { echo ""; err "'rmc' aborted because the kernel failed to install or canceled on removal."; }
 }
 
@@ -79,7 +79,7 @@ kernel_remove() {
         done
     done
 
-    pacman -R "${pkgremove[@]}"
+    pacman -Rn "${pkgremove[@]}"
 }
 
 IFS=. read -r major minor _ <<< "$(uname -r)"
-- 
2.36.1

Commit it with useful text and run:

git format-patch -1 HEAD 
1 Like

I have tested it the pacsave file still exist anyway.
So we have to add something to remove the file on the correct location.

New version which works with expected behavior, we should update pacman-hook as well for this.

$ diff --color=auto a/mhwd/scripts/mhwd-kernel b/mhwd/scripts/mhwd-kernel 
51c51
-     [[ $rmc = 1 && $? = 0 ]] && pacman -R $(pacman -Qqs $current)
---
+     [[ $rmc = 1 && $? = 0 ]] && pacman -R $(pacman -Qqs $current); $(rm /etc/mkinitcpio.d/*.pacsave)
82c82
-     pacman -R "${pkgremove[@]}"
---
+     pacman -R "${pkgremove[@]}"; $(rm /etc/mkinitcpio.d/*.pacsave)

Better would be a cleanup() function with an additional parameter. This can be extended then.

1 Like

@pheiduck Please check this:

From 524fe1174085783dce72e222958520f83aa49539 Mon Sep 17 00:00:00 2001
From: Megavolt <max.megavolt@posteo.de>
Date: Mon, 30 May 2022 18:22:23 +0200
Subject: [PATCH] mhwd-kernel: Add cleanup function

---
 scripts/mhwd-kernel | 53 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 2 deletions(-)

diff --git a/scripts/mhwd-kernel b/scripts/mhwd-kernel
index b7c1207..fddc457 100755
--- a/scripts/mhwd-kernel
+++ b/scripts/mhwd-kernel
@@ -18,7 +18,8 @@ kernel_usage() {
     -i  --install           Install a new kernel        [kernel(s)] [optional: rmc = remove current kernel]
     -l  --list              List all available kernels
     -li --listinstalled     List installed kernels
-    -r  --remove            Remove a kernel             [kernel(s)]"
+    -r  --remove            Remove a kernel             [kernel(s)]
+    -c  --cleanup           Remove garbage of non-existing or removed kernels"
 }
 
 kernel_install() {
@@ -30,7 +31,7 @@ kernel_install() {
         [[ $kernel != linux[0-9][0-9]?([0-9]) && $kernel != linux[0-9][0-9]?([0-9])-rt && $kernel != "rmc" ]] && err "Invalid argument.\nPlease choose one of the $(kernel_repo)"
         [[ $current = $kernel ]] && err "You can't reinstall your current kernel. Please use 'pacman -Syu' instead to update."
         [[ -z $(pacman -Ssq "^$kernel$") ]] && err "Please make sure if the given kernel(s) exist(s).\n$(kernel_repo)"
-        
+
         for pkg in $(pacman -Qqs "$current"); do
             pkg=${pkg//$current/$kernel}
             [[ -n $(pacman -Ssq "^$pkg$" | grep -E "^$pkg$|^$pkg-\S+$") ]] && pkginstall+=("$pkg")
@@ -82,6 +83,51 @@ kernel_remove() {
     pacman -R "${pkgremove[@]}"
 }
 
+kernel_cleanup() {
+
+kver_list=( $(pacman -Ssq "^linux[0-9][0-9]?([0-9])($|-rt$)" | \
+              sed -r "s/linux([4,5])([0-9]+)(|-rt)/\\1.\\2\\3/g") )
+kver_inst=( $(pacman -Qqs "^linux[0-9][0-9]?([0-9])($|-rt$)" | \
+              sed -r "s/linux([4,5])([0-9]+)(|-rt)/\\1.\\2\\3/g") )
+
+    for target in ${kver_inst[@]}; do
+        for i in "${!kver_list[@]}"; do
+           if [[ ${kver_list[i]} == $target ]]; then
+              unset 'kver_list[i]'
+           fi
+        done
+    done
+
+    echo "Not installed Kernels: ${kver_list[@]}"
+
+    echo "Search for garbage..."
+    garbage=()
+    for i in ${kver_list[@]}; do
+
+    vmlinuz="/boot/vmlinuz-${i}-$(uname -m)"
+    initramfs="/boot/initramfs-${i}-$(uname -m).img"
+    fallback="/boot/initramfs-${i}-$(uname -m)-fallback.img"
+    kver="/boot/linux${i/./}-$(uname -m).kver"
+    preset="/etc/mkinitcpio.d/linux${i/./}.preset"
+    pacsave="/etc/mkinitcpio.d/linux${i/./}.preset.pacsave"
+
+    [[ -f "$vmlinuz" ]] && echo -e "$vmlinuz" && garbage+=( "$vmlinuz" )
+    [[ -f "$initramfs" ]] && echo "$initramfs" && garbage+=( "$initramfs" )
+    [[ -f "$fallback" ]] && echo "$fallback" && garbage+=( "$fallback" )
+    [[ -f "$kver" ]] && echo "$kver" && garbage+=( "$kver" )
+    [[ -f "$preset" ]] && echo "$preset" && garbage+=( "$preset" )
+    [[ -f "$pacsave" ]] && echo "$pacsave" && garbage+=( "$pacsave" )
+
+    done
+
+    if [[ -n $garbage ]]; then
+        read -p "Delete garbage? [ENTER/CTRL+C]"
+        sudo rm -fv ${garbage[@]}
+    else
+        echo "No garbage found."
+    fi
+}
+
 IFS=. read -r major minor _ <<< "$(uname -r)"
 current="linux$major$minor"
 
@@ -103,6 +149,9 @@ case "$1" in
                             root_check
                             kernel_remove $@
                             exit 0 ;;
+    -c | --cleanup)         args_check $# 1 1
+                            kernel_cleanup $@
+                            exit 0 ;;
     -*)                     err "Invalid argument (use -h for help)." ;;
     *)                      err "No arguments given (use -h for help)." ;;
 esac
-- 
2.36.1

I tried it and it works. Any suggestions?

  1. No idea how to check kernel versions of non-existing packages (removed from the repo entirely)
  2. Eventually update-grub needs to be run if initramfs has been removed.
1 Like

this shouldn’t be removed otherwise it come to bad issues

I only want to remove this file nothing else.

and it should work in combination with the remove / rmc command

this is also not a good idea O.o
i think you misunderstood my idea. it was only about removing the .pacsave files as declared here pacsave="/etc/mkinitcpio.d/linux${i/./}.preset.pacsave" nothing else.
But very cool you worked on this <3

Can you explain in which situations that would be bad? I don’t see why it would be bad.

  1. It checks installed kernel packages and exclude them from the install-able packages.
  2. Now it checks only for linux versions, which are not installed.

No, I don’t think so. As I have resolved some issues like this, this garbage remains on systems that have stable kernels installed but removed from the repo. The kernel module folder is removed, but the images etc are not. That has to be done manually then.

I just thought a little further.

And as you see:

Your file is included.

It looks like this:

$ ./mhwd-kernel -c                
Not installed Kernels: 4.19 5.17 5.18 5.4 5.18-rt
Search for garbage...
/etc/mkinitcpio.d/linux419.preset.pacsave
/etc/mkinitcpio.d/linux54.preset.pacsave
Delete garbage? [ENTER/CTRL+C]^C
$ ./mhwd-kernel -li                                                                          
Currently running: 5.15.43-1-MANJARO (linux515)
The following kernels are installed in your system:
   * linux510
   * linux515
   * linux515-rt
1 Like

Go ahead make PR for this would be glad to see.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.