[systemd-devel] [PATCH 1/2] units: add ConditionInitrd=

Ivan Shapovalov intelfx100 at gmail.com
Wed Aug 27 09:26:13 PDT 2014


---
 man/systemd.unit.xml                  | 13 +++++++++++++
 src/core/condition.c                  | 17 +++++++++++++++++
 src/core/load-fragment-gperf.gperf.m4 |  1 +
 src/shared/condition-util.c           |  1 +
 src/shared/condition-util.h           |  1 +
 5 files changed, 33 insertions(+)

diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml
index c8d9300..4cd5201 100644
--- a/man/systemd.unit.xml
+++ b/man/systemd.unit.xml
@@ -919,6 +919,7 @@
                                 <term><varname>ConditionACPower=</varname></term>
                                 <term><varname>ConditionNeedsUpdate=</varname></term>
                                 <term><varname>ConditionFirstBoot=</varname></term>
+                                <term><varname>ConditionInitrd=</varname></term>
                                 <term><varname>ConditionPathExists=</varname></term>
                                 <term><varname>ConditionPathExistsGlob=</varname></term>
                                 <term><varname>ConditionPathIsDirectory=</varname></term>
@@ -1132,6 +1133,18 @@
                                 when a new system instances boots up
                                 for the first time.</para>
 
+                                <para><varname>ConditionInitrd=</varname>
+                                may be used to check whether the root
+                                filesystem is an initramfs at the time
+                                of activation of the unit. It takes a
+                                boolean argument. If set to
+                                <varname>true</varname>, the condition
+                                will hold only if the system runs from
+                                an initramfs. Conversely, if set to
+                                <varname>false</varname>, the condition
+                                will hold only if the system runs from
+                                the real root.</para>
+
                                 <para>With
                                 <varname>ConditionPathExists=</varname>
                                 a file existence condition is
diff --git a/src/core/condition.c b/src/core/condition.c
index 353e0c9..6be3d58 100644
--- a/src/core/condition.c
+++ b/src/core/condition.c
@@ -134,6 +134,20 @@ static bool condition_test_first_boot(Condition *c) {
         return ((access("/run/systemd/first-boot", F_OK) >= 0) == !!r) == !c->negate;
 }
 
+static bool condition_test_initrd(Condition *c) {
+        int r;
+
+        assert(c);
+        assert(c->parameter);
+        assert(c->type == CONDITION_INITRD);
+
+        r = parse_boolean(c->parameter);
+        if (r < 0)
+                return c->negate;
+
+        return (in_initrd() == !!r) == !c->negate;
+}
+
 static bool condition_test(Condition *c) {
         assert(c);
 
@@ -219,6 +233,9 @@ static bool condition_test(Condition *c) {
         case CONDITION_FIRST_BOOT:
                 return condition_test_first_boot(c);
 
+        case CONDITION_INITRD:
+                return condition_test_initrd(c);
+
         case CONDITION_NULL:
                 return !c->negate;
 
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
index 24aa80d..698f81f 100644
--- a/src/core/load-fragment-gperf.gperf.m4
+++ b/src/core/load-fragment-gperf.gperf.m4
@@ -170,6 +170,7 @@ Unit.ConditionSecurity,          config_parse_unit_condition_string, CONDITION_S
 Unit.ConditionCapability,        config_parse_unit_condition_string, CONDITION_CAPABILITY,          0
 Unit.ConditionHost,              config_parse_unit_condition_string, CONDITION_HOST,                0
 Unit.ConditionACPower,           config_parse_unit_condition_string, CONDITION_AC_POWER,            0
+Unit.ConditionInitrd,            config_parse_unit_condition_string, CONDITION_INITRD,              0
 Unit.ConditionNull,              config_parse_unit_condition_null,   0,                             0
 m4_dnl
 Service.PIDFile,                 config_parse_unit_path_printf,      0,                             offsetof(Service, pid_file)
diff --git a/src/shared/condition-util.c b/src/shared/condition-util.c
index ff4a8ec..e5779ce 100644
--- a/src/shared/condition-util.c
+++ b/src/shared/condition-util.c
@@ -261,6 +261,7 @@ static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
         [CONDITION_ARCHITECTURE] = "ConditionArchitecture",
         [CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate",
         [CONDITION_FIRST_BOOT] = "ConditionFirstBoot",
+        [CONDITION_INITRD] = "ConditionInitrd",
         [CONDITION_NULL] = "ConditionNull"
 };
 
diff --git a/src/shared/condition-util.h b/src/shared/condition-util.h
index 047fdbf..a78fcd9 100644
--- a/src/shared/condition-util.h
+++ b/src/shared/condition-util.h
@@ -46,6 +46,7 @@ typedef enum ConditionType {
         CONDITION_ARCHITECTURE,
         CONDITION_NEEDS_UPDATE,
         CONDITION_FIRST_BOOT,
+        CONDITION_INITRD,
         CONDITION_NULL,
         _CONDITION_TYPE_MAX,
         _CONDITION_TYPE_INVALID = -1
-- 
2.1.0



More information about the systemd-devel mailing list