Debian Codegraph @

heartbeat/iface-macvlan

raw
1 #!/bin/sh
2 #
3 # OCF Resource Agent compliant iface-macvlan script.
4 #
5 # Implements network MACVLAN interface management
6 #
7 # Resource script for MACVLAN dervice from Fabio M. Di Nitto iface-vlan
8 # script.
9 #
10 # Author: Ulrich Goettlich
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of version 2 of the GNU General Public License as
14 # published by the Free Software Foundation.
15 #
16 # This program is distributed in the hope that it would be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 #
20 # Further, this software is distributed without any warranty that it is
21 # free of the rightful claim of any third person regarding infringement
22 # or the like. Any license provided herein, whether implied or
23 # otherwise, applies only to this software file. Patent licenses, if
24 # any, provided herein do not apply to combinations of this program with
25 # other software, or any other product whatsoever.
26 #
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write the Free Software Foundation,
29 # Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
30 #
31 #
32
33 # TODO:
34 #
35 # OCF parameters are as below
36 # OCF_RESKEY_device
37 # OCF_RESKEY_name
38 # OCF_RESKEY_mode
39 # OCF_RESKEY_mac
40 #
41
42 #######################################################################
43 # Initialization:
44
45 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
46 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
47
48 # Defaults
49 OCF_RESKEY_mode_default="bridge"
50
51 : ${OCF_RESKEY_mode=${OCF_RESKEY_mode_default}}
52
53 #######################################################################
54
55 macvlan_usage() {
56 cat <<END
57 usage: $0 {start|stop|status|monitor|validate-all|meta-data}
58
59 Expects to have a fully populated OCF RA-compliant environment set.
60 END
61 }
62
63 macvlan_meta_data() {
64 cat <<END
65 <?xml version="1.0"?>
66 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
67 <resource-agent name="iface-macvlan" version="1.0">
68 <version>1.0</version>
69
70 <longdesc lang="en">
71 This resource manages MACVLAN network interfaces.
72 It can add, remove, configure MACVLANs.
73 </longdesc>
74
75 <shortdesc lang="en">
76 Manages MACVLAN network interfaces.
77 </shortdesc>
78
79 <parameters>
80 <parameter name="device" unique="0" required="1">
81 <longdesc lang="en">
82 Define the interface where MACVLAN should be attached.
83 </longdesc>
84 <shortdesc lang="en">
85 Network interface.
86 </shortdesc>
87 <content type="string"/>
88 </parameter>
89
90 <parameter name="name" unique="1" required="1">
91 <longdesc lang="en">
92 Define the MACVLAN NAME. It has to be a valid interface name (max 15 characters).
93 </longdesc>
94 <shortdesc lang="en">
95 Define the MACVLAN NAME.
96 </shortdesc>
97 <content type="string"/>
98 </parameter>
99
100 <parameter name="mode" unique="0">
101 <longdesc lang="en">
102 Define the name of the MACVLAN mode (currently only bridge is supported).
103 </longdesc>
104 <shortdesc lang="en">
105 Mode of the macvlan.
106 </shortdesc>
107 <content type="string" default="${OCF_RESKEY_mode_default}" />
108 </parameter>
109
110 <parameter name="mac" unique="1">
111 <longdesc lang="en">
112 Set the interface MAC address explicitly.
113 </longdesc>
114 <shortdesc lang="en">MAC address of the macvlan</shortdesc>
115 <content type="string" />
116 </parameter>
117 </parameters>
118
119 <actions>
120 <action name="start" timeout="30s" />
121 <action name="stop" timeout="20s" />
122 <action name="status" timeout="20s" depth="0" interval="10s" />
123 <action name="monitor" timeout="20s" depth="0" interval="10s" />
124 <action name="meta-data" timeout="5s" />
125 <action name="validate-all" timeout="20s" />
126 </actions>
127 </resource-agent>
128 END
129 }
130
131 # check if the interface is admin up/down
132
133 iface_is_up() {
134 if ! $IP2UTIL -o link show $1 | \
135 sed -e 's#.*<##g' -e 's#>.*##' -e 's#LOWER_UP##g' | \
136 grep -q UP; then
137 return 1
138 fi
139 return 0
140 }
141
142 # check if the slaves have link layer up/down
143 # see kernel network documentation on meaning of LOWER_UP flag
144 # for more in depth explanation on how it works
145 # NOTE: this check is not reliable in virt environment
146 # since interfaces are always LOWER_UP. There is no way
147 # from the guest to know if the host has disconnected somehow
148
149 iface_lower_is_up() {
150 if ! $IP2UTIL -o link show $1 | \
151 grep -q LOWER_UP; then
152 return 1
153 fi
154 return 0
155 }
156
157 macvlan_validate() {
158 check_binary $IP2UTIL
159
160 if [ -z "$OCF_RESKEY_device" ]; then
161 ocf_log err "Invalid device: value cannot be empty"
162 return $OCF_ERR_CONFIGURED
163 fi
164
165 if [ -z "$OCF_RESKEY_name" ]; then
166 ocf_log err "Invalid name: value cannot be empty"
167 return $OCF_ERR_CONFIGURED
168 fi
169
170 # the echo .. is the equivalent of strlen in bash
171 #
172 # /usr/include/linux/if.h:#define IFNAMSIZ 16
173 # needs to include 0 byte end string
174
175 if [ "${#OCF_RESKEY_name}" -gt 15 ]; then
176 ocf_log err "Invalid name: name is too long"
177 return $OCF_ERR_CONFIGURED
178 fi
179
180 if [ ! -d "/sys/class/net" ]; then
181 ocf_log err "Unable to find sysfs network class in /sys"
182 return $OCF_ERR_GENERIC
183 fi
184
185 if [ ! -e "/sys/class/net/$OCF_RESKEY_device" ]; then
186 ocf_log err "Invalid device: $OCF_RESKEY_device does not exists"
187 return $OCF_ERR_ARGS
188 fi
189
190 if [ "${OCF_RESKEY_mode}" != "bridge" ]; then
191 ocf_log err "Invalid mode: only bridge mode is currently supported (because other modes are not tested)"
192 return $OCF_ERR_CONFIGURED
193 fi
194
195 return 0
196 }
197
198 macvlan_check() {
199 if [ -e "/sys/class/net/$OCF_RESKEY_name" ]; then
200 if [ ! -e "$HA_RSCTMP/iface-macvlan.$OCF_RESKEY_name" ]; then
201 return $OCF_ERR_GENERIC
202 fi
203 else
204 if [ -e "$HA_RSCTMP/iface-macvlan.$OCF_RESKEY_name" ]; then
205 error="$(rm -f "$HA_RSCTMP/iface-macvlan.$OCF_RESKEY_name" 2>&1)"
206 if [ "$?" != "0" ]; then
207 ocf_log err "Unable to remove stale lock file for macvlan $OCF_RESKEY_name: $error"
208 return $OCF_ERR_INSTALLED
209 fi
210 fi
211 return $OCF_NOT_RUNNING
212 fi
213
214 if ! iface_is_up $OCF_RESKEY_name; then
215 ocf_log err "MACVLAN $OCF_RESKEY_name is administratively down"
216 return $OCF_ERR_GENERIC
217 fi
218
219 if ! iface_lower_is_up $OCF_RESKEY_name; then
220 ocf_log err "MACVLAN $OCF_RESKEY_name has no active link-layer"
221 return $OCF_ERR_GENERIC
222 fi
223
224 return $OCF_SUCCESS
225 }
226
227 # we need a simpler stop version to clean after us if start fails
228 # without involving any error checking
229 # rolling back in case of failure is otherwise complex
230
231 macvlan_force_stop() {
232 $IP2UTIL link delete "$OCF_RESKEY_name" >/dev/null 2>&1
233 rm -f "$HA_RSCTMP/iface-macvlan.$OCF_RESKEY_name" 2>&1
234 }
235
236 macvlan_start() {
237 # check if the macvlan already exists
238 macvlan_check
239 ret=$?
240 if [ "$ret" != "$OCF_NOT_RUNNING" ]; then
241 return $ret
242 fi
243
244 # create the MACVLAN
245 set_specific_mac=""
246 [ -n "${OCF_RESKEY_mac}" ] && set_specific_mac="address ${OCF_RESKEY_mac}"
247 error="$($IP2UTIL link add link "$OCF_RESKEY_device" ${set_specific_mac} name "$OCF_RESKEY_name" type macvlan mode "$OCF_RESKEY_mode" 2>&1)"
248 if [ "$?" != "0" ]; then
249 ocf_log err "Unable to create MACVLAN $OCF_RESKEY_name: $error"
250 return $OCF_ERR_GENERIC
251 fi
252
253 # set the interface up
254 error="$($IP2UTIL link set dev "$OCF_RESKEY_device" up 2>&1)"
255 if [ "$?" != "0" ]; then
256 ocf_log err "Unable to set MACVLAN $OCF_RESKEY_device up: $error"
257 return $OCF_ERR_GENERIC
258 fi
259
260 # set the macvlan up
261 error="$($IP2UTIL link set dev "$OCF_RESKEY_name" up 2>&1)"
262 if [ "$?" != "0" ]; then
263 ocf_log err "Unable to set MACVLAN $OCF_RESKEY_name up: $error"
264 return $OCF_ERR_GENERIC
265 fi
266
267 error="$(touch "$HA_RSCTMP/iface-macvlan.$OCF_RESKEY_name" 2>&1)"
268 if [ "$?" != "0" ]; then
269 ocf_log err "Unable to create lock file for MACVLAN $OCF_RESKEY_name: $error"
270 return $OCF_ERR_GENERIC
271 fi
272
273 return $OCF_SUCCESS
274 }
275
276 macvlan_stop() {
277 macvlan_check
278 ret=$?
279 if [ "$ret" = "$OCF_NOT_RUNNING" ]; then
280 return $OCF_SUCCESS
281 fi
282 if [ "$ret" != "$OCF_SUCCESS" ]; then
283 return $ret
284 fi
285
286 # set macvlan down
287 error="$($IP2UTIL link set dev "$OCF_RESKEY_name" down 2>&1)"
288 if [ "$?" != "0" ]; then
289 ocf_log err "Unable to set MACVLAN $OCF_RESKEY_name down: $error"
290 return $OCF_ERR_GENERIC
291 fi
292
293 # delete macvlan
294 error="$($IP2UTIL link delete "$OCF_RESKEY_name" 2>&1)"
295 if [ "$?" != "0" ]; then
296 ocf_log err "Unable to delete MACVLAN $OCF_RESKEY_name: $error"
297 return $OCF_ERR_GENERIC
298 fi
299
300 error="$(rm -f "$HA_RSCTMP/iface-macvlan.$OCF_RESKEY_name" 2>&1)"
301 if [ "$?" != "0" ]; then
302 ocf_log err "Unable to remove lock file for MACVLAN $OCF_RESKEY_name: $error"
303 return $OCF_ERR_GENERIC
304 fi
305
306 return $OCF_SUCCESS
307 }
308
309 case $__OCF_ACTION in
310 meta-data)
311 macvlan_meta_data
312 exit $OCF_SUCCESS
313 ;;
314 usage|help)
315 macvlan_usage
316 exit $OCF_SUCCESS
317 ;;
318 esac
319
320 if [ ! -d "$HA_RSCTMP" ]; then
321 ocf_log debug "$HA_RSCTMP not found, we are probably being executed manually"
322 mkdir -p "$HA_RSCTMP"
323 fi
324
325 if [ -n "$__OCF_ACTION" ] && ! macvlan_validate; then
326 exit $OCF_ERR_CONFIGURED
327 fi
328
329 case $__OCF_ACTION in
330 start|stop)
331 if ! ocf_is_root; then
332 ocf_log err "You must be root for $__OCF_ACTION operation."
333 exit $OCF_ERR_PERM
334 fi
335 ;;
336 esac
337
338 case $__OCF_ACTION in
339 start)
340 macvlan_start
341 ret=$?
342 if [ "$ret" != "$OCF_SUCCESS" ]; then
343 macvlan_force_stop
344 fi
345 exit $ret
346 ;;
347 stop)
348 macvlan_stop
349 exit $?
350 ;;
351 status|monitor)
352 macvlan_check
353 exit $?
354 ;;
355 validate-all)
356 # macvlan_validate above does the trick
357 ;;
358 *)
359 macvlan_usage
360 exit $OCF_ERR_UNIMPLEMENTED
361 ;;
362 esac
363 # vi:sw=4:ts=8: