| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <locale.h> |
| 4 |
#include <unistd.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 "fd-util.h" |
| 14 |
#include "format-table.h" |
| 15 |
#include "import-raw.h" |
| 16 |
#include "import-tar.h" |
| 17 |
#include "import-util.h" |
| 18 |
#include "io-util.h" |
| 19 |
#include "log.h" |
| 20 |
#include "main-func.h" |
| 21 |
#include "options.h" |
| 22 |
#include "parse-argument.h" |
| 23 |
#include "parse-util.h" |
| 24 |
#include "path-util.h" |
| 25 |
#include "runtime-scope.h" |
| 26 |
#include "signal-util.h" |
| 27 |
#include "string-util.h" |
| 28 |
#include "verbs.h" |
| 29 |
|
| 30 |
static char *arg_image_root = NULL; |
| 31 |
static ImportFlags arg_import_flags = IMPORT_BTRFS_SUBVOL | IMPORT_BTRFS_QUOTA | IMPORT_CONVERT_QCOW2 | IMPORT_SYNC; |
| 32 |
static uint64_t arg_offset = UINT64_MAX, arg_size_max = UINT64_MAX; |
| 33 |
static ImageClass arg_class = IMAGE_MACHINE; |
| 34 |
static RuntimeScope arg_runtime_scope = _RUNTIME_SCOPE_INVALID; |
| 35 |
|
| 36 |
STATIC_DESTRUCTOR_REGISTER(arg_image_root, freep); |
| 37 |
|
| 38 |
static int normalize_local(const char *local, char **ret) { |
| 39 |
_cleanup_free_ char *ll = NULL; |
| 40 |
int r; |
| 41 |
|
| 42 |
assert(ret); |
| 43 |
|
| 44 |
if (arg_import_flags & IMPORT_DIRECT) { |
| 45 |
|
| 46 |
if (!local) |
| 47 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No local path specified."); |
| 48 |
|
| 49 |
if (!path_is_absolute(local)) { |
| 50 |
ll = path_join(arg_image_root, local); |
| 51 |
if (!ll) |
| 52 |
return log_oom(); |
| 53 |
|
| 54 |
local = ll; |
| 55 |
} |
| 56 |
|
| 57 |
if (!path_is_valid(local)) |
| 58 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 59 |
"Local path name '%s' is not valid.", local); |
| 60 |
} else { |
| 61 |
if (local) { |
| 62 |
if (!image_name_is_valid(local)) |
| 63 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 64 |
"Local image name '%s' is not valid.", |
| 65 |
local); |
| 66 |
} else |
| 67 |
local = "imported"; |
| 68 |
|
| 69 |
if (!FLAGS_SET(arg_import_flags, IMPORT_FORCE)) { |
| 70 |
r = image_find(arg_runtime_scope, arg_class, local, NULL, NULL); |
| 71 |
if (r < 0) { |
| 72 |
if (r != -ENOENT) |
| 73 |
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); |
| 74 |
} else |
| 75 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 76 |
"Image '%s' already exists.", |
| 77 |
local); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if (!ll) { |
| 82 |
ll = strdup(local); |
| 83 |
if (!ll) |
| 84 |
return log_oom(); |
| 85 |
} |
| 86 |
|
| 87 |
*ret = TAKE_PTR(ll); |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
static int open_source(const char *path, const char *local, int *ret_open_fd) { |
| 92 |
_cleanup_close_ int open_fd = -EBADF; |
| 93 |
int retval; |
| 94 |
|
| 95 |
assert(local); |
| 96 |
assert(ret_open_fd); |
| 97 |
|
| 98 |
if (path) { |
| 99 |
open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
| 100 |
if (open_fd < 0) |
| 101 |
return log_error_errno(errno, "Failed to open source file '%s': %m", path); |
| 102 |
|
| 103 |
retval = open_fd; |
| 104 |
|
| 105 |
if (arg_offset != UINT64_MAX) |
| 106 |
log_info("Importing '%s', saving at offset %" PRIu64 " in '%s'.", path, arg_offset, local); |
| 107 |
else |
| 108 |
log_info("Importing '%s', saving as '%s'.", path, local); |
| 109 |
} else { |
| 110 |
_cleanup_free_ char *pretty = NULL; |
| 111 |
|
| 112 |
retval = STDIN_FILENO; |
| 113 |
|
| 114 |
(void) fd_get_path(STDIN_FILENO, &pretty); |
| 115 |
|
| 116 |
if (arg_offset != UINT64_MAX) |
| 117 |
log_info("Importing '%s', saving at offset %" PRIu64 " in '%s'.", strempty(pretty), arg_offset, local); |
| 118 |
else |
| 119 |
log_info("Importing '%s', saving as '%s'.", strempty(pretty), local); |
| 120 |
} |
| 121 |
|
| 122 |
if (!FLAGS_SET(arg_import_flags, IMPORT_DIRECT)) |
| 123 |
log_info("Operating on image directory '%s'.", arg_image_root); |
| 124 |
|
| 125 |
if (!FLAGS_SET(arg_import_flags, IMPORT_SYNC)) |
| 126 |
log_info("File system synchronization on completion is off."); |
| 127 |
|
| 128 |
*ret_open_fd = TAKE_FD(open_fd); |
| 129 |
return retval; |
| 130 |
} |
| 131 |
|
| 132 |
static void on_tar_finished(TarImport *import, int error, void *userdata) { |
| 133 |
sd_event *event = userdata; |
| 134 |
assert(import); |
| 135 |
|
| 136 |
if (error == 0) |
| 137 |
log_info("Operation completed successfully."); |
| 138 |
|
| 139 |
sd_event_exit(event, ABS(error)); |
| 140 |
} |
| 141 |
|
| 142 |
VERB(verb_tar, "tar", "FILE [NAME]", 2, 3, 0, "Import a TAR image"); |
| 143 |
static int verb_tar(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 144 |
_cleanup_(tar_import_unrefp) TarImport *import = NULL; |
| 145 |
_cleanup_free_ char *ll = NULL, *normalized = NULL; |
| 146 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 147 |
const char *path = NULL, *local = NULL; |
| 148 |
_cleanup_close_ int open_fd = -EBADF; |
| 149 |
int r, fd; |
| 150 |
|
| 151 |
if (argc >= 2) |
| 152 |
path = empty_or_dash_to_null(argv[1]); |
| 153 |
|
| 154 |
if (argc >= 3) |
| 155 |
local = empty_or_dash_to_null(argv[2]); |
| 156 |
else if (path) { |
| 157 |
_cleanup_free_ char *l = NULL; |
| 158 |
|
| 159 |
r = path_extract_filename(path, &l); |
| 160 |
if (r < 0) |
| 161 |
return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); |
| 162 |
|
| 163 |
r = tar_strip_suffixes(l, &ll); |
| 164 |
if (r < 0) |
| 165 |
return log_oom(); |
| 166 |
|
| 167 |
local = ll; |
| 168 |
} |
| 169 |
|
| 170 |
r = normalize_local(local, &normalized); |
| 171 |
if (r < 0) |
| 172 |
return r; |
| 173 |
|
| 174 |
fd = open_source(path, normalized, &open_fd); |
| 175 |
if (fd < 0) |
| 176 |
return fd; |
| 177 |
|
| 178 |
r = import_allocate_event_with_signals(&event); |
| 179 |
if (r < 0) |
| 180 |
return r; |
| 181 |
|
| 182 |
r = tar_import_new(&import, event, arg_image_root, on_tar_finished, event); |
| 183 |
if (r < 0) |
| 184 |
return log_error_errno(r, "Failed to allocate importer: %m"); |
| 185 |
|
| 186 |
r = tar_import_start( |
| 187 |
import, |
| 188 |
fd, |
| 189 |
normalized, |
| 190 |
arg_import_flags & IMPORT_FLAGS_MASK_TAR); |
| 191 |
if (r < 0) |
| 192 |
return log_error_errno(r, "Failed to import image: %m"); |
| 193 |
|
| 194 |
r = sd_event_loop(event); |
| 195 |
if (r < 0) |
| 196 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 197 |
|
| 198 |
log_info("Exiting."); |
| 199 |
return -r; |
| 200 |
} |
| 201 |
|
| 202 |
static void on_raw_finished(RawImport *import, int error, void *userdata) { |
| 203 |
sd_event *event = userdata; |
| 204 |
assert(import); |
| 205 |
|
| 206 |
if (error == 0) |
| 207 |
log_info("Operation completed successfully."); |
| 208 |
|
| 209 |
sd_event_exit(event, ABS(error)); |
| 210 |
} |
| 211 |
|
| 212 |
VERB(verb_raw, "raw", "FILE [NAME]", 2, 3, 0, "Import a RAW image"); |
| 213 |
static int verb_raw(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 214 |
_cleanup_(raw_import_unrefp) RawImport *import = NULL; |
| 215 |
_cleanup_free_ char *ll = NULL, *normalized = NULL; |
| 216 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 217 |
const char *path = NULL, *local = NULL; |
| 218 |
_cleanup_close_ int open_fd = -EBADF; |
| 219 |
int r, fd; |
| 220 |
|
| 221 |
if (argc >= 2) |
| 222 |
path = empty_or_dash_to_null(argv[1]); |
| 223 |
|
| 224 |
if (argc >= 3) |
| 225 |
local = empty_or_dash_to_null(argv[2]); |
| 226 |
else if (path) { |
| 227 |
_cleanup_free_ char *l = NULL; |
| 228 |
|
| 229 |
r = path_extract_filename(path, &l); |
| 230 |
if (r < 0) |
| 231 |
return log_error_errno(r, "Failed to extract filename from path '%s': %m", path); |
| 232 |
|
| 233 |
r = raw_strip_suffixes(l, &ll); |
| 234 |
if (r < 0) |
| 235 |
return log_oom(); |
| 236 |
|
| 237 |
local = ll; |
| 238 |
} |
| 239 |
|
| 240 |
r = normalize_local(local, &normalized); |
| 241 |
if (r < 0) |
| 242 |
return r; |
| 243 |
|
| 244 |
fd = open_source(path, normalized, &open_fd); |
| 245 |
if (fd < 0) |
| 246 |
return fd; |
| 247 |
|
| 248 |
r = import_allocate_event_with_signals(&event); |
| 249 |
if (r < 0) |
| 250 |
return r; |
| 251 |
|
| 252 |
r = raw_import_new(&import, event, arg_image_root, on_raw_finished, event); |
| 253 |
if (r < 0) |
| 254 |
return log_error_errno(r, "Failed to allocate importer: %m"); |
| 255 |
|
| 256 |
r = raw_import_start( |
| 257 |
import, |
| 258 |
fd, |
| 259 |
normalized, |
| 260 |
arg_offset, |
| 261 |
arg_size_max, |
| 262 |
arg_import_flags & IMPORT_FLAGS_MASK_RAW); |
| 263 |
if (r < 0) |
| 264 |
return log_error_errno(r, "Failed to import image: %m"); |
| 265 |
|
| 266 |
r = sd_event_loop(event); |
| 267 |
if (r < 0) |
| 268 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 269 |
|
| 270 |
log_info("Exiting."); |
| 271 |
return -r; |
| 272 |
} |
| 273 |
|
| 274 |
static int help(void) { |
| 275 |
_cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL; |
| 276 |
int r; |
| 277 |
|
| 278 |
r = verbs_get_help_table(&verbs); |
| 279 |
if (r < 0) |
| 280 |
return r; |
| 281 |
|
| 282 |
r = option_parser_get_help_table(&options); |
| 283 |
if (r < 0) |
| 284 |
return r; |
| 285 |
|
| 286 |
(void) table_sync_column_widths(0, verbs, options); |
| 287 |
|
| 288 |
printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
| 289 |
"%sImport disk images.%s\n" |
| 290 |
"\n%sCommands:%s\n", |
| 291 |
program_invocation_short_name, |
| 292 |
ansi_highlight(), |
| 293 |
ansi_normal(), |
| 294 |
ansi_underline(), |
| 295 |
ansi_normal()); |
| 296 |
|
| 297 |
r = table_print_or_warn(verbs); |
| 298 |
if (r < 0) |
| 299 |
return r; |
| 300 |
|
| 301 |
printf("\n%sOptions:%s\n", |
| 302 |
ansi_underline(), |
| 303 |
ansi_normal()); |
| 304 |
|
| 305 |
r = table_print_or_warn(options); |
| 306 |
if (r < 0) |
| 307 |
return r; |
| 308 |
|
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
VERB_COMMON_HELP_HIDDEN(help); |
| 313 |
|
| 314 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 315 |
int r; |
| 316 |
|
| 317 |
assert(argc >= 0); |
| 318 |
assert(argv); |
| 319 |
|
| 320 |
OptionParser opts = { argc, argv }; |
| 321 |
|
| 322 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 323 |
switch (c) { |
| 324 |
|
| 325 |
OPTION_COMMON_HELP: |
| 326 |
return help(); |
| 327 |
|
| 328 |
OPTION_COMMON_VERSION: |
| 329 |
return version(); |
| 330 |
|
| 331 |
OPTION_LONG("force", NULL, "Force creation of image"): |
| 332 |
arg_import_flags |= IMPORT_FORCE; |
| 333 |
break; |
| 334 |
|
| 335 |
OPTION_LONG("image-root", "PATH", "Image root directory"): |
| 336 |
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image_root); |
| 337 |
if (r < 0) |
| 338 |
return r; |
| 339 |
break; |
| 340 |
|
| 341 |
OPTION_LONG("read-only", NULL, "Create a read-only image"): |
| 342 |
arg_import_flags |= IMPORT_READ_ONLY; |
| 343 |
break; |
| 344 |
|
| 345 |
OPTION_LONG("direct", NULL, "Import directly to specified file"): |
| 346 |
arg_import_flags |= IMPORT_DIRECT; |
| 347 |
break; |
| 348 |
|
| 349 |
OPTION_LONG("btrfs-subvol", "BOOL", |
| 350 |
"Controls whether to create a btrfs subvolume instead of a directory"): |
| 351 |
r = parse_boolean_argument("--btrfs-subvol=", opts.arg, NULL); |
| 352 |
if (r < 0) |
| 353 |
return r; |
| 354 |
SET_FLAG(arg_import_flags, IMPORT_BTRFS_SUBVOL, r); |
| 355 |
break; |
| 356 |
|
| 357 |
OPTION_LONG("btrfs-quota", "BOOL", |
| 358 |
"Controls whether to set up quota for btrfs subvolume"): |
| 359 |
r = parse_boolean_argument("--btrfs-quota=", opts.arg, NULL); |
| 360 |
if (r < 0) |
| 361 |
return r; |
| 362 |
SET_FLAG(arg_import_flags, IMPORT_BTRFS_QUOTA, r); |
| 363 |
break; |
| 364 |
|
| 365 |
OPTION_LONG("convert-qcow2", "BOOL", |
| 366 |
"Controls whether to convert QCOW2 images to regular disk images"): |
| 367 |
r = parse_boolean_argument("--convert-qcow2=", opts.arg, NULL); |
| 368 |
if (r < 0) |
| 369 |
return r; |
| 370 |
SET_FLAG(arg_import_flags, IMPORT_CONVERT_QCOW2, r); |
| 371 |
break; |
| 372 |
|
| 373 |
OPTION_LONG("sync", "BOOL", "Controls whether to sync() before completing"): |
| 374 |
r = parse_boolean_argument("--sync=", opts.arg, NULL); |
| 375 |
if (r < 0) |
| 376 |
return r; |
| 377 |
SET_FLAG(arg_import_flags, IMPORT_SYNC, r); |
| 378 |
break; |
| 379 |
|
| 380 |
OPTION_LONG("offset", "BYTES", "Offset to seek to in destination"): { |
| 381 |
uint64_t u; |
| 382 |
|
| 383 |
r = safe_atou64(opts.arg, &u); |
| 384 |
if (r < 0) |
| 385 |
return log_error_errno(r, "Failed to parse --offset= argument: %s", opts.arg); |
| 386 |
if (!FILE_SIZE_VALID(u)) |
| 387 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Argument to --offset= switch too large: %s", opts.arg); |
| 388 |
|
| 389 |
arg_offset = u; |
| 390 |
break; |
| 391 |
} |
| 392 |
|
| 393 |
OPTION_LONG("size-max", "BYTES", "Maximum number of bytes to write to destination"): { |
| 394 |
uint64_t u; |
| 395 |
|
| 396 |
r = parse_size(opts.arg, 1024, &u); |
| 397 |
if (r < 0) |
| 398 |
return log_error_errno(r, "Failed to parse --size-max= argument: %s", opts.arg); |
| 399 |
if (!FILE_SIZE_VALID(u)) |
| 400 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Argument to --size-max= switch too large: %s", opts.arg); |
| 401 |
|
| 402 |
arg_size_max = u; |
| 403 |
break; |
| 404 |
} |
| 405 |
|
| 406 |
OPTION_LONG("class", "CLASS", |
| 407 |
"Select image class (machine, sysext, confext, portable)"): |
| 408 |
arg_class = image_class_from_string(opts.arg); |
| 409 |
if (arg_class < 0) |
| 410 |
return log_error_errno(arg_class, "Failed to parse --class= argument: %s", opts.arg); |
| 411 |
break; |
| 412 |
|
| 413 |
OPTION_COMMON_SYSTEM: |
| 414 |
arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
| 415 |
break; |
| 416 |
|
| 417 |
OPTION_COMMON_USER: |
| 418 |
arg_runtime_scope = RUNTIME_SCOPE_USER; |
| 419 |
break; |
| 420 |
} |
| 421 |
|
| 422 |
/* Make sure offset+size is still in the valid range if both set */ |
| 423 |
if (arg_offset != UINT64_MAX && arg_size_max != UINT64_MAX && |
| 424 |
((arg_size_max > (UINT64_MAX - arg_offset)) || |
| 425 |
!FILE_SIZE_VALID(arg_offset + arg_size_max))) |
| 426 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File offset und maximum size out of range."); |
| 427 |
|
| 428 |
if (arg_offset != UINT64_MAX && !FLAGS_SET(arg_import_flags, IMPORT_DIRECT)) |
| 429 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File offset only supported in --direct mode."); |
| 430 |
|
| 431 |
if (!arg_image_root) { |
| 432 |
r = image_root_pick(arg_runtime_scope < 0 ? RUNTIME_SCOPE_SYSTEM : arg_runtime_scope, arg_class, /* runtime= */ false, &arg_image_root); |
| 433 |
if (r < 0) |
| 434 |
return log_error_errno(r, "Failed to pick image root: %m"); |
| 435 |
} |
| 436 |
|
| 437 |
if (arg_runtime_scope == RUNTIME_SCOPE_USER) |
| 438 |
arg_import_flags |= IMPORT_FOREIGN_UID; |
| 439 |
|
| 440 |
*ret_args = option_parser_get_args(&opts); |
| 441 |
return 1; |
| 442 |
} |
| 443 |
|
| 444 |
static void parse_env(void) { |
| 445 |
int r; |
| 446 |
|
| 447 |
/* Let's make these relatively low-level settings also controllable via env vars. User can then set |
| 448 |
* them to systemd-import if they like to tweak behaviour */ |
| 449 |
|
| 450 |
r = getenv_bool("SYSTEMD_IMPORT_BTRFS_SUBVOL"); |
| 451 |
if (r >= 0) |
| 452 |
SET_FLAG(arg_import_flags, IMPORT_BTRFS_SUBVOL, r); |
| 453 |
else if (r != -ENXIO) |
| 454 |
log_warning_errno(r, "Failed to parse $SYSTEMD_IMPORT_BTRFS_SUBVOL: %m"); |
| 455 |
|
| 456 |
r = getenv_bool("SYSTEMD_IMPORT_BTRFS_QUOTA"); |
| 457 |
if (r >= 0) |
| 458 |
SET_FLAG(arg_import_flags, IMPORT_BTRFS_QUOTA, r); |
| 459 |
else if (r != -ENXIO) |
| 460 |
log_warning_errno(r, "Failed to parse $SYSTEMD_IMPORT_BTRFS_QUOTA: %m"); |
| 461 |
|
| 462 |
r = getenv_bool("SYSTEMD_IMPORT_SYNC"); |
| 463 |
if (r >= 0) |
| 464 |
SET_FLAG(arg_import_flags, IMPORT_SYNC, r); |
| 465 |
else if (r != -ENXIO) |
| 466 |
log_warning_errno(r, "Failed to parse $SYSTEMD_IMPORT_SYNC: %m"); |
| 467 |
} |
| 468 |
|
| 469 |
static int run(int argc, char *argv[]) { |
| 470 |
int r; |
| 471 |
|
| 472 |
setlocale(LC_ALL, ""); |
| 473 |
log_setup(); |
| 474 |
|
| 475 |
parse_env(); |
| 476 |
|
| 477 |
char **args = NULL; |
| 478 |
r = parse_argv(argc, argv, &args); |
| 479 |
if (r <= 0) |
| 480 |
return r; |
| 481 |
|
| 482 |
(void) ignore_signals(SIGPIPE); |
| 483 |
|
| 484 |
return dispatch_verb(args, NULL); |
| 485 |
} |
| 486 |
|
| 487 |
DEFINE_MAIN_FUNCTION(run); |