Skip to contentSkip to conversation

scripts:prune-kernel:prune kernel and modules dir from the system

PATCH In review Bhaskar Chowdhury 2 participants 4 messages Oct 29, 2019 – Oct 30, 2019 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts:prune-kernel:prune kernel and modules dir from the system

This patch will remove old kernel and modules directory from the system interactive way and also at once ,provied the parameter given to the invoking script.

---
 scripts/prune-kernel | 58 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
scripts/prune-kernel +58 -0
index 58a7650ce592..a6c990450ddc 100755
@@ -1,2 +1,60 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
+#This script will delete old kernels and modules directory related to it,both
+#automated and interactive way, if you choose -i or --interactive as parameter.
+#For normal operation you have to invoke this script like below
+#prune-kernel -r kernel_ver modules_dir_name
+flag=$1
+kernel_ver=$2
+modules_dir_name=$3
+boot_dir=/boot
+modules_dir=/lib/modules
+
+remove_old_kernel() {
+	cd $boot_dir
+	rm -If vmlinuz-$kernel_version System.map-$kernel_version config-$kernel_version
+	return 0
+}
+
+remove_old_modules_dir() {
+	cd $modules_dir
+	rm -rf $modules_version
+	return 0
+}
+
+usage() {
+	printf "Usage: $(basename $0) [-ri] \n"
+	printf "\n -r | --remove kernel_ver modules_dir_name \n"
+	printf "\n -i | --interactive use as interactive way \n"
+}
+
+while getopts :hir opt;do
+	case "$flag" in
+		-i | --interactive)
+			printf "\nEnter kernel version to remove or blank/empty to exit:%s"
+			read kernel_version
+			if [[ $kernel_version != "" ]]; then
+				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 directory:$modules_version ..Done.\n"
+			else
+				exit 1
+			fi
+			;;
+		-h | --help)
+			usage
+			exit 1
+			;;
+		-r | --remove)
+			shift $(( OPTIND -1 ))
+			cd $boot_dir
+			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

--
2.23.0

#1 lore raw

Hi,

Quoted 35 lines · Bhaskar Chowdhury

This patch will remove old kernel and modules directory from the system interactive way and also at once ,provied the parameter given to the invoking script.

---
 scripts/prune-kernel | 58 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
scripts/prune-kernel +19 -0
index 58a7650ce592..a6c990450ddc 100755
@@ -1,2 +1,60 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
+#This script will delete old kernels and modules directory related to it,both
+#automated and interactive way, if you choose -i or --interactive as parameter.
+#For normal operation you have to invoke this script like below
+#prune-kernel -r kernel_ver modules_dir_name
+flag=$1
+kernel_ver=$2
+modules_dir_name=$3
+boot_dir=/boot
+modules_dir=/lib/modules
+
+remove_old_kernel() {
+	cd $boot_dir
+	rm -If vmlinuz-$kernel_version System.map-$kernel_version config-$kernel_version
+	return 0
+}
+
+remove_old_modules_dir() {
+	cd $modules_dir
+	rm -rf $modules_version

scripts/prune-kernel line 31

Quoted 10 lines
scripts/prune-kernel +10 -0
+	return 0
+}
+
+usage() {
+	printf "Usage: $(basename $0) [-ri] \n"
+	printf "\n -r | --remove kernel_ver modules_dir_name \n"
+	printf "\n -i | --interactive use as interactive way \n"
+}
+
+while getopts :hir opt;do

what is the purpose of "opt" above?
It is not used AFAICT.

My internet searching says that 'getopts' does not support "--options" (long options). But then $flag is used below, not $opt, so the long options are just supported by "flag=$1" at the beginning of the script.

scripts/prune-kernel line 39

Quoted 8 lines
scripts/prune-kernel +8 -0
+	case "$flag" in
+		-i | --interactive)
+			printf "\nEnter kernel version to remove or blank/empty to exit:%s"
+			read kernel_version
+			if [[ $kernel_version != "" ]]; then
+				remove_old_kernel
+				printf "Please give the full modules directory name to remove:%s"
+				read modules_version

Need to handle modules_version = "" here.

scripts/prune-kernel line 50

Quoted 11 lines
scripts/prune-kernel +11 -0
+				remove_old_modules_dir
+				printf "\n\n\n Removed kernel version:$kernel_version and associated modules directory:$modules_version ..Done.\n"
+			else
+				exit 1
+			fi
+			;;
+		-h | --help)
+			usage
+			exit 1
+			;;
+		-r | --remove)

What happens if a user enters:

./scripts/prune-kernel -r
and no kernel_ver or modules_dir_name after -r?

scripts/prune-kernel line 51

Quoted 1 line
scripts/prune-kernel +1 -0
+			shift $(( OPTIND -1 ))

