1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "id128-util.h"
10 #include "machine-id-setup.h"
11 #include "path-util.h"
14 static char *arg_root
= NULL
;
15 static bool arg_commit
= false;
16 static bool arg_print
= false;
18 static void help(void) {
19 printf("%s [OPTIONS...]\n\n"
20 "Initialize /etc/machine-id from a random source.\n\n"
21 " -h --help Show this help\n"
22 " --version Show package version\n"
23 " --root=ROOT Filesystem root\n"
24 " --commit Commit transient ID\n"
25 " --print Print used machine ID\n"
26 , program_invocation_short_name
);
29 static int parse_argv(int argc
, char *argv
[]) {
38 static const struct option options
[] = {
39 { "help", no_argument
, NULL
, 'h' },
40 { "version", no_argument
, NULL
, ARG_VERSION
},
41 { "root", required_argument
, NULL
, ARG_ROOT
},
42 { "commit", no_argument
, NULL
, ARG_COMMIT
},
43 { "print", no_argument
, NULL
, ARG_PRINT
},
52 while ((c
= getopt_long(argc
, argv
, "hqcv", options
, NULL
)) >= 0)
64 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
81 assert_not_reached("Unhandled option");
85 log_error("Extraneous arguments");
92 int main(int argc
, char *argv
[]) {
93 char buf
[SD_ID128_STRING_MAX
];
97 log_parse_environment();
100 r
= parse_argv(argc
, argv
);
105 const char *etc_machine_id
;
107 r
= machine_id_commit(arg_root
);
111 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
112 r
= id128_read(etc_machine_id
, ID128_PLAIN
, &id
);
114 log_error_errno(r
, "Failed to read machine ID back: %m");
118 r
= machine_id_setup(arg_root
, SD_ID128_NULL
, &id
);
124 puts(sd_id128_to_string(id
, buf
));
128 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;