lnlidawei

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

os:linux移除旧内核的脚本(rockylinux9)

 

 

 

 

一、移除旧内核脚本的说明(使用root账户)

 

   1、脚本的参数($1):输入“将要移除内核的版本号

    1.1、示例:[root@fedora ~]# remove_old_kernel 6.1.6-200.fc37

 

  2、应用示例

 

 1 [root@fedora ~]# remove_old_kernel
 2 
 3 
 4   == remove-old-kernel help: start ==
 5         command                 parameter1              function
 6         remove_old_kernel       null                    -- List all kernel files installed.
 7         remove_old_kernel       kernel-old-version      -- Remove the kernel files matched with "kernel-old-version" .
 8 
 9   == examples ==
10         remove_old_kernel                               List all kernel files installed. 
11         remove_old_kernel       6.0.14-300.fc37.x86_64  remove kernel files matched with "6.0.14-300.fc37.x86_64" .
12   == remove-old-kernel help: end ==
13 
14 
15 
16 -- List All Kernel Files: start --
17     SHOW_KERNEL_FILE_1 : kernel-core-6.1.6-200.fc37.x86_64
18     SHOW_KERNEL_FILE_2 : kernel-modules-6.1.6-200.fc37.x86_64
19     SHOW_KERNEL_FILE_3 : kernel-headers-6.1.5-200.fc37.x86_64
20     SHOW_KERNEL_FILE_4 : kernel-6.1.6-200.fc37.x86_64
21     SHOW_KERNEL_FILE_5 : kernel-modules-extra-6.1.6-200.fc37.x86_64
22     SHOW_KERNEL_FILE_6 : kernel-devel-6.1.6-200.fc37.x86_64
23     SHOW_KERNEL_FILE_7 : kernel-core-6.1.7-200.fc37.x86_64
24     SHOW_KERNEL_FILE_8 : kernel-modules-6.1.7-200.fc37.x86_64
25     SHOW_KERNEL_FILE_9 : kernel-6.1.7-200.fc37.x86_64
26     SHOW_KERNEL_FILE_10 : kernel-modules-extra-6.1.7-200.fc37.x86_64
27     SHOW_KERNEL_FILE_11 : kernel-devel-6.1.7-200.fc37.x86_64
28 -- List All Kernel Files: end --
29 
30 [root@fedora ~]# 
31 [root@fedora ~]# 
32 [root@fedora ~]# remove_old_kernel 6.1.6-200.fc37

 

 

 

 

