Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
- local device=${1}
- assert isset device
+ local device="${1}"
+ shift
- local parent=${2}
- assert isset parent
- 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}
- # 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"
- # 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
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}
- 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})"
fi
# Create the VLAN device
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