/
modprobed_db
114 lines (100 loc) · 3.24 KB
/
modprobed_db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
# modprobed_db by graysky
#
# See this wiki article for more details:
# https://wiki.archlinux.org/index.php/Modprobed_db
#
# The purpose of this little script is to keep track of EVERY single module
# that your system has probed over a time period for the purpose of having the
# perfect amount of modules compiled into your kernel via the make localmodconfig
# option.
#
# The script keeps two files:
# 1) A human readable file: /var/log/modprobe.db
# 2) A very convenient space separated file you can simple cat into a modprobe line
# prior to a kernel build: /var/log/modprobe.long
#
VERS="2.12"
export BLD="\e[01m" RED="\e[01;31m" GRN="\e[01;32m" YLW="\e[01;33m" NRM="\e[00m"
echo -e "${RED}Modprobed_db v$VERS${NRM}"
echo
if [[ -f /etc/modprobed_db.conf ]]; then
. /etc/modprobed_db.conf
else
echo -e "${RED}Error${NRM}${BLD}: ${YLW}/etc/modprobed_db.conf${NRM}${BLD} is missing. Reinstall this package and try again."${NRM}
exit 1
fi
[[ -z $DBPATH ]] && DBPATH="/var/log" # default in conf is not to define one so do it here
db="$DBPATH/modprobed.db"
dblong="$DBPATH/modprobed.long"
list() {
if [[ -f "$db" ]]; then
cat "$db"
dbsize=$(wc -l <"$db")
echo
echo -e ${YLW}"$db ${NRM}${BLD}contains this many records: $dbsize"${NRM}
else
echo -e ${BLD}"No database found."${NRM}
echo -e ${BLD}"Create one via: ${RED}/usr/bin/modprobed_db store${NRM}${BLD} and try again."${NRM}
exit 1
fi
}
recall() {
if [[ $EUID -ne 0 ]]; then
echo -e ${BLD}"This function must be called as root!"${NRM} 1>&2
exit 1
fi
if [[ -f "$dblong" ]]; then
modprobe -a $(cat "$dblong")
echo
echo -e ${BLD}"Done!"${NRM}
else
echo -e ${BLD}"No database found."${NRM}
echo -e ${BLD}"Create one via: ${RED}/usr/bin/modprobed_db store${NRM}${BLD} and try again."${NRM}
exit 1
fi
}
store() {
if [[ ! -f "$db" ]]; then
echo -e ${BLD}"No db present so making ${YLW}$db"${NRM}
cat /proc/modules | gawk '{print $1}' | sort -k 1,1 | grep -Ev "`echo ${IGNORE[*]} | sed -e 's/^/^(/' -e 's/ /|/g' -e 's/$/)$/'`" > "$db"
sed ':a;N;$!ba;s/\n/ /g' "$db" > "$dblong"
fi
dbsize=$(wc -l <"$db")
echo -e ${BLD}"The db currently contains this many records: $dbsize"${NRM}
dbcheck=$(md5sum "$db" | cut -c1-32)
cat /proc/modules | gawk '{print $1}' | sort -k 1,1 | grep -Ev "`echo ${IGNORE[*]} | sed -e 's/^/^(/' -e 's/ /|/g' -e 's/$/)$/'`" > /tmp/.inmem
sort -k 1,1 "$db" /tmp/.inmem | uniq > /tmp/.potential_new_db
newcheck=$(md5sum /tmp/.potential_new_db | cut -c1-32)
if [ "$dbcheck" != "$newcheck" ]; then
WHATSNEW=$(grep -Fxvf "$db" /tmp/.potential_new_db)
echo -e ${YLW}"New module(s) detected: ${NRM}${BLD}$WHATSNEW"${NRM}
cp /tmp/.potential_new_db "$db"
newdbsize=$(wc -l <"$db")
echo -e ${BLD}"The db now contains this many records: $newdbsize"${NRM}
sed ':a;N;$!ba;s/\n/ /g' "$db" > "$dblong"
fi
}
cleanup() {
[[ -f /tmp/.inmem ]] && rm -f /tmp/.inmem
[[ -f /tmp/.potential_new_db ]] && rm -f /tmp/.potential_new_db
}
case $1 in
l|L|List|list)
echo -e ${BLD}"Attempting to list contents of database..."${NRM}
list
exit 0
;;
r|R|Recall|recall)
recall
exit 0
;;
s|S|Store|store)
store
cleanup
;;
*)
echo -e ${BLD}"$0${NRM}${GRN} {list|recall|store}"${NRM}
echo -e ${BLD}"See manpage for additional details"${NRM}
exit 0
esac