二、脚本的源码

 

  1 [root@fedora ~]# cat  /root/user/tools/remove_old_kernel 
  2 #!/usr/bin/bash
  3 
  4 
  5 
  6 
  7 # file_name = remove old kernel
  8 
  9 
 10 
 11 
 12 # show all kernel files installed
 13 
 14 function list()
 15 {
 16 
 17 
 18         kf=$(rpm -qa | grep -E "^kernel*")
 19 
 20 
 21         ic=1
 22 
 23         echo
 24         echo '-- List All Kernel Files: start --'
 25         for kf in ${kf}
 26         do
 27                 echo '    SHOW_KERNEL_FILE_'${ic} :     ${kf}
 28                 ic=$((${ic}+1))
 29         done
 30         echo '-- List All Kernel Files: end --'
 31         echo
 32 
 33 
 34 }
 35 
 36 
 37 
 38 
 39 # delete old kernel, $1 is set to delete version
 40 # eg: set $1
 41 #       $1 = 6.0.14-300.fc37.x86_64
 42 
 43 function delete_old_kernel()
 44 {
 45 
 46         echo
 47         echo '-- remove old kernel files: starting --'
 48         echo '  OS->: Delete_Old_Kernel_Version :='     $1
 49         if [ $1 ]
 50         then
 51                 kd=$(rpm -qa | grep -E "^kernel*" | grep -E "^kernel.*$1.*")
 52         else
 53                 echo '  OS->: $1 is NULL, the program is end !'
 54                 exit 3
 55         fi
 56 
 57 
 58         di=1
 59 
 60         for df in ${kd}
 61         do
 62                 echo '    REMOVE_KERNEL_FILE_'${di} :   ${df}
 63                 dnf -y remove ${df}
 64                 di=$((${di}+1))
 65         done
 66         echo '-- remove old kernel files: over  --'
 67         echo
 68 }
 69 
 70 
 71 
 72 
 73 function remove_old_kernel_help()
 74 {
 75         echo
 76         echo
 77         echo '  == remove-old-kernel help: start =='
 78         echo '  command                 parameter1              function'
 79         echo '  remove_old_kernel       null                    -- List all kernel files installed.'
 80         echo '  remove_old_kernel       kernel-old-version      -- Remove the kernel files matched with "kernel-old-version" .'
 81         echo
 82         echo '  == examples =='
 83         echo '  remove_old_kernel                               List all kernel files installed. '
 84         echo '  remove_old_kernel       6.0.14-300.fc37.x86_64  remove kernel files matched with "6.0.14-300.fc37.x86_64" .'
 85         echo '  == remove-old-kernel help: end =='
 86         echo
 87         echo
 88 }
 89 
 90 
 91 
 92 
 93 function run()
 94 {
 95 
 96         remove_old_kernel_help
 97 
 98         if [ -z $1 ]
 99         then
100         #       echo '**TEST:           $1 is empty.'
101                 list
102         else
103         #       echo '**TEST:           $1 is not empty.'
104                 list
105                 delete_old_kernel $1
106                 list
107         fi
108 }
109 
110 
111 
112 
113 run $1
114 
115 [root@fedora ~]# 
116 [root@fedora ~]# 

 

 

 

 

 

 

