Skip to contentSkip to conversation

scripts:prune-kernel:prune old kernels and modules dir

PATCH NAK Bhaskar Chowdhury 2 participants 6 messages Oct 25, 2019 – Oct 28, 2019 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts:prune-kernel:prune old kernels and modules dir

This patch will remmove old kernel and associated modules directory from the system.

Few interaction with the script ,below

1) ✔ ~/git-linux/linux-kbuild [master ↑·18|✔]
10:40 $ ./scripts/prune-kernel

2)10:41 $ ./scripts/prune-kernel -h
You need to use this script like this :

./scripts/prune-kernel -r kernel_version modules_directory_name
./scripts/prune-kernel -i option for interactive way to use it.

3) 10:41 $ ./scripts/prune-kernel -r 5.2.2 5.2.2-gentoo Removed kernel version:5.2.2 and modules directory:5.2.2-gentoo from the system.

4)10:41 $ ./scripts/prune-kernel -i

Want to removing old kernels and modules dir [YN]: Y Please give another version to remove: 5.2.2 /boot/vmlinuz-5.3.7-050307-generic /boot/vmlinuz-5.3.6-050306-generic
find: ‘/boot/efi’: Permission denied
Please give the full modules directory name to remove: 5.2.2-gentoo 5.3.6-050306-generic
5.3.7-050307-generic

Removed kernel version:5.2.2 and associated modules:5.2.2-gentoo ..Done.

---
To Bruce,
I have incorporated all the changes you asked for ,kindly review.

 scripts/prune-kernel | 75 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 56 insertions(+), 19 deletions(-)
scripts/prune-kernel +56 -19
index e8aa940bc0a9..292ba70d7770 100755
@@ -1,21 +1,58 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
-
-# because I use CONFIG_LOCALVERSION_AUTO, not the same version again and
-# again, /boot and /lib/modules/ eventually fill up.
-# Dumb script to purge that stuff:
-
-for f in "$@"
-do
-        if rpm -qf "/lib/modules/$f" >/dev/null; then
-                echo "keeping $f (installed from rpm)"
-        elif [ $(uname -r) = "$f" ]; then
-                echo "keeping $f (running kernel) "
-        else
-                echo "removing $f"
-                rm -f "/boot/initramfs-$f.img" "/boot/System.map-$f"
-                rm -f "/boot/vmlinuz-$f"   "/boot/config-$f"
-                rm -rf "/lib/modules/$f"
-                new-kernel-pkg --remove $f
-        fi
-done
+#This script will delete old kernels and modules directory related to it interactively.
+#if you choose "-i" as interactive otherwise it will just go ahead and do the stuff once
+#you mentione the kernel_version and modules_directory_name as parameter.
+flag=$1
+kernel_ver=$2
+modules_dir_name=$3
+boot_dir=/boot
+modules_dir=/lib/modules
+remove_old_kernel() {
+	cd $boot_dir
+	find $boot_dir -name "vmlinuz-*" -type f -exec ls -1 {} \;
+	rm -If vmlinuz-$kernel_version System.map-$kernel_version config-$kernel_version
+	return 0
+}
+remove_old_modules_dir() {
+	cd $modules_dir
+	find $modules_dir -maxdepth 0 -type d -exec ls -1 {} \;
+	rm -rf $modules_version
+	return 0
+}
+while getopts :hir opt;
+do 
+		 case "$1" in
+			 -i | --interactive)
+		 printf "\n\n Want to removing old kernels and modules dir [YN]: %s"
+		 read response
+		 if [[ $response == "Y" ]];then
+			 printf "Please give another version to remove: %s"
+			 read kernel_version
+			 remove_old_kernel
+			 printf "Please give the full modules directory name to remove: %s"
+			 read modules_version
+			 remove_old_modules_dir
+			 printf "\n\n\n Removed kernel version:$kernel_version and associated modules:$modules_version ..Done. \n\n"
+		 elif [[ $response == "N" ]];then
+			 exit 1
+		 fi
+		 ;;
+	                  -h | --help)
+                    
+                     printf "You need to use this script like this :\n
+	             $0 -r kernel_version modules_directory_name\n
+                     $0 -i option for interactive way to use it.\n\n"
+	      
+	         exit 1
+		     ;;
+	                 -r | --remove)
+			 shift $(( OPTIND - 1 ))
+			 rm -f kernel_ver
+                         cd $modules_dir
+	                 rm -rf $modules_dir_name
+	                 printf "Removed  kernel version:$kernel_ver and modules directory:$modules_dir_name from the system.\n\n"
+			 exit 0
+                         ;;
+ esac
+     done
Signature · 1 line
2.20.1
#1 lore raw
Quoted 21 lines · Bhaskar Chowdhury

