| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <locale.h> |
| 4 |
#include <stdio.h> |
| 5 |
|
| 6 |
#include "sd-event.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "ansi-color.h" |
| 10 |
#include "build.h" |
| 11 |
#include "discover-image.h" |
| 12 |
#include "env-util.h" |
| 13 |
#include "format-table.h" |
| 14 |
#include "hexdecoct.h" |
| 15 |
#include "import-common.h" |
| 16 |
#include "import-util.h" |
| 17 |
#include "io-util.h" |
| 18 |
#include "iovec-util.h" |
| 19 |
#include "log.h" |
| 20 |
#include "main-func.h" |
| 21 |
#include "oci-util.h" |
| 22 |
#include "options.h" |
| 23 |
#include "parse-argument.h" |
| 24 |
#include "parse-util.h" |
| 25 |
#include "path-util.h" |
| 26 |
#include "pull-oci.h" |
| 27 |
#include "pull-raw.h" |
| 28 |
#include "pull-tar.h" |
| 29 |
#include "runtime-scope.h" |
| 30 |
#include "signal-util.h" |
| 31 |
#include "string-util.h" |
| 32 |
#include "verbs.h" |
| 33 |
#include "web-util.h" |
| 34 |
|
| 35 |
static char *arg_image_root = NULL; |
| 36 |
static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE; |
| 37 |
static ImportFlags arg_import_flags = IMPORT_PULL_SETTINGS | IMPORT_PULL_ROOTHASH | IMPORT_PULL_ROOTHASH_SIGNATURE | IMPORT_PULL_VERITY | IMPORT_BTRFS_SUBVOL | IMPORT_BTRFS_QUOTA | IMPORT_CONVERT_QCOW2 | IMPORT_SYNC; |
| 38 |
static uint64_t arg_offset = UINT64_MAX, arg_size_max = UINT64_MAX; |
| 39 |
static struct iovec arg_checksum = {}; |
| 40 |
static ImageClass arg_class = IMAGE_MACHINE; |
| 41 |
static RuntimeScope arg_runtime_scope = _RUNTIME_SCOPE_INVALID; |
| 42 |
|
| 43 |
STATIC_DESTRUCTOR_REGISTER(arg_checksum, iovec_done); |
| 44 |
STATIC_DESTRUCTOR_REGISTER(arg_image_root, freep); |
| 45 |
|
| 46 |
static int normalize_local(const char *local, const char *url, char **ret) { |
| 47 |
_cleanup_free_ char *ll = NULL; |
| 48 |
int r; |
| 49 |
|
| 50 |
assert(ret); |
| 51 |
|
| 52 |
if (arg_import_flags & IMPORT_DIRECT) { |
| 53 |
|
| 54 |
if (!local) |
| 55 |
log_debug("Writing downloaded data to STDOUT."); |
| 56 |
else { |
| 57 |
if (!path_is_absolute(local)) { |
| 58 |
ll = path_join(arg_image_root, local); |
| 59 |
if (!ll) |
| 60 |
return log_oom(); |
| 61 |
|
| 62 |
local = ll; |
| 63 |
} |
| 64 |
|
| 65 |
if (!path_is_valid(local)) |
| 66 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 67 |
"Local path name '%s' is not valid.", local); |
| 68 |
} |
| 69 |
|
| 70 |
} else if (local) { |
| 71 |
|
| 72 |
if (!image_name_is_valid(local)) |
| 73 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 74 |
"Local image name '%s' is not valid.", |
| 75 |
local); |
| 76 |
|
| 77 |
if (!FLAGS_SET(arg_import_flags, IMPORT_FORCE)) { |
| 78 |
r = image_find(arg_runtime_scope, arg_class, local, NULL, NULL); |
| 79 |
if (r < 0) { |
| 80 |
if (r != -ENOENT) |
| 81 |
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); |
| 82 |
} else |
| 83 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 84 |
"Image '%s' already exists.", |
| 85 |
local); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (!ll && local) { |
| 90 |
ll = strdup(local); |
| 91 |
if (!ll) |
| 92 |
return log_oom(); |
| 93 |
} |
| 94 |
|
| 95 |
if (ll) { |
| 96 |
if (arg_offset != UINT64_MAX) |
| 97 |
log_info("Pulling '%s', saving at offset %" PRIu64 " in '%s'.", url, arg_offset, ll); |
| 98 |
else |
| 99 |
log_info("Pulling '%s', saving as '%s'.", url, ll); |
| 100 |
} else |
| 101 |
log_info("Pulling '%s'.", url); |
| 102 |
|
| 103 |
if (!FLAGS_SET(arg_import_flags, IMPORT_DIRECT)) |
| 104 |
log_info("Operating on image directory '%s'.", arg_image_root); |
| 105 |
|
| 106 |
if (!FLAGS_SET(arg_import_flags, IMPORT_SYNC)) |
| 107 |
log_info("File system synchronization on completion is off."); |
| 108 |
|
| 109 |
*ret = TAKE_PTR(ll); |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
static void on_tar_finished(TarPull *pull, int error, void *userdata) { |
| 114 |
sd_event *event = userdata; |
| 115 |
assert(pull); |
| 116 |
|
| 117 |
if (error == 0) |
| 118 |
log_info("Operation completed successfully."); |
| 119 |
|
| 120 |
sd_event_exit(event, ABS(error)); |
| 121 |
} |
| 122 |
|
| 123 |
VERB(verb_tar, "tar", "URL [NAME]", 2, 3, 0, "Download a TAR image"); |
| 124 |
static int verb_tar(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 125 |
_cleanup_free_ char *ll = NULL, *normalized = NULL; |
| 126 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 127 |
_cleanup_(tar_pull_unrefp) TarPull *pull = NULL; |
| 128 |
const char *url, *local; |
| 129 |
int r; |
| 130 |
|
| 131 |
url = argv[1]; |
| 132 |
if (!http_url_is_valid(url) && !file_url_is_valid(url)) |
| 133 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url); |
| 134 |
|
| 135 |
if (argc >= 3) |
| 136 |
local = empty_or_dash_to_null(argv[2]); |
| 137 |
else { |
| 138 |
_cleanup_free_ char *l = NULL; |
| 139 |
|
| 140 |
r = import_url_last_component(url, &l); |
| 141 |
if (r < 0) |
| 142 |
return log_error_errno(r, "Failed to get final component of URL: %m"); |
| 143 |
|
| 144 |
r = tar_strip_suffixes(l, &ll); |
| 145 |
if (r < 0) |
| 146 |
return log_oom(); |
| 147 |
|
| 148 |
local = ll; |
| 149 |
} |
| 150 |
|
| 151 |
if (!local && FLAGS_SET(arg_import_flags, IMPORT_DIRECT)) |
| 152 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Pulling tar images to STDOUT is not supported."); |
| 153 |
|
| 154 |
r = normalize_local(local, url, &normalized); |
| 155 |
if (r < 0) |
| 156 |
return r; |
| 157 |
|
| 158 |
r = import_allocate_event_with_signals(&event); |
| 159 |
if (r < 0) |
| 160 |
return r; |
| 161 |
|
| 162 |
r = tar_pull_new(&pull, event, arg_image_root, on_tar_finished, event); |
| 163 |
if (r < 0) |
| 164 |
return log_error_errno(r, "Failed to allocate puller: %m"); |
| 165 |
|
| 166 |
r = tar_pull_start( |
| 167 |
pull, |
| 168 |
url, |
| 169 |
normalized, |
| 170 |
arg_import_flags & IMPORT_PULL_FLAGS_MASK_TAR, |
| 171 |
arg_verify, |
| 172 |
&arg_checksum); |
| 173 |
if (r < 0) |
| 174 |
return log_error_errno(r, "Failed to pull image: %m"); |
| 175 |
|
| 176 |
r = sd_event_loop(event); |
| 177 |
if (r < 0) |
| 178 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 179 |
|
| 180 |
log_info("Exiting."); |
| 181 |
return -r; |
| 182 |
} |
| 183 |
|
| 184 |
static void on_raw_finished(RawPull *pull, int error, void *userdata) { |
| 185 |
sd_event *event = userdata; |
| 186 |
assert(pull); |
| 187 |
|
| 188 |
if (error == 0) |
| 189 |
log_info("Operation completed successfully."); |
| 190 |
|
| 191 |
sd_event_exit(event, ABS(error)); |
| 192 |
} |
| 193 |
|
| 194 |
VERB(verb_raw, "raw", "URL [NAME]", 2, 3, 0, "Download a RAW image"); |
| 195 |
static int verb_raw(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 196 |
_cleanup_free_ char *ll = NULL, *normalized = NULL; |
| 197 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 198 |
_cleanup_(raw_pull_unrefp) RawPull *pull = NULL; |
| 199 |
const char *url, *local; |
| 200 |
int r; |
| 201 |
|
| 202 |
url = argv[1]; |
| 203 |
if (!http_url_is_valid(url) && !file_url_is_valid(url)) |
| 204 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url); |
| 205 |
|
| 206 |
if (argc >= 3) |
| 207 |
local = empty_or_dash_to_null(argv[2]); |
| 208 |
else { |
| 209 |
_cleanup_free_ char *l = NULL; |
| 210 |
|
| 211 |
r = import_url_last_component(url, &l); |
| 212 |
if (r < 0) |
| 213 |
return log_error_errno(r, "Failed to get final component of URL: %m"); |
| 214 |
|
| 215 |
r = raw_strip_suffixes(l, &ll); |
| 216 |
if (r < 0) |
| 217 |
return log_oom(); |
| 218 |
|
| 219 |
local = ll; |
| 220 |
} |
| 221 |
|
| 222 |
r = normalize_local(local, url, &normalized); |
| 223 |
if (r < 0) |
| 224 |
return r; |
| 225 |
|
| 226 |
r = import_allocate_event_with_signals(&event); |
| 227 |
if (r < 0) |
| 228 |
return r; |
| 229 |
|
| 230 |
r = raw_pull_new(&pull, event, arg_image_root, on_raw_finished, event); |
| 231 |
if (r < 0) |
| 232 |
return log_error_errno(r, "Failed to allocate puller: %m"); |
| 233 |
|
| 234 |
r = raw_pull_start( |
| 235 |
pull, |
| 236 |
url, |
| 237 |
normalized, |
| 238 |
arg_offset, |
| 239 |
arg_size_max, |
| 240 |
arg_import_flags & IMPORT_PULL_FLAGS_MASK_RAW, |
| 241 |
arg_verify, |
| 242 |
&arg_checksum); |
| 243 |
if (r < 0) |
| 244 |
return log_error_errno(r, "Failed to pull image: %m"); |
| 245 |
|
| 246 |
r = sd_event_loop(event); |
| 247 |
if (r < 0) |
| 248 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 249 |
|
| 250 |
log_info("Exiting."); |
| 251 |
return -r; |
| 252 |
} |
| 253 |
|
| 254 |
static void on_oci_finished(OciPull *pull, int error, void *userdata) { |
| 255 |
sd_event *event = userdata; |
| 256 |
assert(pull); |
| 257 |
|
| 258 |
if (error == 0) |
| 259 |
log_info("Operation completed successfully."); |
| 260 |
|
| 261 |
sd_event_exit(event, ABS(error)); |
| 262 |
} |
| 263 |
|
| 264 |
VERB(verb_oci, "oci", "REF [NAME]", 2, 3, 0, "Download an OCI image"); |
| 265 |
static int verb_oci(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 266 |
int r; |
| 267 |
|
| 268 |
const char *ref = argv[1]; |
| 269 |
|
| 270 |
_cleanup_free_ char *image = NULL; |
| 271 |
r = oci_ref_parse(ref, /* ret_registry= */ NULL, &image, /* ret_tag= */ NULL); |
| 272 |
if (r == -EINVAL) |
| 273 |
return log_error_errno(r, "OCI ref '%s' is invalid.", ref); |
| 274 |
if (r < 0) |
| 275 |
return log_error_errno(r, "Failed to check of OCI ref '%s' is valid: %m", ref); |
| 276 |
|
| 277 |
_cleanup_free_ char *l = NULL; |
| 278 |
const char *local; |
| 279 |
if (argc >= 3) |
| 280 |
local = empty_or_dash_to_null(argv[2]); |
| 281 |
else { |
| 282 |
r = path_extract_filename(image, &l); |
| 283 |
if (r < 0) |
| 284 |
return log_error_errno(r, "Failed to get extract final component of '%s': %m", image); |
| 285 |
|
| 286 |
local = l; |
| 287 |
} |
| 288 |
|
| 289 |
_cleanup_free_ char *normalized = NULL; |
| 290 |
r = normalize_local(local, ref, &normalized); |
| 291 |
if (r < 0) |
| 292 |
return r; |
| 293 |
|
| 294 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 295 |
r = import_allocate_event_with_signals(&event); |
| 296 |
if (r < 0) |
| 297 |
return r; |
| 298 |
|
| 299 |
_cleanup_(oci_pull_unrefp) OciPull *pull = NULL; |
| 300 |
r = oci_pull_new(&pull, event, arg_image_root, on_oci_finished, event); |
| 301 |
if (r < 0) |
| 302 |
return log_error_errno(r, "Failed to allocate puller: %m"); |
| 303 |
|
| 304 |
r = oci_pull_start( |
| 305 |
pull, |
| 306 |
ref, |
| 307 |
normalized, |
| 308 |
arg_import_flags & IMPORT_PULL_FLAGS_MASK_OCI); |
| 309 |
if (r < 0) |
| 310 |
return log_error_errno(r, "Failed to pull image: %m"); |
| 311 |
|
| 312 |
r = sd_event_loop(event); |
| 313 |
if (r < 0) |
| 314 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 315 |
|
| 316 |
log_info("Exiting."); |
| 317 |
return -r; |
| 318 |
} |
| 319 |
|
| 320 |
static int help(void) { |
| 321 |
_cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL; |
| 322 |
int r; |
| 323 |
|
| 324 |
r = verbs_get_help_table(&verbs); |
| 325 |
if (r < 0) |
| 326 |
return r; |
| 327 |
|
| 328 |
r = option_parser_get_help_table(&options); |
| 329 |
if (r < 0) |
| 330 |
return r; |
| 331 |
|
| 332 |
(void) table_sync_column_widths(0, verbs, options); |
| 333 |
|
| 334 |
printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
| 335 |
"%sDownload disk images.%s\n" |
| 336 |
"\n%sCommands:%s\n", |
| 337 |
program_invocation_short_name, |
| 338 |
ansi_highlight(), |
| 339 |
ansi_normal(), |
| 340 |
ansi_underline(), |
| 341 |
ansi_normal()); |
| 342 |
|
| 343 |
r = table_print_or_warn(verbs); |
| 344 |
if (r < 0) |
| 345 |
return r; |
| 346 |
|
| 347 |
printf("\n%sOptions:%s\n", |
| 348 |
ansi_underline(), |
| 349 |
ansi_normal()); |
| 350 |
|
| 351 |
r = table_print_or_warn(options); |
| 352 |
if (r < 0) |
| 353 |
return r; |
| 354 |
|
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
VERB_COMMON_HELP_HIDDEN(help); |
| 359 |
|
| 360 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 361 |
int r; |
| 362 |
bool auto_settings = true, auto_keep_download = true; |
| 363 |
|
| 364 |
assert(argc >= 0); |
| 365 |
assert(argv); |
| 366 |
|
| 367 |
OptionParser opts = { argc, argv }; |
| 368 |
|
| 369 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 370 |
switch (c) { |
| 371 |
|
| 372 |
OPTION_COMMON_HELP: |
| 373 |
return help(); |
| 374 |
|
| 375 |
OPTION_COMMON_VERSION: |
| 376 |
return version(); |
| 377 |
|
| 378 |
OPTION_LONG("force", NULL, "Force creation of image"): |
| 379 |
arg_import_flags |= IMPORT_FORCE; |
| 380 |
break; |
| 381 |
|
| 382 |
OPTION_LONG("image-root", "PATH", "Image root directory"): |
| 383 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image_root); |
| 384 |
if (r < 0) |
| 385 |
return r; |
| 386 |
break; |
| 387 |
|
| 388 |
OPTION_LONG("verify", "MODE", |
| 389 |
"Verify downloaded image, one of: 'no', 'checksum', 'signature' or literal SHA256 hash"): { |
| 390 |
ImportVerify v; |
| 391 |
|
| 392 |
v = import_verify_from_string(opts.arg); |
| 393 |
if (v < 0) { |
| 394 |
_cleanup_free_ void *h = NULL; |
| 395 |
size_t n; |
| 396 |
|
| 397 |
/* If this is not a valid verification mode, maybe it's a literally specified |
| 398 |
* SHA256 hash? We can handle that too... */ |
| 399 |
|
| 400 |
r = unhexmem(opts.arg, &h, &n); |
| 401 |
if (r < 0 || n == 0) |
| 402 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 403 |
"Invalid verification setting: %s", opts.arg); |
| 404 |
if (n != 32) |
| 405 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 406 |
"64 hex character SHA256 hash required when specifying explicit checksum, %zu specified", n * 2); |
| 407 |
|
| 408 |
iovec_done(&arg_checksum); |
| 409 |
arg_checksum = IOVEC_MAKE(TAKE_PTR(h), n); |
| 410 |
|
| 411 |
arg_import_flags &= ~(IMPORT_PULL_SETTINGS|IMPORT_PULL_ROOTHASH|IMPORT_PULL_ROOTHASH_SIGNATURE|IMPORT_PULL_VERITY); |
| 412 |
arg_verify = _IMPORT_VERIFY_INVALID; |
| 413 |
} else |
| 414 |
arg_verify = v; |
| 415 |
|
| 416 |
break; |
| 417 |
} |
| 418 |
|
| 419 |
OPTION_LONG("settings", "BOOL", "Download settings file with image"): |
| 420 |
r = parse_boolean_argument("--settings=", opts.arg, NULL); |
| 421 |
if (r < 0) |
| 422 |
return r; |
| 423 |
|
| 424 |
SET_FLAG(arg_import_flags, IMPORT_PULL_SETTINGS, r); |
| 425 |
auto_settings = false; |
| 426 |
break; |
| 427 |
|
| 428 |
OPTION_LONG("roothash", "BOOL", "Download root hash file with image"): |
| 429 |
r = parse_boolean_argument("--roothash=", opts.arg, NULL); |
| 430 |
if (r < 0) |
| 431 |
return r; |
| 432 |
|
| 433 |
SET_FLAG(arg_import_flags, IMPORT_PULL_ROOTHASH, r); |
| 434 |
|
| 435 |
/* If we were asked to turn off the root hash, implicitly also turn off the root hash signature */ |
| 436 |
if (!r) |
| 437 |
SET_FLAG(arg_import_flags, IMPORT_PULL_ROOTHASH_SIGNATURE, false); |
| 438 |
break; |
| 439 |
|
| 440 |
OPTION_LONG("roothash-signature", "BOOL", |
| 441 |
"Download root hash signature file with image"): |
| 442 |
r = parse_boolean_argument("--roothash-signature=", opts.arg, NULL); |
| 443 |
if (r < 0) |
| 444 |
return r; |
| 445 |
|
| 446 |
SET_FLAG(arg_import_flags, IMPORT_PULL_ROOTHASH_SIGNATURE, r); |
| 447 |
break; |
| 448 |
|
| 449 |
OPTION_LONG("verity", "BOOL", "Download verity file with image"): |
| 450 |
r = parse_boolean_argument("--verity=", opts.arg, NULL); |
| 451 |
if (r < 0) |
| 452 |
return r; |
| 453 |
|
| 454 |
SET_FLAG(arg_import_flags, IMPORT_PULL_VERITY, r); |
| 455 |
break; |
| 456 |
|
| 457 |
OPTION_LONG("read-only", NULL, "Create a read-only image"): |
| 458 |
arg_import_flags |= IMPORT_READ_ONLY; |
| 459 |
break; |
| 460 |
|
| 461 |
OPTION_LONG("direct", NULL, "Download directly to specified file"): |
| 462 |
arg_import_flags |= IMPORT_DIRECT; |
| 463 |
arg_import_flags &= ~(IMPORT_PULL_SETTINGS|IMPORT_PULL_ROOTHASH|IMPORT_PULL_ROOTHASH_SIGNATURE|IMPORT_PULL_VERITY); |
| 464 |
break; |
| 465 |
|
| 466 |
OPTION_LONG("btrfs-subvol", "BOOL", |
| 467 |
"Controls whether to create a btrfs subvolume instead of a directory"): |
| 468 |
r = parse_boolean_argument("--btrfs-subvol=", opts.arg, NULL); |
| 469 |
if (r < 0) |
| 470 |
return r; |
| 471 |
SET_FLAG(arg_import_flags, IMPORT_BTRFS_SUBVOL, r); |
| 472 |
break; |
| 473 |
|
| 474 |
OPTION_LONG("btrfs-quota", "BOOL", |
| 475 |
"Controls whether to set up quota for btrfs subvolume"): |
| 476 |
r = parse_boolean_argument("--btrfs-quota=", opts.arg, NULL); |
| 477 |
if (r < 0) |
| 478 |
return r; |
| 479 |
SET_FLAG(arg_import_flags, IMPORT_BTRFS_QUOTA, r); |
| 480 |
break; |
| 481 |
|
| 482 |
OPTION_LONG("convert-qcow2", "BOOL", |
| 483 |
"Controls whether to convert QCOW2 images to regular disk images"): |
| 484 |
r = parse_boolean_argument("--convert-qcow2=", opts.arg, NULL); |
| 485 |
if (r < 0) |
| 486 |
return r; |
| 487 |
SET_FLAG(arg_import_flags, IMPORT_CONVERT_QCOW2, r); |
| 488 |
break; |
| 489 |
|
| 490 |
OPTION_LONG("sync", "BOOL", "Controls whether to sync() before completing"): |
| 491 |
r = parse_boolean_argument("--sync=", opts.arg, NULL); |
| 492 |
if (r < 0) |
| 493 |
return r; |
| 494 |
SET_FLAG(arg_import_flags, IMPORT_SYNC, r); |
| 495 |
break; |
| 496 |
|
| 497 |
OPTION_LONG("offset", "BYTES", "Offset to seek to in destination"): { |
| 498 |
uint64_t u; |
| 499 |
|
| 500 |
r = safe_atou64(opts.arg, &u); |
| 501 |
if (r < 0) |
| 502 |
return log_error_errno(r, "Failed to parse --offset= argument: %s", opts.arg); |
| 503 |
if (!FILE_SIZE_VALID(u)) |
| 504 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Argument to --offset= switch too large: %s", opts.arg); |
| 505 |
|
| 506 |
arg_offset = u; |
| 507 |
break; |
| 508 |
} |
| 509 |
|
| 510 |
OPTION_LONG("size-max", "BYTES", "Maximum number of bytes to write to destination"): { |
| 511 |
uint64_t u; |
| 512 |
|
| 513 |
r = parse_size(opts.arg, 1024, &u); |
| 514 |
if (r < 0) |
| 515 |
return log_error_errno(r, "Failed to parse --size-max= argument: %s", opts.arg); |
| 516 |
if (!FILE_SIZE_VALID(u)) |
| 517 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Argument to --size-max= switch too large: %s", opts.arg); |
| 518 |
|
| 519 |
arg_size_max = u; |
| 520 |
break; |
| 521 |
} |
| 522 |
|
| 523 |
OPTION_LONG("class", "CLASS", |
| 524 |
"Select image class (machine, sysext, confext, portable)"): |
| 525 |
arg_class = image_class_from_string(opts.arg); |
| 526 |
if (arg_class < 0) |
| 527 |
return log_error_errno(arg_class, "Failed to parse --class= argument: %s", opts.arg); |
| 528 |
break; |
| 529 |
|
| 530 |
OPTION_LONG("keep-download", "BOOL", |
| 531 |
"Keep a pristine copy of the downloaded file around"): |
| 532 |
r = parse_boolean(opts.arg); |
| 533 |
if (r < 0) |
| 534 |
return log_error_errno(r, "Failed to parse --keep-download= argument: %s", opts.arg); |
| 535 |
|
| 536 |
SET_FLAG(arg_import_flags, IMPORT_PULL_KEEP_DOWNLOAD, r); |
| 537 |
auto_keep_download = false; |
| 538 |
break; |
| 539 |
|
| 540 |
OPTION_COMMON_SYSTEM: |
| 541 |
arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
| 542 |
break; |
| 543 |
|
| 544 |
OPTION_COMMON_USER: |
| 545 |
arg_runtime_scope = RUNTIME_SCOPE_USER; |
| 546 |
break; |
| 547 |
} |
| 548 |
|
| 549 |
/* Make sure offset+size is still in the valid range if both set */ |
| 550 |
if (arg_offset != UINT64_MAX && arg_size_max != UINT64_MAX && |
| 551 |
((arg_size_max > (UINT64_MAX - arg_offset)) || |
| 552 |
!FILE_SIZE_VALID(arg_offset + arg_size_max))) |
| 553 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File offset und maximum size out of range."); |
| 554 |
|
| 555 |
if (arg_offset != UINT64_MAX && !FLAGS_SET(arg_import_flags, IMPORT_DIRECT)) |
| 556 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File offset only supported in --direct mode."); |
| 557 |
|
| 558 |
if (iovec_is_set(&arg_checksum) && (arg_import_flags & (IMPORT_PULL_SETTINGS|IMPORT_PULL_ROOTHASH|IMPORT_PULL_ROOTHASH_SIGNATURE|IMPORT_PULL_VERITY)) != 0) |
| 559 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Literal checksum verification only supported if no associated files are downloaded."); |
| 560 |
|
| 561 |
if (!arg_image_root) { |
| 562 |
r = image_root_pick(arg_runtime_scope < 0 ? RUNTIME_SCOPE_SYSTEM : arg_runtime_scope, arg_class, /* runtime= */ false, &arg_image_root); |
| 563 |
if (r < 0) |
| 564 |
return log_error_errno(r, "Failed to pick image root: %m"); |
| 565 |
} |
| 566 |
|
| 567 |
/* .nspawn settings files only really make sense for machine images, not for sysext/confext/portable */ |
| 568 |
if (auto_settings && arg_class != IMAGE_MACHINE) |
| 569 |
arg_import_flags &= ~IMPORT_PULL_SETTINGS; |
| 570 |
|
| 571 |
/* Keep the original pristine downloaded file as a copy only when dealing with machine images, |
| 572 |
* because unlike sysext/confext/portable they are typically modified during runtime. */ |
| 573 |
if (auto_keep_download) |
| 574 |
SET_FLAG(arg_import_flags, IMPORT_PULL_KEEP_DOWNLOAD, arg_class == IMAGE_MACHINE); |
| 575 |
|
| 576 |
if (arg_runtime_scope == RUNTIME_SCOPE_USER) |
| 577 |
arg_import_flags |= IMPORT_FOREIGN_UID; |
| 578 |
|
| 579 |
*ret_args = option_parser_get_args(&opts); |
| 580 |
return 1; |
| 581 |
} |
| 582 |
|
| 583 |
static void parse_env(void) { |
| 584 |
int r; |
| 585 |
|
| 586 |
/* Let's make these relatively low-level settings also controllable via env vars. User can then set |
| 587 |
* them for systemd-importd.service if they like to tweak behaviour */ |
| 588 |
|
| 589 |
r = getenv_bool("SYSTEMD_IMPORT_BTRFS_SUBVOL"); |
| 590 |
if (r >= 0) |
| 591 |
SET_FLAG(arg_import_flags, IMPORT_BTRFS_SUBVOL, r); |
| 592 |
else if (r != -ENXIO) |
| 593 |
log_warning_errno(r, "Failed to parse $SYSTEMD_IMPORT_BTRFS_SUBVOL: %m"); |
| 594 |
|
| 595 |
r = getenv_bool("SYSTEMD_IMPORT_BTRFS_QUOTA"); |
| 596 |
if (r >= 0) |
| 597 |
SET_FLAG(arg_import_flags, IMPORT_BTRFS_QUOTA, r); |
| 598 |
else if (r != -ENXIO) |
| 599 |
log_warning_errno(r, "Failed to parse $SYSTEMD_IMPORT_BTRFS_QUOTA: %m"); |
| 600 |
|
| 601 |
r = getenv_bool("SYSTEMD_IMPORT_SYNC"); |
| 602 |
if (r >= 0) |
| 603 |
SET_FLAG(arg_import_flags, IMPORT_SYNC, r); |
| 604 |
else if (r != -ENXIO) |
| 605 |
log_warning_errno(r, "Failed to parse $SYSTEMD_IMPORT_SYNC: %m"); |
| 606 |
} |
| 607 |
|
| 608 |
static int run(int argc, char *argv[]) { |
| 609 |
int r; |
| 610 |
|
| 611 |
setlocale(LC_ALL, ""); |
| 612 |
log_setup(); |
| 613 |
|
| 614 |
parse_env(); |
| 615 |
|
| 616 |
char **args = NULL; |
| 617 |
r = parse_argv(argc, argv, &args); |
| 618 |
if (r <= 0) |
| 619 |
return r; |
| 620 |
|
| 621 |
(void) ignore_signals(SIGPIPE); |
| 622 |
|
| 623 |
return dispatch_verb(args, NULL); |
| 624 |
} |
| 625 |
|
| 626 |
DEFINE_MAIN_FUNCTION(run); |