三、脚本使用示例

 

  1 [root@fedora ~]# remove_old_kernel 
  2 
  3 
  4   == remove-old-kernel help: start ==
  5         command                 parameter1              function
  6         remove_old_kernel       null                    -- List all kernel files installed.
  7         remove_old_kernel       kernel-old-version      -- Remove the kernel files matched with "kernel-old-version" .
  8 
  9   == examples ==
 10         remove_old_kernel                               List all kernel files installed. 
 11         remove_old_kernel       6.0.14-300.fc37.x86_64  remove kernel files matched with "6.0.14-300.fc37.x86_64" .
 12   == remove-old-kernel help: end ==
 13 
 14 
 15 
 16 -- List All Kernel Files: start --
 17     SHOW_KERNEL_FILE_1 : kernel-core-6.1.6-200.fc37.x86_64
 18     SHOW_KERNEL_FILE_2 : kernel-modules-6.1.6-200.fc37.x86_64
 19     SHOW_KERNEL_FILE_3 : kernel-headers-6.1.5-200.fc37.x86_64
 20     SHOW_KERNEL_FILE_4 : kernel-6.1.6-200.fc37.x86_64
 21     SHOW_KERNEL_FILE_5 : kernel-modules-extra-6.1.6-200.fc37.x86_64
 22     SHOW_KERNEL_FILE_6 : kernel-devel-6.1.6-200.fc37.x86_64
 23     SHOW_KERNEL_FILE_7 : kernel-core-6.1.7-200.fc37.x86_64
 24     SHOW_KERNEL_FILE_8 : kernel-modules-6.1.7-200.fc37.x86_64
 25     SHOW_KERNEL_FILE_9 : kernel-6.1.7-200.fc37.x86_64
 26     SHOW_KERNEL_FILE_10 : kernel-modules-extra-6.1.7-200.fc37.x86_64
 27     SHOW_KERNEL_FILE_11 : kernel-devel-6.1.7-200.fc37.x86_64
 28 -- List All Kernel Files: end --
 29 
 30 [root@fedora ~]# 
 31 [root@fedora ~]# 
 32 [root@fedora ~]# remove_old_kernel 6.1.6-200.fc37
 33 
 34 
 35   == remove-old-kernel help: start ==
 36         command                 parameter1              function
 37         remove_old_kernel       null                    -- List all kernel files installed.
 38         remove_old_kernel       kernel-old-version      -- Remove the kernel files matched with "kernel-old-version" .
 39 
 40   == examples ==
 41         remove_old_kernel                               List all kernel files installed. 
 42         remove_old_kernel       6.0.14-300.fc37.x86_64  remove kernel files matched with "6.0.14-300.fc37.x86_64" .
 43   == remove-old-kernel help: end ==
 44 
 45 
 46 
 47 -- List All Kernel Files: start --
 48     SHOW_KERNEL_FILE_1 : kernel-core-6.1.6-200.fc37.x86_64
 49     SHOW_KERNEL_FILE_2 : kernel-modules-6.1.6-200.fc37.x86_64
 50     SHOW_KERNEL_FILE_3 : kernel-headers-6.1.5-200.fc37.x86_64
 51     SHOW_KERNEL_FILE_4 : kernel-6.1.6-200.fc37.x86_64
 52     SHOW_KERNEL_FILE_5 : kernel-modules-extra-6.1.6-200.fc37.x86_64
 53     SHOW_KERNEL_FILE_6 : kernel-devel-6.1.6-200.fc37.x86_64
 54     SHOW_KERNEL_FILE_7 : kernel-core-6.1.7-200.fc37.x86_64
 55     SHOW_KERNEL_FILE_8 : kernel-modules-6.1.7-200.fc37.x86_64
 56     SHOW_KERNEL_FILE_9 : kernel-6.1.7-200.fc37.x86_64
 57     SHOW_KERNEL_FILE_10 : kernel-modules-extra-6.1.7-200.fc37.x86_64
 58     SHOW_KERNEL_FILE_11 : kernel-devel-6.1.7-200.fc37.x86_64
 59 -- List All Kernel Files: end --
 60 
 61 
 62 -- remove old kernel files: starting --
 63   OS->: Delete_Old_Kernel_Version := 6.1.6-200.fc37
 64     REMOVE_KERNEL_FILE_1 : kernel-core-6.1.6-200.fc37.x86_64
 65 Dependencies resolved.
 66 =========================================================================================================================================================================
 67  Package                                         Architecture                      Version                                     Repository                           Size
 68 =========================================================================================================================================================================
 69 Removing:
 70  kernel-core                                     x86_64                            6.1.6-200.fc37                              @updates                             90 M
 71 Removing dependent packages:
 72  kernel                                          x86_64                            6.1.6-200.fc37                              @updates                              0  
 73  kernel-modules                                  x86_64                            6.1.6-200.fc37                              @updates                             57 M
 74  kernel-modules-extra                            x86_64                            6.1.6-200.fc37                              @updates                            3.2 M
 75 
 76 Transaction Summary
 77 =========================================================================================================================================================================
 78 Remove  4 Packages
 79 
 80 Freed space: 150 M
 81 Running transaction check
 82 Transaction check succeeded.
 83 Running transaction test
 84 Transaction test succeeded.
 85 Running transaction
 86   Preparing        :                                                                                                                                                 1/1 
 87   Erasing          : kernel-modules-extra-6.1.6-200.fc37.x86_64                                                                                                      1/4 
 88   Running scriptlet: kernel-modules-extra-6.1.6-200.fc37.x86_64                                                                                                      1/4 
 89   Erasing          : kernel-6.1.6-200.fc37.x86_64                                                                                                                    2/4 
 90   Running scriptlet: kernel-6.1.6-200.fc37.x86_64                                                                                                                    2/4 
 91   Erasing          : kernel-modules-6.1.6-200.fc37.x86_64                                                                                                            3/4 
 92   Running scriptlet: kernel-modules-6.1.6-200.fc37.x86_64                                                                                                            3/4 
 93   Running scriptlet: kernel-core-6.1.6-200.fc37.x86_64                                                                                                               4/4 
 94   Erasing          : kernel-core-6.1.6-200.fc37.x86_64                                                                                                               4/4 
 95   Running scriptlet: kernel-core-6.1.6-200.fc37.x86_64                                                                                                               4/4 
 96   Verifying        : kernel-6.1.6-200.fc37.x86_64                                                                                                                    1/4 
 97   Verifying        : kernel-core-6.1.6-200.fc37.x86_64                                                                                                               2/4 
 98   Verifying        : kernel-modules-6.1.6-200.fc37.x86_64                                                                                                            3/4 
 99   Verifying        : kernel-modules-extra-6.1.6-200.fc37.x86_64                                                                                                      4/4 
