1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "id128-util.h"
11 #include "machine-id-setup.h"
12 #include "path-util.h"
13 #include "terminal-util.h"
16 static char *arg_root
= NULL
;
17 static bool arg_commit
= false;
18 static bool arg_print
= false;
20 static int help(void) {
21 _cleanup_free_
char *link
= NULL
;
24 r
= terminal_urlify_man("systemd-machine-id-setup", "1", &link
);
28 printf("%s [OPTIONS...]\n\n"
29 "Initialize /etc/machine-id from a random source.\n\n"
30 " -h --help Show this help\n"
31 " --version Show package version\n"
32 " --root=ROOT Filesystem root\n"
33 " --commit Commit transient ID\n"
34 " --print Print used machine ID\n"
35 "\nSee the %s for details.\n"
36 , program_invocation_short_name
43 static int parse_argv(int argc
, char *argv
[]) {
52 static const struct option options
[] = {
53 { "help", no_argument
, NULL
, 'h' },
54 { "version", no_argument
, NULL
, ARG_VERSION
},
55 { "root", required_argument
, NULL
, ARG_ROOT
},
56 { "commit", no_argument
, NULL
, ARG_COMMIT
},
57 { "print", no_argument
, NULL
, ARG_PRINT
},
66 while ((c
= getopt_long(argc
, argv
, "hqcv", options
, NULL
)) >= 0)
77 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
94 assert_not_reached("Unhandled option");
98 log_error("Extraneous arguments");
105 int main(int argc
, char *argv
[]) {
106 char buf
[SD_ID128_STRING_MAX
];
110 log_parse_environment();
113 r
= parse_argv(argc
, argv
);
118 const char *etc_machine_id
;
120 r
= machine_id_commit(arg_root
);
124 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
125 r
= id128_read(etc_machine_id
, ID128_PLAIN
, &id
);
127 log_error_errno(r
, "Failed to read machine ID back: %m");
131 r
= machine_id_setup(arg_root
, SD_ID128_NULL
, &id
);
137 puts(sd_id128_to_string(id
, buf
));
141 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;