| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# OCF Resource Agent compliant iface-vlan script. |
| 4 |
# |
| 5 |
# Implements network VLAN interface management |
| 6 |
# |
| 7 |
# Copyright (C) 2013 Red Hat, Inc. All rights reserved. |
| 8 |
# Author: Fabio M. Di Nitto <[email protected]> |
| 9 |
# |
| 10 |
# This program is free software; you can redistribute it and/or modify |
| 11 |
# it under the terms of version 2 of the GNU General Public License as |
| 12 |
# published by the Free Software Foundation. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it would be useful, but |
| 15 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 |
# |
| 18 |
# Further, this software is distributed without any warranty that it is |
| 19 |
# free of the rightful claim of any third person regarding infringement |
| 20 |
# or the like. Any license provided herein, whether implied or |
| 21 |
# otherwise, applies only to this software file. Patent licenses, if |
| 22 |
# any, provided herein do not apply to combinations of this program with |
| 23 |
# other software, or any other product whatsoever. |
| 24 |
# |
| 25 |
# You should have received a copy of the GNU General Public License |
| 26 |
# along with this program; if not, write the Free Software Foundation, |
| 27 |
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 28 |
# |
| 29 |
# |
| 30 |
|
| 31 |
# TODO: |
| 32 |
# |
| 33 |
# OCF parameters are as below |
| 34 |
# OCF_RESKEY_vlan_interface |
| 35 |
# OCF_RESKEY_vlan_id |
| 36 |
# OCF_RESKEY_vlan_name |
| 37 |
# OCF_RESKEY_vlan_reorder_hdr |
| 38 |
# OCF_RESKEY_vlan_gvrp |
| 39 |
# OCF_RESKEY_vlan_mvrp |
| 40 |
# OCF_RESKEY_vlan_loose_binding |
| 41 |
# |
| 42 |
|
| 43 |
####################################################################### |
| 44 |
# Initialization: |
| 45 |
|
| 46 |
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat} |
| 47 |
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs |
| 48 |
|
| 49 |
# Defaults |
| 50 |
OCF_RESKEY_vlan_reorder_hdr_default=1 |
| 51 |
OCF_RESKEY_vlan_gvrp_default=0 |
| 52 |
OCF_RESKEY_vlan_mvrp_default=0 |
| 53 |
OCF_RESKEY_vlan_loose_binding_default=0 |
| 54 |
OCF_RESKEY_vlan_name_default=${OCF_RESKEY_vlan_interface}.${OCF_RESKEY_vlan_id} |
| 55 |
|
| 56 |
: ${OCF_RESKEY_vlan_name=${OCF_RESKEY_vlan_name_default}} |
| 57 |
: ${OCF_RESKEY_vlan_reorder_hdr=${OCF_RESKEY_vlan_reorder_hdr_default}} |
| 58 |
: ${OCF_RESKEY_vlan_gvrp=${OCF_RESKEY_vlan_gvrp_default}} |
| 59 |
|
| 60 |
# don't set defaults for mvrp or loose binding since both |
| 61 |
# are rather new kernel features and they might not be supported |
| 62 |
#: ${OCF_RESKEY_vlan_mvrp=${OCF_RESKEY_vlan_mvrp_default}} |
| 63 |
#: ${OCF_RESKEY_vlan_loose_binding=${OCF_RESKEY_vlan_loose_binding_default}} |
| 64 |
|
| 65 |
####################################################################### |
| 66 |
|
| 67 |
vlan_usage() { |
| 68 |
cat <<END |
| 69 |
usage: $0 {start|stop|status|monitor|validate-all|meta-data} |
| 70 |
|
| 71 |
Expects to have a fully populated OCF RA-compliant environment set. |
| 72 |
END |
| 73 |
} |
| 74 |
|
| 75 |
vlan_meta_data() { |
| 76 |
cat <<END |
| 77 |
<?xml version="1.0"?> |
| 78 |
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> |
| 79 |
<resource-agent name="iface-vlan" version="1.0"> |
| 80 |
<version>1.0</version> |
| 81 |
|
| 82 |
<longdesc lang="en"> |
| 83 |
This resource manages VLAN network interfaces. |
| 84 |
It can add, remove, configure VLANs. |
| 85 |
</longdesc> |
| 86 |
|
| 87 |
<shortdesc lang="en"> |
| 88 |
Manages VLAN network interfaces. |
| 89 |
</shortdesc> |
| 90 |
|
| 91 |
<parameters> |
| 92 |
<parameter name="vlan_interface" unique="0" required="1"> |
| 93 |
<longdesc lang="en"> |
| 94 |
Define the interface where VLAN should be attached. |
| 95 |
</longdesc> |
| 96 |
<shortdesc lang="en"> |
| 97 |
Network interface. |
| 98 |
</shortdesc> |
| 99 |
<content type="string"/> |
| 100 |
</parameter> |
| 101 |
|
| 102 |
<parameter name="vlan_id" unique="0" required="1"> |
| 103 |
<longdesc lang="en"> |
| 104 |
Define the VLAN ID. It has to be a value between 0 and 4094. |
| 105 |
</longdesc> |
| 106 |
<shortdesc lang="en"> |
| 107 |
Define the VLAN ID. |
| 108 |
</shortdesc> |
| 109 |
<content type="integer"/> |
| 110 |
</parameter> |
| 111 |
|
| 112 |
<parameter name="vlan_name" unique="1"> |
| 113 |
<longdesc lang="en"> |
| 114 |
Define the name of the VLAN interface (max 15 charaters). |
| 115 |
</longdesc> |
| 116 |
<shortdesc lang="en"> |
| 117 |
Name of the VLAN. |
| 118 |
</shortdesc> |
| 119 |
<content type="string" default="${OCF_RESKEY_vlan_name_default}" /> |
| 120 |
</parameter> |
| 121 |
|
| 122 |
<parameter name="vlan_reorder_hdr" unique="0"> |
| 123 |
<longdesc lang="en"> |
| 124 |
Enable or disable header reordering. |
| 125 |
</longdesc> |
| 126 |
<shortdesc lang="en"> |
| 127 |
Enable or disable header reordering. |
| 128 |
</shortdesc> |
| 129 |
<content type="boolean" default="${OCF_RESKEY_vlan_reorder_hdr_default}"/> |
| 130 |
</parameter> |
| 131 |
|
| 132 |
<parameter name="vlan_gvrp" unique="0"> |
| 133 |
<longdesc lang="en"> |
| 134 |
Enable or disable GARP VLAN registration protocol. |
| 135 |
</longdesc> |
| 136 |
<shortdesc lang="en"> |
| 137 |
Enable or disable gvrp. |
| 138 |
</shortdesc> |
| 139 |
<content type="boolean" default="${OCF_RESKEY_vlan_gvrp_default}"/> |
| 140 |
</parameter> |
| 141 |
|
| 142 |
<parameter name="vlan_mvrp" unique="0"> |
| 143 |
<longdesc lang="en"> |
| 144 |
Enable or disable Multiple VLAN Registration Protocol. |
| 145 |
Please note that most distributions do not ship a version of iproute2 |
| 146 |
that supports mvrp yet, even if the kernel has support for it. |
| 147 |
Check output of $IPADDR2 link add type vlan --help in the FLAG |
| 148 |
section to verify if mvrp support is available. |
| 149 |
</longdesc> |
| 150 |
<shortdesc lang="en"> |
| 151 |
Enable or disable mvrp. |
| 152 |
</shortdesc> |
| 153 |
<content type="boolean" default="${OCF_RESKEY_vlan_mvrp_default}"/> |
| 154 |
</parameter> |
| 155 |
|
| 156 |
<parameter name="vlan_loose_binding" unique="0"> |
| 157 |
<longdesc lang="en"> |
| 158 |
Enable or disable VLAN loose bind. By default the VLAN interface |
| 159 |
admin status (UP/DOWN) follows the underneath interface status. |
| 160 |
Enabling loose bind allows the VLAN to disconnect from the |
| 161 |
interface status. Be very careful that enabling loose binding |
| 162 |
could invalidate this agent monitor operations. |
| 163 |
Please note that most distributions do not ship a version of iproute2 |
| 164 |
that supports loose_binding yet, even if the kernel has support for it. |
| 165 |
Check output of $IPADDR2 link add type vlan --help in the FLAG |
| 166 |
section to verify if loose_binding support is available. |
| 167 |
</longdesc> |
| 168 |
<shortdesc lang="en"> |
| 169 |
Enable or disable loose binding. |
| 170 |
</shortdesc> |
| 171 |
<content type="boolean" default="${OCF_RESKEY_vlan_loose_binding_default}"/> |
| 172 |
</parameter> |
| 173 |
</parameters> |
| 174 |
|
| 175 |
<actions> |
| 176 |
<action name="start" timeout="30s" /> |
| 177 |
<action name="stop" timeout="20s" /> |
| 178 |
<action name="status" timeout="20s" depth="0" interval="10s" /> |
| 179 |
<action name="monitor" timeout="20s" depth="0" interval="10s" /> |
| 180 |
<action name="meta-data" timeout="5s" /> |
| 181 |
<action name="validate-all" timeout="20s" /> |
| 182 |
</actions> |
| 183 |
</resource-agent> |
| 184 |
END |
| 185 |
} |
| 186 |
|
| 187 |
# check if the interface is admin up/down |
| 188 |
|
| 189 |
iface_is_up() { |
| 190 |
if ! $IP2UTIL -o link show $1 | \ |
| 191 |
sed -e 's#.*<##g' -e 's#>.*##' -e 's#LOWER_UP##g' | \ |
| 192 |
grep -q UP; then |
| 193 |
return 1 |
| 194 |
fi |
| 195 |
return 0 |
| 196 |
} |
| 197 |
|
| 198 |
# check if the slaves have link layer up/down |
| 199 |
# see kernel network documentation on meaning of LOWER_UP flag |
| 200 |
# for more in depth explanation on how it works |
| 201 |
# NOTE: this check is not reliable in virt environment |
| 202 |
# since interfaces are always LOWER_UP. There is no way |
| 203 |
# from the guest to know if the host has disconnected somehow |
| 204 |
|
| 205 |
iface_lower_is_up() { |
| 206 |
if ! $IP2UTIL -o link show $1 | \ |
| 207 |
grep -q LOWER_UP; then |
| 208 |
return 1 |
| 209 |
fi |
| 210 |
return 0 |
| 211 |
} |
| 212 |
|
| 213 |
vlan_validate() { |
| 214 |
check_binary $IP2UTIL |
| 215 |
|
| 216 |
if [ -z "$OCF_RESKEY_vlan_interface" ]; then |
| 217 |
ocf_log err "Invalid OCF_RESKEY_vlan_interface: value cannot be empty" |
| 218 |
return 1 |
| 219 |
fi |
| 220 |
|
| 221 |
# the echo .. is the equivalent of strlen in bash |
| 222 |
# |
| 223 |
# /usr/include/linux/if.h:#define IFNAMSIZ 16 |
| 224 |
# needs to include 0 byte end string |
| 225 |
|
| 226 |
if [ "${#OCF_RESKEY_vlan_interface}" -gt 15 ]; then |
| 227 |
ocf_log err "Invalid OCF_RESKEY_vlan_interface: name is too long" |
| 228 |
return 1 |
| 229 |
fi |
| 230 |
|
| 231 |
if [ ! -d "/sys/class/net" ]; then |
| 232 |
ocf_log err "Unable to find sysfs network class in /sys" |
| 233 |
return 1 |
| 234 |
fi |
| 235 |
|
| 236 |
if [ ! -e "/sys/class/net/$OCF_RESKEY_vlan_interface" ]; then |
| 237 |
ocf_log err "Invalid OCF_RESKEY_vlan_interface: $OCF_RESKEY_vlan_interface does not exists" |
| 238 |
return 1 |
| 239 |
fi |
| 240 |
|
| 241 |
if [ -z "$OCF_RESKEY_vlan_id" ]; then |
| 242 |
ocf_log err "Invalid OCF_RESKEY_vlan_id: value cannot be empty" |
| 243 |
return 1 |
| 244 |
fi |
| 245 |
if ! ocf_is_decimal "$OCF_RESKEY_vlan_id" || \ |
| 246 |
[ "$OCF_RESKEY_vlan_id" -gt "4094" ]; then |
| 247 |
ocf_log err "Invalid OCF_RESKEY_vlan_id: must be a decimal value (0 to 4094 included)" |
| 248 |
return 1 |
| 249 |
fi |
| 250 |
|
| 251 |
if [ "${#OCF_RESKEY_vlan_name}" -gt 15 ]; then |
| 252 |
ocf_log err "Invalid OCF_RESKEY_vlan_name: name is too long" |
| 253 |
return 1 |
| 254 |
fi |
| 255 |
|
| 256 |
return 0 |
| 257 |
} |
| 258 |
|
| 259 |
vlan_check() { |
| 260 |
if [ -e "/sys/class/net/$OCF_RESKEY_vlan_name" ]; then |
| 261 |
if [ ! -e "$HA_RSCTMP/iface-vlan.$OCF_RESKEY_vlan_name" ]; then |
| 262 |
return $OCF_ERR_GENERIC |
| 263 |
fi |
| 264 |
else |
| 265 |
if [ -e "$HA_RSCTMP/iface-vlan.$OCF_RESKEY_vlan_name" ]; then |
| 266 |
error="$(rm -f "$HA_RSCTMP/iface-vlan.$OCF_RESKEY_vlan_name" 2>&1)" |
| 267 |
if [ "$?" != "0" ]; then |
| 268 |
ocf_log err "Unable to remove stale lock file for vlan $OCF_RESKEY_vlan_name: $error" |
| 269 |
return $OCF_ERR_GENERIC |
| 270 |
fi |
| 271 |
fi |
| 272 |
return $OCF_NOT_RUNNING |
| 273 |
fi |
| 274 |
|
| 275 |
if ! iface_is_up $OCF_RESKEY_vlan_interface; then |
| 276 |
if ocf_is_true "$OCF_RESKEY_vlan_loose_binding"; then |
| 277 |
ocf_log warn "Interface $OCF_RESKEY_vlan_interface is administratively down" |
| 278 |
else |
| 279 |
ocf_log err "Interface $OCF_RESKEY_vlan_interface is administratively down" |
| 280 |
return $OCF_ERR_GENERIC |
| 281 |
fi |
| 282 |
fi |
| 283 |
|
| 284 |
if ! iface_is_up $OCF_RESKEY_vlan_name; then |
| 285 |
ocf_log err "VLAN $OCF_RESKEY_vlan_name is administratively down" |
| 286 |
return $OCF_ERR_GENERIC |
| 287 |
fi |
| 288 |
|
| 289 |
if ! iface_lower_is_up $OCF_RESKEY_vlan_name; then |
| 290 |
ocf_log err "VLAN $OCF_RESKEY_vlan_name has no active link-layer" |
| 291 |
return $OCF_ERR_GENERIC |
| 292 |
fi |
| 293 |
|
| 294 |
return $OCF_SUCCESS |
| 295 |
} |
| 296 |
|
| 297 |
# we need a simpler stop version to clean after us if start fails |
| 298 |
# without involving any error checking |
| 299 |
# rolling back in case of failure is otherwise complex |
| 300 |
|
| 301 |
vlan_force_stop() { |
| 302 |
$IP2UTIL link delete "$OCF_RESKEY_vlan_name" >/dev/null 2>&1 |
| 303 |
rm -f "$HA_RSCTMP/iface-vlan.$OCF_RESKEY_vlan_name" 2>&1 |
| 304 |
} |
| 305 |
|
| 306 |
vlan_start() { |
| 307 |
# check if the vlan already exists |
| 308 |
vlan_check |
| 309 |
ret=$? |
| 310 |
if [ "$ret" != "$OCF_NOT_RUNNING" ]; then |
| 311 |
return $ret |
| 312 |
fi |
| 313 |
|
| 314 |
# make sure kernel module is loaded |
| 315 |
if [ ! -e /proc/net/vlan ]; then |
| 316 |
error="$(modprobe 8021q 2>&1)" |
| 317 |
if [ "$?" != "0" ]; then |
| 318 |
ocf_log err "Unable to load kernel 8021q driver: $error" |
| 319 |
return $OCF_ERR_GENERIC |
| 320 |
fi |
| 321 |
fi |
| 322 |
|
| 323 |
# generate options |
| 324 |
VLANOPTS="" |
| 325 |
|
| 326 |
if [ -n "$OCF_RESKEY_vlan_reorder_hdr" ]; then |
| 327 |
if ocf_is_true "$OCF_RESKEY_vlan_reorder_hdr"; then |
| 328 |
VLANOPTS="reorder_hdr on" |
| 329 |
else |
| 330 |
VLANOPTS="reorder_hdr off" |
| 331 |
fi |
| 332 |
fi |
| 333 |
|
| 334 |
if [ -n "$OCF_RESKEY_vlan_gvrp" ]; then |
| 335 |
if ocf_is_true "$OCF_RESKEY_vlan_gvrp"; then |
| 336 |
VLANOPTS="$VLANOPTS gvrp on" |
| 337 |
else |
| 338 |
VLANOPTS="$VLANOPTS gvrp off" |
| 339 |
fi |
| 340 |
fi |
| 341 |
|
| 342 |
if [ -n "$OCF_RESKEY_vlan_mvrp" ]; then |
| 343 |
if ocf_is_true "$OCF_RESKEY_vlan_mvrp"; then |
| 344 |
VLANOPTS="$VLANOPTS mvrp on" |
| 345 |
else |
| 346 |
VLANOPTS="$VLANOPTS mvrp off" |
| 347 |
fi |
| 348 |
fi |
| 349 |
|
| 350 |
if [ -n "$OCF_RESKEY_vlan_loose_binding" ]; then |
| 351 |
if ocf_is_true "$OCF_RESKEY_vlan_loose_binding"; then |
| 352 |
VLANOPTS="$VLANOPTS loose_binding on" |
| 353 |
else |
| 354 |
VLANOPTS="$VLANOPTS loose_binding off" |
| 355 |
fi |
| 356 |
fi |
| 357 |
|
| 358 |
# create the VLAN |
| 359 |
error="$($IP2UTIL link add link "$OCF_RESKEY_vlan_interface" name "$OCF_RESKEY_vlan_name" type vlan id "$OCF_RESKEY_vlan_id" $VLANOPTS 2>&1)" |
| 360 |
if [ "$?" != "0" ]; then |
| 361 |
ocf_log err "Unable to create VLAN $OCF_RESKEY_vlan_name: $error" |
| 362 |
return $OCF_ERR_GENERIC |
| 363 |
fi |
| 364 |
|
| 365 |
# set the interface up |
| 366 |
error="$($IP2UTIL link set dev "$OCF_RESKEY_vlan_interface" up 2>&1)" |
| 367 |
if [ "$?" != "0" ]; then |
| 368 |
ocf_log err "Unable to set VLAN $OCF_RESKEY_vlan_interface up: $error" |
| 369 |
return $OCF_ERR_GENERIC |
| 370 |
fi |
| 371 |
|
| 372 |
# set the vlan up |
| 373 |
error="$($IP2UTIL link set dev "$OCF_RESKEY_vlan_name" up 2>&1)" |
| 374 |
if [ "$?" != "0" ]; then |
| 375 |
ocf_log err "Unable to set VLAN $OCF_RESKEY_vlan_name up: $error" |
| 376 |
return $OCF_ERR_GENERIC |
| 377 |
fi |
| 378 |
|
| 379 |
error="$(touch "$HA_RSCTMP/iface-vlan.$OCF_RESKEY_vlan_name" 2>&1)" |
| 380 |
if [ "$?" != "0" ]; then |
| 381 |
ocf_log err "Unable to create lock file for VLAN $OCF_RESKEY_vlan_name: $error" |
| 382 |
return $OCF_ERR_GENERIC |
| 383 |
fi |
| 384 |
|
| 385 |
return $OCF_SUCCESS |
| 386 |
} |
| 387 |
|
| 388 |
vlan_stop() { |
| 389 |
vlan_check |
| 390 |
ret=$? |
| 391 |
if [ "$ret" = "$OCF_NOT_RUNNING" ]; then |
| 392 |
return $OCF_SUCCESS |
| 393 |
fi |
| 394 |
if [ "$ret" != "$OCF_SUCCESS" ]; then |
| 395 |
return $ret |
| 396 |
fi |
| 397 |
|
| 398 |
# set vlan down |
| 399 |
error="$($IP2UTIL link set dev "$OCF_RESKEY_vlan_name" down 2>&1)" |
| 400 |
if [ "$?" != "0" ]; then |
| 401 |
ocf_log err "Unable to set VLAN $OCF_RESKEY_vlan_name down: $error" |
| 402 |
return $OCF_ERR_GENERIC |
| 403 |
fi |
| 404 |
|
| 405 |
# delete vlan |
| 406 |
error="$($IP2UTIL link delete "$OCF_RESKEY_vlan_name" 2>&1)" |
| 407 |
if [ "$?" != "0" ]; then |
| 408 |
ocf_log err "Unable to delete VLAN $OCF_RESKEY_vlan_name: $error" |
| 409 |
return $OCF_ERR_GENERIC |
| 410 |
fi |
| 411 |
|
| 412 |
error="$(rm -f "$HA_RSCTMP/iface-vlan.$OCF_RESKEY_vlan_name" 2>&1)" |
| 413 |
if [ "$?" != "0" ]; then |
| 414 |
ocf_log err "Unable to remove lock file for VLAN $OCF_RESKEY_vlan_name: $error" |
| 415 |
return $OCF_ERR_GENERIC |
| 416 |
fi |
| 417 |
|
| 418 |
return $OCF_SUCCESS |
| 419 |
} |
| 420 |
|
| 421 |
case $__OCF_ACTION in |
| 422 |
meta-data) |
| 423 |
vlan_meta_data |
| 424 |
exit $OCF_SUCCESS |
| 425 |
;; |
| 426 |
usage|help) |
| 427 |
vlan_usage |
| 428 |
exit $OCF_SUCCESS |
| 429 |
;; |
| 430 |
esac |
| 431 |
|
| 432 |
if [ ! -d "$HA_RSCTMP" ]; then |
| 433 |
ocf_log debug "$HA_RSCTMP not found, we are probably being executed manually" |
| 434 |
mkdir -p "$HA_RSCTMP" |
| 435 |
fi |
| 436 |
|
| 437 |
if [ -n "$__OCF_ACTION" ] && ! vlan_validate; then |
| 438 |
exit $OCF_ERR_CONFIGURED |
| 439 |
fi |
| 440 |
|
| 441 |
case $__OCF_ACTION in |
| 442 |
start|stop) |
| 443 |
if ! ocf_is_root; then |
| 444 |
ocf_log err "You must be root for $__OCF_ACTION operation." |
| 445 |
exit $OCF_ERR_PERM |
| 446 |
fi |
| 447 |
;; |
| 448 |
esac |
| 449 |
|
| 450 |
case $__OCF_ACTION in |
| 451 |
start) |
| 452 |
vlan_start |
| 453 |
ret=$? |
| 454 |
if [ "$ret" != "$OCF_SUCCESS" ]; then |
| 455 |
vlan_force_stop |
| 456 |
fi |
| 457 |
exit $ret |
| 458 |
;; |
| 459 |
stop) |
| 460 |
vlan_stop |
| 461 |
exit $? |
| 462 |
;; |
| 463 |
status|monitor) |
| 464 |
vlan_check |
| 465 |
exit $? |
| 466 |
;; |
| 467 |
validate-all) |
| 468 |
# vlan_validate above does the trick |
| 469 |
;; |
| 470 |
*) |
| 471 |
vlan_usage |
| 472 |
exit $OCF_ERR_UNIMPLEMENTED |
| 473 |
;; |
| 474 |
esac |
| 475 |
# vi:sw=4:ts=8: |