-
Notifications
You must be signed in to change notification settings - Fork 0
/
klog
executable file
·215 lines (190 loc) · 4.37 KB
/
klog
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
#
USAGE="usage: klog [make (ovs <ovs dir>|alone) (verbose|silent), start <path>, stop]"
#
# Description
#
# `klog make`, defaulting to `alone` and `silent`, builds a standalone module.
# For openvswitch support, type `klog make ovs <ovs dir>`.
#
# <ovs dir> points to the ovs directory.
#
# The last argument of `klog make` is always either `verbose` or `silent`.
#
# Once either module is compiled, load it with `klog start`.
#
# If compiled with openvswitch, start with
# `klog start <ovs dir>/datapath/linux`, i.e. the path to the module.
#
# Unload it with
# `klog stop`.
#
# In summary:
#
# a) For a stand-alone version,
#
# $ sudo klog make
# $ sudo klog start
#
# b) For a version compatible with openvswitch 1.2.1 (cf. homework at
# github),
#
# $ sudo klog make ovs $HOME/homework/openvswitch.git
# $ sudo klog start $HOME/homework/openvswitch.git/datapath/linux
#
VERBOSE=false
klog_perror () {
[ -z "$1" ] && echo "error" || echo "$@"
exit 1
}
klog_conf_log () {
[ ! -e "debug.h" ] && klog_perror "debug.h not found"
if [ \( "$1" != "silent" \) -a \( "$1" != "verbose" \) ]; then
return 1
fi
# edit debug.h
rm -f debug.tmp
next=false
tf="debug.tmp"
while read line; do
if $next; then
if [ "$1" == "silent" ]; then
echo "#undef HWDB_VERBOSE" >> $tf
else
echo "#define HWDB_VERBOSE" >> $tf
fi
next=false
else
echo "$line" >> $tf
fi
[ "$line" = "/*klog:var:log*/" ] && next=false
done < debug.h
[ ! -f $tf ] && return 1
mv $tf debug.h
return 0
}
klog_have_ovs () {
[ ! -e "klogger.h" ] && klog_perror "klogger.h not found"
# edit klogger.h
rm -f klogger.tmp
next=false
tf="klogger.tmp"
while read line; do
if $next; then
if ! $1; then
echo "#undef OVS_HWDB" >> $tf
else
echo "#define OVS_HWDB" >> $tf
fi
next=false
else
echo "$line" >> $tf
fi
[ "$line" = "/*klog:var:ovs*/" ] && next=true
done < klogger.h
[ ! -f $tf ] && return 1
mv $tf klogger.h
return 0
}
klog_conf_ovs () {
OVS=$1
actions=$OVS/datapath/actions.c
modules=$OVS/datapath/linux/Modules.mk
[ ! -e "$actions" ] && klog_perror "$actions not found"
[ ! -e "$modules" ] && klog_perror "$modules not found"
t=`pwd`
diff $t/patches/actions.c $actions &>/dev/null
if [ $? -eq 1 ]; then
cp $actions ${actions}.orig
$VERBOSE && echo "$actions => ${actions}.orig"
cp $t/patches/actions.c $actions
$VERBOSE && echo "$t/patches/actions.c => $actions"
fi
diff $t/patches/Modules.mk $modules &>/dev/null
if [ $? -eq 1 ]; then
cp $modules ${modules}.orig
$VERBOSE && echo "$modules => ${modules}.orig"
cp $t/patches/Modules.mk $modules
$VERBOSE && echo "$t/patches/Modules.mk => $modules"
fi
# cp files
cp klogger.* hashtable.* hwdbflow.* khwdb.h debug.h \
$OVS/datapath/linux/
# make
cd $OVS/datapath/
make
cd $t
}
[ $# -eq 0 ] && klog_perror $USAGE
$VERBOSE && echo "[args] $@"
action="$1"
case "$action" in
"make")
[ $# -eq 1 ] && klog_perror $USAGE
shift 1
$VERBOSE && echo "[args] $@"
[ $# -lt 2 ] && klog_perror $USAGE
# There is at least a second argument
if [ "$1" == "alone" ]; then
make clean
klog_have_ovs false
klog_conf_log "$2" || klog_perror $USAGE
make
exit $? # done
elif [ "$1" == "ovs" ]; then
shift 1
$VERBOSE && echo "[args] $@"
[ $# -ne 2 ] && klog_perror $USAGE
# Next argument is a directory
[ ! -d "$1" ] && klog_perror $USAGE
make clean
klog_have_ovs true
klog_conf_log "$2" || klog_perror $USAGE
klog_conf_ovs "$1"
exit $? # done
else
plog_error $USAGE
fi
;;
"start")
DIR=
MOD="klogger"
[ $# -eq 2 ] && DIR=$2
if [ -n $DIR ]; then
[ ! -d $DIR ] && klog_perror "$DIR not found"
MOD="$DIR/klogger_mod"
fi
/sbin/insmod $MOD.ko || klog_perror "failed to load $DIR/$MOD.ko"
# retrieve major number
major=$(awk "\$2==\"hwdb\" {print \$1}" /proc/devices)
$VERBOSE && echo "hwdb's major number $major"
# remove stale nodes
rm -f /dev/hwdb[0-3]
# klogger has four devices
mknod /dev/hwdb0 c $major 0
mknod /dev/hwdb1 c $major 1
mknod /dev/hwdb2 c $major 2
mknod /dev/hwdb3 c $major 3
ln -sf hwdb0 /dev/hwdb
if grep -q '^staff:' /etc/group; then
group="staff"
else
group="wheel"
fi
chgrp $group /dev/hwdb[0-3]
chmod 667 /dev/hwdb[0-3] # or 664
exit 0
;;
"stop")
MOD="klogger"
/sbin/lsmod | grep klogger_mod &>/dev/null
[ $? -eq 0 ] && MOD="klogger_mod"
/sbin/rmmod $MOD 2>/dev/null || klog_perror "$MOD not loaded"
rm -f /dev/hwdb /dev/hwdb[0-3]
exit 0
;;
*)
klog_perror $USAGE
;;
esac
exit 1