What is the purpose of the 'shift' since there is no loop to process more options?

scripts/prune-kernel line 60

Quoted 9 lines
scripts/prune-kernel +9 -0
+			cd $boot_dir
+			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

This patch does not delete the original script loop, so that still follows after the 'done' above. Was that intentional?

Signature · 1 line
~Randy
#2 lore raw
Quoted 1 line · Randy Dunlap

Hi,

Thank you Randy, my answers are inline , kindly look.

The modified version(implemented your suggestions) of the script and their interaction will send in next patch mail.

Quoted 40 lines · Bhaskar Chowdhury
Quoted 38 lines

This patch will remove old kernel and modules directory from the system interactive way and also at once ,provied the parameter given to the invoking script.

---
 scripts/prune-kernel | 58 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
scripts/prune-kernel +23 -0
index 58a7650ce592..a6c990450ddc 100755
@@ -1,2 +1,60 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
+#This script will delete old kernels and modules directory related to it,both
+#automated and interactive way, if you choose -i or --interactive as parameter.
+#For normal operation you have to invoke this script like below
+#prune-kernel -r kernel_ver modules_dir_name
+flag=$1
+kernel_ver=$2
+modules_dir_name=$3
+boot_dir=/boot
+modules_dir=/lib/modules
+
+remove_old_kernel() {
+	cd $boot_dir
+	rm -If vmlinuz-$kernel_version System.map-$kernel_version config-$kernel_version
+	return 0
+}
+
+remove_old_modules_dir() {
+	cd $modules_dir
+	rm -rf $modules_version
+	return 0
+}
+
+usage() {
Quoted 8 lines

scripts/prune-kernel line 31

Quoted 6 lines
scripts/prune-kernel +6 -0
+	printf "Usage: $(basename $0) [-ri] \n"
+	printf "\n -r | --remove kernel_ver modules_dir_name \n"
+	printf "\n -i | --interactive use as interactive way \n"
+}
+
+while getopts :hir opt;do

what is the purpose of "opt" above?

Getting rid of it, using alternative mechanism.

Quoted 6 lines

It is not used AFAICT.

My internet searching says that 'getopts' does not support "--options" (long options). But then $flag is used below, not $opt, so the long options are just supported by "flag=$1" at the beginning of the script.

Right.

Quoted 12 lines

scripts/prune-kernel line 39

Quoted 9 lines
scripts/prune-kernel +8 -0
+	case "$flag" in
+		-i | --interactive)
+			printf "\nEnter kernel version to remove or blank/empty to exit:%s"
+			read kernel_version
+			if [[ $kernel_version != "" ]]; then
+				remove_old_kernel
+				printf "Please give the full modules directory name to remove:%s"
+				read modules_version

Need to handle modules_version = "" here.

Exit if they fail to provide one...inducting that.

Quoted 14 lines

scripts/prune-kernel line 50

Quoted 11 lines
scripts/prune-kernel +11 -0
+				remove_old_modules_dir
+				printf "\n\n\n Removed kernel version:$kernel_version and associated modules directory:$modules_version ..Done.\n"
+			else
+				exit 1
+			fi
+			;;
+		-h | --help)
+			usage
+			exit 1
+			;;
+		-r | --remove)

What happens if a user enters:

Quoted 2 lines

./scripts/prune-kernel -r
and no kernel_ver or modules_dir_name after -r?

Simply die.That is also putting in,because not putting anything will defeat the purpose of having the flag in first place.

Quoted 5 lines

scripts/prune-kernel line 51

Quoted 2 lines
scripts/prune-kernel +1 -0
+			shift $(( OPTIND -1 ))

What is the purpose of the 'shift' since there is no loop to process more options?

Getting rid of it ,as we opted for different mechanism.

Quoted 4 lines
Quoted 4 lines
scripts/prune-kernel +4 -0
+			cd $boot_dir
+			rm -f $kernel_ver
+			cd $modules_dir
+			rm -rf $modules_dir_name
Quoted 8 lines

scripts/prune-kernel line 60

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

This patch does not delete the original script loop, so that still follows after the 'done' above. Was that intentional?

This is confuse me! not sure what you meant. Did you meant to say the do loop inside does not match with this pair???

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

Hi,

Thank you Randy, my answers are inline , kindly look.

Quoted 7 lines

The modified version(implemented your suggestions) of the script and their interaction will send in next patch mail.

Quoted 4 lines

This patch does not delete the original script loop, so that still follows after the 'done' above.  Was that intentional?

This is confuse me! not sure  what you meant. Did you meant to say the do loop inside does not match with this pair???

I mean that the old loop that begins with
for f in "$@"

is still there after your patch.

Signature · 1 line
~Randy