-function zone_is_forwarding() {
- local seconds=45
- local zone=${1}
-
- local device
- while [ ${seconds} -gt 0 ]; do
- for device in /sys/class/net/${zone}/brif/*; do
- [ -e "${device}/state" ] || continue
- if [ "$(<${device}/state)" = "3" ]; then
- return ${EXIT_OK}
- fi
- done
- sleep 1
- seconds=$((${seconds} - 1))
- done
- return ${EXIT_ERROR}
-}
-
-function bridge_devices() {
- local bridge=$1
- [ -z "${bridge}" ] && return 2
- brctl show | grep "^${bridge}" | awk '{ print $NF }' | grep -v "^interfaces$"
-}
-
-function zone_add_port() {
- local zone=${1}
- local port=${2}
-
- brctl addif ${zone} ${port}
-}
-
-function zone_del_port() {
- local zone=${1}
- local port=${2}
-
- brctl delif ${zone} ${port}
-}
-
-function zone_list() {
- local zone
- for zone in $(find ${CONFIG_ZONES}/* 2>/dev/null); do
- [ -d "${zone}" ] && echo ${zone}
- done
-}
-
-function zone_is_red() {
- local zone=${1}
- [ "${zone#red}" != "${zone}" ]
-}
-
-function _run_hooks() {
- local action
- local type
-
- while [ $# -gt 0 ]; do
- case "${1}" in
- --type=*)
- type=${1#--type=}
- ;;
- *)
- action="${1}"
- shift; break
- ;;
- esac
- shift
- done
-
- local dir=${1}; shift
- local failed
- local hook
- local hooks
-
- if [ -z "${action}" ] || [ -z "${dir}" ]; then
- echo "Not enough parameters given." >&2
- return 1
- fi
-
- for hook in $(find ${dir}); do
- # Skip dirs
- [ -d "${hook}" ] && continue
-
- (
- . ${hook}
- # Skip hooks that are not of the given type
- if [ -n "${type}" ] && [ "$(hook_type ${HOOK})" != "${type}" ]; then
- continue
- fi
- if [ -n "${HOOK}" ]; then
- hook_run ${HOOK} --config=${hook} $@ ${action}
- RET=$?
- else
- echo -e "${FAILURE}Unable to process ${hook}. Either"
- echo -e "${FAILURE}the HOOK variable was not set,"
- echo -e "${FAILURE}or the specified hook cannot be executed."
- message=""
- log_failure_msg
- fi
- exit ${RET}
- ) || failed=1
- done
-
- return ${failed}
-}
-
-function hooks_run_all() {
- _run_hooks $@
-}
-
-function hooks_run_ports() {
- _run_hooks --type="port" $@
-}
-
-function hooks_run_zones() {
- _run_hooks --type="zone" $@
-}
-
-function hook_type() {
- local hook=${1}
- (
- eval $(${HOOKS_DIR}/${hook} info)
- echo "${HOOK_TYPE}"
- )
-}
-
-function hook_list() {
- local type=${1}
- local hook
- for hook in ${HOOKS_DIR}/*; do
- [ -x "${hook}" ] || continue
-
- hook=${hook##*/}
-
- [[ ${hook} =~ helper$ ]] && continue
-
- if [ -n "${type}" ] && [ "$(hook_type ${hook})" != "${type}" ]; then
- continue
- fi
- echo "${hook}"
- done
-}
-
-function config_get_hook() {
- local config=${1}
- if [ ! -e "${config}" ]; then
- log_failure_msg "Config file \"${config}\" does not exist."
- return ${EXIT_ERROR}
- fi
- ( . ${config}; echo ${HOOK} )
-}
-
-function hook_run() {
- local hook=${1}
- shift
-
- if ! hook_exists ${hook}; then
- log_failure_msg "Hook ${hook} cannot be found or is not executeable."
- return ${EXIT_ERROR}
- fi
- [ -n "${DEBUG}" ] && echo "Running hook: ${hook} $@"
- DEBUG=${DEBUG} VERBOSE=${VERBOSE} ${HOOKS_DIR}/${hook} $@
- return $?
-}
-
-function hook_run_multiple() {
- local zone
- local config
- local hook
- local hook_type2
- local type
-
- while [ "$#" -gt "0" ]; do
- case "${1}" in
- --type=*)
- type=${1#--type=}
- ;;
- *)
- zone=${1}
- break
- ;;
- esac
- shift
- done
-
- if ! zone_exists ${zone}; then
- return ${EXIT_ERROR}
- fi
-
- for config in $(find ${CONFIG_ZONES}/${zone} 2>/dev/null); do
- hook=$(config_get_hook ${config})
- if [ -n "${type}" ]; then
- hook_type2=$(hook_type ${hook})
- if [ "${type}" != "${hook_type2}" ]; then
- continue
- fi
- fi
- hook_run ${hook} $@
- done
-}
-
-function zone_run() {
- local zone=${1}
- shift
-
- if ! zone_exists ${zone}; then
- log_failure_msg "Zone ${zone} does not exist."
- exit ${EXIT_ERROR}
- fi
- decho "Running zone: ${zone} $@"
- DEBUG=${DEBUG} VERBOSE=${VERBOSE} ${HOME_DIR}/zone --zone=${zone} $@
-}
-
-function zone_valid_name() {
- local zone=${1}
- local match
-
- local i
- for i in ${VALID_ZONES}; do
- match="${match}|${i}[0-9]{1,5}"
- done
- [[ ${zone} =~ ${match:1:${#match}} ]]
-}
-
-function isset() {
- local key=${1}
- [ -n "${!key}" ] && return
- if [[ ${key} =~ port|zone ]]; then
- echo "ERROR: The --${key} flag is not set." >&2
- else
- echo "ERROR: The \"${key}\" variable is not set properly." >&2
- fi
- return 1
-}
-
-# Test if device is attached to the given bridge
-function zone_has_device_attached () {
- local zone=${1}
- local device=${2}
-
- [ -d "/sys/class/net/${zone}/brif/${device}" ]
-}
-
-function device_has_ipv4() {
- local device=${1}
- local ip=${2}
- ip addr show ${device} | grep inet | fgrep -q ${ip}
-}
-
-function check_config() {
- local failed
- local i
-
- for i in $@; do
- isset ${i} || failed=1
- done
- if [ "${failed}" = "1" ]; then
- echo "Exiting..."
- exit ${EXIT_ERROR}
- fi
-}
-
-function mac_generate() {
- local mac="00"
- while [ "${#mac}" -lt 15 ]; do
- mac="${mac}:$(cut -c 1-2 /proc/sys/kernel/random/uuid)"
- done
- echo "${mac}"
-}
-
-function connection() {
- local action
-
- local dns
- local interface
- local iplocal
- local ipremote
- local name
- local status
- local weight
- local zone
-
- while [ $# -gt 0 ]; do
- case "${1}" in
- --up)
- action="up"
- ;;
- --down)
- action="down"
- ;;
- --starting)
- action="starting"
- ;;
- --stopping)
- action="stopping"
- ;;
- --name=*)
- name=${1#--name=}
- ;;
- --zone=*)
- zone=${1#--zone=}
- zone_is_red ${zone} || return 0
- ;;
- --interface=*)
- interface=${1#--interface=}
- ;;
- --iplocal=*)
- iplocal=${1#--iplocal=}
- ;;
- --ipremote=*)
- ipremote=${1#--ipremote=}
- ;;
- --weight=*)
- weight=${1#--weight=}
- ;;
- --dns=*)
- dns=${1#--dns=}
- ;;
- esac
- shift
- done
-
- if [ ! -e "${CONNECTIONS_FILE}" ]; then
- sqlite3 -batch ${CONNECTIONS_FILE} <<EOF
-CREATE TABLE connections(name, zone, interface, iplocal, ipremote, weight, dns, status);
-EOF
- fi
-
- if [ -z "${zone}" ]; then
- return 2
- fi
-
- status=${action}
-
- sqlite3 -batch ${CONNECTIONS_FILE} <<EOF
-DELETE FROM connections WHERE zone = '${zone}';
-INSERT INTO connections(name, zone, interface, iplocal, ipremote, weight, dns, status)
- VALUES('${name}', '${zone}', '${interface}', '${iplocal}', '${ipremote}', '${weight}', '${dns}', '${status}');
-EOF
-
-}
-
-function uuid() {
- cat /proc/sys/kernel/random/uuid
-}