forked from Stefar77/cbsd
/
jmkrctlconf
executable file
·109 lines (97 loc) · 2.24 KB
/
jmkrctlconf
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
#!/usr/local/bin/cbsd
#v10.0.2
CBSDMODULE="jail"
MYARG="jname type mode file"
MYDESC="Import or export from/to ascii rctl.conf from/to SQLite3 tables for jail"
ADDHELP="type= rctl or extra\n\"
mode= tosql or to tofile\n\
file= ascii file\n"
. ${subr}
. ${sharedir}/rctl.conf
. ${workdir}/jrctl.subr
. ${tools}
init $*
header() {
cat >${file} <<EOF
# DO NOT EDIT THIS FILE. PLEASE USE INSTEAD:
# cbsd jrctl-tui jname=${jname}
EOF
}
tofile()
{
truncate -s0 ${file}
case "${type}" in
"rctl")
header
for i in ${RCTL}; do
VAL=$( cbsdsql local SELECT ${i} FROM rctl WHERE jname=\"${jname}\" )
[ "${VAL}" = "(null)" -o -z "${VAL}" -o "${VAL}" = "0" ] && continue
case "${i}" in
readbps|writebps|readiops|writeiops)
echo "${i}:throttle=${VAL}" > ${file}
;;
*)
echo "${i}:deny=${VAL}" > ${file}
;;
esac
done
;;
"extra")
header
for i in ${RCTL_EXTRA}; do
VAL=$( cbsdsql local SELECT ${i} FROM rctl WHERE jname=\"${jname}\" )
[ "${VAL}" = "(null)" -o -z "${VAL}" -o "${VAL}" = "0" ] && continue
echo "${i}=${VAL}" > ${file}
done
;;
*)
err 1 "${MAGENTA}type= must be 'rctl' or 'extra'${NORMAL}"
;;
esac
}
tosql()
{
local _myval _items _res
_num=0
[ ! -f "${file}" ] && err 1 "${MAGENTA}No such file: ${GREEN}${file}${NORMAL}"
case "${type}" in
"rctl")
eval $( tr ":=" " " < ${file} | /usr/bin/awk -F " " '{printf("%s=\"%s\"\n", $1,$3); }' )
;;
"extra")
eval $(cat ${file} )
;;
esac
_res=$( cbsdsql local SELECT jname FROM rctl WHERE jname=\"${jname}\" )
[ -z "${_res}" ] && cbsdsql local "INSERT INTO rctl ( jname ) VALUES ( \"${jname}\" )"
case "${type}" in
"rctl")
for _items in ${RCTL}; do
eval _myval=\$${_items}
[ -z "${_myval}" ] && _myval="0"
cbsdsql local UPDATE rctl SET ${_items}=\"${_myval}\" WHERE jname=\"${jname}\"
done
;;
"extra")
for _items in ${RCTL_EXTRA}; do
eval _myval=\$${_items}
[ -z "${_myval}" ] && _myval="0"
cbsdsql local UPDATE rctl SET ${_items}=\"${_myval}\" WHERE jname=\"${jname}\"
done
;;
esac
}
# MAIN
case "${mode}" in
"tofile")
tofile
;;
"tosql")
tosql
;;
*)
err 1 "${MAGENTA}mode= must be 'tosql' or 'tofile'${NORMAL}"
;;
esac
# exit code 0 is nessesary for dot()
exit 0