lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

[ubuntu2504]  ubuntu2504----移除旧内核(脚本)

 

 

 

 

一、基本问题

 

  1.  操作系统:  ubuntu2504:  Linux ubuntu 6.14.0-22-generic

 

  2.  注意事项:  ROOT_PASSWORD,要改成你的账户密码。

 

 

 

二、脚本内容

[wit@ubuntu:tools]$ cat  remove_old_kernel
#!/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




[wit@ubuntu:tools]$ 
[wit@ubuntu:tools]$ 

 

 

三、参考文档

 

  1、  无

 

posted on 2025-08-21 16:48  lnlidawei  阅读(15)  评论(0)    收藏  举报