1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "id128-util.h"
28 #include "machine-id-setup.h"
29 #include "path-util.h"
32 static char *arg_root
= NULL
;
33 static bool arg_commit
= false;
34 static bool arg_print
= false;
36 static void help(void) {
37 printf("%s [OPTIONS...]\n\n"
38 "Initialize /etc/machine-id from a random source.\n\n"
39 " -h --help Show this help\n"
40 " --version Show package version\n"
41 " --root=ROOT Filesystem root\n"
42 " --commit Commit transient ID\n"
43 " --print Print used machine ID\n"
44 , program_invocation_short_name
);
47 static int parse_argv(int argc
, char *argv
[]) {
56 static const struct option options
[] = {
57 { "help", no_argument
, NULL
, 'h' },
58 { "version", no_argument
, NULL
, ARG_VERSION
},
59 { "root", required_argument
, NULL
, ARG_ROOT
},
60 { "commit", no_argument
, NULL
, ARG_COMMIT
},
61 { "print", no_argument
, NULL
, ARG_PRINT
},
70 while ((c
= getopt_long(argc
, argv
, "hqcv", options
, NULL
)) >= 0)
82 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
99 assert_not_reached("Unhandled option");
103 log_error("Extraneous arguments");
110 int main(int argc
, char *argv
[]) {
111 char buf
[SD_ID128_STRING_MAX
];
115 log_parse_environment();
118 r
= parse_argv(argc
, argv
);
123 const char *etc_machine_id
;
125 r
= machine_id_commit(arg_root
);
129 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
130 r
= id128_read(etc_machine_id
, ID128_PLAIN
, &id
);
132 log_error_errno(r
, "Failed to read machine ID back: %m");
136 r
= machine_id_setup(arg_root
, SD_ID128_NULL
, &id
);
142 puts(sd_id128_to_string(id
, buf
));
146 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;