vlan: Refactor vlan_create()
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Jun 2019 10:28:17 +0000 (12:28 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Jun 2019 10:28:17 +0000 (12:28 +0200)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/functions/functions.vlan
src/hooks/ports/vlan

index d83e3ad..99a8baa 100644 (file)
@@ -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() {
index 98178e3..4715b1f 100644 (file)
@@ -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() {