From d3a0f73d7b2b6d4f634083f5620752e57a7a691b Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 3 Jun 2019 12:28:17 +0200 Subject: [PATCH] vlan: Refactor vlan_create() Signed-off-by: Michael Tremer --- src/functions/functions.vlan | 81 +++++++++++++++++++++++------------- src/hooks/ports/vlan | 10 ++++- 2 files changed, 60 insertions(+), 31 deletions(-) diff --git a/src/functions/functions.vlan b/src/functions/functions.vlan index d83e3ad3..99a8baa3 100644 --- a/src/functions/functions.vlan +++ b/src/functions/functions.vlan @@ -38,53 +38,76 @@ EOF } vlan_create() { - local device=${1} - assert isset device + local device="${1}" + shift - local parent=${2} - assert isset parent + assert isset device - local tag=${3} - assert isinteger tag + local address + local parent + local tag + + # Parse command line arguments + while [ $# -gt 0 ]; do + case "${1}" in + --address=*) + address=$(cli_get_val "${1}") + ;; + --parent=*) + parent=$(cli_get_val "${1}") + ;; + --tag=*) + tag=$(cli_get_val "${1}") + ;; + *) + error "Unrecognized argument: ${1}" + return ${EXIT_ERROR} + ;; + esac + shift + done + + # Generate a random MAC address if none was passed + if ! isset address; then + address="$(mac_generate)" + fi - local address=${4} - if isset address; then - assert ismac address + # Check if address is valid + if ! ismac address; then + log ERROR "Invalid mac address: ${address}" + return ${EXIT_ERROR} fi - # Check if a device with the name does already exist. - if device_exists ${device}; then - log ERROR "device '${device}' does already exist" + # Check if a device with the name does already exist + if device_exists "${device}"; then + log ERROR "Device '${device}' already exists" return ${EXIT_ERROR} fi - # Check if the parent device exists. - if ! device_exists ${parent}; then - log ERROR "parent device '${parent}' does not exist" + # Check if the parent device exists + if ! device_exists "${parent}"; then + log ERROR "Parent device '${parent}' does not exist" return ${EXIT_ERROR} fi # Load ebtables stuff. vlan_init - local command="ip link add link ${parent} name ${device}" + # Make the command + local command=( + ip link add link "${parent}" name "${device}" + address "${address}" type vlan id "${tag}" + ) - if isset address; then - command="${command} address ${address}" + # Run the command + if ! cmd_quiet "${command[*]}"; then + log ERROR "Could not create VLAN device ${device}: $?" + return ${EXIT_ERROR} fi - command="${command} type vlan id ${tag}" - - cmd_quiet ${command} - local ret=$? - - if [ ${ret} -eq ${EXIT_OK} ]; then - log DEBUG "vlan device '${device}' has been created" - else - log ERROR "could not create vlan device '${device}': ${ret}" - fi + log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${tag})" - return ${ret} + return ${EXIT_OK} } vlan_remove() { diff --git a/src/hooks/ports/vlan b/src/hooks/ports/vlan index 98178e3d..4715b1f6 100644 --- a/src/hooks/ports/vlan +++ b/src/hooks/ports/vlan @@ -114,9 +114,15 @@ hook_create() { fi # Create the VLAN device - vlan_create "${port}" "${PARENT_PORT}" "${TAG}" "${ADDRESS}" + if ! vlan_create "${port}" \ + --address="${ADDRESS}" \ + --parent="${PARENT_PORT}" \ + --tag="${TAG}"; then + error "Could not create port: ${port}" + return ${EXIT_ERROR} + fi - exit ${EXIT_OK} + return ${EXIT_OK} } hook_remove() { -- 2.39.2