100 
101 Removed:
102   kernel-6.1.6-200.fc37.x86_64       kernel-core-6.1.6-200.fc37.x86_64       kernel-modules-6.1.6-200.fc37.x86_64       kernel-modules-extra-6.1.6-200.fc37.x86_64      
103 
104 Complete!
105     REMOVE_KERNEL_FILE_2 : kernel-modules-6.1.6-200.fc37.x86_64
106 No match for argument: kernel-modules-6.1.6-200.fc37.x86_64
107 No packages marked for removal.
108 Dependencies resolved.
109 Nothing to do.
110 Complete!
111     REMOVE_KERNEL_FILE_3 : kernel-6.1.6-200.fc37.x86_64
112 No match for argument: kernel-6.1.6-200.fc37.x86_64
113 No packages marked for removal.
114 Dependencies resolved.
115 Nothing to do.
116 Complete!
117     REMOVE_KERNEL_FILE_4 : kernel-modules-extra-6.1.6-200.fc37.x86_64
118 No match for argument: kernel-modules-extra-6.1.6-200.fc37.x86_64
119 No packages marked for removal.
120 Dependencies resolved.
121 Nothing to do.
122 Complete!
123     REMOVE_KERNEL_FILE_5 : kernel-devel-6.1.6-200.fc37.x86_64
124 Dependencies resolved.
125 =========================================================================================================================================================================
126  Package                                   Architecture                        Version                                       Repository                             Size
127 =========================================================================================================================================================================
128 Removing:
129  kernel-devel                              x86_64                              6.1.6-200.fc37                                @updates                               66 M
130 
131 Transaction Summary
132 =========================================================================================================================================================================
133 Remove  1 Package
134 
135 Freed space: 66 M
136 Running transaction check
137 Transaction check succeeded.
138 Running transaction test
139 Transaction test succeeded.
140 Running transaction
141   Preparing        :                                                                                                                                                 1/1 
142   Erasing          : kernel-devel-6.1.6-200.fc37.x86_64                                                                                                              1/1 
143   Verifying        : kernel-devel-6.1.6-200.fc37.x86_64                                                                                                              1/1 
144 
145 Removed:
146   kernel-devel-6.1.6-200.fc37.x86_64                                                                                                                                     
147 
148 Complete!
149 -- remove old kernel files: over  --
150 
151 
152 -- List All Kernel Files: start --
153     SHOW_KERNEL_FILE_1 : kernel-headers-6.1.5-200.fc37.x86_64
154     SHOW_KERNEL_FILE_2 : kernel-core-6.1.7-200.fc37.x86_64
155     SHOW_KERNEL_FILE_3 : kernel-modules-6.1.7-200.fc37.x86_64
156     SHOW_KERNEL_FILE_4 : kernel-6.1.7-200.fc37.x86_64
157     SHOW_KERNEL_FILE_5 : kernel-modules-extra-6.1.7-200.fc37.x86_64
158     SHOW_KERNEL_FILE_6 : kernel-devel-6.1.7-200.fc37.x86_64
159 -- List All Kernel Files: end --
160 
161 [root@fedora ~]# 
162 [root@fedora ~]# 

 

 

 

 

 

posted on 2022-12-27 00:21  lnlidawei  阅读(330)  评论(0编辑  收藏  举报