]> git.ipfire.org Git - people/stevee/network.git/commitdiff
Introduce enable/disable option for network zones. systemd-network
authorStefan Schantl <stefan.schantl@ipfire.org>
Sun, 19 May 2013 14:18:40 +0000 (16:18 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 19 May 2013 14:25:41 +0000 (16:25 +0200)
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
functions.zone
man/network-zone.xml
network
systemd/network-init.service [new file with mode: 0644]
systemd/network.service [deleted file]
systemd/network@.service

index 97de2c907dbddf6fc3b2fa060761a46a9165fbaa..6b5b73c9aea14f53c4cb4cb9955cba61ab877733 100644 (file)
@@ -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
index 3a5c60fb75e59282f3195bcae646c2a78672e280..b18d4548fad2c0e47b649fd56410d89eb81bc850 100644 (file)
@@ -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})
 }
 
index 15908dba92e02bca9e85b198902e99cd8e3cc8cf..182b95fb4d372e1a8b4140d877713954c33bb8ab 100644 (file)
                                </listitem>
                        </varlistentry>
 
+                       <varlistentry>
+                               <term>
+                                       <command><replaceable>ZONE</replaceable> [enable|disable]</command>
+                               </term>
+
+                               <listitem>
+                                       <para>
+                                               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.
+                                       </para>
+                               </listitem>
+                       </varlistentry>
+
                        <varlistentry>
                                <term>
                                        <command><replaceable>ZONE</replaceable> status</command>
diff --git a/network b/network
index 5a5aba2899fb36e7fd1207aa07051b14a58ccbad..f3418d8976fe7a1b947e974a9b55e2564c64edde 100755 (executable)
--- 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 (file)
index 0000000..8e19244
--- /dev/null
@@ -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 (file)
index 5b884ed..0000000
+++ /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
index 07df33adbd7a4225d365fc35a7d9ab176c213a8d..592e9feb176f48f7939a9d81afe0558f163a03d2 100644 (file)
@@ -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