From 5c5b8e363515adae098d27b60d865794bd19cfc2 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Sun, 19 May 2013 16:18:40 +0200 Subject: [PATCH] Introduce enable/disable option for network zones. In the past all network zones have been started during the boot processs of the system, even if they cannot be started because for example the required device is missing. From now it will be possible to enable or disable network zones for startup. We use the systemd network.target to set which zones should be started. --- functions.service | 30 ++++++++++++++++++ functions.zone | 59 ++++++++++++++++++++++++++++++++++++ man/network-zone.xml | 14 +++++++++ network | 2 +- systemd/network-init.service | 13 ++++++++ systemd/network.service | 15 --------- systemd/network@.service | 10 ++++-- 7 files changed, 125 insertions(+), 18 deletions(-) create mode 100644 systemd/network-init.service delete mode 100644 systemd/network.service diff --git a/functions.service b/functions.service index 97de2c90..6b5b73c9 100644 --- a/functions.service +++ b/functions.service @@ -69,6 +69,36 @@ function service_status() { return $? } +# This function calls the "enable" command from systemd, +# to mark services to be automatically started during +# boot up. +function service_enable() { + local name=${1} + assert isset name + + systemctl enable "${name}" >/dev/null 2>&1 +} + +# This function calls the "disable" command of systemd, +# to drop the autostart ability of the service during the +# boot up. +function service_disable() { + local name=${1} + assert isset name + + systemctl disable "${name}" >/dev/null 2>&1 +} + +# This function uses the systemd command "is-enabled" to check, +# if a service has been enabled or not. +function service_is_enabled() { + local name="${1}" + assert isset name + + systemctl is-enabled "${name}" >/dev/null 2>&1 + return $? +} + function service_is_active() { local name=${1} assert isset name diff --git a/functions.zone b/functions.zone index 3a5c60fb..b18d4548 100644 --- a/functions.zone +++ b/functions.zone @@ -91,6 +91,58 @@ function zone_stop() { service_stop "network@${zone}.service" } +function zone_enable() { + # This function will enable the zone + # with help of systemd. + + local zone="${1}" + assert zone_exists "${zone}" + + # Enable service for the zone + service_enable "network@${zone}.service" + local ret=$? + + if [ ${ret} -eq ${EXIT_OK} ]; then + log INFO "Auto-start enabled for zone ${zone}" + return ${EXIT_OK} + fi + + log ERROR "Could not enable zone ${zone}: ${ret}" + return ${ret} +} + +function zone_disable() { + # This function will disable the zone + # with help of systemd. + + local zone="${1}" + assert zone_exists "${zone}" + + # Disable service for the zone + service_disable "network@${zone}.service" + local ret=$? + + if [ ${ret} -eq ${EXIT_OK} ]; then + log INFO "Auto-start disabled for zone ${zone}" + return ${EXIT_OK} + fi + + log ERROR "Could not disable zone ${zone}: ${ret}" + return ${ret} +} + +function zone_is_enabled() { + local zone="${1}" + assert isset zone + + # Ask systemd if the zone is enabled. + if service_is_enabled "network@${zone}.service"; then + return ${EXIT_TRUE} + fi + + return ${EXIT_FALSE} +} + function zone_create() { local zone=${1} local hook=${2} @@ -123,7 +175,11 @@ function zone_create() { # If this is the case we remove the created zone immediately. if [ "${ret}" = "${EXIT_ERROR}" ]; then zone_remove_now ${zone} + return ${EXIT_ERROR} fi + + # Automatically enable zone. + zone_enable "${zone}" } function zone_edit() { @@ -186,6 +242,9 @@ function zone_remove_now() { # Force the zone down. zone_is_up ${zone} && zone_set_down ${zone} + # Disable zone. + zone_disable "${zone}" + rm -rf $(zone_dir ${zone}) } diff --git a/man/network-zone.xml b/man/network-zone.xml index 15908dba..182b95fb 100644 --- a/man/network-zone.xml +++ b/man/network-zone.xml @@ -154,6 +154,20 @@ + + + ZONE [enable|disable] + + + + + These commands will enable or disable the zone. An enabled + zone will automatically be started either during the boot process + or a hotplug event of an associated port or other device. + + + + ZONE status diff --git a/network b/network index 5a5aba28..f3418d89 100755 --- a/network +++ b/network @@ -482,7 +482,7 @@ function cli_zone() { esac case "${action}" in - config|down|edit|port|status|up) + config|disable|down|edit|enable|port|status|up) zone_${action} ${zone} $@ ;; *) diff --git a/systemd/network-init.service b/systemd/network-init.service new file mode 100644 index 00000000..8e192448 --- /dev/null +++ b/systemd/network-init.service @@ -0,0 +1,13 @@ +[Unit] +Description=Basic Initialization for Network Connectivity. +After=firewall-init.service +Before=network.target +Wants=network.target +Requires=firewall-init.service + +[Service] +Type=oneshot +ExecStart=/usr/sbin/network init + +[Install] +WantedBy=multi-user.target diff --git a/systemd/network.service b/systemd/network.service deleted file mode 100644 index 5b884ed9..00000000 --- a/systemd/network.service +++ /dev/null @@ -1,15 +0,0 @@ -[Unit] -Description=Network Connectivity -After=firewall-init.service -Before=network.target -Requires=firewall-init.service - -[Service] -Type=oneshot -RemainAfterExit=yes -ExecStartPre=/usr/sbin/network init -ExecStart=/usr/sbin/network start -ExecStop=/usr/sbin/network stop - -[Install] -WantedBy=multi-user.target diff --git a/systemd/network@.service b/systemd/network@.service index 07df33ad..592e9feb 100644 --- a/systemd/network@.service +++ b/systemd/network@.service @@ -1,10 +1,16 @@ [Unit] Description=Network Connectivity for zone %I -After=firewall-init.service -Requires=firewall-init.service +After=firewall-init.service network-init.service +Requires=firewall-init.service network-init.service +Wants=network.target +Before=network.target [Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/sbin/network zone %I up ExecStop=/usr/sbin/network zone %I down + +[Install] +WantedBy=network.target +Alias=network.target.wants/network@%i.service -- 2.39.2