【linux】:redhat_debian移除旧内核的脚本
一、说明
1. 说明1: redhat,代表采用RPM软件包、带有DNF包管理器的操作系统;redhat类型操作系统,使用“redhat移除旧内核脚本”;
2. 说明2: debian,代表采用DEB软件包、带有APT包管理器的操作系统;debian类型操作系统,使用“debian移除旧内核脚本”;
二、【XX.RPM:DNF:redhat/centos/fedora/rockylinux/openEuler/...】redhat移除旧内核脚本
#!/usr/bin/env bash
# name = remove_old_kernel_redhat
#USER_PASSWORD="GAI_CHENG_NI_DE_MI_MA"
#USER_PASSWORD="GAI_CHENG_NI_DE_MI_MA"
# remove_old_kernel_redhat_help
function remove_old_kernel_redhat_help()
{
echo
echo ${USER_PASSWORD} | sudo -S echo ""
echo -e "\t== remove_old_kernel_redhat_help: BEGIN =="
echo -e '\t\tCOMMAND PARAMETER FUNCTION'
echo -e '\t\tremove_old_kernel_redhat null -- List all kernel files installed.'
echo -e '\t\tremove_old_kernel_redhat kernel-old-version -- Remove the old kernel.'
echo -e '\t == examples =='
echo -e '\t\tremove_old_kernel_redhat -- List all kernel files installed. '
echo -e '\t\tremove_old_kernel_redhat 6.16.8-200.fc42.x86_64 -- Remove old kernel: 6.16.8-200.fc42.x86_64 '
echo -e '\t\t[wit@redhat]$ remove_old_kernel_redhat 6.16.8-200.fc42.x86_64'
echo -e "\t== remove_old_kernel_redhat_help: END =="
echo
}
# display kernel files by current using
function current_use_kernel()
{
echo -e "\n\t-- current_use_kernel: begin --"
msg=$(echo ${USER_PASSWORD} | sudo -S uname -snr)
echo -e "\t\t" ${msg}
echo -e "\t-- current_use_kernel: end --\n"
}
# list kernel files and return a parameter of type of array.
function list_kernel()
{
GET_KERNEL_FILES=$(echo ${USER_PASSWORD} | sudo -S rpm -qa | grep -E "^kernel*")
echo -e "\n\t-- list_kernel: begin --"
i=1
for f in ${GET_KERNEL_FILES}
do
echo -e "\t\tLIST_FILE_$i := " ${f}
i=$(($i+1))
done
echo -e "\t-- list_kernel: over --\n"
i=0
}
# delete files of old kernels
function delete_old_kernel_files()
{
if [ $1 ]
then
echo -e "\n\t-- delete_old_kernel_files: begin --"
echo -e "\n\t\tREMOVLE_OLD_KERNEL := " $1 "\n"
GET_DELETE_KERNEL_FILES=$( echo ${USER_PASSWORD} | sudo -S rpm -qa | grep -E "^kernel*" | grep -E "^kernel.*$1.*" )
i=1
for f in ${GET_DELETE_KERNEL_FILES}
do
echo -e "\n\n\t\tDELETE_FILE_$i := " ${f} "\n"
echo ${USER_PASSWORD} | sudo -S dnf remove -y ${f}
i=$(($i+1))
done
echo -e "\t-- delete_old_kernel_files: over --\n"
else
echo -e "\t[ ERROR ]:\t" "\$1 is NULL, this programe is over !"
exit 3
fi
}
#list_libs(): list libs in '/lib/modules/'
function list_libs()
{
echo -e "\n\t-- list_libs:begin --"
i=1
for e in $(ls "/lib/modules/")
do
echo -e "\t\t[lib_${i}]#\t${e}"
done
echo -e "\t-- list_libs:end --"
i=0
}
#delete_libs(): remove old modules in '/lib/modules/'
function delete_libs()
{
cur_dir=$(pwd)
lib_home='/lib/modules/'
cd ${lib_home}
# list libs
list_libs
echo
echo -e "\n\t== remove_list_libs:begin =="
i=1
for e in $( ls "/lib/modules/" | grep -E -v "$(uname -r)" )
do
if [ ! ${e} ]
then
break
fi
echo -e "\t[libs_${i}]:\t${e}"
i=$((i+1))
echo ${USER_PASSWORD} | sudo -S rm -rf ${e}
done
i=0
echo -e "\t== remove_list_libs:end =="
# list libs
list_libs
cd ${cur_dir}
echo
echo
}
# fina_work(): remove old information of old kernel, and then clean, and then update-grub
function final_work()
{
echo -e "\n\t-- final_work: begin --\n"
echo -e "\n\t-- final_work: over --\n"
}
# run
function run()
{
echo
echo ${USER_PASSWORD} | sudo -S echo ""
echo -e "\n== remove_old_kernel_redhat: begin ==\n"
# remove_old_kernel_redhat_help
remove_old_kernel_redhat_help
# delete_libs_test
#delete_libs
# delete all files of kernel
if [ $1 ]
then
delete_old_kernel_files $1
# delete libs in '/lib/modules'
delete_libs
# final_work
final_work
fi
# list all files of kernel
list_kernel
# list libs in '/lib/modules/'
list_libs
# display current kernel
current_use_kernel
echo -e "\n== remove_old_kernel_redhat: over ==\n"
echo
}
run $1
三、【XX.DEB:APT:debian/ubuntu/...】debian移除旧内核脚本
#!/usr/bin/env bash # filename = remove_old_kernel_ubuntu #USER_PASSWORD="GAI_WEI_NI_DE_MI_MA" #USER_PASSWORD="GAI_WEI_NI_DE_MI_MA" # remove_old_kernel_ubuntu_help function remove_old_kernel_ubuntu_help() { echo echo ${USER_PASSWORD} | sudo -S echo "" echo -e "\t== remove_old_kernel_ubuntu_help: BEGIN ==" echo -e '\t\tCOMMAND PARAMETER FUNCTION' echo -e '\t\tremove_old_kernel_ubuntu null -- List all kernel files installed.' echo -e '\t\tremove_old_kernel_ubuntu kernel-old-version -- Remove the old kernel.' echo -e '\t == examples ==' echo -e '\t\tremove_old_kernel_ubuntu -- List all kernel files installed. ' echo -e '\t\tremove_old_kernel_ubuntu 6.2.0-36 -- Remove old kernel: 6.2.0-36 ' echo -e '\t\t[wit@ubuntu:tools]$ remove_old_kernel_ubuntu 6.2.0-36' echo -e "\t== remove_old_kernel_ubuntu_help: END ==" echo } # display kernel files by current using function current_use_kernel() { echo -e "\n\t-- current_use_kernel: begin --"; msg=$(echo ${USER_PASSWORD} | sudo -S uname -snr) echo -e "\t\t" ${msg} echo -e "\t-- current_use_kernel: end --\n"; } # list kernel files and return a parameter of type of array. function list_kernel() { echo -e "\n\t-- list_kernel: begin --"; i=1 for f in $(echo ${USER_PASSWORD} | sudo -S dpkg --get-selections | grep -E "^linux" | awk '{ print $1}') do echo -e "\t\tLIST_FILE_$i := " ${f} i=$(($i+1)) done echo -e "\t-- list_kernel: over --\n"; } # delete files of old kernels function delete_old_kernel_files() { if [ $1 ] then echo -e "\n\t-- delete_old_kernel_files: begin --"; echo -e "\n\t\tREMOVLE_OLD_KERNEL := " $1 "\n" kernel_files=$(echo ${USER_PASSWORD} | sudo -S dpkg --get-selections | grep -E "^linux.*$1-?*" | awk '{ print $1}') i=1 for f in ${kernel_files} do echo -e "\n\n\t\tDELETE_FILE_$i := " ${f} "\n" echo ${USER_PASSWORD} | sudo -S apt purge -y ${f} i=$(($i+1)) done echo -e "\t-- delete_old_kernel_files: over --\n"; else echo -e "\t[ ERROR ]:\t" "\$1 is NULL, this programe is over !" exit 3 fi } #list_libs(): list libs in '/lib/modules/' function list_libs() { echo -e "\n\t-- list_libs:begin --" i=1 for e in $(ls "/lib/modules/") do echo -e "\t\t[lib_${i}]:\t${e}" done echo -e "\t-- list_libs:end --" i=0 }
#delete_libs(): remove old modules in '/lib/modules/' function delete_libs() { cur_dir=$(pwd) lib_home='/lib/modules/' cd ${lib_home} # list libs list_libs echo echo -e "\n\t== remove_list_libs:begin ==" i=1 for e in $(ls "/lib/modules/" | grep -E -v "$(uname -r)") do if [ ! ${e} ] then break fi echo -e "\t[libs_${i}]:\t${e}" i=$((i+1)) echo ${USER_PASSWORD} | sudo -S rm -rf ${e} done i=0 echo -e "\t== remove_list_libs:end ==" # list libs list_libs cd ${cur_dir} echo echo } # fina_work(): remove old information of old kernel, and then clean, and then update-grub function final_work() { echo -e "\n\t-- final_work: begin --\n"; #clean_files=$(echo ${USER_PASSWORD} | sudo -S dpkg --get-selections | grep linux | grep reinstall | awk '{ print $1}') clean_files=$(echo ${USER_PASSWORD} | sudo -S dpkg --get-selections | grep linux | grep reinstall | awk '{ print $1}') if [ ${clean_files} ] then for f in ${clean_files} do echo ${USER_PASSWORD} | sudo -S dpkg -P ${f} done fi echo ${USER_PASSWORD} | sudo -S apt autoremove -y; #echo ${USER_PASSWORD} | sudo -S update-grub; echo ${USER_PASSWORD} | sudo -S update-grub; echo -e "\n\t-- final_work: over --\n"; } # run function run() { echo echo ${USER_PASSWORD} | sudo -S echo "" echo -e "\n== remove_old_kernel_ubuntu: begin ==\n"; # remove_old_kernel_ubuntu_help remove_old_kernel_ubuntu_help # delete_libs_test #delete_libs # delete all files of kernel if [ $1 ] then delete_old_kernel_files $1 # delete libs in '/lib/modules' delete_libs # final_work final_work fi # list all files of kernel list_kernel # list libs in '/lib/modules/' list_libs # display current kernel current_use_kernel echo -e "\n== remove_old_kernel_ubuntu: over ==\n"; echo } run $1
四、参考资料
1. https://www.cnblogs.com/lnlidawei/p/19051083
.
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章版权归属【原创作者】; 转载或引用时【请保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/19132542