]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine-id-setup/machine-id-setup-main.c
Merge pull request #31531 from poettering/verity-userspace-optional
[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;
84be0c71 25static ImagePolicy *arg_image_policy = NULL;
92f2f92e 26
0166c428 27STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
e0b4bc23 28STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
84be0c71 29STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
0166c428 30
37ec0fdd
LP
31static 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
57static 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 137static int run(int argc, char *argv[]) {
e0b4bc23 138 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
a4b3e942 139 _cleanup_(umount_and_freep) char *mounted_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 158 DISSECT_IMAGE_FSCK |
f4a63ce2
LP
159 DISSECT_IMAGE_GROWFS |
160 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
a4b3e942 161 &mounted_dir,
a133d2c3 162 /* ret_dir_fd= */ NULL,
e330f97a 163 &loop_device);
e0b4bc23
LP
164 if (r < 0)
165 return r;
166
a4b3e942 167 arg_root = strdup(mounted_dir);
e0b4bc23
LP
168 if (!arg_root)
169 return log_oom();
170 }
171
487ddeb8 172 if (arg_commit) {
62281c78
DDM
173 sd_id128_t id;
174
4a9b1dd4 175 r = machine_id_commit(arg_root);
487ddeb8 176 if (r < 0)
0166c428 177 return r;
487ddeb8 178
c1d74108 179 r = id128_get_machine(arg_root, &id);
0166c428
YW
180 if (r < 0)
181 return log_error_errno(r, "Failed to read machine ID back: %m");
62281c78
DDM
182
183 if (arg_print)
184 puts(SD_ID128_TO_STRING(id));
185
186 } else if (id128_get_machine(arg_root, NULL) == -ENOPKG) {
187 if (arg_print)
188 puts("uninitialized");
487ddeb8 189 } else {
62281c78
DDM
190 sd_id128_t id;
191
3023f2fe 192 r = machine_id_setup(arg_root, false, SD_ID128_NULL, &id);
487ddeb8 193 if (r < 0)
0166c428 194 return r;
487ddeb8 195
62281c78
DDM
196 if (arg_print)
197 puts(SD_ID128_TO_STRING(id));
198 }
4a9b1dd4 199
0166c428 200 return 0;
d7ccca2e 201}
0166c428
YW
202
203DEFINE_MAIN_FUNCTION(run);