Debian Codegraph @

src/sysupdate/sysupdate.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-daemon.h"
6
7 #include "build.h"
8 #include "conf-files.h"
9 #include "constants.h"
10 #include "dissect-image.h"
11 #include "format-table.h"
12 #include "glyph-util.h"
13 #include "hexdecoct.h"
14 #include "image-policy.h"
15 #include "loop-util.h"
16 #include "main-func.h"
17 #include "mount-util.h"
18 #include "options.h"
19 #include "os-util.h"
20 #include "pager.h"
21 #include "parse-argument.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "pretty-print.h"
25 #include "set.h"
26 #include "sort-util.h"
27 #include "specifier.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "sysupdate.h"
31 #include "sysupdate-feature.h"
32 #include "sysupdate-instance.h"
33 #include "sysupdate-transfer.h"
34 #include "sysupdate-update-set.h"
35 #include "sysupdate-util.h"
36 #include "utf8.h"
37 #include "verbs.h"
38
39 static char *arg_definitions = NULL;
40 bool arg_sync = true;
41 uint64_t arg_instances_max = UINT64_MAX;
42 static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
43 static PagerFlags arg_pager_flags = 0;
44 static bool arg_legend = true;
45 char *arg_root = NULL;
46 static char *arg_image = NULL;
47 static bool arg_reboot = false;
48 static char *arg_component = NULL;
49 static int arg_verify = -1;
50 static ImagePolicy *arg_image_policy = NULL;
51 static bool arg_offline = false;
52 char *arg_transfer_source = NULL;
53
54 STATIC_DESTRUCTOR_REGISTER(arg_definitions, freep);
55 STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
56 STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
57 STATIC_DESTRUCTOR_REGISTER(arg_component, freep);
58 STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
59 STATIC_DESTRUCTOR_REGISTER(arg_transfer_source, freep);
60
61 const Specifier specifier_table[] = {
62 COMMON_SYSTEM_SPECIFIERS,
63 COMMON_TMP_SPECIFIERS,
64 {}
65 };
66
67 typedef struct Context {
68 Transfer **transfers;
69 size_t n_transfers;
70
71 Transfer **disabled_transfers;
72 size_t n_disabled_transfers;
73
74 Hashmap *features; /* Defined features, keyed by ID */
75
76 UpdateSet **update_sets;
77 size_t n_update_sets;
78
79 UpdateSet *newest_installed, *candidate;
80
81 Hashmap *web_cache; /* Cache for downloaded resources, keyed by URL */
82 } Context;
83
84 static Context* context_free(Context *c) {
85 if (!c)
86 return NULL;
87
88 FOREACH_ARRAY(tr, c->transfers, c->n_transfers)
89 transfer_free(*tr);
90 free(c->transfers);
91
92 FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers)
93 transfer_free(*tr);
94 free(c->disabled_transfers);
95
96 hashmap_free(c->features);
97
98 FOREACH_ARRAY(us, c->update_sets, c->n_update_sets)
99 update_set_free(*us);
100 free(c->update_sets);
101
102 hashmap_free(c->web_cache);
103
104 return mfree(c);
105 }
106
107 DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
108
109 static Context* context_new(void) {
110 /* For now, no fields to initialize non-zero */
111 return new0(Context, 1);
112 }
113
114 static DEFINE_POINTER_ARRAY_FREE_FUNC(Transfer*, transfer_free);
115
116 static int read_definitions(
117 Context *c,
118 const char **dirs,
119 const char *suffix,
120 const char *node) {
121
122 ConfFile **files = NULL;
123 Transfer **transfers = NULL, **disabled = NULL;
124 size_t n_files = 0, n_transfers = 0, n_disabled = 0;
125 int r;
126
127 CLEANUP_ARRAY(files, n_files, conf_file_free_array);
128 CLEANUP_ARRAY(transfers, n_transfers, transfer_free_array);
129 CLEANUP_ARRAY(disabled, n_disabled, transfer_free_array);
130
131 assert(c);
132 assert(dirs);
133 assert(suffix);
134
135 r = conf_files_list_strv_full(suffix, arg_root,
136 CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED|CONF_FILES_WARN,
137 dirs, &files, &n_files);
138 if (r < 0)
139 return log_error_errno(r, "Failed to enumerate sysupdate.d/*%s definitions: %m", suffix);
140
141 FOREACH_ARRAY(i, files, n_files) {
142 _cleanup_(transfer_freep) Transfer *t = NULL;
143 Transfer **appended;
144 ConfFile *e = *i;
145
146 t = transfer_new(c);
147 if (!t)
148 return log_oom();
149
150 r = transfer_read_definition(t, e->result, dirs, c->features);
151 if (r < 0)
152 return r;
153
154 r = transfer_resolve_paths(t, arg_root, node);
155 if (r < 0)
156 return r;
157
158 if (t->enabled)
159 appended = GREEDY_REALLOC_APPEND(transfers, n_transfers, &t, 1);
160 else
161 appended = GREEDY_REALLOC_APPEND(disabled, n_disabled, &t, 1);
162 if (!appended)
163 return log_oom();
164 TAKE_PTR(t);
165 }
166
167 c->transfers = TAKE_PTR(transfers);
168 c->n_transfers = n_transfers;
169 c->disabled_transfers = TAKE_PTR(disabled);
170 c->n_disabled_transfers = n_disabled;
171 return 0;
172 }
173
174 typedef enum ReadDefinitionsFlags {
175 READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS = 1 << 0,
176 READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS = 1 << 1,
177 } ReadDefinitionsFlags;
178
179 static int context_read_definitions(Context *c, const char* node, ReadDefinitionsFlags flags) {
180 _cleanup_strv_free_ char **dirs = NULL;
181 int r;
182
183 assert(c);
184
185 if (arg_definitions)
186 dirs = strv_new(arg_definitions);
187 else if (arg_component) {
188 char **l = CONF_PATHS_STRV("");
189 size_t i = 0;
190
191 dirs = new0(char*, strv_length(l) + 1);
192 if (!dirs)
193 return log_oom();
194
195 STRV_FOREACH(dir, l) {
196 char *j;
197
198 j = strjoin(*dir, "sysupdate.", arg_component, ".d");
199 if (!j)
200 return log_oom();
201
202 dirs[i++] = j;
203 }
204 } else
205 dirs = strv_new(CONF_PATHS("sysupdate.d"));
206 if (!dirs)
207 return log_oom();
208
209 ConfFile **files = NULL;
210 size_t n_files = 0;
211
212 CLEANUP_ARRAY(files, n_files, conf_file_free_array);
213
214 r = conf_files_list_strv_full(".feature", arg_root,
215 CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED|CONF_FILES_WARN,
216 (const char**) dirs, &files, &n_files);
217 if (r < 0)
218 return log_error_errno(r, "Failed to enumerate sysupdate.d/*.feature definitions: %m");
219
220 FOREACH_ARRAY(i, files, n_files) {
221 _cleanup_(feature_unrefp) Feature *f = NULL;
222 ConfFile *e = *i;
223
224 f = feature_new();
225 if (!f)
226 return log_oom();
227
228 r = feature_read_definition(f, e->result, (const char**) dirs);
229 if (r < 0)
230 return r;
231
232 r = hashmap_ensure_put(&c->features, &feature_hash_ops, f->id, f);
233 if (r < 0)
234 return log_error_errno(r, "Failed to insert feature '%s' into map: %m", f->id);
235 TAKE_PTR(f);
236 }
237
238 r = read_definitions(c, (const char**) dirs, ".transfer", node);
239 if (r < 0)
240 return r;
241
242 if (c->n_transfers + c->n_disabled_transfers == 0) {
243 /* Backwards-compat: If no .transfer defs are found, fall back to trying .conf! */
244 r = read_definitions(c, (const char**) dirs, ".conf", node);
245 if (r < 0)
246 return r;
247
248 if (c->n_transfers + c->n_disabled_transfers > 0)
249 log_warning("As of v257, transfer definitions should have the '.transfer' extension.");
250 }
251
252 if (FLAGS_SET(flags, READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS) &&
253 c->n_transfers + (FLAGS_SET(flags, READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS) ? 0 : c->n_disabled_transfers) == 0) {
254 if (arg_component)
255 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
256 "No transfer definitions for component '%s' found.",
257 arg_component);
258
259 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
260 "No transfer definitions found.");
261 }
262
263 return 0;
264 }
265
266 static int context_load_installed_instances(Context *c) {
267 int r;
268
269 assert(c);
270
271 log_info("Discovering installed instances%s", glyph(GLYPH_ELLIPSIS));
272
273 FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
274 Transfer *t = *tr;
275
276 r = resource_load_instances(
277 &t->target,
278 arg_verify >= 0 ? arg_verify : t->verify,
279 &c->web_cache);
280 if (r < 0)
281 return r;
282 }
283
284 FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
285 Transfer *t = *tr;
286
287 r = resource_load_instances(
288 &t->target,
289 arg_verify >= 0 ? arg_verify : t->verify,
290 &c->web_cache);
291 if (r < 0)
292 return r;
293 }
294
295 return 0;
296 }
297
298 static int context_load_available_instances(Context *c) {
299 int r;
300
301 assert(c);
302
303 log_info("Discovering available instances%s", glyph(GLYPH_ELLIPSIS));
304
305 FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
306 Transfer *t = *tr;
307
308 r = resource_load_instances(
309 &t->source,
310 arg_verify >= 0 ? arg_verify : t->verify,
311 &c->web_cache);
312 if (r < 0)
313 return r;
314 }
315
316 return 0;
317 }
318
319 static int context_discover_update_sets_by_flag(Context *c, UpdateSetFlags flags) {
320 _cleanup_free_ char *boundary = NULL;
321 bool newest_found = false;
322 int r;
323
324 assert(c);
325 assert(IN_SET(flags, UPDATE_AVAILABLE, UPDATE_INSTALLED));
326
327 for (;;) {
328 _cleanup_free_ Instance **cursor_instances = NULL;
329 bool skip = false;
330 UpdateSetFlags extra_flags = 0;
331 _cleanup_free_ char *cursor = NULL;
332 UpdateSet *us = NULL;
333
334 /* First, let's find the newest version that's older than the boundary. */
335 FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
336 Resource *rr;
337
338 assert(*tr);
339
340 if (flags == UPDATE_AVAILABLE)
341 rr = &(*tr)->source;
342 else {
343 assert(flags == UPDATE_INSTALLED);
344 rr = &(*tr)->target;
345 }
346
347 FOREACH_ARRAY(inst, rr->instances, rr->n_instances) {
348 Instance *i = *inst; /* Sorted newest-to-oldest */
349
350 assert(i);
351
352 if (boundary && strverscmp_improved(i->metadata.version, boundary) >= 0)
353 continue; /* Not older than the boundary */
354
355 if (cursor && strverscmp(i->metadata.version, cursor) <= 0)
356 break; /* Not newer than the cursor. The same will be true for all
357 * subsequent instances (due to sorting) so let's skip to the
358 * next transfer. */
359
360 if (free_and_strdup(&cursor, i->metadata.version) < 0)
361 return log_oom();
362
363 break; /* All subsequent instances will be older than this one */
364 }
365
366 if (flags == UPDATE_AVAILABLE && !cursor)
367 break; /* This transfer didn't have a version older than the boundary,
368 * so any older-than-boundary version that might exist in a different
369 * transfer must always be incomplete. For reasons described below,
370 * we don't include incomplete versions for AVAILABLE updates. So we
371 * are completely done looking. */
372 }
373
374 if (!cursor) /* We didn't find anything older than the boundary, so we're done. */
375 break;
376
377 cursor_instances = new0(Instance*, c->n_transfers);
378 if (!cursor_instances)
379 return log_oom();
380
381 /* Now let's find all the instances that match the version of the cursor, if we have them */
382 for (size_t k = 0; k < c->n_transfers; k++) {
383 Transfer *t = c->transfers[k];
384 Instance *match = NULL;
385
386 assert(t);
387
388 if (flags == UPDATE_AVAILABLE) {
389 match = resource_find_instance(&t->source, cursor);
390 if (!match) {
391 /* When we're looking for updates to download, we don't offer
392 * incomplete versions at all. The server wants to send us an update
393 * with parts of the OS missing. For robustness sake, let's not do
394 * that. */
395 skip = true;
396 break;
397 }
398 } else {
399 assert(flags == UPDATE_INSTALLED);
400
401 match = resource_find_instance(&t->target, cursor);
402 if (!match && !(extra_flags & (UPDATE_PARTIAL|UPDATE_PENDING)))
403 /* When we're looking for installed versions, let's be robust and treat
404 * an incomplete installation as an installation. Otherwise, there are
405 * situations that can lead to sysupdate wiping the currently booted OS.
406 * See https://github.com/systemd/systemd/issues/33339 */
407 extra_flags |= UPDATE_INCOMPLETE;
408 }
409
410 cursor_instances[k] = match;
411
412 if (t->min_version && strverscmp_improved(t->min_version, cursor) > 0)
413 extra_flags |= UPDATE_OBSOLETE;
414
415 if (strv_contains(t->protected_versions, cursor))
416 extra_flags |= UPDATE_PROTECTED;
417
418 /* Partial or pending updates by definition are not incomplete, they’re
419 * partial/pending instead. While an individual Instance cannot be both partial and
420 * pending, an UpdateSet as a whole can contain both partial and pending instances. */
421 assert(!match || !(match->is_partial && match->is_pending));
422
423 if (match && match->is_partial)
424 extra_flags = (extra_flags | UPDATE_PARTIAL) & ~UPDATE_INCOMPLETE;
425
426 if (match && match->is_pending)
427 extra_flags = (extra_flags | UPDATE_PENDING) & ~UPDATE_INCOMPLETE;
428 }
429
430 r = free_and_strdup_warn(&boundary, cursor);
431 if (r < 0)
432 return r;
433
434 if (skip)
435 continue;
436
437 /* See if we already have this update set in our table */
438 FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
439 UpdateSet *u = *update_set;
440
441 if (strverscmp_improved(u->version, cursor) != 0)
442 continue;
443
444 /* Merge in what we've learned and continue onto the next version */
445
446 if (FLAGS_SET(u->flags, UPDATE_INCOMPLETE) ||
447 FLAGS_SET(u->flags, UPDATE_PARTIAL) ||
448 FLAGS_SET(u->flags, UPDATE_PENDING)) {
449 assert(u->n_instances == c->n_transfers);
450
451 /* Incomplete updates will have picked NULL instances for the transfers that
452 * are missing. Now we have more information, so let's try to fill them in. */
453
454 for (size_t j = 0; j < u->n_instances; j++) {
455 if (!u->instances[j])
456 u->instances[j] = cursor_instances[j];
457
458 /* Make sure that the list is full if the update is AVAILABLE */
459 assert(flags != UPDATE_AVAILABLE || u->instances[j]);
460 }
461 }
462
463 u->flags |= flags | extra_flags;
464
465 /* If this is the newest installed version, that is incomplete and just became marked
466 * as available, and if there is no other candidate available, we promote this to be
467 * the candidate. Ignore partial or pending status on the update set. */
468 if (FLAGS_SET(u->flags, UPDATE_NEWEST|UPDATE_INSTALLED|UPDATE_INCOMPLETE|UPDATE_AVAILABLE) &&
469 !c->candidate && !FLAGS_SET(u->flags, UPDATE_OBSOLETE))
470 c->candidate = u;
471
472 skip = true;
473 newest_found = true;
474 break;
475 }
476
477 if (skip)
478 continue;
479
480 /* Doesn't exist yet, let's add it */
481 if (!GREEDY_REALLOC(c->update_sets, c->n_update_sets + 1))
482 return log_oom();
483
484 us = new(UpdateSet, 1);
485 if (!us)
486 return log_oom();
487
488 *us = (UpdateSet) {
489 .flags = flags | (newest_found ? 0 : UPDATE_NEWEST) | extra_flags,
490 .version = TAKE_PTR(cursor),
491 .instances = TAKE_PTR(cursor_instances),
492 .n_instances = c->n_transfers,
493 };
494
495 c->update_sets[c->n_update_sets++] = us;
496
497 newest_found = true;
498
499 /* Remember which one is the newest installed */
500 if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED)) == (UPDATE_NEWEST|UPDATE_INSTALLED))
501 c->newest_installed = us;
502
503 /* Remember which is the newest non-obsolete, available (and not installed) version, which we declare the "candidate".
504 * It may be partial or pending. */
505 if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_OBSOLETE)) == (UPDATE_NEWEST|UPDATE_AVAILABLE))
506 c->candidate = us;
507 }
508
509 /* Newest installed is newer than or equal to candidate? Then suppress the candidate */
510 if (c->newest_installed && !FLAGS_SET(c->newest_installed->flags, UPDATE_INCOMPLETE) &&
511 c->candidate && strverscmp_improved(c->newest_installed->version, c->candidate->version) >= 0)
512 c->candidate = NULL;
513
514 /* Newest installed is still pending or partial and no candidate is set? Then it becomes the candidate. */
515 if (c->newest_installed &&
516 (c->newest_installed->flags & (UPDATE_PENDING|UPDATE_PARTIAL)) &&
517 !c->candidate)
518 c->candidate = c->newest_installed;
519
520 return 0;
521 }
522
523 static int context_discover_update_sets(Context *c) {
524 int r;
525
526 assert(c);
527
528 log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
529
530 r = context_discover_update_sets_by_flag(c, UPDATE_INSTALLED);
531 if (r < 0)
532 return r;
533
534 if (!arg_offline) {
535 log_info("Determining available update sets%s", glyph(GLYPH_ELLIPSIS));
536
537 r = context_discover_update_sets_by_flag(c, UPDATE_AVAILABLE);
538 if (r < 0)
539 return r;
540 }
541
542 typesafe_qsort(c->update_sets, c->n_update_sets, update_set_cmp);
543 return 0;
544 }
545
546 static int context_show_table(Context *c) {
547 _cleanup_(table_unrefp) Table *t = NULL;
548 int r;
549
550 assert(c);
551
552 t = table_new("", "version", "installed", "available", "assessment");
553 if (!t)
554 return log_oom();
555
556 (void) table_set_align_percent(t, table_get_cell(t, 0, 0), 100);
557 (void) table_set_align_percent(t, table_get_cell(t, 0, 2), 50);
558 (void) table_set_align_percent(t, table_get_cell(t, 0, 3), 50);
559
560 FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
561 UpdateSet *us = *update_set;
562 const char *color;
563
564 color = update_set_flags_to_color(us->flags);
565
566 r = table_add_many(t,
567 TABLE_STRING, update_set_flags_to_glyph(us->flags),
568 TABLE_SET_COLOR, color,
569 TABLE_STRING, us->version,
570 TABLE_SET_COLOR, color,
571 TABLE_STRING, glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_INSTALLED)),
572 TABLE_SET_COLOR, color,
573 TABLE_STRING, glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_AVAILABLE)),
574 TABLE_SET_COLOR, color,
575 TABLE_STRING, update_set_flags_to_string(us->flags),
576 TABLE_SET_COLOR, color);
577 if (r < 0)
578 return table_log_add_error(r);
579 }
580
581 return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
582 }
583
584 static UpdateSet* context_update_set_by_version(Context *c, const char *version) {
585 assert(c);
586 assert(version);
587
588 FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets)
589 if (streq((*update_set)->version, version))
590 return *update_set;
591
592 return NULL;
593 }
594
595 static int context_show_version(Context *c, const char *version) {
596 bool show_fs_columns = false, show_partition_columns = false,
597 have_fs_attributes = false, have_partition_attributes = false,
598 have_size = false, have_tries = false, have_no_auto = false,
599 have_read_only = false, have_growfs = false, have_sha256 = false;
600 _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
601 _cleanup_(table_unrefp) Table *t = NULL;
602 _cleanup_strv_free_ char **changelog_urls = NULL;
603 UpdateSet *us;
604 int r;
605
606 assert(c);
607 assert(version);
608
609 us = context_update_set_by_version(c, version);
610 if (!us)
611 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
612
613 if (arg_json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO))
614 pager_open(arg_pager_flags);
615
616 if (!sd_json_format_enabled(arg_json_format_flags))
617 printf("%s%s%s Version: %s\n"
618 " State: %s%s%s\n"
619 "Installed: %s%s%s%s\n"
620 "Available: %s%s\n"
621 "Protected: %s%s%s\n"
622 " Obsolete: %s%s%s\n\n",
623 strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_glyph(us->flags), ansi_normal(), us->version,
624 strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_string(us->flags), ansi_normal(),
625 yes_no(us->flags & UPDATE_INSTALLED), FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_NEWEST) ? " (newest)" : "",
626 FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PENDING) ? " (pending)" : "", FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PARTIAL) ? " (partial)" : "",
627 yes_no(us->flags & UPDATE_AVAILABLE), (us->flags & (UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_NEWEST)) == (UPDATE_AVAILABLE|UPDATE_NEWEST) ? " (newest)" : "",
628 FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED) ? ansi_highlight() : "", yes_no(FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED)), ansi_normal(),
629 us->flags & UPDATE_OBSOLETE ? ansi_highlight_red() : "", yes_no(us->flags & UPDATE_OBSOLETE), ansi_normal());
630
631 t = table_new("type", "path", "ptuuid", "ptflags", "mtime", "mode", "size", "tries-done", "tries-left", "noauto", "ro", "growfs", "sha256");
632 if (!t)
633 return log_oom();
634
635 (void) table_set_align_percent(t, table_get_cell(t, 0, 3), 100);
636 (void) table_set_align_percent(t, table_get_cell(t, 0, 4), 100);
637 (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
638 (void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
639 (void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
640 (void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
641 table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
642
643 /* Starting in v257, these fields would be automatically formatted with underscores. This would have
644 * been a breaking change, so to avoid that let's hard-code their original names. */
645 (void) table_set_json_field_name(t, 7, "tries-done");
646 (void) table_set_json_field_name(t, 8, "tries-left");
647
648 /* Determine if the target will make use of partition/fs attributes for any of the transfers */
649 FOREACH_ARRAY(transfer, c->transfers, c->n_transfers) {
650 Transfer *tr = *transfer;
651
652 if (tr->target.type == RESOURCE_PARTITION)
653 show_partition_columns = true;
654 if (RESOURCE_IS_FILESYSTEM(tr->target.type))
655 show_fs_columns = true;
656
657 STRV_FOREACH(changelog, tr->changelog) {
658 assert(*changelog);
659
660 _cleanup_free_ char *changelog_url = strreplace(*changelog, "@v", version);
661 if (!changelog_url)
662 return log_oom();
663
664 /* Avoid duplicates */
665 if (strv_contains(changelog_urls, changelog_url))
666 continue;
667
668 /* changelog_urls takes ownership of expanded changelog_url */
669 r = strv_consume(&changelog_urls, TAKE_PTR(changelog_url));
670 if (r < 0)
671 return log_oom();
672 }
673 }
674
675 FOREACH_ARRAY(inst, us->instances, us->n_instances) {
676 Instance *i = *inst;
677
678 if (!i) {
679 assert(us->flags & (UPDATE_INCOMPLETE|UPDATE_PARTIAL|UPDATE_PENDING));
680 continue;
681 }
682
683 r = table_add_many(t,
684 TABLE_STRING, resource_type_to_string(i->resource->type),
685 TABLE_PATH, i->path);
686 if (r < 0)
687 return table_log_add_error(r);
688
689 if (i->metadata.partition_uuid_set) {
690 have_partition_attributes = true;
691 r = table_add_cell(t, NULL, TABLE_UUID, &i->metadata.partition_uuid);
692 } else
693 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
694 if (r < 0)
695 return table_log_add_error(r);
696
697 if (i->metadata.partition_flags_set) {
698 have_partition_attributes = true;
699 r = table_add_cell(t, NULL, TABLE_UINT64_HEX, &i->metadata.partition_flags);
700 } else
701 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
702 if (r < 0)
703 return table_log_add_error(r);
704
705 if (i->metadata.mtime != USEC_INFINITY) {
706 have_fs_attributes = true;
707 r = table_add_cell(t, NULL, TABLE_TIMESTAMP, &i->metadata.mtime);
708 } else
709 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
710 if (r < 0)
711 return table_log_add_error(r);
712
713 if (i->metadata.mode != MODE_INVALID) {
714 have_fs_attributes = true;
715 r = table_add_cell(t, NULL, TABLE_MODE, &i->metadata.mode);
716 } else
717 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
718 if (r < 0)
719 return table_log_add_error(r);
720
721 if (i->metadata.size != UINT64_MAX) {
722 have_size = true;
723 r = table_add_cell(t, NULL, TABLE_SIZE, &i->metadata.size);
724 } else
725 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
726 if (r < 0)
727 return table_log_add_error(r);
728
729 if (i->metadata.tries_done != UINT64_MAX) {
730 have_tries = true;
731 r = table_add_cell(t, NULL, TABLE_UINT64, &i->metadata.tries_done);
732 } else
733 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
734 if (r < 0)
735 return table_log_add_error(r);
736
737 if (i->metadata.tries_left != UINT64_MAX) {
738 have_tries = true;
739 r = table_add_cell(t, NULL, TABLE_UINT64, &i->metadata.tries_left);
740 } else
741 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
742 if (r < 0)
743 return table_log_add_error(r);
744
745 if (i->metadata.no_auto >= 0) {
746 bool b;
747
748 have_no_auto = true;
749 b = i->metadata.no_auto;
750 r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
751 } else
752 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
753 if (r < 0)
754 return table_log_add_error(r);
755 if (i->metadata.read_only >= 0) {
756 bool b;
757
758 have_read_only = true;
759 b = i->metadata.read_only;
760 r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
761 } else
762 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
763 if (r < 0)
764 return table_log_add_error(r);
765
766 if (i->metadata.growfs >= 0) {
767 bool b;
768
769 have_growfs = true;
770 b = i->metadata.growfs;
771 r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
772 } else
773 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
774 if (r < 0)
775 return table_log_add_error(r);
776
777 if (i->metadata.sha256sum_set) {
778 _cleanup_free_ char *formatted = NULL;
779
780 have_sha256 = true;
781
782 formatted = hexmem(i->metadata.sha256sum, sizeof(i->metadata.sha256sum));
783 if (!formatted)
784 return log_oom();
785
786 r = table_add_cell(t, NULL, TABLE_STRING, formatted);
787 } else
788 r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
789 if (r < 0)
790 return table_log_add_error(r);
791 }
792
793 /* Hide the fs/partition columns if we don't have any data to show there */
794 if (!have_fs_attributes)
795 show_fs_columns = false;
796 if (!have_partition_attributes)
797 show_partition_columns = false;
798
799 if (!show_partition_columns)
800 (void) table_hide_column_from_display(t, 2, 3);
801 if (!show_fs_columns)
802 (void) table_hide_column_from_display(t, 4, 5);
803 if (!have_size)
804 (void) table_hide_column_from_display(t, 6);
805 if (!have_tries)
806 (void) table_hide_column_from_display(t, 7, 8);
807 if (!have_no_auto)
808 (void) table_hide_column_from_display(t, 9);
809 if (!have_read_only)
810 (void) table_hide_column_from_display(t, 10);
811 if (!have_growfs)
812 (void) table_hide_column_from_display(t, 11);
813 if (!have_sha256)
814 (void) table_hide_column_from_display(t, 12);
815
816 if (!sd_json_format_enabled(arg_json_format_flags)) {
817 printf("%s%s%s Version: %s\n"
818 " State: %s%s%s\n"
819 "Installed: %s%s%s%s%s%s%s\n"
820 "Available: %s%s\n"
821 "Protected: %s%s%s\n"
822 " Obsolete: %s%s%s\n",
823 strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_glyph(us->flags), ansi_normal(), us->version,
824 strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_string(us->flags), ansi_normal(),
825 yes_no(us->flags & UPDATE_INSTALLED), FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_NEWEST) ? " (newest)" : "",
826 FLAGS_SET(us->flags, UPDATE_INCOMPLETE) ? ansi_highlight_yellow() : "", FLAGS_SET(us->flags, UPDATE_INCOMPLETE) ? " (incomplete)" : "", ansi_normal(),
827 FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PENDING) ? " (pending)" : "", FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PARTIAL) ? " (partial)" : "",
828 yes_no(us->flags & UPDATE_AVAILABLE), (us->flags & (UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_NEWEST)) == (UPDATE_AVAILABLE|UPDATE_NEWEST) ? " (newest)" : "",
829 FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED) ? ansi_highlight() : "", yes_no(FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED)), ansi_normal(),
830 us->flags & UPDATE_OBSOLETE ? ansi_highlight_red() : "", yes_no(us->flags & UPDATE_OBSOLETE), ansi_normal());
831
832 STRV_FOREACH(url, changelog_urls) {
833 _cleanup_free_ char *changelog_link = NULL;
834 r = terminal_urlify(*url, NULL, &changelog_link);
835 if (r < 0)
836 return log_oom();
837 printf("ChangeLog: %s\n", changelog_link);
838 }
839 printf("\n");
840
841 return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
842 } else {
843 _cleanup_(sd_json_variant_unrefp) sd_json_variant *t_json = NULL;
844
845 r = table_to_json(t, &t_json);
846 if (r < 0)
847 return log_error_errno(r, "failed to convert table to JSON: %m");
848
849 r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("version", us->version),
850 SD_JSON_BUILD_PAIR_BOOLEAN("newest", FLAGS_SET(us->flags, UPDATE_NEWEST)),
851 SD_JSON_BUILD_PAIR_BOOLEAN("available", FLAGS_SET(us->flags, UPDATE_AVAILABLE)),
852 SD_JSON_BUILD_PAIR_BOOLEAN("installed", FLAGS_SET(us->flags, UPDATE_INSTALLED)),
853 SD_JSON_BUILD_PAIR_BOOLEAN("partial", FLAGS_SET(us->flags, UPDATE_PARTIAL)),
854 SD_JSON_BUILD_PAIR_BOOLEAN("pending", FLAGS_SET(us->flags, UPDATE_PENDING)),
855 SD_JSON_BUILD_PAIR_BOOLEAN("obsolete", FLAGS_SET(us->flags, UPDATE_OBSOLETE)),
856 SD_JSON_BUILD_PAIR_BOOLEAN("protected", FLAGS_SET(us->flags, UPDATE_PROTECTED)),
857 SD_JSON_BUILD_PAIR_BOOLEAN("incomplete", FLAGS_SET(us->flags, UPDATE_INCOMPLETE)),
858 SD_JSON_BUILD_PAIR_STRV("changelogUrls", changelog_urls),
859 SD_JSON_BUILD_PAIR_VARIANT("contents", t_json));
860 if (r < 0)
861 return log_error_errno(r, "Failed to create JSON: %m");
862
863 r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
864 if (r < 0)
865 return log_error_errno(r, "Failed to print JSON: %m");
866
867 return 0;
868 }
869 }
870
871 static int context_vacuum(
872 Context *c,
873 uint64_t space,
874 const char *extra_protected_version) {
875
876 size_t disabled_count = 0;
877 int r, count = 0;
878
879 assert(c);
880
881 if (space == 0)
882 log_info("Making room%s", glyph(GLYPH_ELLIPSIS));
883 else
884 log_info("Making room for %" PRIu64 " updates%s", space, glyph(GLYPH_ELLIPSIS));
885
886 FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
887 Transfer *t = *tr;
888
889 /* Don't bother clearing out space if we're not going to be downloading anything */
890 if (extra_protected_version && resource_find_instance(&t->target, extra_protected_version))
891 continue;
892
893 r = transfer_vacuum(t, space, extra_protected_version);
894 if (r < 0)
895 return r;
896
897 count = MAX(count, r);
898 }
899
900 FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
901 r = transfer_vacuum(*tr, UINT64_MAX /* wipe all instances */, NULL);
902 if (r < 0)
903 return r;
904 if (r > 0)
905 disabled_count++;
906 }
907
908 if (!sd_json_format_enabled(arg_json_format_flags)) {
909 if (count > 0 && disabled_count > 0)
910 log_info("Removed %i instances, and %zu disabled transfers.", count, disabled_count);
911 else if (count > 0)
912 log_info("Removed %i instances.", count);
913 else if (disabled_count > 0)
914 log_info("Removed %zu disabled transfers.", disabled_count);
915 else
916 log_info("Found nothing to remove.");
917 } else {
918 _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
919
920 r = sd_json_buildo(&json,
921 SD_JSON_BUILD_PAIR_INTEGER("removed", count),
922 SD_JSON_BUILD_PAIR_UNSIGNED("disabledTransfers", disabled_count));
923 if (r < 0)
924 return log_error_errno(r, "Failed to create JSON: %m");
925
926 r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
927 if (r < 0)
928 return log_error_errno(r, "Failed to print JSON: %m");
929 }
930
931 return 0;
932 }
933
934 static int context_make_offline(Context **ret, const char *node, ReadDefinitionsFlags read_definitions_flags) {
935 _cleanup_(context_freep) Context* context = NULL;
936 int r;
937
938 assert(ret);
939
940 /* Allocates a context object and initializes everything we can initialize offline, i.e. without
941 * checking on the update source (i.e. the Internet) what versions are available */
942
943 context = context_new();
944 if (!context)
945 return log_oom();
946
947 r = context_read_definitions(context, node, read_definitions_flags);
948 if (r < 0)
949 return r;
950
951 r = context_load_installed_instances(context);
952 if (r < 0)
953 return r;
954
955 *ret = TAKE_PTR(context);
956 return 0;
957 }
958
959 static int context_make_online(Context **ret, const char *node) {
960 _cleanup_(context_freep) Context* context = NULL;
961 int r;
962
963 assert(ret);
964
965 /* Like context_make_offline(), but also communicates with the update source looking for new
966 * versions (as long as --offline is not specified on the command line). */
967
968 r = context_make_offline(&context, node,
969 READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS | READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
970 if (r < 0)
971 return r;
972
973 if (!arg_offline) {
974 r = context_load_available_instances(context);
975 if (r < 0)
976 return r;
977 }
978
979 r = context_discover_update_sets(context);
980 if (r < 0)
981 return r;
982
983 *ret = TAKE_PTR(context);
984 return 0;
985 }
986
987 static int context_on_acquire_progress(const Transfer *t, const Instance *inst, unsigned percentage) {
988 const Context *c = ASSERT_PTR(t->context);
989 size_t i, n = c->n_transfers;
990 uint64_t base, scaled;
991 unsigned overall;
992
993 for (i = 0; i < n; i++)
994 if (c->transfers[i] == t)
995 break;
996 assert(i < n); /* We should have found the index */
997
998 base = (100 * 100 * i) / n;
999 scaled = (100 * percentage) / n;
1000 overall = (unsigned) ((base + scaled) / 100);
1001 assert(overall <= 100);
1002
1003 log_debug("Transfer %zu/%zu is %u%% complete (%u%% overall).", i+1, n, percentage, overall);
1004 return sd_notifyf(/* unset_environment= */ false, "X_SYSUPDATE_PROGRESS=%u\n"
1005 "X_SYSUPDATE_TRANSFERS_LEFT=%zu\n"
1006 "X_SYSUPDATE_TRANSFERS_DONE=%zu\n"
1007 "STATUS=Updating to '%s' (%u%% complete).",
1008 overall, n - i, i, inst->metadata.version, overall);
1009 }
1010
1011 static int context_process_partial_and_pending(Context *c, const char *version);
1012
1013 static int context_acquire(
1014 Context *c,
1015 const char *version) {
1016
1017 UpdateSet *us = NULL;
1018 int r;
1019
1020 assert(c);
1021
1022 if (version) {
1023 us = context_update_set_by_version(c, version);
1024 if (!us)
1025 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
1026 } else {
1027 if (!c->candidate) {
1028 log_info("No update needed.");
1029
1030 return 0;
1031 }
1032
1033 us = c->candidate;
1034 }
1035
1036 if (FLAGS_SET(us->flags, UPDATE_INCOMPLETE))
1037 log_info("Selected update '%s' is already installed, but incomplete. Repairing.", us->version);
1038 else if (FLAGS_SET(us->flags, UPDATE_PARTIAL)) {
1039 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), "Selected update '%s' is already acquired and partially installed. Vacuum it to try installing again.", us->version);
1040 } else if (FLAGS_SET(us->flags, UPDATE_PENDING)) {
1041 log_info("Selected update '%s' is already acquired and pending installation.", us->version);
1042
1043 return context_process_partial_and_pending(c, version);
1044 } else if (FLAGS_SET(us->flags, UPDATE_INSTALLED)) {
1045 log_info("Selected update '%s' is already installed. Skipping update.", us->version);
1046
1047 return 0;
1048 }
1049
1050 if (!FLAGS_SET(us->flags, UPDATE_AVAILABLE))
1051 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is not available, refusing.", us->version);
1052 if (FLAGS_SET(us->flags, UPDATE_OBSOLETE))
1053 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is obsolete, refusing.", us->version);
1054
1055 if (!FLAGS_SET(us->flags, UPDATE_NEWEST))
1056 log_notice("Selected update '%s' is not the newest, proceeding anyway.", us->version);
1057 if (c->newest_installed && strverscmp_improved(c->newest_installed->version, us->version) > 0)
1058 log_notice("Selected update '%s' is older than newest installed version, proceeding anyway.", us->version);
1059
1060 log_info("Selected update '%s' for install.", us->version);
1061
1062 _cleanup_free_ InstanceMetadata *metadata = new0(InstanceMetadata, c->n_transfers);
1063 if (!metadata)
1064 return log_oom();
1065
1066 /* Compute up the temporary paths before vacuuming so we don't vacuum anything if we fail to compute
1067 * any paths because of failed validations (e.g. exceeding the gpt partition label size). */
1068 for (size_t i = 0; i < c->n_transfers; i++) {
1069 Instance *inst = us->instances[i];
1070 Transfer *t = c->transfers[i];
1071
1072 assert(inst);
1073
1074 r = transfer_compute_temporary_paths(t, inst, metadata + i);
1075 if (r < 0)
1076 return r;
1077 }
1078
1079 (void) sd_notifyf(/* unset_environment= */ false,
1080 "READY=1\n"
1081 "X_SYSUPDATE_VERSION=%s\n"
1082 "STATUS=Making room for '%s'.", us->version, us->version);
1083
1084 /* Let's make some room. We make sure for each transfer we have one free space to fill. While
1085 * removing stuff we'll protect the version we are trying to acquire. Why that? Maybe an earlier
1086 * download succeeded already, in which case we shouldn't remove it just to acquire it again */
1087 r = context_vacuum(
1088 c,
1089 /* space= */ 1,
1090 /* extra_protected_version= */ us->version);
1091 if (r < 0)
1092 return r;
1093
1094 if (arg_sync)
1095 sync();
1096
1097 (void) sd_notifyf(/* unset_environment= */ false,
1098 "STATUS=Updating to '%s'.", us->version);
1099
1100 /* There should now be one instance picked for each transfer, and the order is the same */
1101 assert(us->n_instances == c->n_transfers);
1102
1103 for (size_t i = 0; i < c->n_transfers; i++) {
1104 Instance *inst = us->instances[i];
1105 Transfer *t = c->transfers[i];
1106
1107 assert(inst); /* ditto */
1108
1109 if (inst->resource == &t->target) { /* a present transfer in an incomplete installation */
1110 assert(FLAGS_SET(us->flags, UPDATE_INCOMPLETE));
1111 continue;
1112 }
1113
1114 r = transfer_acquire_instance(t, inst, metadata + i, context_on_acquire_progress, c);
1115 if (r < 0)
1116 return r;
1117 }
1118
1119 if (arg_sync)
1120 sync();
1121
1122 return 1;
1123 }
1124
1125 /* Check to see if we have an update set acquired and pending installation. */
1126 static int context_process_partial_and_pending(
1127 Context *c,
1128 const char *version) {
1129
1130 UpdateSet *us = NULL;
1131 int r;
1132
1133 assert(c);
1134
1135 if (version) {
1136 us = context_update_set_by_version(c, version);
1137 if (!us)
1138 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
1139 } else {
1140 if (!c->candidate) {
1141 log_info("No update needed.");
1142
1143 return 0;
1144 }
1145
1146 us = c->candidate;
1147 }
1148
1149 if (FLAGS_SET(us->flags, UPDATE_INCOMPLETE))
1150 log_info("Selected update '%s' is already installed, but incomplete. Repairing.", us->version);
1151 else if ((us->flags & (UPDATE_PARTIAL|UPDATE_PENDING|UPDATE_INSTALLED)) == UPDATE_INSTALLED) {
1152 log_info("Selected update '%s' is already installed. Skipping update.", us->version);
1153
1154 return 0;
1155 }
1156
1157 if (FLAGS_SET(us->flags, UPDATE_PARTIAL))
1158 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is only partially downloaded, refusing.", us->version);
1159 if (!FLAGS_SET(us->flags, UPDATE_PENDING))
1160 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is not pending installation, refusing.", us->version);
1161
1162 if (FLAGS_SET(us->flags, UPDATE_OBSOLETE))
1163 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is obsolete, refusing.", us->version);
1164
1165 if (!FLAGS_SET(us->flags, UPDATE_NEWEST))
1166 log_notice("Selected update '%s' is not the newest, proceeding anyway.", us->version);
1167 if (c->newest_installed && strverscmp_improved(c->newest_installed->version, us->version) > 0)
1168 log_notice("Selected update '%s' is older than newest installed version, proceeding anyway.", us->version);
1169
1170 log_info("Selected update '%s' for install.", us->version);
1171
1172 /* There should now be one instance picked for each transfer, and the order is the same */
1173 assert(us->n_instances == c->n_transfers);
1174
1175 for (size_t i = 0; i < c->n_transfers; i++) {
1176 Instance *inst = us->instances[i];
1177 Transfer *t = c->transfers[i];
1178
1179 assert(inst);
1180
1181 r = transfer_process_partial_and_pending_instance(t, inst);
1182 if (r < 0)
1183 return r;
1184 }
1185
1186 return 1;
1187 }
1188
1189 static int context_install(
1190 Context *c,
1191 const char *version,
1192 UpdateSet **ret_applied) {
1193
1194 UpdateSet *us = NULL;
1195 int r;
1196
1197 assert(c);
1198
1199 if (version) {
1200 us = context_update_set_by_version(c, version);
1201 if (!us)
1202 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
1203 } else {
1204 if (!c->candidate) {
1205 log_info("No update needed.");
1206
1207 return 0;
1208 }
1209
1210 us = c->candidate;
1211 }
1212
1213 (void) sd_notifyf(/* unset_environment=*/ false,
1214 "READY=1\n"
1215 "X_SYSUPDATE_VERSION=%s\n"
1216 "STATUS=Installing '%s'.", us->version, us->version);
1217
1218 for (size_t i = 0; i < c->n_transfers; i++) {
1219 Instance *inst = us->instances[i];
1220 Transfer *t = c->transfers[i];
1221
1222 if (inst->resource == &t->target &&
1223 !inst->is_pending)
1224 continue;
1225
1226 r = transfer_install_instance(t, inst, arg_root);
1227 if (r < 0)
1228 return r;
1229 }
1230
1231 log_info("%s Successfully installed update '%s'.", glyph(GLYPH_SPARKLES), us->version);
1232
1233 (void) sd_notifyf(/* unset_environment= */ false,
1234 "STATUS=Installed '%s'.", us->version);
1235
1236 if (ret_applied)
1237 *ret_applied = us;
1238
1239 return 1;
1240 }
1241
1242 static int process_image(
1243 bool ro,
1244 char **ret_mounted_dir,
1245 LoopDevice **ret_loop_device) {
1246
1247 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1248 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
1249 int r;
1250
1251 assert(ret_mounted_dir);
1252 assert(ret_loop_device);
1253
1254 if (!arg_image)
1255 return 0;
1256
1257 assert(!arg_root);
1258
1259 r = mount_image_privately_interactively(
1260 arg_image,
1261 arg_image_policy,
1262 (ro ? DISSECT_IMAGE_READ_ONLY : 0) |
1263 DISSECT_IMAGE_FSCK |
1264 DISSECT_IMAGE_MKDIR |
1265 DISSECT_IMAGE_GROWFS |
1266 DISSECT_IMAGE_RELAX_VAR_CHECK |
1267 DISSECT_IMAGE_USR_NO_ROOT |
1268 DISSECT_IMAGE_GENERIC_ROOT |
1269 DISSECT_IMAGE_REQUIRE_ROOT |
1270 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1271 &mounted_dir,
1272 /* ret_dir_fd= */ NULL,
1273 &loop_device);
1274 if (r < 0)
1275 return r;
1276
1277 arg_root = strdup(mounted_dir);
1278 if (!arg_root)
1279 return log_oom();
1280
1281 *ret_mounted_dir = TAKE_PTR(mounted_dir);
1282 *ret_loop_device = TAKE_PTR(loop_device);
1283
1284 return 0;
1285 }
1286
1287 VERB(verb_list, "list", "[VERSION]", VERB_ANY, 2, VERB_DEFAULT,
1288 "Show installed and available versions");
1289 static int verb_list(int argc, char *argv[], uintptr_t _data, void *userdata) {
1290 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1291 _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
1292 _cleanup_(context_freep) Context* context = NULL;
1293 _cleanup_strv_free_ char **appstream_urls = NULL;
1294 const char *version;
1295 int r;
1296
1297 assert(argc <= 2);
1298 version = argc >= 2 ? argv[1] : NULL;
1299
1300 r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
1301 if (r < 0)
1302 return r;
1303
1304 r = context_make_online(&context, loop_device ? loop_device->node : NULL);
1305 if (r < 0)
1306 return r;
1307
1308 if (version)
1309 return context_show_version(context, version);
1310 else if (!sd_json_format_enabled(arg_json_format_flags))
1311 return context_show_table(context);
1312 else {
1313 _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
1314 _cleanup_strv_free_ char **versions = NULL;
1315 const char *current = NULL;
1316 bool current_is_pending = false;
1317
1318 FOREACH_ARRAY(update_set, context->update_sets, context->n_update_sets) {
1319 UpdateSet *us = *update_set;
1320
1321 if (FLAGS_SET(us->flags, UPDATE_INSTALLED) &&
1322 FLAGS_SET(us->flags, UPDATE_NEWEST)) {
1323 current = us->version;
1324 current_is_pending = FLAGS_SET(us->flags, UPDATE_PENDING);
1325 }
1326
1327 r = strv_extend(&versions, us->version);
1328 if (r < 0)
1329 return log_oom();
1330 }
1331
1332 FOREACH_ARRAY(tr, context->transfers, context->n_transfers)
1333 STRV_FOREACH(appstream_url, (*tr)->appstream) {
1334 /* Avoid duplicates */
1335 if (strv_contains(appstream_urls, *appstream_url))
1336 continue;
1337
1338 r = strv_extend(&appstream_urls, *appstream_url);
1339 if (r < 0)
1340 return log_oom();
1341 }
1342
1343 r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING(current_is_pending ? "current+pending" : "current", current),
1344 SD_JSON_BUILD_PAIR_STRV("all", versions),
1345 SD_JSON_BUILD_PAIR_STRV("appstreamUrls", appstream_urls));
1346 if (r < 0)
1347 return log_error_errno(r, "Failed to create JSON: %m");
1348
1349 r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
1350 if (r < 0)
1351 return log_error_errno(r, "Failed to print JSON: %m");
1352
1353 return 0;
1354 }
1355 }
1356
1357 VERB(verb_features, "features", "[FEATURE]", VERB_ANY, 2, 0,
1358 "Show optional features");
1359 static int verb_features(int argc, char *argv[], uintptr_t _data, void *userdata) {
1360 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1361 _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
1362 _cleanup_(context_freep) Context* context = NULL;
1363 _cleanup_(table_unrefp) Table *table = NULL;
1364 const char *feature_id;
1365 Feature *f;
1366 int r;
1367
1368 assert(argc <= 2);
1369 feature_id = argc >= 2 ? argv[1] : NULL;
1370
1371 r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
1372 if (r < 0)
1373 return r;
1374
1375 r = context_make_offline(&context, loop_device ? loop_device->node : NULL,
1376 READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
1377 if (r < 0)
1378 return r;
1379
1380 if (feature_id) {
1381 _cleanup_strv_free_ char **transfers = NULL;
1382
1383 f = hashmap_get(context->features, feature_id);
1384 if (!f)
1385 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
1386 "Optional feature not found: %s",
1387 feature_id);
1388
1389 table = table_new_vertical();
1390 if (!table)
1391 return log_oom();
1392
1393 FOREACH_ARRAY(tr, context->transfers, context->n_transfers) {
1394 Transfer *t = *tr;
1395
1396 if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
1397 continue;
1398
1399 r = strv_extend(&transfers, t->id);
1400 if (r < 0)
1401 return log_oom();
1402 }
1403
1404 FOREACH_ARRAY(tr, context->disabled_transfers, context->n_disabled_transfers) {
1405 Transfer *t = *tr;
1406
1407 if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
1408 continue;
1409
1410 r = strv_extend(&transfers, t->id);
1411 if (r < 0)
1412 return log_oom();
1413 }
1414
1415 r = table_add_many(table,
1416 TABLE_FIELD, "Name",
1417 TABLE_STRING, f->id,
1418 TABLE_FIELD, "Enabled",
1419 TABLE_BOOLEAN, f->enabled);
1420 if (r < 0)
1421 return table_log_add_error(r);
1422
1423 if (f->description) {
1424 r = table_add_many(table, TABLE_FIELD, "Description", TABLE_STRING, f->description);
1425 if (r < 0)
1426 return table_log_add_error(r);
1427 }
1428
1429 if (f->documentation) {
1430 r = table_add_many(table,
1431 TABLE_FIELD, "Documentation",
1432 TABLE_STRING, f->documentation,
1433 TABLE_SET_URL, f->documentation);
1434 if (r < 0)
1435 return table_log_add_error(r);
1436 }
1437
1438 if (f->appstream) {
1439 r = table_add_many(table,
1440 TABLE_FIELD, "AppStream",
1441 TABLE_STRING, f->appstream,
1442 TABLE_SET_URL, f->appstream);
1443 if (r < 0)
1444 return table_log_add_error(r);
1445 }
1446
1447 if (!strv_isempty(transfers)) {
1448 r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, transfers);
1449 if (r < 0)
1450 return table_log_add_error(r);
1451 }
1452
1453 return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
1454 } else if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) {
1455 table = table_new("", "feature", "description", "documentation");
1456 if (!table)
1457 return log_oom();
1458
1459 HASHMAP_FOREACH(f, context->features) {
1460 r = table_add_many(table,
1461 TABLE_BOOLEAN_CHECKMARK, f->enabled,
1462 TABLE_SET_COLOR, ansi_highlight_green_red(f->enabled),
1463 TABLE_STRING, f->id,
1464 TABLE_STRING, f->description,
1465 TABLE_STRING, f->documentation,
1466 TABLE_SET_URL, f->documentation);
1467 if (r < 0)
1468 return table_log_add_error(r);
1469 }
1470
1471 return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
1472 } else {
1473 _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
1474 _cleanup_strv_free_ char **features = NULL;
1475
1476 HASHMAP_FOREACH(f, context->features) {
1477 r = strv_extend(&features, f->id);
1478 if (r < 0)
1479 return log_oom();
1480 }
1481
1482 r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRV("features", features));
1483 if (r < 0)
1484 return log_error_errno(r, "Failed to create JSON: %m");
1485
1486 r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
1487 if (r < 0)
1488 return log_error_errno(r, "Failed to print JSON: %m");
1489 }
1490
1491 return 0;
1492 }
1493
1494 VERB_NOARG(verb_check_new, "check-new",
1495 "Check if there's a new version available");
1496 static int verb_check_new(int argc, char *argv[], uintptr_t _data, void *userdata) {
1497 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1498 _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
1499 _cleanup_(context_freep) Context* context = NULL;
1500 int r;
1501
1502 assert(argc <= 1);
1503
1504 r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
1505 if (r < 0)
1506 return r;
1507
1508 r = context_make_online(&context, loop_device ? loop_device->node : NULL);
1509 if (r < 0)
1510 return r;
1511
1512 if (!sd_json_format_enabled(arg_json_format_flags)) {
1513 if (!context->candidate) {
1514 log_debug("No candidate found.");
1515 return EXIT_FAILURE;
1516 }
1517
1518 puts(context->candidate->version);
1519 } else {
1520 _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
1521
1522 if (context->candidate)
1523 r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("available", context->candidate->version));
1524 else
1525 r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_NULL("available"));
1526 if (r < 0)
1527 return log_error_errno(r, "Failed to create JSON: %m");
1528
1529 r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
1530 if (r < 0)
1531 return log_error_errno(r, "Failed to print JSON: %m");
1532 }
1533
1534 return EXIT_SUCCESS;
1535 }
1536
1537 typedef enum {
1538 UPDATE_ACTION_ACQUIRE = 1 << 0,
1539 UPDATE_ACTION_INSTALL = 1 << 1,
1540 } UpdateActionFlags;
1541
1542 static int verb_update_impl(int argc, char **argv, UpdateActionFlags action_flags) {
1543 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1544 _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
1545 _cleanup_(context_freep) Context* context = NULL;
1546 _cleanup_free_ char *booted_version = NULL;
1547 UpdateSet *applied = NULL;
1548 const char *version;
1549 int r;
1550
1551 assert(argc <= 2);
1552 version = argc >= 2 ? argv[1] : NULL;
1553
1554 if (arg_instances_max < 2)
1555 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1556 "The --instances-max argument must be >= 2 while updating");
1557
1558 if (arg_reboot) {
1559 /* If automatic reboot on completion is requested, let's first determine the currently booted image */
1560
1561 r = parse_os_release(arg_root, "IMAGE_VERSION", &booted_version);
1562 if (r < 0)
1563 return log_error_errno(r, "Failed to parse /etc/os-release: %m");
1564 if (!booted_version)
1565 return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "/etc/os-release lacks IMAGE_VERSION field.");
1566 }
1567
1568 r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
1569 if (r < 0)
1570 return r;
1571
1572 r = context_make_online(&context, loop_device ? loop_device->node : NULL);
1573 if (r < 0)
1574 return r;
1575
1576 if (action_flags & UPDATE_ACTION_ACQUIRE)
1577 r = context_acquire(context, version);
1578 else
1579 r = context_process_partial_and_pending(context, version);
1580 if (r < 0)
1581 return r; /* error */
1582
1583 if (action_flags & UPDATE_ACTION_INSTALL && r > 0) /* update needed */
1584 r = context_install(context, version, &applied);
1585 if (r < 0)
1586 return r;
1587
1588 if (r > 0 && arg_reboot) {
1589 assert(applied);
1590 assert(booted_version);
1591
1592 if (strverscmp_improved(applied->version, booted_version) > 0) {
1593 log_notice("Newly installed version is newer than booted version, rebooting.");
1594 return reboot_now();
1595 }
1596
1597 if (strverscmp_improved(applied->version, booted_version) == 0 &&
1598 FLAGS_SET(applied->flags, UPDATE_INCOMPLETE)) {
1599 log_notice("Currently booted version was incomplete and has been repaired, rebooting.");
1600 return reboot_now();
1601 }
1602
1603 log_info("Booted version is newer or identical to newly installed version, not rebooting.");
1604 }
1605
1606 return 0;
1607 }
1608
1609 VERB(verb_update, "update", "[VERSION]", VERB_ANY, 2, 0,
1610 "Install new version now");
1611 static int verb_update(int argc, char *argv[], uintptr_t _data, void *userdata) {
1612 UpdateActionFlags flags = UPDATE_ACTION_INSTALL;
1613
1614 if (!arg_offline)
1615 flags |= UPDATE_ACTION_ACQUIRE;
1616
1617 return verb_update_impl(argc, argv, flags);
1618 }
1619
1620 VERB(verb_acquire, "acquire", "[VERSION]", VERB_ANY, 2, 0,
1621 "Acquire (download) new version now");
1622 static int verb_acquire(int argc, char *argv[], uintptr_t _data, void *userdata) {
1623 return verb_update_impl(argc, argv, UPDATE_ACTION_ACQUIRE);
1624 }
1625
1626 VERB_NOARG(verb_vacuum, "vacuum",
1627 "Make room, by deleting old versions");
1628 static int verb_vacuum(int argc, char *argv[], uintptr_t _data, void *userdata) {
1629 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1630 _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
1631 _cleanup_(context_freep) Context* context = NULL;
1632 int r;
1633
1634 assert(argc <= 1);
1635
1636 if (arg_instances_max < 1)
1637 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1638 "The --instances-max argument must be >= 1 while vacuuming");
1639
1640 r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
1641 if (r < 0)
1642 return r;
1643
1644 r = context_make_offline(&context, loop_device ? loop_device->node : NULL,
1645 READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
1646 if (r < 0)
1647 return r;
1648
1649 return context_vacuum(context, 0, NULL);
1650 }
1651
1652 VERB(verb_pending_or_reboot, "pending", NULL, 1, 1, 0,
1653 "Report whether a newer version is installed than currently booted");
1654 VERB(verb_pending_or_reboot, "reboot", NULL, 1, 1, 0,
1655 "Reboot if a newer version is installed than booted");
1656 static int verb_pending_or_reboot(int argc, char *argv[], uintptr_t _data, void *userdata) {
1657 _cleanup_(context_freep) Context* context = NULL;
1658 _cleanup_free_ char *booted_version = NULL;
1659 int r;
1660
1661 assert(argc == 1);
1662
1663 if (arg_image || arg_root)
1664 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1665 "The --root=/--image= switches may not be combined with the '%s' operation.", argv[0]);
1666
1667 r = context_make_offline(&context, /* node= */ NULL,
1668 READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS | READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
1669 if (r < 0)
1670 return r;
1671
1672 log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
1673
1674 r = context_discover_update_sets_by_flag(context, UPDATE_INSTALLED);
1675 if (r < 0)
1676 return r;
1677 if (!context->newest_installed)
1678 return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "Couldn't find any suitable installed versions.");
1679
1680 r = parse_os_release(arg_root, "IMAGE_VERSION", &booted_version);
1681 if (r < 0) /* yes, arg_root is NULL here, but we have to pass something, and it's a lot more readable
1682 * if we see what the first argument is about */
1683 return log_error_errno(r, "Failed to parse /etc/os-release: %m");
1684 if (!booted_version)
1685 return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "/etc/os-release lacks IMAGE_VERSION= field.");
1686
1687 r = strverscmp_improved(context->newest_installed->version, booted_version);
1688 if (r > 0) {
1689 log_notice("Newest installed version '%s' is newer than booted version '%s'.%s",
1690 context->newest_installed->version, booted_version,
1691 streq(argv[0], "pending") ? " Reboot recommended." : "");
1692
1693 if (streq(argv[0], "reboot"))
1694 return reboot_now();
1695
1696 return EXIT_SUCCESS;
1697 } else if (r == 0)
1698 log_info("Newest installed version '%s' matches booted version '%s'.",
1699 context->newest_installed->version, booted_version);
1700 else
1701 log_warning("Newest installed version '%s' is older than booted version '%s'.",
1702 context->newest_installed->version, booted_version);
1703
1704 if (streq(argv[0], "pending")) /* When called as 'pending' tell the caller via failure exit code that there's nothing newer installed */
1705 return EXIT_FAILURE;
1706
1707 return EXIT_SUCCESS;
1708 }
1709
1710 static int component_name_valid(const char *c) {
1711 _cleanup_free_ char *j = NULL;
1712
1713 /* See if the specified string enclosed in the directory prefix+suffix would be a valid file name */
1714
1715 if (isempty(c))
1716 return false;
1717
1718 if (string_has_cc(c, NULL))
1719 return false;
1720
1721 if (!utf8_is_valid(c))
1722 return false;
1723
1724 j = strjoin("sysupdate.", c, ".d");
1725 if (!j)
1726 return -ENOMEM;
1727
1728 return filename_is_valid(j);
1729 }
1730
1731 VERB_NOARG(verb_components, "components",
1732 "Show list of components");
1733 static int verb_components(int argc, char *argv[], uintptr_t _data, void *userdata) {
1734 _cleanup_(context_freep) Context* context = NULL;
1735 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1736 _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
1737 _cleanup_set_free_ Set *names = NULL;
1738 bool has_default_component = false;
1739 int r;
1740
1741 assert(argc <= 1);
1742
1743 r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
1744 if (r < 0)
1745 return r;
1746
1747 r = context_make_offline(&context, loop_device ? loop_device->node : NULL, 0);
1748 if (r < 0)
1749 return r;
1750
1751 ConfFile **directories = NULL;
1752 size_t n_directories = 0;
1753
1754 CLEANUP_ARRAY(directories, n_directories, conf_file_free_array);
1755
1756 r = conf_files_list_strv_full(".d", arg_root, CONF_FILES_DIRECTORY|CONF_FILES_WARN,
1757 (const char * const *) CONF_PATHS_STRV(""), &directories, &n_directories);
1758 if (r < 0)
1759 return log_error_errno(r, "Failed to enumerate directories: %m");
1760
1761 FOREACH_ARRAY(i, directories, n_directories) {
1762 ConfFile *e = *i;
1763
1764 if (streq(e->filename, "sysupdate.d")) {
1765 continue;
1766 }
1767
1768 const char *s = startswith(e->filename, "sysupdate.");
1769 if (!s)
1770 continue;
1771
1772 const char *a = endswith(s, ".d");
1773 if (!a)
1774 continue;
1775
1776 _cleanup_free_ char *n = strndup(s, a - s);
1777 if (!n)
1778 return log_oom();
1779
1780 r = component_name_valid(n);
1781 if (r < 0)
1782 return log_error_errno(r, "Unable to validate component name '%s': %m", n);
1783 if (r == 0)
1784 continue;
1785
1786 r = set_ensure_put(&names, &string_hash_ops_free, n);
1787 if (r < 0 && r != -EEXIST)
1788 return log_error_errno(r, "Failed to add component '%s' to set: %m", n);
1789 TAKE_PTR(n);
1790 }
1791
1792 /* Does the system have at least one transfer file in /etc/sysupdate.d, which can be considered a
1793 * TARGET_HOST? See target_get_argument() in sysupdated.c */
1794 has_default_component = (!arg_definitions &&
1795 !arg_component &&
1796 !arg_root &&
1797 !arg_image &&
1798 context->n_transfers > 0);
1799
1800 /* We use simple free() rather than strv_free() here, since set_free() will free the strings for us */
1801 _cleanup_free_ char **z = set_get_strv(names);
1802 if (!z)
1803 return log_oom();
1804
1805 strv_sort(z);
1806
1807 if (!sd_json_format_enabled(arg_json_format_flags)) {
1808 if (!has_default_component && set_isempty(names)) {
1809 log_info("No components defined.");
1810 return 0;
1811 }
1812
1813 if (has_default_component)
1814 printf("%s<default>%s\n",
1815 ansi_highlight(), ansi_normal());
1816
1817 STRV_FOREACH(i, z)
1818 puts(*i);
1819 } else {
1820 _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
1821
1822 r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_BOOLEAN("default", has_default_component),
1823 SD_JSON_BUILD_PAIR_STRV("components", z));
1824 if (r < 0)
1825 return log_error_errno(r, "Failed to create JSON: %m");
1826
1827 r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
1828 if (r < 0)
1829 return log_error_errno(r, "Failed to print JSON: %m");
1830 }
1831
1832 return 0;
1833 }
1834
1835 static int help(void) {
1836 _cleanup_free_ char *link = NULL;
1837 _cleanup_(table_unrefp) Table *common_options = NULL, *options = NULL, *verbs = NULL;
1838 int r;
1839
1840 r = terminal_urlify_man("systemd-sysupdate", "8", &link);
1841 if (r < 0)
1842 return log_oom();
1843
1844 r = verbs_get_help_table(&verbs);
1845 if (r < 0)
1846 return r;
1847
1848 r = option_parser_get_help_table(&common_options);
1849 if (r < 0)
1850 return r;
1851
1852 r = option_parser_get_help_table_group("Options", &options);
1853 if (r < 0)
1854 return r;
1855
1856 (void) table_sync_column_widths(0, verbs, common_options, options);
1857
1858 printf("%s [OPTIONS...] [VERSION]\n"
1859 "\n%sUpdate OS images.%s\n"
1860 "\n%sCommands:%s\n",
1861 program_invocation_short_name,
1862 ansi_highlight(), ansi_normal(),
1863 ansi_underline(), ansi_normal());
1864
1865 r = table_print_or_warn(verbs);
1866 if (r < 0)
1867 return r;
1868
1869 r = table_print_or_warn(common_options);
1870 if (r < 0)
1871 return r;
1872
1873 printf("\n%sOptions:%s\n", ansi_underline(), ansi_normal());
1874 r = table_print_or_warn(options);
1875 if (r < 0)
1876 return r;
1877
1878 printf("\nSee the %s for details.\n", link);
1879 return 0;
1880 }
1881
1882 VERB_COMMON_HELP_HIDDEN(help);
1883
1884 static int parse_argv(int argc, char *argv[], char ***remaining_args) {
1885 assert(argc >= 0);
1886 assert(argv);
1887 assert(remaining_args);
1888
1889 OptionParser opts = { argc, argv };
1890 int r;
1891
1892 FOREACH_OPTION_OR_RETURN(c, &opts)
1893 switch (c) {
1894
1895 OPTION_COMMON_HELP:
1896 return help();
1897
1898 OPTION_COMMON_VERSION:
1899 return version();
1900
1901 OPTION_GROUP("Options"):
1902 break;
1903
1904 OPTION('C', "component", "NAME",
1905 "Select component to update"):
1906 if (isempty(opts.arg)) {
1907 arg_component = mfree(arg_component);
1908 break;
1909 }
1910
1911 r = component_name_valid(opts.arg);
1912 if (r < 0)
1913 return log_error_errno(r, "Failed to determine if component name is valid: %m");
1914 if (r == 0)
1915 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Component name invalid: %s", opts.arg);
1916
1917 r = free_and_strdup_warn(&arg_component, opts.arg);
1918 if (r < 0)
1919 return r;
1920
1921 break;
1922
1923 OPTION_LONG("definitions", "DIR",
1924 "Find transfer definitions in specified directory"):
1925 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_definitions);
1926 if (r < 0)
1927 return r;
1928 break;
1929
1930 OPTION_LONG("root", "PATH",
1931 "Operate on an alternate filesystem root"):
1932 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_root);
1933 if (r < 0)
1934 return r;
1935 break;
1936
1937 OPTION_LONG("image", "PATH",
1938 "Operate on disk image as filesystem root"):
1939 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image);
1940 if (r < 0)
1941 return r;
1942 break;
1943
1944 OPTION_LONG("image-policy", "POLICY",
1945 "Specify disk image dissection policy"):
1946 r = parse_image_policy_argument(opts.arg, &arg_image_policy);
1947 if (r < 0)
1948 return r;
1949 break;
1950
1951 OPTION_LONG("transfer-source", "PATH",
1952 "Specify the directory to transfer sources from"):
1953 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_transfer_source);
1954 if (r < 0)
1955 return r;
1956
1957 break;
1958
1959 OPTION('m', "instances-max", "INT",
1960 "How many instances to maintain"):
1961 r = safe_atou64(opts.arg, &arg_instances_max);
1962 if (r < 0)
1963 return log_error_errno(r, "Failed to parse --instances-max= parameter: %s", opts.arg);
1964
1965 break;
1966
1967 OPTION_LONG("sync", "BOOL",
1968 "Controls whether to sync data to disk"):
1969 r = parse_boolean_argument("--sync=", opts.arg, &arg_sync);
1970 if (r < 0)
1971 return r;
1972 break;
1973
1974 OPTION_LONG("verify", "BOOL",
1975 "Force signature verification on or off"): {
1976 bool b;
1977
1978 r = parse_boolean_argument("--verify=", opts.arg, &b);
1979 if (r < 0)
1980 return r;
1981
1982 arg_verify = b;
1983 break;
1984 }
1985
1986 OPTION_LONG("reboot", NULL,
1987 "Reboot after updating to newer version"):
1988 arg_reboot = true;
1989 break;
1990
1991 OPTION_LONG("offline", NULL,
1992 "Do not fetch metadata from the network"):
1993 arg_offline = true;
1994 break;
1995
1996 OPTION_COMMON_NO_PAGER:
1997 arg_pager_flags |= PAGER_DISABLE;
1998 break;
1999
2000 OPTION_COMMON_NO_LEGEND:
2001 arg_legend = false;
2002 break;
2003
2004 OPTION_COMMON_JSON:
2005 r = parse_json_argument(opts.arg, &arg_json_format_flags);
2006 if (r <= 0)
2007 return r;
2008
2009 break;
2010 }
2011
2012 if (arg_image && arg_root)
2013 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
2014
2015 if ((arg_image || arg_root) && arg_reboot)
2016 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The --reboot switch may not be combined with --root= or --image=.");
2017
2018 if (arg_definitions && arg_component)
2019 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The --definitions= and --component= switches may not be combined.");
2020
2021 *remaining_args = option_parser_get_args(&opts);
2022 return 1;
2023 }
2024
2025 static int run(int argc, char *argv[]) {
2026 int r;
2027
2028 log_setup();
2029
2030 char **args = NULL;
2031 r = parse_argv(argc, argv, &args);
2032 if (r <= 0)
2033 return r;
2034
2035 return dispatch_verb(args, NULL);
2036 }
2037
2038 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);