-
Notifications
You must be signed in to change notification settings - Fork 0
/
carp.subr
226 lines (175 loc) · 5.64 KB
/
carp.subr
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
215
216
217
218
219
220
221
222
223
224
225
226
if [ ! "$_CBSD_CARP_SUBR" ]; then
_CBSD_CARP_SUBR=1
###
carp_init()
{
/sbin/kldstat -qm carp || /sbin/kldload carp
if [ ! -f "/usr/local/etc/devd/cbsd_carp.conf" ]; then
[ ! -d "/usr/local/etc/devd" ] && mkdir -p /usr/local/etc/devd
/bin/cp ${sharedir}/cbsd_carp.conf /usr/local/etc/devd/
/usr/sbin/service devd restart
${ECHO} "${MAGENTA}DEVD configuration for cbsd carp created: ${GREEN}/usr/local/etc/devd/cbsd_carp.conf${MAGENTA}"
fi
}
# $1 - id
# return 1 if vhid already set and print ip for it
get_ip4_by_vhid()
{
local _res
if [ -z "${ip}" ]; then
_res=$( /sbin/ifconfig ${interface}| ${GREP_CMD} "inet *.*.*.* netmask [[:alnum:]]* *.*.*.* vhid ${1} "|/usr/bin/awk {'printf $2" "'} )
else
ipwmask "${ip}"
[ -n "${IWM}" ] && ip="${IWM}"
_res=$( /sbin/ifconfig ${interface}| ${GREP_CMD} "inet ${ip} netmask [[:alnum:]]* *.*.*.* vhid ${1} "|/usr/bin/awk {'printf $2" "'} )
fi
[ -z "${_res}" ] && return 0
printf "${_res}"
return 1
}
carp_unset()
{
local _res
local _resip
_res=$( get_ip4_by_vhid ${vhid} )
[ $? -eq 0 ] && err 1 "${MAGENTA}vhid not initializated: ${GREEN}${vhid}${NORMAL}"
# iptype ?
for _resip in ${_res}; do
/sbin/ifconfig ${interface} inet ${_resip} -alias
done
}
check_vhid()
{
local vhid_count=0 _res=
if [ -n "${vhid}" ]; then
is_number ${vhid} && err 1 "${MAGENTA}Error: vhid should be number${NORMAL}"
else
err 1 "${MAGENTA}No such mandatory params: ${GREEN}vhid, e.g. vhid=1${NORMAL}"
fi
# we are prefered settings from CLI. Skip SQLite query when params already set:
_res=1
[ -z "${advskew}" ] && _res=$(( _res * 0 ))
[ -z "${pass}" ] && _res=$(( _res * 0 ))
[ -z "${interface}" ] && _res=$(( _res * 0 ))
[ -z "${state}" ] && _res=$(( _res * 0 ))
[ ${_res} -eq 1 ] && return 0
vhid_count=$( cbsdsql local SELECT count\(id\) FROM carp WHERE vhid=\"${vhid}\" )
[ "${vhid_count}" = "0" ] && err 1 "${MAGENTA}Error: no vhid in database with id: ${GREEN}${vhid}${NORMAL}"
_res=$( cbsdsql local SELECT id,vhid,advskew,pass,interface,state FROM carp WHERE vhid=\"${vhid}\" 2>/dev/null )
[ -z "${_res}" ] && err 1 "${MAGETA}Wrong data structure in carp database for vhid: ${GREEN}${vhid}${NORMAL}"
sqllist "${_res}" id vhid advskew pass interface state
}
# return jname list who have vhid = $1
get_jname_by_vhid()
{
[ -z "${1}" ] && return 1
local vhid="${1}"
local pattern="vhid_${vhid}#"
local jname
jname=$( cbsdsql local SELECT jname FROM jails WHERE ip4_addr LIKE \"%${pattern}%\" AND emulator != \"bhyve\" | /usr/bin/xargs )
[ -n "${jname}" ] && echo "${jname}"
}
check_interface()
{
local _res
if [ "${interface}" != "auto" ]; then
# check for interface exist
_res=$( ${miscdir}/nics-list -s "lo" |while read _iface; do
[ "${interface}" = "${_iface}" ] && echo "${_iface}" ] && exit 0
done )
[ -z "${_res}" ] && err 1 "${MAGENTA}No such interface: ${GREEN}${interface}${NORMAL}"
else
auto_iface=$( /sbin/route -n get 0.0.0.0 |/usr/bin/awk '/interface/{print $2}' )
[ -z "${auto_iface}" ] && err 1 "${MAGENTA}Can't determine uplink interface${NORMAL}"
interface="${auto_iface}"
fi
[ -z "${interface}" ] && err 1 "${MAGENTA}No such interface: ${GREEN}${interface}, e.g: interface=auto${NORMAL}"
return 0
}
carp_set()
{
local _res _state i
# do not set ip when already exist
if [ ${show} -eq 0 ]; then
_res=$( get_ip4_by_vhid ${vhid} )
ipwmask ${ip}
for i in ${_res}; do
[ "${i}" = "${IWM}" ] && err 1 "${MAGENTA}vhid ${GREEN}${vhid}${MAGENTA} already initializated for: ${GREEN}${_res}${NORMAL}"
done
fi
[ -z "${advskew}" ] && err 1 "${MAGENTA}No such mandatory params: ${GREEN}advskew, e.g: advskew=100${NORMAL}"
[ -z "${pass}" ] && err 1 "${MAGENTA}No such mandatory params: ${GREEN}pass, e.g: pass=pass${NORMAL}"
[ -z "${ip}" ] && err 1 "${MAGENTA}No such mandatory params: ${GREEN}ip, e.g: ip=10.10.10.10/24${NORMAL}"
[ -n "${state}" ] && _state="state ${state}"
if [ ${show} -eq 1 ]; then
echo "${interface}|${ip} vhid ${vhid} advskew ${advskew} pass ${pass} ${_state}"
return 0
else
echo "[DEBUG] /sbin/ifconfig ${interface} vhid ${vhid} advskew ${advskew} pass ${pass} ${ip} ${_state} alias"
fi
/sbin/ifconfig ${interface} vhid ${vhid} advskew ${advskew} pass ${pass} ${ip} ${_state} alias
}
carp_update()
{
local _res _cmd
[ -n "${advskew}" ] && _cmd="${_cmd} advskew ${advskew}"
[ -n "${pass}" ] && _cmd="${_cmd} pass ${pass}"
[ -n "${state}" ] && _cmd="${_cmd} state ${state}"
# must be last
[ -n "${ip}" ] && _cmd="${_cmd} ${ip} alias"
[ -z "${_cmd}" ] && return 1
echo "/sbin/ifconfig ${interface} vhid ${vhid} ${_cmd}"
/sbin/ifconfig ${interface} vhid ${vhid} ${_cmd}
}
# form for $allow_sysvipc
get_construct_carp_state()
{
local _input _res
local GET_CARP_STATE_MSG="Default state on boot"
local title="${GET_ALLOW_SYSVIPC_MSG}"
local defaultitem="${allow_sysvipc}"
local menu_list="
'master' 'master' 'Primary role, active state'
'backup' 'backup' 'Secondary/Standby role, passive'
" # END-QUOTE
cbsd_menubox
retval=$?
case $retval in
${DIALOG_OK})
[ -n "${mtag}" ] && carp_state="${mtag}"
;;
*)
;;
esac
return ${retval}
}
get_construct_carp_vhid()
{
title=" vhid "
prompt=" Enter vhid "
defaultitem="${carp_vhid}"
cbsd_inputbox_simple && carp_vhid="${mtag}"
}
get_construct_carp_advskew()
{
title=" advskew "
prompt=" Enter advskew "
defaultitem="${carp_advskew}"
cbsd_inputbox_simple && carp_advskew="${mtag}"
}
get_construct_carp_pass()
{
title=" pass "
prompt=" Enter pass "
defaultitem="${carp_pass}"
cbsd_inputbox_simple && carp_pass="${mtag}"
}
get_construct_carp_interface()
{
title=" pass "
prompt=" Enter pass "
defaultitem="${carp_pass}"
get_construct_interface -d 1 -s "tap bridge vboxnet"
}
###
fi