id128: allow combining --app with show

This effectively exposes sd_id128_get_app_specific() on the commandline.

Fixes https://github.com/systemd/systemd/issues/27514.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2023-08-26 13:39:02 +02:00
parent aea3f594db
commit b37e8184a5
4 changed files with 57 additions and 13 deletions

View file

@ -70,12 +70,6 @@
<para>With <command>boot-id</command>, the identifier of the current boot will be
printed.</para>
<para>Both <command>machine-id</command> and <command>boot-id</command> may be combined
with the <option>--app-specific=<replaceable>app-id</replaceable></option> switch to
generate application-specific IDs. See
<citerefentry><refentrytitle>sd_id128_get_machine</refentrytitle><manvolnum>3</manvolnum></citerefentry>
for the discussion when this is useful.</para>
<para>With <command>invocation-id</command>, the identifier of the current service invocation
will be printed. This is available in systemd services. See
<citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
@ -87,6 +81,12 @@
then printed with their name, or arbitrary IDs, which are then printed with a placeholder name. Combine
with <option>--uuid</option> to list the IDs in UUID style, i.e. the way GPT partition type UUIDs are
usually shown.</para>
<para><command>machine-id</command>, <command>boot-id</command>, and <command>show</command> may be
combined with the <option>--app-specific=<replaceable>app-id</replaceable></option> switch to generate
application-specific IDs. See
<citerefentry><refentrytitle>sd_id128_get_machine</refentrytitle><manvolnum>3</manvolnum></citerefentry>
for the discussion when this is useful.</para>
</refsect1>
<refsect1>
@ -116,10 +116,14 @@
<term><option>-a <replaceable>app-id</replaceable></option></term>
<term><option>--app-specific=<replaceable>app-id</replaceable></option></term>
<listitem><para>With this option, an identifier that is the result of hashing the
application identifier <replaceable>app-id</replaceable> and the machine identifier will be
printed. The <replaceable>app-id</replaceable> argument must be a valid sd-id128 string
identifying the application.</para>
<listitem><para>With this option, identifiers will be printed that are the result of hashing the
application identifier <replaceable>app-id</replaceable> and another ID. The
<replaceable>app-id</replaceable> argument must be a valid sd-id128 string identifying the
application. When used with <command>machine-id</command>, the other ID will be the machine ID as
described in
<citerefentry><refentrytitle>machine-id</refentrytitle><manvolnum>5</manvolnum></citerefentry>, when
used with <command>boot-id</command>, the other ID will be the boot ID, and when used with
<command>show</command>, the other ID or IDs should be specified via the positional arguments.</para>
<xi:include href="version-info.xml" xpointer="v240"/>
</listitem>
@ -165,6 +169,29 @@ NAME ID
user-home 773f91ef66d449b5bd83d683bf40ad16
</programlisting>
</example>
<example>
<title>Generate an application-specific UUID</title>
<programlisting>
$ systemd-id128 machine-id -u
3a9d668b-4db7-4939-8a4a-5e78a03bffb7
$ systemd-id128 new -u
1fb8f24b-02df-458d-9659-cc8ace68e28a
$ systemd-id128 --app=1fb8f24b-02df-458d-9659-cc8ace68e28a
47b82cb1-5339-43da-b2a6-1c350aef1bd1
$ systemd-id128 -Pu show 3a9d668b-4db7-4939-8a4a-5e78a03bffb7 --app=1fb8f24b-02df-458d-9659-cc8ace68e28a
47b82cb1-5339-43da-b2a6-1c350aef1bd1
</programlisting>
<para>On a given machine with the ID 3a9d668b-4db7-4939-8a4a-5e78a03bffb7, for the application
1fb8f24b-02df-458d-9659-cc8ace68e28a, we generate an application-specific machine ID. If we want to
later recreate the same calculation on a different machine, we need to specify both IDs explicitly.
</para>
</example>
</refsect1>
<refsect1>

View file

@ -68,8 +68,16 @@ static int verb_invocation_id(int argc, char **argv, void *userdata) {
}
static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first) {
sd_id128_t u;
int r;
assert(table);
if (sd_id128_is_null(arg_app))
u = uuid;
else
assert_se(sd_id128_get_app_specific(uuid, arg_app, &u) == 0);
if (arg_mode == ID128_PRINT_PRETTY) {
_cleanup_free_ char *id = NULL;
@ -79,7 +87,7 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first
ascii_strupper(id);
r = id128_pretty_print_sample(id, uuid);
r = id128_pretty_print_sample(id, u);
if (r < 0)
return r;
if (!first)
@ -87,7 +95,7 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first
return 0;
} else if (arg_value)
return id128_pretty_print(uuid, arg_mode);
return id128_pretty_print(u, arg_mode);
else {
if (!*table) {
@ -100,7 +108,7 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first
return table_add_many(*table,
TABLE_STRING, name,
arg_mode == ID128_PRINT_ID128 ? TABLE_ID128 : TABLE_UUID,
uuid);
u);
}
}

View file

@ -180,6 +180,7 @@ TEST(id128) {
assert_se(lseek(fd, 0, SEEK_SET) == 0);
assert_se(id128_read_fd(fd, ID128_FORMAT_ANY, NULL) == -EUCLEAN);
/* build/systemd-id128 -a f03daaeb1c334b43a732172944bf772e show 51df0b4bc3b04c9780e299b98ca373b8 */
assert_se(sd_id128_get_app_specific(SD_ID128_MAKE(51,df,0b,4b,c3,b0,4c,97,80,e2,99,b9,8c,a3,73,b8),
SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id) >= 0);
assert_se(sd_id128_equal(id, SD_ID128_MAKE(1d,ee,59,54,e7,5c,4d,6f,b9,6c,c6,c0,4c,a1,8a,86)));

View file

@ -13,6 +13,14 @@ systemd-id128 show --pretty | tail
systemd-id128 show --value | tail
systemd-id128 show 4f68bce3e8cd4db196e7fbcaf984b709 # root-x86-64
systemd-id128 show --pretty 4f68bce3e8cd4db196e7fbcaf984b709
systemd-id128 show root-x86-64
systemd-id128 show --pretty root-x86-64
[[ "$(systemd-id128 show 4f68bce3e8cd4db196e7fbcaf984b709)" = "$(systemd-id128 show root-x86-64)" ]]
[[ "$(systemd-id128 show 4f68bce3-e8cd-4db1-96e7-fbcaf984b709)" = "$(systemd-id128 show root-x86-64)" ]]
systemd-id128 show root-x86-64 --app-specific=4f68bce3e8cd4db196e7fbcaf984b709
systemd-id128 show --pretty root-x86-64 --app-specific=4f68bce3e8cd4db196e7fbcaf984b709
[[ "$(systemd-id128 show root-x86-64 --app-specific=4f68bce3e8cd4db196e7fbcaf984b709 -P)" = "8ee5535e7cb14c249e1d28b8dfbb939c" ]]
[[ "$(systemd-id128 new | wc -c)" -eq 33 ]]
systemd-id128 new -p