]>
Commit | Line | Data |
---|---|---|
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 | 21 | static char *arg_root = NULL; |
e0b4bc23 | 22 | static char *arg_image = NULL; |
4a9b1dd4 | 23 | static bool arg_commit = false; |
487ddeb8 | 24 | static bool arg_print = false; |
84be0c71 | 25 | static ImagePolicy *arg_image_policy = NULL; |
92f2f92e | 26 | |
0166c428 | 27 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
e0b4bc23 | 28 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
84be0c71 | 29 | STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); |
0166c428 | 30 | |
37ec0fdd LP |
31 | static int help(void) { |
32 | _cleanup_free_ char *link = NULL; | |
33 | int r; | |
34 | ||
35 | r = terminal_urlify_man("systemd-machine-id-setup", "1", &link); | |
36 | if (r < 0) | |
37 | return log_oom(); | |
38 | ||
e0b4bc23 LP |
39 | printf("%s [OPTIONS...]\n" |
40 | "\n%sInitialize /etc/machine-id from a random source.%s\n\n" | |
84be0c71 LP |
41 | " -h --help Show this help\n" |
42 | " --version Show package version\n" | |
43 | " --root=PATH Operate on an alternate filesystem root\n" | |
44 | " --image=PATH Operate on disk image as filesystem root\n" | |
45 | " --image-policy=POLICY Specify disk image dissection policy\n" | |
46 | " --commit Commit transient ID\n" | |
47 | " --print Print used machine ID\n" | |
bc556335 DDM |
48 | "\nSee the %s for details.\n", |
49 | program_invocation_short_name, | |
e0b4bc23 LP |
50 | ansi_highlight(), |
51 | ansi_normal(), | |
bc556335 | 52 | link); |
37ec0fdd LP |
53 | |
54 | return 0; | |
984bf931 LP |
55 | } |
56 | ||
57 | static int parse_argv(int argc, char *argv[]) { | |
58 | ||
59 | enum { | |
92f2f92e GKH |
60 | ARG_VERSION = 0x100, |
61 | ARG_ROOT, | |
e0b4bc23 | 62 | ARG_IMAGE, |
06e78680 | 63 | ARG_IMAGE_POLICY, |
4a9b1dd4 | 64 | ARG_COMMIT, |
487ddeb8 | 65 | ARG_PRINT, |
984bf931 LP |
66 | }; |
67 | ||
68 | static const struct option options[] = { | |
84be0c71 LP |
69 | { "help", no_argument, NULL, 'h' }, |
70 | { "version", no_argument, NULL, ARG_VERSION }, | |
71 | { "root", required_argument, NULL, ARG_ROOT }, | |
72 | { "image", required_argument, NULL, ARG_IMAGE }, | |
06e78680 | 73 | { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY }, |
84be0c71 LP |
74 | { "commit", no_argument, NULL, ARG_COMMIT }, |
75 | { "print", no_argument, NULL, ARG_PRINT }, | |
eb9da376 | 76 | {} |
984bf931 LP |
77 | }; |
78 | ||
0f03c2a4 | 79 | int c, r; |
984bf931 LP |
80 | |
81 | assert(argc >= 0); | |
82 | assert(argv); | |
83 | ||
4a434023 | 84 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
984bf931 LP |
85 | |
86 | switch (c) { | |
87 | ||
88 | case 'h': | |
37ec0fdd | 89 | return help(); |
984bf931 LP |
90 | |
91 | case ARG_VERSION: | |
3f6fd1ba | 92 | return version(); |
984bf931 | 93 | |
92f2f92e | 94 | case ARG_ROOT: |
614b022c | 95 | r = parse_path_argument(optarg, true, &arg_root); |
0f03c2a4 LP |
96 | if (r < 0) |
97 | return r; | |
92f2f92e GKH |
98 | break; |
99 | ||
e0b4bc23 LP |
100 | case ARG_IMAGE: |
101 | r = parse_path_argument(optarg, false, &arg_image); | |
102 | if (r < 0) | |
103 | return r; | |
104 | break; | |
105 | ||
06e78680 YW |
106 | case ARG_IMAGE_POLICY: |
107 | r = parse_image_policy_argument(optarg, &arg_image_policy); | |
108 | if (r < 0) | |
109 | return r; | |
110 | break; | |
111 | ||
4a9b1dd4 LP |
112 | case ARG_COMMIT: |
113 | arg_commit = true; | |
114 | break; | |
115 | ||
487ddeb8 LP |
116 | case ARG_PRINT: |
117 | arg_print = true; | |
118 | break; | |
119 | ||
984bf931 LP |
120 | case '?': |
121 | return -EINVAL; | |
122 | ||
123 | default: | |
04499a70 | 124 | assert_not_reached(); |
984bf931 | 125 | } |
984bf931 | 126 | |
baaa35ad ZJS |
127 | if (optind < argc) |
128 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
129 | "Extraneous arguments"); | |
984bf931 | 130 | |
e0b4bc23 LP |
131 | if (arg_image && arg_root) |
132 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); | |
133 | ||
984bf931 LP |
134 | return 1; |
135 | } | |
d7ccca2e | 136 | |
0166c428 | 137 | static int run(int argc, char *argv[]) { |
e0b4bc23 | 138 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
e0b4bc23 | 139 | _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL; |
984bf931 | 140 | int r; |
d7ccca2e | 141 | |
d7ccca2e LP |
142 | log_parse_environment(); |
143 | log_open(); | |
144 | ||
984bf931 LP |
145 | r = parse_argv(argc, argv); |
146 | if (r <= 0) | |
0166c428 | 147 | return r; |
984bf931 | 148 | |
e0b4bc23 LP |
149 | if (arg_image) { |
150 | assert(!arg_root); | |
151 | ||
152 | r = mount_image_privately_interactively( | |
153 | arg_image, | |
84be0c71 | 154 | arg_image_policy, |
e0b4bc23 LP |
155 | DISSECT_IMAGE_REQUIRE_ROOT | |
156 | DISSECT_IMAGE_VALIDATE_OS | | |
157 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
c65f854a LP |
158 | DISSECT_IMAGE_FSCK | |
159 | DISSECT_IMAGE_GROWFS, | |
e0b4bc23 | 160 | &unlink_dir, |
a133d2c3 | 161 | /* ret_dir_fd= */ NULL, |
e330f97a | 162 | &loop_device); |
e0b4bc23 LP |
163 | if (r < 0) |
164 | return r; | |
165 | ||
166 | arg_root = strdup(unlink_dir); | |
167 | if (!arg_root) | |
168 | return log_oom(); | |
169 | } | |
170 | ||
487ddeb8 | 171 | if (arg_commit) { |
62281c78 DDM |
172 | sd_id128_t id; |
173 | ||
4a9b1dd4 | 174 | r = machine_id_commit(arg_root); |
487ddeb8 | 175 | if (r < 0) |
0166c428 | 176 | return r; |
487ddeb8 | 177 | |
c1d74108 | 178 | r = id128_get_machine(arg_root, &id); |
0166c428 YW |
179 | if (r < 0) |
180 | return log_error_errno(r, "Failed to read machine ID back: %m"); | |
62281c78 DDM |
181 | |
182 | if (arg_print) | |
183 | puts(SD_ID128_TO_STRING(id)); | |
184 | ||
185 | } else if (id128_get_machine(arg_root, NULL) == -ENOPKG) { | |
186 | if (arg_print) | |
187 | puts("uninitialized"); | |
487ddeb8 | 188 | } else { |
62281c78 DDM |
189 | sd_id128_t id; |
190 | ||
3023f2fe | 191 | r = machine_id_setup(arg_root, false, SD_ID128_NULL, &id); |
487ddeb8 | 192 | if (r < 0) |
0166c428 | 193 | return r; |
487ddeb8 | 194 | |
62281c78 DDM |
195 | if (arg_print) |
196 | puts(SD_ID128_TO_STRING(id)); | |
197 | } | |
4a9b1dd4 | 198 | |
0166c428 | 199 | return 0; |
d7ccca2e | 200 | } |
0166c428 YW |
201 | |
202 | DEFINE_MAIN_FUNCTION(run); |