This patch will remmove old kernel and associated modules directory from the system.

Few interaction with the script ,below

1) ✔ ~/git-linux/linux-kbuild [master ↑·18|✔]
10:40 $ ./scripts/prune-kernel

2)10:41 $ ./scripts/prune-kernel -h
You need to use this script like this :

./scripts/prune-kernel -r kernel_version modules_directory_name
./scripts/prune-kernel -i option for interactive way to use it.

3) 10:41 $ ./scripts/prune-kernel -r 5.2.2 5.2.2-gentoo Removed kernel version:5.2.2 and modules directory:5.2.2-gentoo from the system.

Quoted 5 lines

4)10:41 $ ./scripts/prune-kernel -i

Want to removing old kernels and modules dir [YN]: Y Please give another version to remove: 5.2.2

These 2 lines above could (should) be combined into one line. E.g.:

Enter kernel version to remove or blank/emtpy to exit:

Quoted 3 lines

/boot/vmlinuz-5.3.7-050307-generic
/boot/vmlinuz-5.3.6-050306-generic
find: ‘/boot/efi’: Permission denied

too noisy.

Quoted 3 lines

Please give the full modules directory name to remove: 5.2.2-gentoo 5.3.6-050306-generic
5.3.7-050307-generic

too noisy.

Quoted 33 lines

Removed kernel version:5.2.2 and associated modules:5.2.2-gentoo ..Done.

---
To Bruce,
I have incorporated all the changes you asked for ,kindly review.

 scripts/prune-kernel | 75 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 56 insertions(+), 19 deletions(-)
scripts/prune-kernel +0 -12
index e8aa940bc0a9..292ba70d7770 100755
@@ -1,21 +1,58 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
-
-# because I use CONFIG_LOCALVERSION_AUTO, not the same version again and
-# again, /boot and /lib/modules/ eventually fill up.
-# Dumb script to purge that stuff:
-
-for f in "$@"
-do
-        if rpm -qf "/lib/modules/$f" >/dev/null; then
-                echo "keeping $f (installed from rpm)"
-        elif [ $(uname -r) = "$f" ]; then
-                echo "keeping $f (running kernel) "
-        else

scripts/prune-kernel line 5

Quoted 10 lines
scripts/prune-kernel +3 -7
-                echo "removing $f"
-                rm -f "/boot/initramfs-$f.img" "/boot/System.map-$f"
-                rm -f "/boot/vmlinuz-$f"   "/boot/config-$f"
-                rm -rf "/lib/modules/$f"
-                new-kernel-pkg --remove $f
-        fi
-done
+#This script will delete old kernels and modules directory related to it interactively.
+#if you choose "-i" as interactive otherwise it will just go ahead and do the stuff once
+#you mentione the kernel_version and modules_directory_name as parameter.

mention
or better:
enter

scripts/prune-kernel line 6

Quoted 1 line
scripts/prune-kernel +1 -0
+flag=$1

$flag is not used anywhere.

scripts/prune-kernel line 7

Quoted 1 line
scripts/prune-kernel +1 -0
+kernel_ver=$2

