From: Yu Watanabe Date: Sat, 17 Aug 2024 02:26:32 +0000 (+0900) Subject: analyze: introduce --instance= option to control instance name for template units X-Git-Tag: v257-rc1~687 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6e4918a94496bd45b3108d7a24336e13be572104;p=thirdparty%2Fsystemd.git analyze: introduce --instance= option to control instance name for template units 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. --- diff --git a/man/systemd-analyze.xml b/man/systemd-analyze.xml index 1d20574cee6..e0fb212239b 100644 --- a/man/systemd-analyze.xml +++ b/man/systemd-analyze.xml @@ -726,7 +726,9 @@ $ systemd-analyze compare-versions 1 ge 2; echo $? augment the compiled in set of unit load paths; see systemd.unit5. All units files present in the directories containing the command line arguments will be used in preference - to the other paths. + to the other paths. If a template unit without an instance name is specified (e.g. + foo@.service), test_instance will be used as the instance + name, which can be controlled by option. The following errors are currently detected: @@ -1154,6 +1156,20 @@ io.systemd.credential:vmm.notify_socket=vsock-stream:2:254570042 + + + + + Specifies fallback instance name for template units. This will be used when one or more + template units without an instance name (e.g. foo@.service) specified for + systemd-analyze condition with , + systemd-analyze security, and systemd-analyze verify. + If unspecified, test_instance will be used. + + + + + @@ -1555,7 +1571,9 @@ io.systemd.credential:vmm.notify_socket=vsock-stream:2:254570042 compiled in set of unit load paths; see systemd.unit5. All units files present in the directory containing the specified unit will be used in preference to the - other paths. + other paths. If a template unit without an instance name is specified (e.g. + foo@.service), test_instance will be used as the instance + name, which can be controlled by option. diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index 9a247a085a7..f7e9b942132 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -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(); diff --git a/src/analyze/analyze-verify-util.c b/src/analyze/analyze-verify-util.c index 8e83c9a589b..0c54bba9ddd 100644 --- a/src/analyze/analyze-verify-util.c +++ b/src/analyze/analyze-verify-util.c @@ -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; } diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index ea62fc0d99b..150379ca240 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -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) diff --git a/src/analyze/analyze.h b/src/analyze/analyze.h index 246a880b627..48d9fabbd04 100644 --- a/src/analyze/analyze.h +++ b/src/analyze/analyze.h @@ -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; diff --git a/src/analyze/test-verify.c b/src/analyze/test-verify.c index d37e54bca63..e8ed7b54359 100644 --- a/src/analyze/test-verify.c +++ b/src/analyze/test-verify.c @@ -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); diff --git a/test/units/TEST-65-ANALYZE.sh b/test/units/TEST-65-ANALYZE.sh index 83abc0ac1a0..9fc2de1a21d 100755 --- a/test/units/TEST-65-ANALYZE.sh +++ b/test/units/TEST-65-ANALYZE.sh @@ -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