Debian Codegraph @

src/import/importctl.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <locale.h>
4 #include <unistd.h>
5
6 #include "sd-bus.h"
7 #include "sd-event.h"
8
9 #include "alloc-util.h"
10 #include "build.h"
11 #include "bus-error.h"
12 #include "bus-locator.h"
13 #include "bus-util.h"
14 #include "discover-image.h"
15 #include "fd-util.h"
16 #include "format-table.h"
17 #include "import-common.h"
18 #include "import-util.h"
19 #include "log.h"
20 #include "main-func.h"
21 #include "oci-util.h"
22 #include "options.h"
23 #include "os-util.h"
24 #include "pager.h"
25 #include "parse-argument.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "polkit-agent.h"
29 #include "pretty-print.h"
30 #include "runtime-scope.h"
31 #include "string-table.h"
32 #include "string-util.h"
33 #include "strv.h"
34 #include "verbs.h"
35 #include "web-util.h"
36
37 static PagerFlags arg_pager_flags = 0;
38 static bool arg_legend = true;
39 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
40 static const char *arg_host = NULL;
41 static ImportFlags arg_import_flags = 0;
42 static ImportFlags arg_import_flags_mask = 0; /* Indicates which flags have been explicitly set to on or to off */
43 static bool arg_quiet = false;
44 static bool arg_ask_password = true;
45 static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
46 static const char* arg_format = NULL;
47 static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
48 static ImageClass arg_image_class = _IMAGE_CLASS_INVALID;
49 static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
50
51 #define PROGRESS_PREFIX "Total:"
52
53 static int settle_image_class(void) {
54 int r;
55
56 if (arg_image_class < 0) {
57 _cleanup_free_ char *j = NULL;
58
59 for (ImageClass class = 0; class < _IMAGE_CLASS_MAX; class++) {
60 _cleanup_free_ char *root = NULL;
61
62 r = image_root_pick(arg_runtime_scope, class, /* runtime= */ false, &root);
63 if (r < 0)
64 return log_error_errno(r, "Failed to pick image root: %m");
65
66 if (strextendf_with_separator(&j, ", ", "%s (downloads to %s/)",
67 image_class_to_string(class),
68 root) < 0)
69 return log_oom();
70 }
71
72 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
73 "No image class specified, retry with --class= set to one of: %s.", j);
74 }
75
76 /* Keep the original pristine downloaded file as a copy only when dealing with machine images,
77 * because unlike sysext/confext/portable they are typically modified during runtime. */
78 if (!FLAGS_SET(arg_import_flags_mask, IMPORT_PULL_KEEP_DOWNLOAD))
79 SET_FLAG(arg_import_flags, IMPORT_PULL_KEEP_DOWNLOAD, arg_image_class == IMAGE_MACHINE);
80
81 return 0;
82 }
83
84 typedef struct Context {
85 const char *object_path;
86 double progress;
87 } Context;
88
89 static int match_log_message(sd_bus_message *m, void *userdata, sd_bus_error *error) {
90 Context *c = ASSERT_PTR(userdata);
91 const char *line;
92 unsigned priority;
93 int r;
94
95 assert(m);
96
97 if (!streq_ptr(c->object_path, sd_bus_message_get_path(m)))
98 return 0;
99
100 r = sd_bus_message_read(m, "us", &priority, &line);
101 if (r < 0) {
102 bus_log_parse_error(r);
103 return 0;
104 }
105
106 if (arg_quiet && LOG_PRI(priority) >= LOG_INFO)
107 return 0;
108
109 if (!arg_quiet)
110 clear_progress_bar(PROGRESS_PREFIX);
111
112 log_full(priority, "%s", line);
113
114 if (!arg_quiet)
115 draw_progress_bar(PROGRESS_PREFIX, c->progress * 100);
116
117 return 0;
118 }
119
120 static int match_progress_update(sd_bus_message *m, void *userdata, sd_bus_error *error) {
121 Context *c = ASSERT_PTR(userdata);
122 int r;
123
124 assert(m);
125
126 if (!streq_ptr(c->object_path, sd_bus_message_get_path(m)))
127 return 0;
128
129 r = sd_bus_message_read(m, "d", &c->progress);
130 if (r < 0) {
131 bus_log_parse_error(r);
132 return 0;
133 }
134
135 if (!arg_quiet)
136 draw_progress_bar(PROGRESS_PREFIX, c->progress * 100);
137
138 return 0;
139 }
140
141 static int match_transfer_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
142 Context *c = ASSERT_PTR(userdata);
143 const char *path, *result;
144 uint32_t id;
145 int r;
146
147 assert(m);
148
149 if (!arg_quiet)
150 clear_progress_bar(PROGRESS_PREFIX);
151
152 r = sd_bus_message_read(m, "uos", &id, &path, &result);
153 if (r < 0) {
154 bus_log_parse_error(r);
155 return 0;
156 }
157
158 if (!streq_ptr(c->object_path, path))
159 return 0;
160
161 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), !streq_ptr(result, "done"));
162 return 0;
163 }
164
165 static int transfer_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
166 assert(s);
167 assert(si);
168
169 if (!arg_quiet)
170 clear_progress_bar(PROGRESS_PREFIX);
171
172 if (!arg_quiet)
173 log_info("Continuing download in the background. Use \"%s cancel-transfer %" PRIu32 "\" to abort transfer.",
174 program_invocation_short_name,
175 PTR_TO_UINT32(userdata));
176
177 sd_event_exit(sd_event_source_get_event(s), EINTR);
178 return 0;
179 }
180
181 static int transfer_image_common(sd_bus *bus, sd_bus_message *m) {
182 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_job_removed = NULL, *slot_log_message = NULL, *slot_progress_update = NULL;
183 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
184 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
185 _cleanup_(sd_event_unrefp) sd_event* event = NULL;
186 Context c = {};
187 uint32_t id;
188 int r;
189
190 assert(bus);
191 assert(m);
192
193 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
194
195 r = sd_event_default(&event);
196 if (r < 0)
197 return log_error_errno(r, "Failed to get event loop: %m");
198
199 r = sd_bus_attach_event(bus, event, 0);
200 if (r < 0)
201 return log_error_errno(r, "Failed to attach bus to event loop: %m");
202
203 r = bus_match_signal_async(
204 bus,
205 &slot_job_removed,
206 bus_import_mgr,
207 "TransferRemoved",
208 match_transfer_removed,
209 /* install_callback= */ NULL,
210 &c);
211 if (r < 0)
212 return log_error_errno(r, "Failed to request match: %m");
213
214 r = sd_bus_match_signal_async(
215 bus,
216 &slot_log_message,
217 "org.freedesktop.import1",
218 /* path= */ NULL,
219 "org.freedesktop.import1.Transfer",
220 "LogMessage",
221 match_log_message,
222 /* install_callback= */ NULL,
223 &c);
224 if (r < 0)
225 return log_error_errno(r, "Failed to request match: %m");
226
227 r = sd_bus_match_signal_async(
228 bus,
229 &slot_progress_update,
230 "org.freedesktop.import1",
231 /* path= */ NULL,
232 "org.freedesktop.import1.Transfer",
233 "ProgressUpdate",
234 match_progress_update,
235 /* install_callback= */ NULL,
236 &c);
237 if (r < 0)
238 return log_error_errno(r, "Failed to request match: %m");
239
240 r = sd_bus_call(bus, m, 0, &error, &reply);
241 if (r < 0)
242 return log_error_errno(r, "Failed to transfer image: %s", bus_error_message(&error, r));
243
244 r = sd_bus_message_read(reply, "uo", &id, &c.object_path);
245 if (r < 0)
246 return bus_log_parse_error(r);
247
248 if (!arg_quiet) {
249 clear_progress_bar(PROGRESS_PREFIX);
250 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id);
251 draw_progress_bar(PROGRESS_PREFIX, c.progress);
252 }
253
254 (void) sd_event_add_signal(event, NULL, SIGINT|SD_EVENT_SIGNAL_PROCMASK, transfer_signal_handler, UINT32_TO_PTR(id));
255 (void) sd_event_add_signal(event, NULL, SIGTERM|SD_EVENT_SIGNAL_PROCMASK, transfer_signal_handler, UINT32_TO_PTR(id));
256
257 r = sd_event_loop(event);
258 if (r < 0)
259 return log_error_errno(r, "Failed to run event loop: %m");
260
261 return -r;
262 }
263
264 VERB(verb_pull_tar, "pull-tar", "URL [NAME]", 2, 3, 0, "Download a TAR container image");
265 static int verb_pull_tar(int argc, char *argv[], uintptr_t _data, void *userdata) {
266 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
267 _cleanup_free_ char *l = NULL, *ll = NULL;
268 const char *local, *remote;
269 sd_bus *bus = ASSERT_PTR(userdata);
270 int r;
271
272 r = settle_image_class();
273 if (r < 0)
274 return r;
275
276 remote = argv[1];
277 if (!http_url_is_valid(remote) && !file_url_is_valid(remote))
278 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
279 "URL '%s' is not valid.", remote);
280
281 if (argc >= 3)
282 local = argv[2];
283 else {
284 r = import_url_last_component(remote, &l);
285 if (r < 0)
286 return log_error_errno(r, "Failed to get final component of URL: %m");
287
288 local = l;
289 }
290
291 local = empty_or_dash_to_null(local);
292
293 if (local) {
294 r = tar_strip_suffixes(local, &ll);
295 if (r < 0)
296 return log_oom();
297
298 local = ll;
299
300 if (!image_name_is_valid(local))
301 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
302 "Local name %s is not a suitable image name.",
303 local);
304 }
305
306 if (arg_image_class == IMAGE_MACHINE && (arg_import_flags & ~IMPORT_FORCE) == 0) {
307 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "PullTar");
308 if (r < 0)
309 return bus_log_create_error(r);
310
311 r = sd_bus_message_append(
312 m,
313 "sssb",
314 remote,
315 local,
316 import_verify_to_string(arg_verify),
317 FLAGS_SET(arg_import_flags, IMPORT_FORCE));
318 } else {
319 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "PullTarEx");
320 if (r < 0)
321 return bus_log_create_error(r);
322
323 r = sd_bus_message_append(
324 m,
325 "sssst",
326 remote,
327 local,
328 image_class_to_string(arg_image_class),
329 import_verify_to_string(arg_verify),
330 (uint64_t) arg_import_flags & (IMPORT_FORCE|IMPORT_READ_ONLY|IMPORT_PULL_KEEP_DOWNLOAD));
331 }
332 if (r < 0)
333 return bus_log_create_error(r);
334
335 return transfer_image_common(bus, m);
336 }
337
338 VERB(verb_pull_raw, "pull-raw", "URL [NAME]", 2, 3, 0, "Download a RAW container or VM image");
339 static int verb_pull_raw(int argc, char *argv[], uintptr_t _data, void *userdata) {
340 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
341 _cleanup_free_ char *l = NULL, *ll = NULL;
342 const char *local, *remote;
343 sd_bus *bus = ASSERT_PTR(userdata);
344 int r;
345
346 r = settle_image_class();
347 if (r < 0)
348 return r;
349
350 remote = argv[1];
351 if (!http_url_is_valid(remote) && !file_url_is_valid(remote))
352 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
353 "URL '%s' is not valid.", remote);
354
355 if (argc >= 3)
356 local = argv[2];
357 else {
358 r = import_url_last_component(remote, &l);
359 if (r < 0)
360 return log_error_errno(r, "Failed to get final component of URL: %m");
361
362 local = l;
363 }
364
365 local = empty_or_dash_to_null(local);
366
367 if (local) {
368 r = raw_strip_suffixes(local, &ll);
369 if (r < 0)
370 return log_oom();
371
372 local = ll;
373
374 if (!image_name_is_valid(local))
375 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
376 "Local name %s is not a suitable image name.",
377 local);
378 }
379
380 if (arg_image_class == IMAGE_MACHINE && (arg_import_flags & ~IMPORT_FORCE) == 0) {
381 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "PullRaw");
382 if (r < 0)
383 return bus_log_create_error(r);
384
385 r = sd_bus_message_append(
386 m,
387 "sssb",
388 remote,
389 local,
390 import_verify_to_string(arg_verify),
391 FLAGS_SET(arg_import_flags, IMPORT_FORCE));
392 } else {
393 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "PullRawEx");
394 if (r < 0)
395 return bus_log_create_error(r);
396
397 r = sd_bus_message_append(
398 m,
399 "sssst",
400 remote,
401 local,
402 image_class_to_string(arg_image_class),
403 import_verify_to_string(arg_verify),
404 (uint64_t) arg_import_flags & (IMPORT_FORCE|IMPORT_READ_ONLY|IMPORT_PULL_KEEP_DOWNLOAD));
405 }
406 if (r < 0)
407 return bus_log_create_error(r);
408
409 return transfer_image_common(bus, m);
410 }
411
412 VERB(verb_pull_oci, "pull-oci", "REF [NAME]", 2, 3, 0, "Download an OCI container image");
413 static int verb_pull_oci(int argc, char *argv[], uintptr_t _data, void *userdata) {
414 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
415 _cleanup_free_ char *l = NULL;
416 const char *local, *remote;
417 sd_bus *bus = ASSERT_PTR(userdata);
418 int r;
419
420 r = settle_image_class();
421 if (r < 0)
422 return r;
423
424 remote = argv[1];
425 _cleanup_free_ char *image = NULL;
426 r = oci_ref_parse(remote, /* ret_registry= */ NULL, &image, /* ret_tag= */ NULL);
427 if (r == -EINVAL)
428 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Ref '%s' is not valid.", remote);
429 if (r < 0)
430 return log_error_errno(r, "Failed to determine if ref '%s' is valid.", remote);
431
432 if (argc >= 3)
433 local = argv[2];
434 else {
435 r = path_extract_filename(image, &l);
436 if (r < 0)
437 return log_error_errno(r, "Failed to get final component of reference: %m");
438
439 local = l;
440 }
441
442 local = empty_or_dash_to_null(local);
443
444 if (local) {
445 if (!image_name_is_valid(local))
446 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
447 "Local name %s is not a suitable image name.",
448 local);
449 }
450
451 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "PullOci");
452 if (r < 0)
453 return bus_log_create_error(r);
454
455 r = sd_bus_message_append(
456 m,
457 "ssst",
458 remote,
459 local,
460 image_class_to_string(arg_image_class),
461 (uint64_t) arg_import_flags & (IMPORT_FORCE|IMPORT_READ_ONLY));
462 if (r < 0)
463 return bus_log_create_error(r);
464
465 return transfer_image_common(bus, m);
466 }
467
468 VERB(verb_import_tar, "import-tar", "FILE [NAME]", 2, 3, 0, "Import a local TAR container image");
469 static int verb_import_tar(int argc, char *argv[], uintptr_t _data, void *userdata) {
470 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
471 _cleanup_free_ char *ll = NULL, *fn = NULL;
472 const char *local = NULL, *path = NULL;
473 _cleanup_close_ int fd = -EBADF;
474 sd_bus *bus = ASSERT_PTR(userdata);
475 int r;
476
477 r = settle_image_class();
478 if (r < 0)
479 return r;
480
481 if (argc >= 2)
482 path = empty_or_dash_to_null(argv[1]);
483
484 if (argc >= 3)
485 local = empty_or_dash_to_null(argv[2]);
486 else if (path) {
487 r = path_extract_filename(path, &fn);
488 if (r < 0)
489 return log_error_errno(r, "Cannot extract container name from filename: %m");
490 if (r == O_DIRECTORY)
491 return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
492 "Path '%s' refers to directory, but we need a regular file.", path);
493
494 local = fn;
495 }
496 if (!local)
497 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
498 "Need either path or local name.");
499
500 r = tar_strip_suffixes(local, &ll);
501 if (r < 0)
502 return log_oom();
503
504 local = ll;
505
506 if (!image_name_is_valid(local))
507 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
508 "Local name %s is not a suitable image name.",
509 local);
510
511 if (path) {
512 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
513 if (fd < 0)
514 return log_error_errno(errno, "Failed to open %s: %m", path);
515 }
516
517 if (arg_image_class == IMAGE_MACHINE && (arg_import_flags & ~(IMPORT_FORCE|IMPORT_READ_ONLY)) == 0) {
518 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ImportTar");
519 if (r < 0)
520 return bus_log_create_error(r);
521
522 r = sd_bus_message_append(
523 m,
524 "hsbb",
525 fd >= 0 ? fd : STDIN_FILENO,
526 local,
527 FLAGS_SET(arg_import_flags, IMPORT_FORCE),
528 FLAGS_SET(arg_import_flags, IMPORT_READ_ONLY));
529 } else {
530 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ImportTarEx");
531 if (r < 0)
532 return bus_log_create_error(r);
533
534 r = sd_bus_message_append(
535 m,
536 "hsst",
537 fd >= 0 ? fd : STDIN_FILENO,
538 local,
539 image_class_to_string(arg_image_class),
540 (uint64_t) arg_import_flags & (IMPORT_FORCE|IMPORT_READ_ONLY));
541 }
542 if (r < 0)
543 return bus_log_create_error(r);
544
545 return transfer_image_common(bus, m);
546 }
547
548 VERB(verb_import_raw, "import-raw", "FILE [NAME]", 2, 3, 0, "Import a local RAW container or VM image");
549 static int verb_import_raw(int argc, char *argv[], uintptr_t _data, void *userdata) {
550 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
551 _cleanup_free_ char *ll = NULL, *fn = NULL;
552 const char *local = NULL, *path = NULL;
553 _cleanup_close_ int fd = -EBADF;
554 sd_bus *bus = ASSERT_PTR(userdata);
555 int r;
556
557 r = settle_image_class();
558 if (r < 0)
559 return r;
560
561 if (argc >= 2)
562 path = empty_or_dash_to_null(argv[1]);
563
564 if (argc >= 3)
565 local = empty_or_dash_to_null(argv[2]);
566 else if (path) {
567 r = path_extract_filename(path, &fn);
568 if (r < 0)
569 return log_error_errno(r, "Cannot extract container name from filename: %m");
570 if (r == O_DIRECTORY)
571 return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
572 "Path '%s' refers to directory, but we need a regular file.", path);
573
574 local = fn;
575 }
576 if (!local)
577 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
578 "Need either path or local name.");
579
580 r = raw_strip_suffixes(local, &ll);
581 if (r < 0)
582 return log_oom();
583
584 local = ll;
585
586 if (!image_name_is_valid(local))
587 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
588 "Local name %s is not a suitable image name.",
589 local);
590
591 if (path) {
592 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
593 if (fd < 0)
594 return log_error_errno(errno, "Failed to open %s: %m", path);
595 }
596
597 if (arg_image_class == IMAGE_MACHINE && (arg_import_flags & ~(IMPORT_FORCE|IMPORT_READ_ONLY)) == 0) {
598 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ImportRaw");
599 if (r < 0)
600 return bus_log_create_error(r);
601
602 r = sd_bus_message_append(
603 m,
604 "hsbb",
605 fd >= 0 ? fd : STDIN_FILENO,
606 local,
607 FLAGS_SET(arg_import_flags, IMPORT_FORCE),
608 FLAGS_SET(arg_import_flags, IMPORT_READ_ONLY));
609 } else {
610 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ImportRawEx");
611 if (r < 0)
612 return bus_log_create_error(r);
613
614 r = sd_bus_message_append(
615 m,
616 "hsst",
617 fd >= 0 ? fd : STDIN_FILENO,
618 local,
619 image_class_to_string(arg_image_class),
620 (uint64_t) arg_import_flags & (IMPORT_FORCE|IMPORT_READ_ONLY));
621 }
622 if (r < 0)
623 return bus_log_create_error(r);
624
625 return transfer_image_common(bus, m);
626 }
627
628 VERB(verb_import_fs, "import-fs", "DIRECTORY [NAME]", 2, 3, 0, "Import a local directory container image");
629 static int verb_import_fs(int argc, char *argv[], uintptr_t _data, void *userdata) {
630 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
631 const char *local = NULL, *path = NULL;
632 _cleanup_free_ char *fn = NULL;
633 _cleanup_close_ int fd = -EBADF;
634 sd_bus *bus = ASSERT_PTR(userdata);
635 int r;
636
637 r = settle_image_class();
638 if (r < 0)
639 return r;
640
641 if (argc >= 2)
642 path = empty_or_dash_to_null(argv[1]);
643
644 if (argc >= 3)
645 local = empty_or_dash_to_null(argv[2]);
646 else if (path) {
647 r = path_extract_filename(path, &fn);
648 if (r < 0)
649 return log_error_errno(r, "Cannot extract container name from filename: %m");
650
651 local = fn;
652 }
653 if (!local)
654 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
655 "Need either path or local name.");
656
657 if (!image_name_is_valid(local))
658 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
659 "Local name %s is not a suitable image name.",
660 local);
661
662 if (path) {
663 fd = open(path, O_DIRECTORY|O_RDONLY|O_CLOEXEC);
664 if (fd < 0)
665 return log_error_errno(errno, "Failed to open directory '%s': %m", path);
666 }
667
668 if (arg_image_class == IMAGE_MACHINE && (arg_import_flags & ~(IMPORT_FORCE|IMPORT_READ_ONLY)) == 0) {
669 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ImportFileSystem");
670 if (r < 0)
671 return bus_log_create_error(r);
672
673 r = sd_bus_message_append(
674 m,
675 "hsbb",
676 fd >= 0 ? fd : STDIN_FILENO,
677 local,
678 FLAGS_SET(arg_import_flags, IMPORT_FORCE),
679 FLAGS_SET(arg_import_flags, IMPORT_READ_ONLY));
680 } else {
681 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ImportFileSystemEx");
682 if (r < 0)
683 return bus_log_create_error(r);
684
685 r = sd_bus_message_append(
686 m,
687 "hsst",
688 fd >= 0 ? fd : STDIN_FILENO,
689 local,
690 image_class_to_string(arg_image_class),
691 (uint64_t) arg_import_flags & (IMPORT_FORCE|IMPORT_READ_ONLY));
692 }
693 if (r < 0)
694 return bus_log_create_error(r);
695
696 return transfer_image_common(bus, m);
697 }
698
699 static void determine_compression_from_filename(const char *p) {
700 if (arg_format)
701 return;
702
703 if (!p)
704 return;
705
706 if (endswith(p, ".xz"))
707 arg_format = "xz";
708 else if (endswith(p, ".gz"))
709 arg_format = "gzip";
710 else if (endswith(p, ".bz2"))
711 arg_format = "bzip2";
712 else if (endswith(p, ".zst"))
713 arg_format = "zstd";
714 }
715
716 VERB(verb_export_tar, "export-tar", "NAME [FILE]", 2, 3, 0, "Export a TAR container image locally");
717 static int verb_export_tar(int argc, char *argv[], uintptr_t _data, void *userdata) {
718 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
719 _cleanup_close_ int fd = -EBADF;
720 const char *local = NULL, *path = NULL;
721 sd_bus *bus = ASSERT_PTR(userdata);
722 int r;
723
724 r = settle_image_class();
725 if (r < 0)
726 return r;
727
728 local = argv[1];
729 if (!image_name_is_valid(local))
730 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
731 "Image name %s is not valid.", local);
732
733 if (argc >= 3)
734 path = argv[2];
735 path = empty_or_dash_to_null(path);
736
737 if (path) {
738 determine_compression_from_filename(path);
739
740 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
741 if (fd < 0)
742 return log_error_errno(errno, "Failed to open %s: %m", path);
743 }
744
745 if (arg_image_class == IMAGE_MACHINE && arg_import_flags == 0) {
746 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ExportTar");
747 if (r < 0)
748 return bus_log_create_error(r);
749
750 r = sd_bus_message_append(
751 m,
752 "shs",
753 local,
754 fd >= 0 ? fd : STDOUT_FILENO,
755 arg_format);
756 } else {
757 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ExportTarEx");
758 if (r < 0)
759 return bus_log_create_error(r);
760
761 r = sd_bus_message_append(
762 m,
763 "sshst",
764 local,
765 image_class_to_string(arg_image_class),
766 fd >= 0 ? fd : STDOUT_FILENO,
767 arg_format,
768 /* flags= */ UINT64_C(0));
769 }
770 if (r < 0)
771 return bus_log_create_error(r);
772
773 return transfer_image_common(bus, m);
774 }
775
776 VERB(verb_export_raw, "export-raw", "NAME [FILE]", 2, 3, 0, "Export a RAW container or VM image locally");
777 static int verb_export_raw(int argc, char *argv[], uintptr_t _data, void *userdata) {
778 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
779 _cleanup_close_ int fd = -EBADF;
780 const char *local = NULL, *path = NULL;
781 sd_bus *bus = ASSERT_PTR(userdata);
782 int r;
783
784 r = settle_image_class();
785 if (r < 0)
786 return r;
787
788 local = argv[1];
789 if (!image_name_is_valid(local))
790 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
791 "Image name %s is not valid.", local);
792
793 if (argc >= 3)
794 path = argv[2];
795 path = empty_or_dash_to_null(path);
796
797 if (path) {
798 determine_compression_from_filename(path);
799
800 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
801 if (fd < 0)
802 return log_error_errno(errno, "Failed to open %s: %m", path);
803 }
804
805 if (arg_image_class == IMAGE_MACHINE && arg_import_flags == 0) {
806 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ExportRaw");
807 if (r < 0)
808 return bus_log_create_error(r);
809
810 r = sd_bus_message_append(
811 m,
812 "shs",
813 local,
814 fd >= 0 ? fd : STDOUT_FILENO,
815 arg_format);
816 } else {
817 r = bus_message_new_method_call(bus, &m, bus_import_mgr, "ExportRawEx");
818 if (r < 0)
819 return bus_log_create_error(r);
820
821 r = sd_bus_message_append(
822 m,
823 "sshst",
824 local,
825 image_class_to_string(arg_image_class),
826 fd >= 0 ? fd : STDOUT_FILENO,
827 arg_format,
828 /* flags= */ UINT64_C(0));
829 }
830 if (r < 0)
831 return bus_log_create_error(r);
832
833 return transfer_image_common(bus, m);
834 }
835
836 VERB_DEFAULT_NOARG(verb_list_transfers, "list-transfers", "Show list of transfers in progress");
837 static int verb_list_transfers(int argc, char *argv[], uintptr_t _data, void *userdata) {
838 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
839 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
840 _cleanup_(table_unrefp) Table *t = NULL;
841 sd_bus *bus = ASSERT_PTR(userdata);
842 int r;
843
844 pager_open(arg_pager_flags);
845
846 bool ex;
847 r = bus_call_method(bus, bus_import_mgr, "ListTransfersEx", &error, &reply, "st", image_class_to_string(arg_image_class), UINT64_C(0));
848 if (r < 0) {
849 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) {
850 sd_bus_error_free(&error);
851
852 r = bus_call_method(bus, bus_import_mgr, "ListTransfers", &error, &reply, NULL);
853 }
854 if (r < 0)
855 return log_error_errno(r, "Could not get transfers: %s", bus_error_message(&error, r));
856
857 ex = false;
858 r = sd_bus_message_enter_container(reply, 'a', "(usssdo)");
859 } else {
860 ex = true;
861 r = sd_bus_message_enter_container(reply, 'a', "(ussssdo)");
862 }
863 if (r < 0)
864 return bus_log_parse_error(r);
865
866 t = table_new("id", "progress", "type", "class", "local", "remote");
867 if (!t)
868 return log_oom();
869
870 (void) table_set_sort(t, (size_t) 4, (size_t) 0);
871 table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
872
873 for (;;) {
874 const char *type, *remote, *local, *class = "machine";
875 double progress;
876 uint32_t id;
877
878 if (ex)
879 r = sd_bus_message_read(reply, "(ussssdo)", &id, &type, &remote, &local, &class, &progress, NULL);
880 else
881 r = sd_bus_message_read(reply, "(usssdo)", &id, &type, &remote, &local, &progress, NULL);
882 if (r < 0)
883 return bus_log_parse_error(r);
884 if (r == 0)
885 break;
886
887 /* Ideally we use server-side filtering. But if the server can't do it, we need to do it client side */
888 if (arg_image_class >= 0 && image_class_from_string(class) != arg_image_class)
889 continue;
890
891 r = table_add_many(
892 t,
893 TABLE_UINT32, id,
894 TABLE_SET_ALIGN_PERCENT, 100);
895 if (r < 0)
896 return table_log_add_error(r);
897
898 if (progress < 0)
899 r = table_add_many(
900 t,
901 TABLE_EMPTY,
902 TABLE_SET_ALIGN_PERCENT, 100);
903 else
904 r = table_add_many(
905 t,
906 TABLE_PERCENT, (int) (progress * 100),
907 TABLE_SET_ALIGN_PERCENT, 100);
908 if (r < 0)
909 return table_log_add_error(r);
910 r = table_add_many(
911 t,
912 TABLE_STRING, type,
913 TABLE_STRING, class,
914 TABLE_STRING, local,
915 TABLE_STRING, remote,
916 TABLE_SET_URL, remote);
917 if (r < 0)
918 return table_log_add_error(r);
919 }
920
921 r = sd_bus_message_exit_container(reply);
922 if (r < 0)
923 return bus_log_parse_error(r);
924
925 if (!table_isempty(t)) {
926 r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
927 if (r < 0)
928 return r;
929 }
930
931 if (arg_legend) {
932 if (!table_isempty(t))
933 printf("\n%zu transfers listed.\n", table_get_rows(t) - 1);
934 else
935 printf("No transfers.\n");
936 }
937
938 return 0;
939 }
940
941 VERB(verb_cancel_transfer, "cancel-transfer", "[ID...]", 2, VERB_ANY, 0, "Cancel a transfer");
942 static int verb_cancel_transfer(int argc, char *argv[], uintptr_t _data, void *userdata) {
943 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
944 sd_bus *bus = ASSERT_PTR(userdata);
945 int r;
946
947 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
948
949 for (int i = 1; i < argc; i++) {
950 uint32_t id;
951
952 r = safe_atou32(argv[i], &id);
953 if (r < 0)
954 return log_error_errno(r, "Failed to parse transfer id: %s", argv[i]);
955
956 r = bus_call_method(bus, bus_import_mgr, "CancelTransfer", &error, NULL, "u", id);
957 if (r < 0)
958 return log_error_errno(r, "Could not cancel transfer: %s", bus_error_message(&error, r));
959 }
960
961 return 0;
962 }
963
964 VERB_NOARG(verb_list_images, "list-images", "Show list of installed images");
965 static int verb_list_images(int argc, char *argv[], uintptr_t _data, void *userdata) {
966 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
967 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
968 _cleanup_(table_unrefp) Table *t = NULL;
969 sd_bus *bus = ASSERT_PTR(userdata);
970 int r;
971
972 pager_open(arg_pager_flags);
973
974 r = bus_call_method(bus, bus_import_mgr, "ListImages", &error, &reply, "st", image_class_to_string(arg_image_class), UINT64_C(0));
975 if (r < 0)
976 return log_error_errno(r, "Could not list images: %s", bus_error_message(&error, r));
977
978 r = sd_bus_message_enter_container(reply, 'a', "(ssssbtttttt)");
979 if (r < 0)
980 return bus_log_parse_error(r);
981
982 t = table_new("class", "name", "type", "path", "ro", "crtime", "mtime", "usage", "usage-exclusive", "limit", "limit-exclusive");
983 if (!t)
984 return log_oom();
985
986 (void) table_set_sort(t, (size_t) 0, (size_t) 1);
987 table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
988
989 /* Hide the exclusive columns for now */
990 (void) table_hide_column_from_display(t, 8);
991 (void) table_hide_column_from_display(t, 10);
992
993 /* Starting in v257, these fields would be automatically formatted with underscores. However, this
994 * command was introduced in v256, so changing the field name would be a breaking change. */
995 (void) table_set_json_field_name(t, 8, "usage-exclusive");
996 (void) table_set_json_field_name(t, 10, "limit-exclusive");
997
998 for (;;) {
999 uint64_t crtime, mtime, usage, usage_exclusive, limit, limit_exclusive;
1000 const char *class, *name, *type, *path;
1001 int read_only;
1002
1003 r = sd_bus_message_read(reply, "(ssssbtttttt)", &class, &name, &type, &path, &read_only, &crtime, &mtime, &usage, &usage_exclusive, &limit, &limit_exclusive);
1004 if (r < 0)
1005 return bus_log_parse_error(r);
1006 if (r == 0)
1007 break;
1008
1009 r = table_add_many(
1010 t,
1011 TABLE_STRING, class,
1012 TABLE_STRING, name,
1013 TABLE_STRING, type,
1014 TABLE_PATH, path);
1015 if (r < 0)
1016 return table_log_add_error(r);
1017
1018 if (!sd_json_format_enabled(arg_json_format_flags))
1019 r = table_add_many(
1020 t,
1021 TABLE_STRING, read_only ? "ro" : "rw",
1022 TABLE_SET_COLOR, read_only ? ANSI_HIGHLIGHT_RED : ANSI_HIGHLIGHT_GREEN);
1023 else
1024 r = table_add_many(
1025 t,
1026 TABLE_BOOLEAN, read_only);
1027 if (r < 0)
1028 return table_log_add_error(r);
1029
1030 r = table_add_many(
1031 t,
1032 TABLE_TIMESTAMP, crtime,
1033 TABLE_TIMESTAMP, mtime,
1034 TABLE_SIZE, usage,
1035 TABLE_SIZE, usage_exclusive,
1036 TABLE_SIZE, limit,
1037 TABLE_SIZE, limit_exclusive);
1038 if (r < 0)
1039 return table_log_add_error(r);
1040 }
1041
1042 r = sd_bus_message_exit_container(reply);
1043 if (r < 0)
1044 return bus_log_parse_error(r);
1045
1046 if (!table_isempty(t)) {
1047 r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
1048 if (r < 0)
1049 return r;
1050 }
1051
1052 if (arg_legend) {
1053 if (!table_isempty(t))
1054 printf("\n%zu images listed.\n", table_get_rows(t) - 1);
1055 else
1056 printf("No images.\n");
1057 }
1058
1059 return 0;
1060 }
1061
1062 static int help(void) {
1063 _cleanup_free_ char *link = NULL;
1064 _cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL;
1065 int r;
1066
1067 pager_open(arg_pager_flags);
1068
1069 r = terminal_urlify_man("importctl", "1", &link);
1070 if (r < 0)
1071 return log_oom();
1072
1073 r = verbs_get_help_table(&verbs);
1074 if (r < 0)
1075 return r;
1076
1077 r = option_parser_get_help_table(&options);
1078 if (r < 0)
1079 return r;
1080
1081 (void) table_sync_column_widths(0, verbs, options);
1082
1083 printf("%s [OPTIONS...] COMMAND ...\n\n"
1084 "%sDownload, import or export disk images%s\n"
1085 "\n%sCommands:%s\n",
1086 program_invocation_short_name,
1087 ansi_highlight(),
1088 ansi_normal(),
1089 ansi_underline(),
1090 ansi_normal());
1091
1092 r = table_print_or_warn(verbs);
1093 if (r < 0)
1094 return r;
1095
1096 printf("\n%sOptions:%s\n",
1097 ansi_underline(),
1098 ansi_normal());
1099
1100 r = table_print_or_warn(options);
1101 if (r < 0)
1102 return r;
1103
1104 printf("\nSee the %s for details.\n", link);
1105 return 0;
1106 }
1107
1108 VERB_COMMON_HELP(help);
1109
1110 static int parse_argv(int argc, char *argv[], char ***ret_args) {
1111 int r;
1112
1113 assert(argc >= 0);
1114 assert(argv);
1115
1116 OptionParser opts = { argc, argv };
1117
1118 FOREACH_OPTION_OR_RETURN(c, &opts)
1119 switch (c) {
1120
1121 OPTION_COMMON_HELP:
1122 return help();
1123
1124 OPTION_COMMON_VERSION:
1125 return version();
1126
1127 OPTION_COMMON_NO_PAGER:
1128 arg_pager_flags |= PAGER_DISABLE;
1129 break;
1130
1131 OPTION_COMMON_NO_LEGEND:
1132 arg_legend = false;
1133 break;
1134
1135 OPTION_COMMON_NO_ASK_PASSWORD:
1136 arg_ask_password = false;
1137 break;
1138
1139 OPTION_COMMON_HOST:
1140 arg_transport = BUS_TRANSPORT_REMOTE;
1141 arg_host = opts.arg;
1142 break;
1143
1144 OPTION_COMMON_MACHINE:
1145 r = parse_machine_argument(opts.arg, &arg_host, &arg_transport);
1146 if (r < 0)
1147 return r;
1148 break;
1149
1150 OPTION_LONG("system", NULL, "Connect to system machine manager"):
1151 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
1152 break;
1153
1154 OPTION_LONG("user", NULL, "Connect to user machine manager"):
1155 arg_runtime_scope = RUNTIME_SCOPE_USER;
1156 break;
1157
1158 OPTION_LONG("read-only", NULL, "Create read-only image"):
1159 arg_import_flags |= IMPORT_READ_ONLY;
1160 arg_import_flags_mask |= IMPORT_READ_ONLY;
1161 break;
1162
1163 OPTION('q', "quiet", NULL, "Suppress output"):
1164 arg_quiet = true;
1165 break;
1166
1167 OPTION_COMMON_JSON:
1168 r = parse_json_argument(opts.arg, &arg_json_format_flags);
1169 if (r <= 0)
1170 return r;
1171 arg_legend = false;
1172 break;
1173
1174 OPTION_COMMON_LOWERCASE_J:
1175 arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO;
1176 arg_legend = false;
1177 break;
1178
1179 OPTION_LONG("verify", "MODE",
1180 "Verification mode for downloaded images (no, checksum, signature)"):
1181 if (streq(opts.arg, "help"))
1182 return DUMP_STRING_TABLE(import_verify, ImportVerify, _IMPORT_VERIFY_MAX);
1183
1184 r = import_verify_from_string(opts.arg);
1185 if (r < 0)
1186 return log_error_errno(r, "Failed to parse --verify= setting: %s", opts.arg);
1187 arg_verify = r;
1188 break;
1189
1190 OPTION_LONG("format", "FORMAT",
1191 "Desired output format for export (zstd, xz, gzip, bzip2)"):
1192 if (!STR_IN_SET(opts.arg, "uncompressed", "xz", "gzip", "bzip2", "zstd"))
1193 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1194 "Unknown format: %s", opts.arg);
1195 arg_format = opts.arg;
1196 break;
1197
1198 OPTION_LONG("force", NULL, "Install image even if already exists"):
1199 arg_import_flags |= IMPORT_FORCE;
1200 arg_import_flags_mask |= IMPORT_FORCE;
1201 break;
1202
1203 OPTION_LONG("class", "TYPE", "Install as the specified TYPE"):
1204 arg_image_class = image_class_from_string(opts.arg);
1205 if (arg_image_class < 0)
1206 return log_error_errno(arg_image_class, "Failed to parse --class= parameter: %s", opts.arg);
1207 break;
1208
1209 OPTION_SHORT('m', NULL, "Install as --class=machine, machine image"):
1210 arg_image_class = IMAGE_MACHINE;
1211 break;
1212
1213 OPTION_SHORT('P', NULL, "Install as --class=portable, portable service image"):
1214 arg_image_class = IMAGE_PORTABLE;
1215 break;
1216
1217 OPTION_SHORT('S', NULL, "Install as --class=sysext, system extension image"):
1218 arg_image_class = IMAGE_SYSEXT;
1219 break;
1220
1221 OPTION_SHORT('C', NULL, "Install as --class=confext, configuration extension image"):
1222 arg_image_class = IMAGE_CONFEXT;
1223 break;
1224
1225 OPTION_LONG("keep-download", "BOOL",
1226 "Control whether to keep pristine copy of download"):
1227 r = parse_boolean(opts.arg);
1228 if (r < 0)
1229 return log_error_errno(r, "Failed to parse --keep-download= value: %s", opts.arg);
1230
1231 SET_FLAG(arg_import_flags, IMPORT_PULL_KEEP_DOWNLOAD, r);
1232 arg_import_flags_mask |= IMPORT_PULL_KEEP_DOWNLOAD;
1233 break;
1234
1235 OPTION_SHORT('N', NULL, "Same as --keep-download=no"):
1236 arg_import_flags &= ~IMPORT_PULL_KEEP_DOWNLOAD;
1237 arg_import_flags_mask |= IMPORT_PULL_KEEP_DOWNLOAD;
1238 break;
1239 }
1240
1241 *ret_args = option_parser_get_args(&opts);
1242 return 1;
1243 }
1244
1245 static int run(int argc, char *argv[]) {
1246 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1247 int r;
1248
1249 setlocale(LC_ALL, "");
1250 log_setup();
1251
1252 char **args = NULL;
1253 r = parse_argv(argc, argv, &args);
1254 if (r <= 0)
1255 return r;
1256
1257 r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, &bus);
1258 if (r < 0)
1259 return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
1260
1261 (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1262
1263 return dispatch_verb(args, bus);
1264 }
1265
1266 DEFINE_MAIN_FUNCTION(run);