remove_old_kernel() uses $kernel_version, not $kernel_ver.

scripts/prune-kernel line 8

Quoted 1 line
scripts/prune-kernel +1 -0
+modules_dir_name=$3

remove_old_modules_dir() uses $modules_version, not $modules_dir_name.

scripts/prune-kernel line 10

Quoted 2 lines
scripts/prune-kernel +2 -0
+boot_dir=/boot
+modules_dir=/lib/modules

need a blank line here.

scripts/prune-kernel line 13

Quoted 3 lines
scripts/prune-kernel +3 -0
+remove_old_kernel() {
+	cd $boot_dir
+	find $boot_dir -name "vmlinuz-*" -type f -exec ls -1 {} \;

why the 'ls'? too noisy IMO.

scripts/prune-kernel line 16

Quoted 3 lines
scripts/prune-kernel +3 -0
+	rm -If vmlinuz-$kernel_version System.map-$kernel_version config-$kernel_version
+	return 0
+}

need a blank line here.

scripts/prune-kernel line 19

Quoted 3 lines
scripts/prune-kernel +3 -0
+remove_old_modules_dir() {
+	cd $modules_dir
+	find $modules_dir -maxdepth 0 -type d -exec ls -1 {} \;

'ls' is too noisy.

scripts/prune-kernel line 23

Quoted 4 lines
scripts/prune-kernel +4 -0
+	rm -rf $modules_version
+	return 0
+}
+while getopts :hir opt;

$opt is not used anywhere.

Does 'getopt's support long option strings? E.g., --interactive, --help, --remove.

scripts/prune-kernel line 24

Quoted 1 line
scripts/prune-kernel +1 -0
+do 

'do' has a trailing space after it. drop it.

scripts/prune-kernel line 25

Quoted 1 line
scripts/prune-kernel +1 -0
+		 case "$1" in

Is $1 the same as $flag here?

scripts/prune-kernel line 26

Quoted 1 line
scripts/prune-kernel +1 -0
+			 -i | --interactive)

bad indentation above.

Quoted 5 lines
scripts/prune-kernel +5 -0
+		 printf "\n\n Want to removing old kernels and modules dir [YN]: %s"
+		 read response
+		 if [[ $response == "Y" ]];then
+			 printf "Please give another version to remove: %s"
+			 read kernel_version

scripts/prune-kernel line 41

Quoted 10 lines
scripts/prune-kernel +10 -0
+			 remove_old_kernel
+			 printf "Please give the full modules directory name to remove: %s"
+			 read modules_version
+			 remove_old_modules_dir
+			 printf "\n\n\n Removed kernel version:$kernel_version and associated modules:$modules_version ..Done. \n\n"
+		 elif [[ $response == "N" ]];then
+			 exit 1
+		 fi
+		 ;;
+	                  -h | --help)

bad indentation.

scripts/prune-kernel line 42

Quoted 1 line
scripts/prune-kernel +1 -0
+                    

line above is just a bunch of spaces. bad.

scripts/prune-kernel line 43

Quoted 1 line
scripts/prune-kernel +1 -0
+                     printf "You need to use this script like this :\n

use tabs to indent, not spaces.

scripts/prune-kernel line 45

Quoted 2 lines
scripts/prune-kernel +2 -0
+	             $0 -r kernel_version modules_directory_name\n
+                     $0 -i option for interactive way to use it.\n\n"

inconsistent indentation.

scripts/prune-kernel line 46

Quoted 1 line
scripts/prune-kernel +1 -0
+	      

line above is just spaces. Empty lines are OK, even good for readablility, but they should not end with spaces or tabs.

scripts/prune-kernel line 50

Quoted 4 lines
scripts/prune-kernel +4 -0
+	         exit 1
+		     ;;
+	                 -r | --remove)
+			 shift $(( OPTIND - 1 ))

What is the purpose of the shift, when this case ends with exit?

