1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-21 22:04:01 +03:00

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.
This commit is contained in:
Yu Watanabe 2024-08-17 11:26:32 +09:00
parent f32538e1cc
commit 6e4918a944
7 changed files with 38 additions and 4 deletions

View 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>

View 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();

View 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;
}

View 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)

View 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;

View 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);

View File

@ -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