analyze: introduce --instance= option to control instance name for template units
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 17 Aug 2024 02:26:32 +0000 (11:26 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 18 Aug 2024 19:29:23 +0000 (04:29 +0900)
Note, `systemd-analyze foo@.service --instance=hoge` is equivalent to
`systemd-analyze foo@hoge.service`. But, the option may be useful when
e.g. passing multiple template units that have restriction on their
instance name:
```
$ ls
template_aaa@.service   template_bbb@.service   template_ccc@.service
$ systemd-analyze ./template_* --instance=hoge
```
Without the option, we need to embed an instance name into each unit
name, so cannot use globs.

Prompted by #33681.

man/systemd-analyze.xml
src/analyze/analyze-security.c
src/analyze/analyze-verify-util.c
src/analyze/analyze.c
src/analyze/analyze.h
src/analyze/test-verify.c
test/units/TEST-65-ANALYZE.sh

index 1d20574cee67229a130a730b50696d52693445b2..e0fb212239b1ce59997361164c281f2ed5dc0138 100644 (file)
@@ -726,7 +726,9 @@ $ systemd-analyze compare-versions 1 ge 2; echo $?
       augment the compiled in set of unit load paths; see
       <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>. All
       units files present in the directories containing the command line arguments will be used in preference
-      to the other paths.</para>
+      to the other paths. If a template unit without an instance name is specified (e.g.
+      <filename>foo@.service</filename>), <literal>test_instance</literal> will be used as the instance
+      name, which can be controlled by <option>--instance=</option> option.</para>
 
       <para>The following errors are currently detected:</para>
       <itemizedlist>
@@ -1154,6 +1156,20 @@ io.systemd.credential:vmm.notify_socket=vsock-stream:2:254570042
         <xi:include href="version-info.xml" xpointer="v235"/></listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><option>--instance=NAME</option></term>
+
+        <listitem>
+          <para>Specifies fallback instance name for template units. This will be used when one or more
+          template units without an instance name (e.g. <filename>foo@.service</filename>) specified for
+          <command>systemd-analyze condition</command> with <option>--unit=</option>,
+          <command>systemd-analyze security</command>, and <command>systemd-analyze verify</command>.
+          If unspecified, <literal>test_instance</literal> will be used.</para>
+
+          <xi:include href="version-info.xml" xpointer="v257"/>
+        </listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><option>--recursive-errors=<replaceable>MODE</replaceable></option></term>
 
@@ -1555,7 +1571,9 @@ io.systemd.credential:vmm.notify_socket=vsock-stream:2:254570042
         compiled in set of unit load paths; see
         <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>. All
         units files present in the directory containing the specified unit will be used in preference to the
-        other paths.</para>
+        other paths. If a template unit without an instance name is specified (e.g.
+        <filename>foo@.service</filename>), <literal>test_instance</literal> will be used as the instance
+        name, which can be controlled by <option>--instance=</option> option.</para>
 
         <xi:include href="version-info.xml" xpointer="v250"/></listitem>
       </varlistentry>
index 9a247a085a7a7ba8ed42e1f06ca3c189d8ddd0b0..f7e9b9421325246fb3a042e4689930d3d4c1086d 100644 (file)
@@ -2869,7 +2869,7 @@ static int analyze_security(sd_bus *bus,
                                                        *i);
 
                         if (unit_name_is_valid(mangled, UNIT_NAME_TEMPLATE)) {
-                                r = unit_name_replace_instance(mangled, "test-instance", &instance);
+                                r = unit_name_replace_instance(mangled, arg_instance, &instance);
                                 if (r < 0)
                                         return log_oom();
 
index 8e83c9a589b1707013a337e466bcd692686e323f..0c54bba9ddddb99e01e446c04d5eb921b7e41093 100644 (file)
@@ -5,6 +5,7 @@
 #include "all-units.h"
 #include "alloc-util.h"
 #include "analyze-verify-util.h"
+#include "analyze.h"
 #include "bus-error.h"
 #include "bus-util.h"
 #include "log.h"
@@ -55,7 +56,7 @@ int verify_prepare_filename(const char *filename, char **ret) {
                 return -EINVAL;
 
         if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
-                r = unit_name_replace_instance(name, "i", &with_instance);
+                r = unit_name_replace_instance(name, arg_instance, &with_instance);
                 if (r < 0)
                         return r;
         }
index ea62fc0d99bd040a78ed67e08afdf8c0233d4cea..150379ca240a51a79aac9abf2ae5fac03b84cb87 100644 (file)
@@ -102,6 +102,7 @@ RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
 RecursiveErrors arg_recursive_errors = _RECURSIVE_ERRORS_INVALID;
 bool arg_man = true;
 bool arg_generators = false;
+const char *arg_instance = "test_instance";
 double arg_svg_timescale = 1.0;
 bool arg_detailed_svg = false;
 char *arg_root = NULL;
@@ -272,6 +273,7 @@ static int help(int argc, char *argv[], void *userdata) {
                "     --man[=BOOL]            Do [not] check for existence of man pages\n"
                "     --generators[=BOOL]     Do [not] run unit generators\n"
                "                             (requires privileges)\n"
+               "     --instance=NAME         Specify fallback instance name for template units\n"
                "     --iterations=N          Show the specified number of iterations\n"
                "     --base-time=TIMESTAMP   Calculate calendar times relative to\n"
                "                             specified time\n"
@@ -319,6 +321,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_NO_PAGER,
                 ARG_MAN,
                 ARG_GENERATORS,
+                ARG_INSTANCE,
                 ARG_ITERATIONS,
                 ARG_BASE_TIME,
                 ARG_RECURSIVE_ERRORS,
@@ -356,6 +359,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "no-pager",         no_argument,       NULL, ARG_NO_PAGER         },
                 { "man",              optional_argument, NULL, ARG_MAN              },
                 { "generators",       optional_argument, NULL, ARG_GENERATORS       },
+                { "instance",         required_argument, NULL, ARG_INSTANCE         },
                 { "host",             required_argument, NULL, 'H'                  },
                 { "machine",          required_argument, NULL, 'M'                  },
                 { "iterations",       required_argument, NULL, ARG_ITERATIONS       },
@@ -484,6 +488,10 @@ static int parse_argv(int argc, char *argv[]) {
                                 return r;
                         break;
 
+                case ARG_INSTANCE:
+                        arg_instance = optarg;
+                        break;
+
                 case ARG_OFFLINE:
                         r = parse_boolean_argument("--offline", optarg, &arg_offline);
                         if (r < 0)
index 246a880b62796333a9e39f3445332914eaac6799..48d9fabbd0461d22c469918069dcf775ff6b577a 100644 (file)
@@ -35,6 +35,7 @@ extern RuntimeScope arg_runtime_scope;
 extern RecursiveErrors arg_recursive_errors;
 extern bool arg_man;
 extern bool arg_generators;
+extern const char *arg_instance;
 extern double arg_svg_timescale;
 extern bool arg_detailed_svg;
 extern char *arg_root;
index d37e54bca634b17377cc39d5b1e35f6a5907cd80..e8ed7b54359e4bf851c32c3f2a9d31b08b18e8a2 100644 (file)
@@ -3,6 +3,8 @@
 #include "analyze-verify-util.h"
 #include "tests.h"
 
+const char *arg_instance = "test_instance";
+
 TEST(verify_nonexistent) {
         /* Negative cases */
         assert_se(verify_executable(NULL, &(ExecCommand) {.flags = EXEC_COMMAND_IGNORE_FAILURE, .path = (char*) "/non/existent"}, NULL) == 0);
index 83abc0ac1a0d133393e90f671ca7e8da9de328f4..9fc2de1a21d6c13dada9ab6d735d6db028ea3811 100755 (executable)
@@ -961,6 +961,10 @@ systemd-analyze architectures uname
 systemd-analyze smbios11
 systemd-analyze smbios11 -q
 
+systemd-analyze condition --instance=tmp --unit=systemd-growfs@.service
+systemd-analyze verify --instance=tmp --man=no systemd-growfs@.service
+systemd-analyze security --instance=tmp systemd-growfs@.service
+
 systemd-analyze log-level info
 
 touch /testok