scripts/prune-kernel line 51

Quoted 1 line
scripts/prune-kernel +1 -0
+			 rm -f kernel_ver

$kernel_ver

Probably need to cd $boot_dir for above 'rm'.
or just rm -f $boot_dir/$kernel_ver

scripts/prune-kernel line 57

Quoted 6 lines
scripts/prune-kernel +6 -0
+                         cd $modules_dir
+	                 rm -rf $modules_dir_name
+	                 printf "Removed  kernel version:$kernel_ver and modules directory:$modules_dir_name from the system.\n\n"
+			 exit 0
+                         ;;
+ esac

esac indentation does not match case.

scripts/prune-kernel line 58

Quoted 1 line
scripts/prune-kernel +1 -0
+     done

done indentation does not match do.

Nack.

Signature · 1 line
~Randy
#2 lore raw

Thank you Randy to point out the mistakes, will correct it and send it for your review. Below mention things am doing...

Quoted 30 lines · Randy Dunlap
Quoted 21 lines · Bhaskar Chowdhury

This patch will remmove old kernel and associated modules directory from the system.

Few interaction with the script ,below

1) ✔ ~/git-linux/linux-kbuild [master ↑·18|✔]
10:40 $ ./scripts/prune-kernel

2)10:41 $ ./scripts/prune-kernel -h
You need to use this script like this :

./scripts/prune-kernel -r kernel_version modules_directory_name
./scripts/prune-kernel -i option for interactive way to use it.

3) 10:41 $ ./scripts/prune-kernel -r 5.2.2 5.2.2-gentoo Removed kernel version:5.2.2 and modules directory:5.2.2-gentoo from the system.

Quoted 5 lines

4)10:41 $ ./scripts/prune-kernel -i

Want to removing old kernels and modules dir [YN]: Y Please give another version to remove: 5.2.2

These 2 lines above could (should) be combined into one line. E.g.:

Quoted 8 lines

Enter kernel version to remove or blank/emtpy to exit:

Quoted 3 lines

/boot/vmlinuz-5.3.7-050307-generic
/boot/vmlinuz-5.3.6-050306-generic
find: ‘/boot/efi’: Permission denied

too noisy.

modified , it will not spit this again.

Quoted 6 lines
Quoted 3 lines

Please give the full modules directory name to remove: 5.2.2-gentoo 5.3.6-050306-generic
5.3.7-050307-generic

too noisy.

Same as above ...modified and will not spit again.

Quoted 39 lines
Quoted 39 lines

Removed kernel version:5.2.2 and associated modules:5.2.2-gentoo ..Done.

---
To Bruce,
I have incorporated all the changes you asked for ,kindly review.

 scripts/prune-kernel | 75 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 56 insertions(+), 19 deletions(-)
scripts/prune-kernel +0 -18
index e8aa940bc0a9..292ba70d7770 100755
@@ -1,21 +1,58 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
-
-# because I use CONFIG_LOCALVERSION_AUTO, not the same version again and
-# again, /boot and /lib/modules/ eventually fill up.
-# Dumb script to purge that stuff:
-
-for f in "$@"
-do
-        if rpm -qf "/lib/modules/$f" >/dev/null; then
-                echo "keeping $f (installed from rpm)"
-        elif [ $(uname -r) = "$f" ]; then
-                echo "keeping $f (running kernel) "
-        else
-                echo "removing $f"
-                rm -f "/boot/initramfs-$f.img" "/boot/System.map-$f"
-                rm -f "/boot/vmlinuz-$f"   "/boot/config-$f"
-                rm -rf "/lib/modules/$f"
-                new-kernel-pkg --remove $f
-        fi
Quoted 9 lines

scripts/prune-kernel line 5

Quoted 4 lines
scripts/prune-kernel +3 -1
-done
+#This script will delete old kernels and modules directory related to it interactively.
+#if you choose "-i" as interactive otherwise it will just go ahead and do the stuff once
+#you mentione the kernel_version and modules_directory_name as parameter.

