Linux_Kernel_Management_Tool.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/bin/bash
  2. # Check root Permissions
  3. clear
  4. if [ "$(id -u)" -ne 0 ]; then
  5. echo "You must run this script as root."
  6. exit 1
  7. fi
  8. # Program: Display Current Boot Linux Kernel version
  9. function display_current_kernel() {
  10. echo ""
  11. echo "Current Boot Linux Kernel:"
  12. uname -r
  13. sleep 3s
  14. }
  15. # Program: Display Next Boot Default Kernel version
  16. function display_default_kernel() {
  17. default_kernel=$(grubby --default-kernel)
  18. echo ""
  19. echo "Next Boot Default Kernel:"
  20. echo "$default_kernel"
  21. sleep 3s
  22. }
  23. # Program: Identify Linux (RHEL) version
  24. CURRENT_LINUX_MAIN_VERSION=$(cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/')
  25. echo "Current Linux Main Version is: " $CURRENT_LINUX_MAIN_VERSION
  26. ELREPO_KEY_URL="https://www.elrepo.org/RPM-GPG-KEY-elrepo.org"
  27. ELREPO_RPM_URL="https://www.elrepo.org/elrepo-release-$CURRENT_LINUX_MAIN_VERSION.el$CURRENT_LINUX_MAIN_VERSION.elrepo.noarch.rpm"
  28. RPM_PACKAGE="elrepo-release"
  29. # Program: Upgrade Kernel (LongTerm or MainLine Kernel Canbe Select)
  30. function upgrade_kernel() {
  31. # Program: Check ELRepo public key
  32. echo "Check and Import ELRepo public key...."
  33. curl -s -o /tmp/elrepo-public-key.temp $ELREPO_KEY_URL
  34. rpm --import /tmp/elrepo-public-key.temp 2>/dev/null
  35. if [ $? -eq 0 ]; then
  36. echo "Finished"
  37. else
  38. echo "Error"
  39. rm -f /tmp/elrepo-public-key.temp
  40. exit 1
  41. fi
  42. rm -f /tmp/elrepo-public-key.temp
  43. # Program: Install ELRepo
  44. if ! dnf list installed | grep -q $RPM_PACKAGE
  45. then
  46. echo "Install ELRepo..."
  47. dnf install -y $ELREPO_RPM_URL &>/dev/null
  48. sed -i 's/mirrorlist=/#mirrorlist=/g' /etc/yum.repos.d/elrepo.repo
  49. sed -i 's#elrepo.org/linux#mirrors.tuna.tsinghua.edu.cn/elrepo#g' /etc/yum.repos.d/elrepo.repo
  50. dnf makecache &>/dev/null
  51. dnf --disablerepo=\* --enablerepo=elrepo-kernel repolist
  52. echo "ELRepo Install Done."
  53. else
  54. echo "ELRepo has been Installed."
  55. fi
  56. # Program: Check ELRepo Installation status
  57. if dnf list installed | grep -q $RPM_PACKAGE
  58. then
  59. echo "ELRepo Successfully Installed and Available."
  60. else
  61. echo "ELRepo Installation Failed or NotFound."
  62. exit 1
  63. fi
  64. # Display available Kernel packages
  65. echo "available Kernel packages:"
  66. dnf --disablerepo=\* --enablerepo=elrepo-kernel list 'kernel*'
  67. echo "Select Kernel version to Upgrade:"
  68. echo "1. LongTerm(lt)"
  69. echo "2. MainLine(ml)"
  70. echo -n "Please Select (1-2): "
  71. read -r kernel_type
  72. case $kernel_type in
  73. 1)
  74. # Upgrade to LongTerm(lt) Kernel
  75. echo "Upgrade to LongTerm(lt) Kernel..."
  76. dnf --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-lt.x86_64 &>/dev/null1
  77. dnf remove kernel-tools-libs.x86_64 kernel-tools.x86_64 -y &>/dev/null
  78. dnf --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-lt-tools.x86_64 &>/dev/null
  79. if [ $? -eq 0 ]; then
  80. echo "LongTerm(lt) Kernel Upgrade Completed."
  81. else
  82. echo "Failed to Upgrade Linux Kernel."
  83. fi
  84. sleep 3s
  85. ;;
  86. 2)
  87. # Upgrade to MainLine(ml) Kernel
  88. echo "Upgrade to MainLine(ml) Kernel..."
  89. dnf --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-ml.x86_64 &>/dev/null
  90. dnf remove kernel-tools-libs.x86_64 kernel-tools.x86_64 -y &>/dev/null
  91. dnf --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-ml-tools.x86_64 &>/dev/null
  92. if [ $? -eq 0 ]; then
  93. echo "MainLine(ml) Kernel Upgrade Completed."
  94. else
  95. echo "Failed to Upgrade Linux Kernel."
  96. fi
  97. sleep 3s
  98. ;;
  99. *)
  100. echo "Invalid Input, Check keyboard input."
  101. sleep 3s
  102. ;;
  103. esac
  104. }
  105. # Program: Set Next Boot Kernel
  106. function set_next_boot_kernel() {
  107. echo "List the Installed Kernels and index Number."
  108. grubby --info=ALL | grep -E '^(index=|kernel=)'
  109. echo -n "Enter the Index Number of the Kernel for Next Boot: "
  110. read -r modir_type
  111. grubby --set-default $modir_type
  112. echo "The Next Default Boot Kernel is set successfully."
  113. grubby --default-kernel
  114. # Ask the user for restart server
  115. read -p "Do you want to restart the server? (y/n): " response
  116. # Restart System Option
  117. case "$response" in
  118. [yY][eE][sS]|[yY])
  119. echo "Server is Restarting......"
  120. sleep 3s
  121. reboot
  122. ;;
  123. *)
  124. echo "User Ignores Restart Server."
  125. sleep 3s
  126. ;;
  127. esac
  128. }
  129. # Program: Clean Kernel
  130. function clean_kernel() {
  131. # List All Installed Kernel packages.
  132. INSTALLED_KERNELS=$(rpm -qa | grep kernel | grep -v "^kernel-devel" | grep -v "^kernel-headers" | grep -v "^kernel-tools" | sort -V)
  133. # Get Currently Boot Kernel version.
  134. CURRENT_KERNEL=$(uname -r)
  135. # Displays All Installed Kernel versions.
  136. echo "Installed Kernel version: "
  137. echo "$INSTALLED_KERNELS"
  138. echo
  139. # Prompt User to Enter the Kernel Version to be Deleted (Multiple versions can be Entered, Separated by Spaces).
  140. read -p "Please enter the kernel version to be deleted (separated by spaces, or enter 'all' to delete all kernels except the current boot): " KERNELS_TO_REMOVE
  141. # If User inputs 'all', All Kernels will be Deleted except the Currently Boot Kernel.
  142. if [ "$KERNELS_TO_REMOVE" == "all" ]; then
  143. KERNELS_TO_REMOVE=$(echo "$INSTALLED_KERNELS" | grep -v "$CURRENT_KERNEL")
  144. fi
  145. # Traverse the Kernel version Entered by user, check whether it contains the currently boot kernel.
  146. for KERNEL in $KERNELS_TO_REMOVE; do
  147. if [ "$KERNEL" == "$CURRENT_KERNEL" ]; then
  148. echo "Error: Cannot Delete Currently Boot Kernel. $CURRENT_KERNEL"
  149. exit 1
  150. fi
  151. done
  152. # Ask User If you are Sure you want to Delete these Kernels.
  153. read -p "Are you sure you want to delete the following kernel? $KERNELS_TO_REMOVE (y/n): " CONFIRM
  154. if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; then
  155. echo "Wrong Input, Operation Cancelled."
  156. exit 0
  157. fi
  158. # Delete the User-Specified Kernel package.
  159. for KERNEL in $KERNELS_TO_REMOVE; do
  160. echo "Deleting Kernel $KERNEL..."
  161. rpm -e --nodeps "$KERNEL"
  162. if [ $? -ne 0 ]; then
  163. echo "Error: Unable Delete Kernel. $KERNEL"
  164. else
  165. echo "Kernel $KERNEL Successfully Deleted."
  166. fi
  167. done
  168. echo "Operation Completed."
  169. sleep 3s
  170. }
  171. # Show Kernel Manger Menu
  172. function show_menu() {
  173. clear
  174. echo ""
  175. echo "================================"
  176. echo "Linux Kernel Management Tool"
  177. echo "for RHEL/CentOS/Rocky/Alma/Oracle"
  178. echo "via: cxthhhhh.com"
  179. echo "================================"
  180. echo "Please Select:"
  181. echo "1. Display Currently Boot Linux Kernel"
  182. echo "2. Display Next Boot Default Kernel"
  183. echo "3. Upgrade Kernel"
  184. echo "4. Set Next Boot Kernel"
  185. echo "5. Clean Kernel"
  186. echo "0. Quit"
  187. echo "================================"
  188. echo ""
  189. }
  190. # Menu
  191. while true; do
  192. show_menu
  193. read -p "Please Select (0-5): " choice
  194. case $choice in
  195. 1)
  196. display_current_kernel
  197. ;;
  198. 2)
  199. display_default_kernel
  200. ;;
  201. 3)
  202. upgrade_kernel
  203. ;;
  204. 4)
  205. set_next_boot_kernel
  206. ;;
  207. 5)
  208. clean_kernel
  209. ;;
  210. 0)
  211. echo "Quit Tool..."
  212. exit 0
  213. ;;
  214. *)
  215. echo "Invalid Input, Check keyboard input."
  216. sleep 3s
  217. ;;
  218. esac
  219. done