# Automatically enable zone.
zone_enable "${zone}"
+
+ # Bring up the zone immediately after
+ zone_start "${zone}"
}
function zone_edit() {
log INFO "Removing zone '${zone}' right now."
# Force the zone down.
- zone_is_up "${zone}" && zone_set_down "${zone}"
+ zone_is_active "${zone}" && zone_stop "${zone}"
# Disable zone.
zone_disable "${zone}"
fi
}
-function zone_port() {
- local zone=${1}
- local action=${2}
- shift 2
-
- assert isset zone
- assert isset action
- assert zone_exists ${zone}
-
- case "${action}" in
- add|edit|remove)
- zone_port_${action} ${zone} $@
- ;;
- *)
- error "Unrecognized argument: ${action}"
- cli_usage root-zone-port-subcommands
- exit ${EXIT_ERROR}
- ;;
- esac
-}
-
-function zone_port_add() {
- local zone="${1}"
- assert isset zone
-
- local port="${2}"
- assert isset port
-
- shift 2
-
- # Check if the port actually exists.
- if ! port_exists "${port}"; then
- error "Cannot add port '${port}' which does not exist"
- return ${EXIT_ERROR}
- fi
-
- # Check if the port is already connected to this or any other zone.
- local z
- for z in $(zones_get_all); do
- if zone_has_port "${z}" "${port}"; then
- error "Port '${port}' is already assigned to zone '${z}'"
- return ${EXIT_ERROR}
- fi
- done
-
- local hook=$(zone_get_hook "${zone}")
- assert isset hook
-
- hook_zone_exec "${hook}" "port_add" "${zone}" "${port}" "$@"
-}
-
-function zone_port_edit() {
- local zone="${1}"
- assert isset zone
-
- local port="${2}"
- assert isset port
-
- shift 2
-
- # Check if the port actually exists.
- if ! port_exists "${port}"; then
- error "Port '${port}' does not exist"
- return ${EXIT_ERROR}
- fi
-
- # Check if the zone actually has this port.
- if ! zone_has_port "${zone}" "${port}"; then
- error "Port '${port}' is not attached to zone '${zone}'"
- return ${EXIT_ERROR}
- fi
-
- local hook=$(zone_get_hook "${zone}")
- assert isset hook
-
- hook_zone_exec "${hook}" "port_edit" "${zone}" "${port}" "$@"
-}
-
-function zone_port_remove() {
- local zone="${1}"
- assert isset zone
-
- local port="${2}"
- assert isset port
-
- shift 2
-
- # Check if the zone actually has this port.
- if ! zone_has_port "${zone}" "${port}"; then
- error "Port '${port}' is not attached to zone '${zone}'"
- return ${EXIT_ERROR}
- fi
-
- local hook=$(zone_get_hook "${zone}")
- assert isset hook
-
- hook_zone_exec "${hook}" "port_remove" "${zone}" "${port}" "$@"
-}
-
function zone_get_ports() {
local zone=${1}
done
}
-function zone_ports_cmd() {
- local cmd=${1}
- local zone=${2}
+function zone_port_attach() {
+ local zone="${1}"
+ assert isset zone
+
+ local port="${2}"
+ assert isset port
+
+ shift 2
+
+ # Check if the port actually exists.
+ if ! port_exists "${port}"; then
+ error "Cannot attach port '${port}' which does not exist"
+ return ${EXIT_ERROR}
+ fi
+
+ # Check if the port is already connected to this or any other zone.
+ local z
+ for z in $(zones_get_all); do
+ if zone_has_port "${z}" "${port}"; then
+ error "Port '${port}' is already attached to zone '${z}'"
+ return ${EXIT_ERROR}
+ fi
+ done
+
+ local hook="$(zone_get_hook "${zone}")"
+ assert isset hook
+
+ hook_zone_exec "${hook}" "port_attach" "${zone}" "${port}" "$@"
+ local ret="${?}"
+
+ case "${ret}" in
+ ${EXIT_OK})
+ log INFO "${port} has been attached to ${zone}"
+
+ # Automatically connect the port
+ zone_port_start "${zone}" "${port}"
+ ;;
+ *)
+ log CRITICAL "${port} could not be attached to ${zone}"
+ ;;
+ esac
+
+ return ${ret}
+}
+
+function zone_port_edit() {
+ local zone="${1}"
+ assert isset zone
+
+ local port="${2}"
+ assert isset port
+
+ shift 2
+
+ # Check if the port actually exists.
+ if ! port_exists "${port}"; then
+ error "Port '${port}' does not exist"
+ return ${EXIT_ERROR}
+ fi
+
+ # Check if the zone actually has this port.
+ if ! zone_has_port "${zone}" "${port}"; then
+ error "Port '${port}' is not attached to zone '${zone}'"
+ return ${EXIT_ERROR}
+ fi
+
+ local hook=$(zone_get_hook "${zone}")
+ assert isset hook
+
+ hook_zone_exec "${hook}" "port_edit" "${zone}" "${port}" "$@"
+}
+
+function zone_port_detach() {
+ local zone="${1}"
+ assert isset zone
+
+ local port="${2}"
+ assert isset port
+
shift 2
+ # Check if the zone actually has this port.
+ if ! zone_has_port "${zone}" "${port}"; then
+ error "Port '${port}' is not attached to zone '${zone}'"
+ return ${EXIT_ERROR}
+ fi
+
+ local hook=$(zone_get_hook "${zone}")
+ assert isset hook
+
+ hook_zone_exec "${hook}" "port_detach" "${zone}" "${port}" "$@"
+ local ret="${?}"
+
+ case "${ret}" in
+ ${EXIT_OK})
+ log INFO "${port} has been detached from ${zone}"
+
+ # Bring down the port if needed
+ zone_port_stop "${zone}" "${port}"
+ ;;
+ *)
+ log CRITICAL "${port} could not be detached from ${zone}"
+ ;;
+ esac
+
+ return ${ret}
+}
+
+function zone_port_cmd() {
+ local cmd="${1}"
assert isset cmd
+
+ local zone="${2}"
assert isset zone
- assert zone_exists ${zone}
+ local port="${3}"
+ assert isset port
- local hook=$(zone_get_hook ${zone})
+ shift 3
+
+ local hook="$(zone_get_hook "${zone}")"
+ assert isset hook
+
+ # Dispatch command to hook
+ hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" $@
+}
+
+function zone_port_create() {
+ zone_port_cmd "port_create" $@
+}
+
+function zone_port_remove() {
+ zone_port_cmd "port_remove" $@
+}
+
+function zone_port_up() {
+ zone_port_cmd "port_up" $@
+}
+
+function zone_port_down() {
+ zone_port_cmd "port_down" $@
+}
+
+# The next two functions automagically bring up and down
+# port that are attached to a bridge or similar.
+# The problem that is tried to overcome here is that there
+# are ports which exist all the time (like ethernet ports)
+# and therefore do not dispatch a hotplug event when
+# port_create is called.
+
+function zone_port_start() {
+ local zone="${1}"
+ local port="${2}"
+
+ if zone_is_active "${zone}"; then
+ if device_exists "${port}"; then
+ zone_port_up "${zone}" "${port}"
+ return ${?}
+ else
+ zone_port_create "${zone}" "${port}"
+ return ${?}
+ fi
+ fi
+
+ return ${EXIT_OK}
+}
+
+function zone_port_stop() {
+ local zone="${1}"
+ local port="${2}"
+
+ # Shut down the port if necessary
+ if zone_is_active "${zone}" && port_is_up "${port}"; then
+ zone_port_down "${zone}" "${port}"
+ fi
+
+ # Remove the port
+ zone_port_remove "${zone}" "${port}"
+}
+
+function zone_port_status() {
+ zone_port_cmd "port_status" $@
+}
+
+function zone_ports_cmd() {
+ local cmd="${1}"
+ assert isset cmd
+
+ local zone="${2}"
+ assert isset zone
+
+ shift 2
+
+ local hook="$(zone_get_hook "${zone}")"
local port
for port in $(zone_get_ports ${zone}); do
- hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
+ hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" $@
done
}
}
function zone_ports_up() {
- zone_ports_cmd port_up $@
+ zone_ports_cmd "port_up" $@
}
function zone_ports_down() {
- zone_ports_cmd port_down $@
+ zone_ports_cmd "port_down" $@
}
function zone_ports_status() {
- zone_ports_cmd port_status $@
+ zone_ports_cmd "port_status" $@
}
function zone_configs_cmd() {
function zone_settings_read() {
local zone=${1}
assert isset zone
+ shift
+
+ local args
+ if [ $# -eq 0 ] && [ -n "${HOOK_SETTINGS}" ]; then
+ list_append args ${HOOK_SETTINGS}
+ else
+ list_append args $@
+ fi
# Save the HOOK variable.
local hook="${HOOK}"
- settings_read "${zone}" ${HOOK_SETTINGS}
+ settings_read "$(zone_file "${zone}")" ${args}
# Restore hook.
HOOK="${hook}"
}
function zone_config_settings_read() {
- assert [ $# -gt 2 ]
+ assert [ $# -ge 2 ]
local zone="${1}"
local config="${2}"
shift 2
+ local args
+ if [ $# -eq 0 ] && [ -n "${HOOK_CONFIG_SETTINGS}" ]; then
+ list_append args ${HOOK_CONFIG_SETTINGS}
+ else
+ list_append args $@
+ fi
+
local path="$(zone_dir "${zone}")/configs/${config}"
- settings_read "${path}" "$@"
+ settings_read "${path}" ${args}
}
function zone_config_settings_write() {
- assert [ $# -gt 2 ]
+ assert [ $# -ge 2 ]
local zone="${1}"
local config="${2}"
shift 2
+ local args
+ if function_exists "hook_check_config_settings"; then
+ list_append args "--check=\"hook_check_config_settings\""
+ fi
+ list_append args ${HOOK_CONFIG_SETTINGS}
+
local path="$(zone_dir "${zone}")/configs/${config}"
- settings_write "${path}" "$@"
+ settings_write "${path}" ${args}
}
function zone_port_settings_read() {
- assert [ $# -gt 2 ]
+ assert [ $# -ge 2 ]
local zone="${1}"
local port="${2}"
shift 2
+ local args
+ if [ $# -eq 0 ] && [ -n "${HOOK_PORT_SETTINGS}" ]; then
+ list_append args ${HOOK_PORT_SETTINGS}
+ else
+ list_append args $@
+ fi
+
local path="$(zone_dir "${zone}")/ports/${port}"
- settings_read "${path}" "$@"
+ settings_read "${path}" ${args}
}
function zone_port_settings_write() {
if function_exists "hook_check_port_settings"; then
list_append args "--check=\"hook_check_port_settings\""
fi
- list_append args $@
+ list_append args ${HOOK_PORT_SETTINGS}
local path="$(zone_dir "${zone}")/ports/${port}"
settings_write "${path}" ${args}