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 "main-func.h"
13 #include "path-util.h"
14 #include "pretty-print.h"
17 static char *arg_root
= NULL
;
18 static bool arg_commit
= false;
19 static bool arg_print
= false;
21 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
23 static int help(void) {
24 _cleanup_free_
char *link
= NULL
;
27 r
= terminal_urlify_man("systemd-machine-id-setup", "1", &link
);
31 printf("%s [OPTIONS...]\n\n"
32 "Initialize /etc/machine-id from a random source.\n\n"
33 " -h --help Show this help\n"
34 " --version Show package version\n"
35 " --root=ROOT Filesystem root\n"
36 " --commit Commit transient ID\n"
37 " --print Print used machine ID\n"
38 "\nSee the %s for details.\n"
39 , program_invocation_short_name
46 static int parse_argv(int argc
, char *argv
[]) {
55 static const struct option options
[] = {
56 { "help", no_argument
, NULL
, 'h' },
57 { "version", no_argument
, NULL
, ARG_VERSION
},
58 { "root", required_argument
, NULL
, ARG_ROOT
},
59 { "commit", no_argument
, NULL
, ARG_COMMIT
},
60 { "print", no_argument
, NULL
, ARG_PRINT
},
69 while ((c
= getopt_long(argc
, argv
, "hqcv", options
, NULL
)) >= 0)
80 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
97 assert_not_reached("Unhandled option");
101 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
102 "Extraneous arguments");
107 static int run(int argc
, char *argv
[]) {
108 char buf
[SD_ID128_STRING_MAX
];
112 log_parse_environment();
115 r
= parse_argv(argc
, argv
);
120 const char *etc_machine_id
;
122 r
= machine_id_commit(arg_root
);
126 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
127 r
= id128_read(etc_machine_id
, ID128_PLAIN
, &id
);
129 return 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
));
142 DEFINE_MAIN_FUNCTION(run
);