]> git.draconx.ca Git - scripts.git/blob - clean-modules.zsh
Add script to convert "JWK"-format RSA keys to normal.
[scripts.git] / clean-modules.zsh
1 #!/usr/bin/env zsh
2
3 # Copyright © 2012 Nick Bowler
4 #
5 # Prunes directories in /lib/modules that do not correspond to any kernel
6 # version in /boot.  If the -f option is not specified, only print out what
7 # will be removed.
8 #
9 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
10 # This is free software: you are free to do what the fuck you want to.
11 # There is NO WARRANTY, to the extent permitted by law.
12
13 force=no
14 verbose=no
15 while getopts 'fv' opt $@; do
16         case $opt in
17         f) force=yes ;;
18         v) verbose=yes ;;
19         \?) exit 1 ;;
20         esac
21 done
22
23 [[ force = yes ]] || verbose=yes
24
25 boot=/boot
26 kernbase=$boot/vmlinuz-
27 kernels=($kernbase*(N))
28
29 if ! [[ $#kernels -ge 1 ]]; then
30         printf '%s: no kernels found in %s, aborting\n' "$0" "$boot"
31         exit 1
32 fi
33
34 typeset -A kernmap
35 for v in ${${kernels%.old}#$kernbase}; do
36         kernmap[$v]=false
37 done
38
39 modbase=/lib/modules
40 for d in $modbase/*; do
41         v=${d#$modbase/}
42         if $kernmap[$v] :; then
43                 [[ $verbose = yes ]] && printf '%s\n' "$d"
44                 [[ $force = yes   ]] && rm -rf "$d"
45         fi
46 done