| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "build.h" |
| 7 |
#include "dissect-image.h" |
| 8 |
#include "format-table.h" |
| 9 |
#include "id128-util.h" |
| 10 |
#include "image-policy.h" |
| 11 |
#include "log.h" |
| 12 |
#include "loop-util.h" |
| 13 |
#include "machine-id-setup.h" |
| 14 |
#include "main-func.h" |
| 15 |
#include "mount-util.h" |
| 16 |
#include "options.h" |
| 17 |
#include "parse-argument.h" |
| 18 |
#include "pretty-print.h" |
| 19 |
|
| 20 |
static char *arg_root = NULL; |
| 21 |
static char *arg_image = NULL; |
| 22 |
static bool arg_commit = false; |
| 23 |
static bool arg_print = false; |
| 24 |
static ImagePolicy *arg_image_policy = NULL; |
| 25 |
|
| 26 |
STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
| 27 |
STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
| 28 |
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); |
| 29 |
|
| 30 |
static int help(void) { |
| 31 |
_cleanup_free_ char *link = NULL; |
| 32 |
_cleanup_(table_unrefp) Table *commands = NULL, *options = 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 |
|
| 39 |
r = option_parser_get_help_table(&commands); |
| 40 |
if (r < 0) |
| 41 |
return r; |
| 42 |
|
| 43 |
r = option_parser_get_help_table_group("Options", &options); |
| 44 |
if (r < 0) |
| 45 |
return r; |
| 46 |
|
| 47 |
(void) table_sync_column_widths(0, commands, options); |
| 48 |
|
| 49 |
printf("%s [OPTIONS...]\n\n" |
| 50 |
"%sInitialize /etc/machine-id from a random source.%s\n" |
| 51 |
"\n%sCommands:%s\n", |
| 52 |
program_invocation_short_name, |
| 53 |
ansi_highlight(), |
| 54 |
ansi_normal(), |
| 55 |
ansi_underline(), |
| 56 |
ansi_normal()); |
| 57 |
|
| 58 |
r = table_print_or_warn(commands); |
| 59 |
if (r < 0) |
| 60 |
return r; |
| 61 |
|
| 62 |
printf("\n%sOptions:%s\n", |
| 63 |
ansi_underline(), |
| 64 |
ansi_normal()); |
| 65 |
|
| 66 |
r = table_print_or_warn(options); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
|
| 70 |
printf("\nSee the %s for details.\n", link); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
static int parse_argv(int argc, char *argv[]) { |
| 75 |
assert(argc >= 0); |
| 76 |
assert(argv); |
| 77 |
|
| 78 |
OptionParser opts = { argc, argv }; |
| 79 |
int r; |
| 80 |
|
| 81 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 82 |
switch (c) { |
| 83 |
|
| 84 |
OPTION_COMMON_HELP: |
| 85 |
return help(); |
| 86 |
|
| 87 |
OPTION_COMMON_VERSION: |
| 88 |
return version(); |
| 89 |
|
| 90 |
OPTION_LONG("commit", NULL, "Commit transient ID"): |
| 91 |
arg_commit = true; |
| 92 |
break; |
| 93 |
|
| 94 |
OPTION_GROUP("Options"): {} |
| 95 |
|
| 96 |
OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"): |
| 97 |
r = parse_path_argument(opts.arg, true, &arg_root); |
| 98 |
if (r < 0) |
| 99 |
return r; |
| 100 |
break; |
| 101 |
|
| 102 |
OPTION_LONG("image", "PATH", "Operate on disk image as filesystem root"): |
| 103 |
r = parse_path_argument(opts.arg, false, &arg_image); |
| 104 |
if (r < 0) |
| 105 |
return r; |
| 106 |
break; |
| 107 |
|
| 108 |
OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"): |
| 109 |
r = parse_image_policy_argument(opts.arg, &arg_image_policy); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
break; |
| 113 |
|
| 114 |
OPTION_LONG("print", NULL, "Print used machine ID"): |
| 115 |
arg_print = true; |
| 116 |
break; |
| 117 |
} |
| 118 |
|
| 119 |
if (option_parser_get_n_args(&opts) > 0) |
| 120 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 121 |
"Extraneous arguments"); |
| 122 |
|
| 123 |
if (arg_image && arg_root) |
| 124 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); |
| 125 |
|
| 126 |
return 1; |
| 127 |
} |
| 128 |
|
| 129 |
static int run(int argc, char *argv[]) { |
| 130 |
_cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
| 131 |
_cleanup_(umount_and_freep) char *mounted_dir = NULL; |
| 132 |
int r; |
| 133 |
|
| 134 |
log_setup(); |
| 135 |
|
| 136 |
r = parse_argv(argc, argv); |
| 137 |
if (r <= 0) |
| 138 |
return r; |
| 139 |
|
| 140 |
if (arg_image) { |
| 141 |
assert(!arg_root); |
| 142 |
|
| 143 |
r = mount_image_privately_interactively( |
| 144 |
arg_image, |
| 145 |
arg_image_policy, |
| 146 |
DISSECT_IMAGE_REQUIRE_ROOT | |
| 147 |
DISSECT_IMAGE_VALIDATE_OS | |
| 148 |
DISSECT_IMAGE_RELAX_VAR_CHECK | |
| 149 |
DISSECT_IMAGE_FSCK | |
| 150 |
DISSECT_IMAGE_GROWFS | |
| 151 |
DISSECT_IMAGE_ALLOW_USERSPACE_VERITY, |
| 152 |
&mounted_dir, |
| 153 |
/* ret_dir_fd= */ NULL, |
| 154 |
&loop_device); |
| 155 |
if (r < 0) |
| 156 |
return r; |
| 157 |
|
| 158 |
arg_root = strdup(mounted_dir); |
| 159 |
if (!arg_root) |
| 160 |
return log_oom(); |
| 161 |
} |
| 162 |
|
| 163 |
if (arg_commit) { |
| 164 |
sd_id128_t id; |
| 165 |
|
| 166 |
r = machine_id_commit(arg_root); |
| 167 |
if (r < 0) |
| 168 |
return r; |
| 169 |
|
| 170 |
r = id128_get_machine(arg_root, &id); |
| 171 |
if (r < 0) |
| 172 |
return log_error_errno(r, "Failed to read machine ID back: %m"); |
| 173 |
|
| 174 |
if (arg_print) |
| 175 |
puts(SD_ID128_TO_STRING(id)); |
| 176 |
|
| 177 |
} else if (id128_get_machine(arg_root, NULL) == -ENOPKG) { |
| 178 |
if (arg_print) |
| 179 |
puts("uninitialized"); |
| 180 |
} else { |
| 181 |
sd_id128_t id; |
| 182 |
|
| 183 |
r = machine_id_setup(arg_root, SD_ID128_NULL, /* flags= */ 0, &id); |
| 184 |
if (r < 0) |
| 185 |
return r; |
| 186 |
|
| 187 |
if (arg_print) |
| 188 |
puts(SD_ID128_TO_STRING(id)); |
| 189 |
} |
| 190 |
|
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
DEFINE_MAIN_FUNCTION(run); |