1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
10 #include "dissect-image.h"
11 #include "id128-util.h"
13 #include "machine-id-setup.h"
14 #include "main-func.h"
15 #include "mount-util.h"
16 #include "parse-argument.h"
17 #include "path-util.h"
18 #include "pretty-print.h"
19 #include "terminal-util.h"
21 static char *arg_root
= NULL
;
22 static char *arg_image
= NULL
;
23 static bool arg_commit
= false;
24 static bool arg_print
= false;
25 static ImagePolicy
*arg_image_policy
= NULL
;
27 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
28 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
29 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
31 static int help(void) {
32 _cleanup_free_
char *link
= NULL
;
35 r
= terminal_urlify_man("systemd-machine-id-setup", "1", &link
);
39 printf("%s [OPTIONS...]\n"
40 "\n%sInitialize /etc/machine-id from a random source.%s\n\n"
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"
48 "\nSee the %s for details.\n",
49 program_invocation_short_name
,
57 static int parse_argv(int argc
, char *argv
[]) {
68 static const struct option options
[] = {
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
},
73 { "image-policy", required_argument
, NULL
, ARG_IMAGE_POLICY
},
74 { "commit", no_argument
, NULL
, ARG_COMMIT
},
75 { "print", no_argument
, NULL
, ARG_PRINT
},
84 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
95 r
= parse_path_argument(optarg
, true, &arg_root
);
101 r
= parse_path_argument(optarg
, false, &arg_image
);
106 case ARG_IMAGE_POLICY
:
107 r
= parse_image_policy_argument(optarg
, &arg_image_policy
);
124 assert_not_reached();
128 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
129 "Extraneous arguments");
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.");
137 static int run(int argc
, char *argv
[]) {
138 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
139 _cleanup_(umount_and_freep
) char *mounted_dir
= NULL
;
142 log_parse_environment();
145 r
= parse_argv(argc
, argv
);
152 r
= mount_image_privately_interactively(
155 DISSECT_IMAGE_REQUIRE_ROOT
|
156 DISSECT_IMAGE_VALIDATE_OS
|
157 DISSECT_IMAGE_RELAX_VAR_CHECK
|
159 DISSECT_IMAGE_GROWFS
,
161 /* ret_dir_fd= */ NULL
,
166 arg_root
= strdup(mounted_dir
);
174 r
= machine_id_commit(arg_root
);
178 r
= id128_get_machine(arg_root
, &id
);
180 return log_error_errno(r
, "Failed to read machine ID back: %m");
183 puts(SD_ID128_TO_STRING(id
));
185 } else if (id128_get_machine(arg_root
, NULL
) == -ENOPKG
) {
187 puts("uninitialized");
191 r
= machine_id_setup(arg_root
, false, SD_ID128_NULL
, &id
);
196 puts(SD_ID128_TO_STRING(id
));
202 DEFINE_MAIN_FUNCTION(run
);