]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine-id-setup/machine-id-setup-main.c
machine-id-setup: use id128_get_machine() at one more place
[thirdparty/systemd.git] / src / machine-id-setup / machine-id-setup-main.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
d7ccca2e 2
984bf931 3#include <errno.h>
3f6fd1ba
LP
4#include <getopt.h>
5#include <stdio.h>
6#include <stdlib.h>
d7ccca2e 7
37ec0fdd 8#include "alloc-util.h"
d6b4d1c7 9#include "build.h"
e0b4bc23 10#include "dissect-image.h"
a6f72863 11#include "id128-util.h"
d7ccca2e 12#include "log.h"
3f6fd1ba 13#include "machine-id-setup.h"
0166c428 14#include "main-func.h"
e0b4bc23 15#include "mount-util.h"
614b022c 16#include "parse-argument.h"
0f03c2a4 17#include "path-util.h"
294bf0c3 18#include "pretty-print.h"
e0b4bc23 19#include "terminal-util.h"
984bf931 20
0f03c2a4 21static char *arg_root = NULL;
e0b4bc23 22static char *arg_image = NULL;
4a9b1dd4 23static bool arg_commit = false;
487ddeb8 24static bool arg_print = false;
92f2f92e 25
0166c428 26STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
e0b4bc23 27STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
0166c428 28
37ec0fdd
LP
29static int help(void) {
30 _cleanup_free_ char *link = NULL;
31 int r;
32
33 r = terminal_urlify_man("systemd-machine-id-setup", "1", &link);
34 if (r < 0)
35 return log_oom();
36
e0b4bc23
LP
37 printf("%s [OPTIONS...]\n"
38 "\n%sInitialize /etc/machine-id from a random source.%s\n\n"
984bf931 39 " -h --help Show this help\n"
92f2f92e 40 " --version Show package version\n"
e0b4bc23
LP
41 " --root=PATH Operate relative to root path\n"
42 " --image=PATH Operate relative to image file\n"
4a9b1dd4 43 " --commit Commit transient ID\n"
487ddeb8 44 " --print Print used machine ID\n"
bc556335
DDM
45 "\nSee the %s for details.\n",
46 program_invocation_short_name,
e0b4bc23
LP
47 ansi_highlight(),
48 ansi_normal(),
bc556335 49 link);
37ec0fdd
LP
50
51 return 0;
984bf931
LP
52}
53
54static int parse_argv(int argc, char *argv[]) {
55
56 enum {
92f2f92e
GKH
57 ARG_VERSION = 0x100,
58 ARG_ROOT,
e0b4bc23 59 ARG_IMAGE,
4a9b1dd4 60 ARG_COMMIT,
487ddeb8 61 ARG_PRINT,
984bf931
LP
62 };
63
64 static const struct option options[] = {
65 { "help", no_argument, NULL, 'h' },
66 { "version", no_argument, NULL, ARG_VERSION },
92f2f92e 67 { "root", required_argument, NULL, ARG_ROOT },
e0b4bc23 68 { "image", required_argument, NULL, ARG_IMAGE },
4a9b1dd4 69 { "commit", no_argument, NULL, ARG_COMMIT },
487ddeb8 70 { "print", no_argument, NULL, ARG_PRINT },
eb9da376 71 {}
984bf931
LP
72 };
73
0f03c2a4 74 int c, r;
984bf931
LP
75
76 assert(argc >= 0);
77 assert(argv);
78
4a434023 79 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
984bf931
LP
80
81 switch (c) {
82
83 case 'h':
37ec0fdd 84 return help();
984bf931
LP
85
86 case ARG_VERSION:
3f6fd1ba 87 return version();
984bf931 88
92f2f92e 89 case ARG_ROOT:
614b022c 90 r = parse_path_argument(optarg, true, &arg_root);
0f03c2a4
LP
91 if (r < 0)
92 return r;
92f2f92e
GKH
93 break;
94
e0b4bc23
LP
95 case ARG_IMAGE:
96 r = parse_path_argument(optarg, false, &arg_image);
97 if (r < 0)
98 return r;
99 break;
100
4a9b1dd4
LP
101 case ARG_COMMIT:
102 arg_commit = true;
103 break;
104
487ddeb8
LP
105 case ARG_PRINT:
106 arg_print = true;
107 break;
108
984bf931
LP
109 case '?':
110 return -EINVAL;
111
112 default:
04499a70 113 assert_not_reached();
984bf931 114 }
984bf931 115
baaa35ad
ZJS
116 if (optind < argc)
117 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
118 "Extraneous arguments");
984bf931 119
e0b4bc23
LP
120 if (arg_image && arg_root)
121 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
122
984bf931
LP
123 return 1;
124}
d7ccca2e 125
0166c428 126static int run(int argc, char *argv[]) {
e0b4bc23 127 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
e0b4bc23 128 _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL;
487ddeb8 129 sd_id128_t id;
984bf931 130 int r;
d7ccca2e 131
d7ccca2e
LP
132 log_parse_environment();
133 log_open();
134
984bf931
LP
135 r = parse_argv(argc, argv);
136 if (r <= 0)
0166c428 137 return r;
984bf931 138
e0b4bc23
LP
139 if (arg_image) {
140 assert(!arg_root);
141
142 r = mount_image_privately_interactively(
143 arg_image,
144 DISSECT_IMAGE_REQUIRE_ROOT |
145 DISSECT_IMAGE_VALIDATE_OS |
146 DISSECT_IMAGE_RELAX_VAR_CHECK |
c65f854a
LP
147 DISSECT_IMAGE_FSCK |
148 DISSECT_IMAGE_GROWFS,
e0b4bc23 149 &unlink_dir,
a133d2c3 150 /* ret_dir_fd= */ NULL,
e330f97a 151 &loop_device);
e0b4bc23
LP
152 if (r < 0)
153 return r;
154
155 arg_root = strdup(unlink_dir);
156 if (!arg_root)
157 return log_oom();
158 }
159
487ddeb8 160 if (arg_commit) {
4a9b1dd4 161 r = machine_id_commit(arg_root);
487ddeb8 162 if (r < 0)
0166c428 163 return r;
487ddeb8 164
c1d74108 165 r = id128_get_machine(arg_root, &id);
0166c428
YW
166 if (r < 0)
167 return log_error_errno(r, "Failed to read machine ID back: %m");
487ddeb8 168 } else {
3023f2fe 169 r = machine_id_setup(arg_root, false, SD_ID128_NULL, &id);
487ddeb8 170 if (r < 0)
0166c428 171 return r;
487ddeb8
LP
172 }
173
174 if (arg_print)
85b55869 175 puts(SD_ID128_TO_STRING(id));
4a9b1dd4 176
0166c428 177 return 0;
d7ccca2e 178}
0166c428
YW
179
180DEFINE_MAIN_FUNCTION(run);