1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
11 #include "id128-util.h"
13 #include "machine-id-setup.h"
14 #include "path-util.h"
17 static char *arg_root
= NULL
;
18 static bool arg_commit
= false;
19 static bool arg_print
= false;
21 static void help(void) {
22 printf("%s [OPTIONS...]\n\n"
23 "Initialize /etc/machine-id from a random source.\n\n"
24 " -h --help Show this help\n"
25 " --version Show package version\n"
26 " --root=ROOT Filesystem root\n"
27 " --commit Commit transient ID\n"
28 " --print Print used machine ID\n"
29 , program_invocation_short_name
);
32 static int parse_argv(int argc
, char *argv
[]) {
41 static const struct option options
[] = {
42 { "help", no_argument
, NULL
, 'h' },
43 { "version", no_argument
, NULL
, ARG_VERSION
},
44 { "root", required_argument
, NULL
, ARG_ROOT
},
45 { "commit", no_argument
, NULL
, ARG_COMMIT
},
46 { "print", no_argument
, NULL
, ARG_PRINT
},
55 while ((c
= getopt_long(argc
, argv
, "hqcv", options
, NULL
)) >= 0)
67 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
84 assert_not_reached("Unhandled option");
88 log_error("Extraneous arguments");
95 int main(int argc
, char *argv
[]) {
96 char buf
[SD_ID128_STRING_MAX
];
100 log_parse_environment();
103 r
= parse_argv(argc
, argv
);
108 const char *etc_machine_id
;
110 r
= machine_id_commit(arg_root
);
114 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
115 r
= id128_read(etc_machine_id
, ID128_PLAIN
, &id
);
117 log_error_errno(r
, "Failed to read machine ID back: %m");
121 r
= machine_id_setup(arg_root
, SD_ID128_NULL
, &id
);
127 puts(sd_id128_to_string(id
, buf
));
131 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;