mention
or better:
enter

Replaced with mentioned word i.e enter.

Quoted 4 lines

scripts/prune-kernel line 6

Quoted 1 line
scripts/prune-kernel +1 -0
+flag=$1

$flag is not used anywhere.

For interactive use case we need that.

Quoted 4 lines

scripts/prune-kernel line 7

Quoted 1 line
scripts/prune-kernel +1 -0
+kernel_ver=$2

remove_old_kernel() uses $kernel_version, not $kernel_ver.

Quoted 8 lines

scripts/prune-kernel line 8

Quoted 1 line
scripts/prune-kernel +1 -0
+modules_dir_name=$3

remove_old_modules_dir() uses $modules_version, not $modules_dir_name.

scripts/prune-kernel line 10

Quoted 2 lines
scripts/prune-kernel +2 -0
+boot_dir=/boot
+modules_dir=/lib/modules

need a blank line here.

Fixed.

Quoted 5 lines

scripts/prune-kernel line 13

Quoted 4 lines
scripts/prune-kernel +3 -0
+remove_old_kernel() {
+	cd $boot_dir
+	find $boot_dir -name "vmlinuz-*" -type f -exec ls -1 {} \;

Removed.

Quoted 8 lines

why the 'ls'? too noisy IMO.

scripts/prune-kernel line 16

Quoted 3 lines
scripts/prune-kernel +3 -0
+	rm -If vmlinuz-$kernel_version System.map-$kernel_version config-$kernel_version
+	return 0
+}

need a blank line here.

Fixed.

Quoted 5 lines

scripts/prune-kernel line 19

Quoted 3 lines
scripts/prune-kernel +3 -0
+remove_old_modules_dir() {
+	cd $modules_dir
+	find $modules_dir -maxdepth 0 -type d -exec ls -1 {} \;

'ls' is too noisy.

Removed.

Quoted 7 lines

scripts/prune-kernel line 23

Quoted 5 lines
scripts/prune-kernel +4 -0
+	rm -rf $modules_version
+	return 0
+}
+while getopts :hir opt;

$opt is not used anywhere.

It is required, without it , looks like this:

getopts: usage: getopts optstring name [arg]

Quoted 4 lines

Does 'getopt's support long option strings? E.g., --interactive, --help, --remove.

Yes ..

✔ ~/git-linux/linux-kbuild [master ↑·27|✚ 1]
08:44 $ ./scripts/prune-kernel --interactive

Enter kernel version to remove or blank/emtpy to exit: 5.2.2 Please give the full modules directory name to remove: 5.2.2-whatever

Removed kernel version: and associated modules:5.2.2-whatever ..Done.

Quoted 4 lines

scripts/prune-kernel line 24

Quoted 1 line
scripts/prune-kernel +1 -0
+do 

'do' has a trailing space after it. drop it.

Certain thing ..this space and tab killing me...how fix these damn thing Randy??

Quoted 3 lines

scripts/prune-kernel line 25

Quoted 1 line
scripts/prune-kernel +1 -0
+		 case "$1" in

Is $1 the same as $flag here?

Yes and change it to that variable.

Quoted 4 lines

scripts/prune-kernel line 26

Quoted 1 line
scripts/prune-kernel +1 -0
+			 -i | --interactive)

bad indentation above.

Fixed.

Quoted 9 lines
Quoted 9 lines
scripts/prune-kernel +9 -0
+		 printf "\n\n Want to removing old kernels and modules dir [YN]: %s"
+		 read response
+		 if [[ $response == "Y" ]];then
+			 printf "Please give another version to remove: %s"
+			 read kernel_version
+			 remove_old_kernel
+			 printf "Please give the full modules directory name to remove: %s"
+			 read modules_version
+			 remove_old_modules_dir
Quoted 9 lines

scripts/prune-kernel line 41

Quoted 6 lines
scripts/prune-kernel +6 -0
+			 printf "\n\n\n Removed kernel version:$kernel_version and associated modules:$modules_version ..Done. \n\n"
+		 elif [[ $response == "N" ]];then
+			 exit 1
+		 fi
+		 ;;
+	                  -h | --help)

bad indentation.

Fixed.

Quoted 8 lines

scripts/prune-kernel line 42

Quoted 1 line
scripts/prune-kernel +1 -0
+                    

line above is just a bunch of spaces. bad.

scripts/prune-kernel line 43

Quoted 1 line
scripts/prune-kernel +1 -0
+                     printf "You need to use this script like this :\n

use tabs to indent, not spaces.

Fixed.

Quoted 5 lines

scripts/prune-kernel line 45

Quoted 2 lines
scripts/prune-kernel +2 -0
+	             $0 -r kernel_version modules_directory_name\n
+                     $0 -i option for interactive way to use it.\n\n"

inconsistent indentation.

Fixed.

Quoted 5 lines

scripts/prune-kernel line 46

Quoted 1 line
scripts/prune-kernel +1 -0
+	      

line above is just spaces. Empty lines are OK, even good for readablility, but they should not end with spaces or tabs.

Fixed.

Quoted 7 lines

scripts/prune-kernel line 50

Quoted 4 lines
scripts/prune-kernel +4 -0
+	         exit 1
+		     ;;
+	                 -r | --remove)
+			 shift $(( OPTIND - 1 ))

What is the purpose of the shift, when this case ends with exit?

Positional parameter shifting

Quoted 7 lines

scripts/prune-kernel line 51

Quoted 1 line
scripts/prune-kernel +1 -0
+			 rm -f kernel_ver

$kernel_ver

Probably need to cd $boot_dir for above 'rm'.
or just rm -f $boot_dir/$kernel_ver

Fixed.

Quoted 9 lines

scripts/prune-kernel line 57

Quoted 6 lines
scripts/prune-kernel +6 -0
+                         cd $modules_dir
+	                 rm -rf $modules_dir_name
+	                 printf "Removed  kernel version:$kernel_ver and modules directory:$modules_dir_name from the system.\n\n"
+			 exit 0
+                         ;;
+ esac

esac indentation does not match case.

Fixed

Quoted 4 lines

scripts/prune-kernel line 58

Quoted 1 line
scripts/prune-kernel +1 -0
+     done

done indentation does not match do.

Fixed

Quoted 6 lines

Nack.

Signature · 1 line
~Randy

Bhaskar

#3 lore raw
Quoted 6 lines · Bhaskar Chowdhury
Quoted 4 lines
Quoted 1 line

+do

'do' has a trailing space after it.  drop it.

Certain thing ..this space and tab killing me...how fix these damn thing Randy??

I dunno. It depends on what editor you are using and what options. So what editor?

Signature · 1 line
~Randy
#4 lore raw
Quoted 10 lines · Randy Dunlap
Quoted 6 lines · Bhaskar Chowdhury
Quoted 4 lines
Quoted 1 line

+do

'do' has a trailing space after it.  drop it.

Certain thing ..this space and tab killing me...how fix these damn thing Randy??

I dunno. It depends on what editor you are using and what options. So what editor?

vim all the way.

Quoted 3 lines
Signature · 1 line
~Randy
#5 lore raw
Quoted 4 lines · Bhaskar Chowdhury
Quoted 3 lines · Randy Dunlap
Quoted 2 lines · Bhaskar Chowdhury
Quoted 2 lines
Quoted 1 line

+do

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

'do' has a trailing space after it.  drop it.

Certain thing ..this space and tab killing me...how fix these damn thing Randy??

I dunno.  It depends on what editor you are using and what options.  So what editor?

vim all the way.

Same here. No problems, so I don't understand what's going on.

Signature · 1 line
~Randy