]>
Commit | Line | Data |
---|---|---|
61d0578b LP |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include <errno.h> | |
4 | #include <getopt.h> | |
5 | ||
6 | #include "sd-bus.h" | |
7 | ||
8 | #include "alloc-util.h" | |
9 | #include "bus-error.h" | |
10 | #include "bus-util.h" | |
11 | #include "def.h" | |
12 | #include "dirent-util.h" | |
13 | #include "fd-util.h" | |
14 | #include "fileio.h" | |
15 | #include "format-table.h" | |
16 | #include "fs-util.h" | |
17 | #include "locale-util.h" | |
18 | #include "machine-image.h" | |
19 | #include "pager.h" | |
20 | #include "parse-util.h" | |
21 | #include "path-util.h" | |
22 | #include "spawn-polkit-agent.h" | |
23 | #include "string-util.h" | |
24 | #include "strv.h" | |
25 | #include "terminal-util.h" | |
26 | #include "verbs.h" | |
27 | ||
0221d68a | 28 | static PagerFlags arg_pager_flags = 0; |
61d0578b LP |
29 | static bool arg_legend = true; |
30 | static bool arg_ask_password = true; | |
31 | static bool arg_quiet = false; | |
32 | static const char *arg_profile = "default"; | |
33 | static const char* arg_copy_mode = NULL; | |
34 | static bool arg_runtime = false; | |
35 | static bool arg_reload = true; | |
36 | static bool arg_cat = false; | |
37 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; | |
38 | static char *arg_host = NULL; | |
39 | ||
40 | static int determine_image(const char *image, bool permit_non_existing, char **ret) { | |
41 | int r; | |
42 | ||
43 | /* If the specified name is a valid image name, we pass it as-is to portabled, which will search for it in the | |
44 | * usual search directories. Otherwise we presume it's a path, and will normalize it on the client's side | |
45 | * (among other things, to make the path independent of the client's working directory) before passing it | |
46 | * over. */ | |
47 | ||
48 | if (image_name_is_valid(image)) { | |
49 | char *c; | |
50 | ||
51 | if (!arg_quiet && laccess(image, F_OK) >= 0) | |
52 | log_warning("Ambiguous invocation: current working directory contains file matching non-path argument '%s', ignoring. " | |
53 | "Prefix argument with './' to force reference to file in current working directory.", image); | |
54 | ||
55 | c = strdup(image); | |
56 | if (!c) | |
57 | return log_oom(); | |
58 | ||
59 | *ret = c; | |
60 | return 0; | |
61 | } | |
62 | ||
63 | if (arg_transport != BUS_TRANSPORT_LOCAL) { | |
64 | log_error("Operations on images by path not supported when connecting to remote systems."); | |
65 | return -EOPNOTSUPP; | |
66 | } | |
67 | ||
68 | r = chase_symlinks(image, NULL, CHASE_TRAIL_SLASH | (permit_non_existing ? CHASE_NONEXISTENT : 0), ret); | |
69 | if (r < 0) | |
70 | return log_error_errno(r, "Cannot normalize specified image path '%s': %m", image); | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
75 | static int extract_prefix(const char *path, char **ret) { | |
76 | _cleanup_free_ char *name = NULL; | |
77 | const char *bn, *underscore; | |
78 | size_t m; | |
79 | ||
80 | bn = basename(path); | |
81 | ||
82 | underscore = strchr(bn, '_'); | |
83 | if (underscore) | |
84 | m = underscore - bn; | |
85 | else { | |
86 | const char *e; | |
87 | ||
88 | e = endswith(bn, ".raw"); | |
89 | if (!e) | |
90 | e = strchr(bn, 0); | |
91 | ||
92 | m = e - bn; | |
93 | } | |
94 | ||
95 | name = strndup(bn, m); | |
96 | if (!name) | |
97 | return -ENOMEM; | |
98 | ||
99 | /* A slightly reduced version of what's permitted in unit names. With ':' and '\' are removed, as well as '_' | |
100 | * which we use as delimiter for the second part of the image string, which we ignore for now. */ | |
101 | if (!in_charset(name, DIGITS LETTERS "-.")) | |
102 | return -EINVAL; | |
103 | ||
104 | if (!filename_is_valid(name)) | |
105 | return -EINVAL; | |
106 | ||
810aa425 | 107 | *ret = TAKE_PTR(name); |
61d0578b LP |
108 | |
109 | return 0; | |
110 | } | |
111 | ||
112 | static int determine_matches(const char *image, char **l, bool allow_any, char ***ret) { | |
66c9dfdd | 113 | _cleanup_strv_free_ char **k = NULL; |
61d0578b LP |
114 | int r; |
115 | ||
116 | /* Determine the matches to apply. If the list is empty we derive the match from the image name. If the list | |
117 | * contains exactly the "-" we return a wildcard list (which is the empty list), but only if this is expressly | |
118 | * permitted. */ | |
119 | ||
120 | if (strv_isempty(l)) { | |
121 | char *prefix; | |
122 | ||
123 | r = extract_prefix(image, &prefix); | |
124 | if (r < 0) | |
125 | return log_error_errno(r, "Failed to extract prefix of image name '%s': %m", image); | |
126 | ||
127 | if (!arg_quiet) | |
128 | log_info("(Matching unit files with prefix '%s'.)", prefix); | |
129 | ||
61d0578b LP |
130 | r = strv_consume(&k, prefix); |
131 | if (r < 0) | |
132 | return log_oom(); | |
133 | ||
134 | } else if (strv_equal(l, STRV_MAKE("-"))) { | |
135 | ||
136 | if (!allow_any) { | |
137 | log_error("Refusing all unit file match."); | |
138 | return -EINVAL; | |
139 | } | |
140 | ||
141 | if (!arg_quiet) | |
142 | log_info("(Matching all unit files.)"); | |
61d0578b | 143 | } else { |
61d0578b LP |
144 | |
145 | k = strv_copy(l); | |
146 | if (!k) | |
147 | return log_oom(); | |
148 | ||
21cffed7 LP |
149 | if (!arg_quiet) { |
150 | _cleanup_free_ char *joined = NULL; | |
151 | ||
152 | joined = strv_join(k, "', '"); | |
153 | if (!joined) | |
154 | return log_oom(); | |
61d0578b | 155 | |
61d0578b | 156 | log_info("(Matching unit files with prefixes '%s'.)", joined); |
21cffed7 | 157 | } |
61d0578b LP |
158 | } |
159 | ||
66c9dfdd | 160 | *ret = TAKE_PTR(k); |
61d0578b LP |
161 | |
162 | return 0; | |
163 | } | |
164 | ||
165 | static int acquire_bus(sd_bus **bus) { | |
166 | int r; | |
167 | ||
168 | assert(bus); | |
169 | ||
170 | if (*bus) | |
171 | return 0; | |
172 | ||
173 | r = bus_connect_transport(arg_transport, arg_host, false, bus); | |
174 | if (r < 0) | |
175 | return log_error_errno(r, "Failed to connect to bus: %m"); | |
176 | ||
177 | (void) sd_bus_set_allow_interactive_authorization(*bus, arg_ask_password); | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
182 | static int maybe_reload(sd_bus **bus) { | |
183 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
184 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
185 | int r; | |
186 | ||
187 | if (!arg_reload) | |
188 | return 0; | |
189 | ||
190 | r = acquire_bus(bus); | |
191 | if (r < 0) | |
192 | return r; | |
193 | ||
194 | r = sd_bus_message_new_method_call( | |
195 | *bus, | |
196 | &m, | |
197 | "org.freedesktop.systemd1", | |
198 | "/org/freedesktop/systemd1", | |
199 | "org.freedesktop.systemd1.Manager", | |
200 | "Reload"); | |
201 | if (r < 0) | |
202 | return bus_log_create_error(r); | |
203 | ||
204 | /* Reloading the daemon may take long, hence set a longer timeout here */ | |
205 | r = sd_bus_call(*bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
206 | if (r < 0) | |
207 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | static int inspect_image(int argc, char *argv[], void *userdata) { | |
213 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
214 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
215 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
216 | _cleanup_strv_free_ char **matches = NULL; | |
217 | _cleanup_free_ char *image = NULL; | |
218 | bool nl = false, header = false; | |
219 | const void *data; | |
220 | const char *path; | |
221 | size_t sz; | |
222 | int r; | |
223 | ||
224 | r = determine_image(argv[1], false, &image); | |
225 | if (r < 0) | |
226 | return r; | |
227 | ||
228 | r = determine_matches(argv[1], argv + 2, true, &matches); | |
229 | if (r < 0) | |
230 | return r; | |
231 | ||
232 | r = acquire_bus(&bus); | |
233 | if (r < 0) | |
234 | return r; | |
235 | ||
236 | r = sd_bus_message_new_method_call( | |
237 | bus, | |
238 | &m, | |
239 | "org.freedesktop.portable1", | |
240 | "/org/freedesktop/portable1", | |
241 | "org.freedesktop.portable1.Manager", | |
242 | "GetImageMetadata"); | |
243 | if (r < 0) | |
244 | return bus_log_create_error(r); | |
245 | ||
246 | r = sd_bus_message_append(m, "s", image); | |
247 | if (r < 0) | |
248 | return bus_log_create_error(r); | |
249 | ||
250 | r = sd_bus_message_append_strv(m, matches); | |
251 | if (r < 0) | |
252 | return bus_log_create_error(r); | |
253 | ||
254 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
255 | if (r < 0) | |
256 | return log_error_errno(r, "Failed to inspect image metadata: %s", bus_error_message(&error, r)); | |
257 | ||
258 | r = sd_bus_message_read(reply, "s", &path); | |
259 | if (r < 0) | |
260 | return bus_log_parse_error(r); | |
261 | ||
262 | r = sd_bus_message_read_array(reply, 'y', &data, &sz); | |
263 | if (r < 0) | |
264 | return bus_log_parse_error(r); | |
265 | ||
0221d68a | 266 | (void) pager_open(arg_pager_flags); |
61d0578b LP |
267 | |
268 | if (arg_cat) { | |
269 | printf("%s-- OS Release: --%s\n", ansi_highlight(), ansi_normal()); | |
270 | fwrite(data, sz, 1, stdout); | |
271 | fflush(stdout); | |
272 | nl = true; | |
273 | } else { | |
8d93d7d8 | 274 | _cleanup_free_ char *pretty_portable = NULL, *pretty_os = NULL; |
61d0578b LP |
275 | |
276 | _cleanup_fclose_ FILE *f; | |
277 | ||
278 | f = fmemopen((void*) data, sz, "re"); | |
279 | if (!f) | |
280 | return log_error_errno(errno, "Failed to open /etc/os-release buffer: %m"); | |
281 | ||
282 | r = parse_env_file(f, "/etc/os-release", NEWLINE, | |
283 | "PORTABLE_PRETTY_NAME", &pretty_portable, | |
284 | "PRETTY_NAME", &pretty_os, | |
285 | NULL); | |
286 | if (r < 0) | |
287 | return log_error_errno(r, "Failed to parse /etc/os-release: %m"); | |
288 | ||
289 | printf("Image:\n\t%s\n" | |
290 | "Portable Service:\n\t%s\n" | |
291 | "Operating System:\n\t%s\n", | |
292 | path, | |
293 | strna(pretty_portable), | |
294 | strna(pretty_os)); | |
295 | } | |
296 | ||
297 | r = sd_bus_message_enter_container(reply, 'a', "{say}"); | |
298 | if (r < 0) | |
299 | return bus_log_parse_error(r); | |
300 | ||
301 | for (;;) { | |
302 | const char *name; | |
303 | ||
304 | r = sd_bus_message_enter_container(reply, 'e', "say"); | |
305 | if (r < 0) | |
306 | return bus_log_parse_error(r); | |
307 | if (r == 0) | |
308 | break; | |
309 | ||
310 | r = sd_bus_message_read(reply, "s", &name); | |
311 | if (r < 0) | |
312 | return bus_log_parse_error(r); | |
313 | ||
314 | r = sd_bus_message_read_array(reply, 'y', &data, &sz); | |
315 | if (r < 0) | |
316 | return bus_log_parse_error(r); | |
317 | ||
318 | if (arg_cat) { | |
319 | if (nl) | |
320 | fputc('\n', stdout); | |
321 | ||
322 | printf("%s-- Unit file: %s --%s\n", ansi_highlight(), name, ansi_normal()); | |
323 | fwrite(data, sz, 1, stdout); | |
324 | fflush(stdout); | |
325 | nl = true; | |
326 | } else { | |
327 | if (!header) { | |
328 | fputs("Unit files:\n", stdout); | |
329 | header = true; | |
330 | } | |
331 | ||
332 | fputc('\t', stdout); | |
333 | fputs(name, stdout); | |
334 | fputc('\n', stdout); | |
335 | } | |
336 | ||
337 | r = sd_bus_message_exit_container(reply); | |
338 | if (r < 0) | |
339 | return bus_log_parse_error(r); | |
340 | } | |
341 | ||
342 | r = sd_bus_message_exit_container(reply); | |
343 | if (r < 0) | |
344 | return bus_log_parse_error(r); | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
349 | static int print_changes(sd_bus_message *m) { | |
350 | int r; | |
351 | ||
352 | if (arg_quiet) | |
353 | return 0; | |
354 | ||
355 | r = sd_bus_message_enter_container(m, 'a', "(sss)"); | |
356 | if (r < 0) | |
357 | return bus_log_parse_error(r); | |
358 | ||
359 | for (;;) { | |
360 | const char *type, *path, *source; | |
361 | ||
362 | r = sd_bus_message_read(m, "(sss)", &type, &path, &source); | |
363 | if (r < 0) | |
364 | return bus_log_parse_error(r); | |
365 | if (r == 0) | |
366 | break; | |
367 | ||
368 | if (streq(type, "symlink")) | |
369 | log_info("Created symlink %s %s %s.", path, special_glyph(ARROW), source); | |
370 | else if (streq(type, "copy")) { | |
371 | if (isempty(source)) | |
372 | log_info("Copied %s.", path); | |
373 | else | |
374 | log_info("Copied %s %s %s.", source, special_glyph(ARROW), path); | |
375 | } else if (streq(type, "unlink")) | |
376 | log_info("Removed %s.", path); | |
377 | else if (streq(type, "write")) | |
378 | log_info("Written %s.", path); | |
379 | else if (streq(type, "mkdir")) | |
380 | log_info("Created directory %s.", path); | |
381 | else | |
382 | log_error("Unexpected change: %s/%s/%s", type, path, source); | |
383 | } | |
384 | ||
385 | r = sd_bus_message_exit_container(m); | |
386 | if (r < 0) | |
387 | return r; | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
392 | static int attach_image(int argc, char *argv[], void *userdata) { | |
393 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
394 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
395 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
396 | _cleanup_strv_free_ char **matches = NULL; | |
397 | _cleanup_free_ char *image = NULL; | |
398 | int r; | |
399 | ||
400 | r = determine_image(argv[1], false, &image); | |
401 | if (r < 0) | |
402 | return r; | |
403 | ||
404 | r = determine_matches(argv[1], argv + 2, false, &matches); | |
405 | if (r < 0) | |
406 | return r; | |
407 | ||
408 | r = acquire_bus(&bus); | |
409 | if (r < 0) | |
410 | return r; | |
411 | ||
412 | (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); | |
413 | ||
414 | r = sd_bus_message_new_method_call( | |
415 | bus, | |
416 | &m, | |
417 | "org.freedesktop.portable1", | |
418 | "/org/freedesktop/portable1", | |
419 | "org.freedesktop.portable1.Manager", | |
420 | "AttachImage"); | |
421 | if (r < 0) | |
422 | return bus_log_create_error(r); | |
423 | ||
424 | r = sd_bus_message_append(m, "s", image); | |
425 | if (r < 0) | |
426 | return bus_log_create_error(r); | |
427 | ||
428 | r = sd_bus_message_append_strv(m, matches); | |
429 | if (r < 0) | |
430 | return bus_log_create_error(r); | |
431 | ||
432 | r = sd_bus_message_append(m, "sbs", arg_profile, arg_runtime, arg_copy_mode); | |
433 | if (r < 0) | |
434 | return bus_log_create_error(r); | |
435 | ||
436 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
437 | if (r < 0) | |
438 | return log_error_errno(r, "Failed to attach image: %s", bus_error_message(&error, r)); | |
439 | ||
440 | (void) maybe_reload(&bus); | |
441 | ||
442 | print_changes(reply); | |
443 | return 0; | |
444 | } | |
445 | ||
446 | static int detach_image(int argc, char *argv[], void *userdata) { | |
447 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
448 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
449 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
450 | _cleanup_free_ char *image = NULL; | |
451 | int r; | |
452 | ||
453 | r = determine_image(argv[1], true, &image); | |
454 | if (r < 0) | |
455 | return r; | |
456 | ||
457 | r = acquire_bus(&bus); | |
458 | if (r < 0) | |
459 | return r; | |
460 | ||
461 | (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); | |
462 | ||
463 | r = sd_bus_call_method( | |
464 | bus, | |
465 | "org.freedesktop.portable1", | |
466 | "/org/freedesktop/portable1", | |
467 | "org.freedesktop.portable1.Manager", | |
468 | "DetachImage", | |
469 | &error, | |
470 | &reply, | |
471 | "sb", image, arg_runtime); | |
472 | if (r < 0) | |
473 | return log_error_errno(r, "Failed to detach image: %s", bus_error_message(&error, r)); | |
474 | ||
475 | (void) maybe_reload(&bus); | |
476 | ||
477 | print_changes(reply); | |
478 | return 0; | |
479 | } | |
480 | ||
481 | static int list_images(int argc, char *argv[], void *userdata) { | |
482 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
483 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
484 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
485 | _cleanup_(table_unrefp) Table *table = NULL; | |
486 | int r; | |
487 | ||
488 | r = acquire_bus(&bus); | |
489 | if (r < 0) | |
490 | return r; | |
491 | ||
492 | r = sd_bus_call_method( | |
493 | bus, | |
494 | "org.freedesktop.portable1", | |
495 | "/org/freedesktop/portable1", | |
496 | "org.freedesktop.portable1.Manager", | |
497 | "ListImages", | |
498 | &error, | |
499 | &reply, | |
500 | NULL); | |
501 | if (r < 0) | |
502 | return log_error_errno(r, "Failed to list images: %s", bus_error_message(&error, r)); | |
503 | ||
504 | table = table_new("NAME", "TYPE", "RO", "CRTIME", "MTIME", "USAGE", "STATE"); | |
505 | if (!table) | |
506 | return log_oom(); | |
507 | ||
508 | r = sd_bus_message_enter_container(reply, 'a', "(ssbtttso)"); | |
509 | if (r < 0) | |
510 | return bus_log_parse_error(r); | |
511 | ||
512 | for (;;) { | |
31d99bd1 | 513 | const char *name, *type, *state; |
61d0578b LP |
514 | uint64_t crtime, mtime, usage; |
515 | TableCell *cell; | |
516 | bool ro_bool; | |
517 | int ro_int; | |
518 | ||
31d99bd1 | 519 | r = sd_bus_message_read(reply, "(ssbtttso)", &name, &type, &ro_int, &crtime, &mtime, &usage, &state, NULL); |
61d0578b LP |
520 | if (r < 0) |
521 | return bus_log_parse_error(r); | |
522 | if (r == 0) | |
523 | break; | |
524 | ||
525 | r = table_add_many(table, | |
526 | TABLE_STRING, name, | |
527 | TABLE_STRING, type); | |
528 | if (r < 0) | |
529 | return log_error_errno(r, "Failed to add row to table: %m"); | |
530 | ||
531 | ro_bool = ro_int; | |
532 | r = table_add_cell(table, &cell, TABLE_BOOLEAN, &ro_bool); | |
533 | if (r < 0) | |
534 | return log_error_errno(r, "Failed to add row to table: %m"); | |
535 | ||
536 | if (ro_bool) { | |
537 | r = table_set_color(table, cell, ansi_highlight_red()); | |
538 | if (r < 0) | |
539 | return log_error_errno(r, "Failed to set table cell color: %m"); | |
540 | } | |
541 | ||
542 | r = table_add_many(table, | |
543 | TABLE_TIMESTAMP, crtime, | |
544 | TABLE_TIMESTAMP, mtime, | |
545 | TABLE_SIZE, usage); | |
546 | if (r < 0) | |
547 | return log_error_errno(r, "Failed to add row to table: %m"); | |
548 | ||
549 | r = table_add_cell(table, &cell, TABLE_STRING, state); | |
550 | if (r < 0) | |
551 | return log_error_errno(r, "Failed to add row to table: %m"); | |
552 | ||
553 | if (!streq(state, "detached")) { | |
554 | r = table_set_color(table, cell, ansi_highlight_green()); | |
555 | if (r < 0) | |
556 | return log_error_errno(r, "Failed to set table cell color: %m"); | |
557 | } | |
558 | } | |
559 | ||
560 | r = sd_bus_message_exit_container(reply); | |
561 | if (r < 0) | |
562 | return bus_log_parse_error(r); | |
563 | ||
564 | if (table_get_rows(table) > 1) { | |
565 | r = table_set_sort(table, (size_t) 0, (size_t) -1); | |
566 | if (r < 0) | |
567 | return log_error_errno(r, "Failed to sort table: %m"); | |
568 | ||
569 | table_set_header(table, arg_legend); | |
570 | ||
571 | r = table_print(table, NULL); | |
572 | if (r < 0) | |
573 | return log_error_errno(r, "Failed to show table: %m"); | |
574 | } | |
575 | ||
576 | if (arg_legend) { | |
577 | if (table_get_rows(table) > 1) | |
578 | printf("\n%zu images listed.\n", table_get_rows(table) - 1); | |
579 | else | |
580 | printf("No images.\n"); | |
581 | } | |
582 | ||
583 | return 0; | |
584 | } | |
585 | ||
586 | static int remove_image(int argc, char *argv[], void *userdata) { | |
587 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
588 | int r, i; | |
589 | ||
590 | r = acquire_bus(&bus); | |
591 | if (r < 0) | |
592 | return r; | |
593 | ||
594 | (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); | |
595 | ||
596 | for (i = 1; i < argc; i++) { | |
597 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
598 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
599 | ||
600 | r = sd_bus_message_new_method_call( | |
601 | bus, | |
602 | &m, | |
603 | "org.freedesktop.portable1", | |
604 | "/org/freedesktop/portable1", | |
605 | "org.freedesktop.portable1.Manager", | |
606 | "RemoveImage"); | |
607 | if (r < 0) | |
608 | return bus_log_create_error(r); | |
609 | ||
610 | r = sd_bus_message_append(m, "s", argv[i]); | |
611 | if (r < 0) | |
612 | return bus_log_create_error(r); | |
613 | ||
614 | /* This is a slow operation, hence turn off any method call timeouts */ | |
615 | r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL); | |
616 | if (r < 0) | |
617 | return log_error_errno(r, "Could not remove image: %s", bus_error_message(&error, r)); | |
618 | } | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
623 | static int read_only_image(int argc, char *argv[], void *userdata) { | |
624 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
625 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
626 | int b = true, r; | |
627 | ||
628 | if (argc > 2) { | |
629 | b = parse_boolean(argv[2]); | |
630 | if (b < 0) | |
631 | return log_error_errno(b, "Failed to parse boolean argument: %s", argv[2]); | |
632 | } | |
633 | ||
634 | r = acquire_bus(&bus); | |
635 | if (r < 0) | |
636 | return r; | |
637 | ||
638 | (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); | |
639 | ||
640 | r = sd_bus_call_method( | |
641 | bus, | |
642 | "org.freedesktop.portable1", | |
643 | "/org/freedesktop/portable1", | |
644 | "org.freedesktop.portable1.Manager", | |
645 | "MarkImageReadOnly", | |
646 | &error, | |
647 | NULL, | |
648 | "sb", argv[1], b); | |
649 | if (r < 0) | |
650 | return log_error_errno(r, "Could not mark image read-only: %s", bus_error_message(&error, r)); | |
651 | ||
652 | return 0; | |
653 | } | |
654 | ||
655 | static int set_limit(int argc, char *argv[], void *userdata) { | |
656 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
657 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
658 | uint64_t limit; | |
659 | int r; | |
660 | ||
661 | r = acquire_bus(&bus); | |
662 | if (r < 0) | |
663 | return r; | |
664 | ||
665 | (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); | |
666 | ||
667 | if (STR_IN_SET(argv[argc-1], "-", "none", "infinity")) | |
668 | limit = (uint64_t) -1; | |
669 | else { | |
670 | r = parse_size(argv[argc-1], 1024, &limit); | |
671 | if (r < 0) | |
672 | return log_error_errno(r, "Failed to parse size: %s", argv[argc-1]); | |
673 | } | |
674 | ||
675 | if (argc > 2) | |
676 | /* With two arguments changes the quota limit of the specified image */ | |
677 | r = sd_bus_call_method( | |
678 | bus, | |
679 | "org.freedesktop.portable1", | |
680 | "/org/freedesktop/portable1", | |
681 | "org.freedesktop.portable1.Manager", | |
682 | "SetImageLimit", | |
683 | &error, | |
684 | NULL, | |
685 | "st", argv[1], limit); | |
686 | else | |
687 | /* With one argument changes the pool quota limit */ | |
688 | r = sd_bus_call_method( | |
689 | bus, | |
690 | "org.freedesktop.portable1", | |
691 | "/org/freedesktop/portable1", | |
692 | "org.freedesktop.portable1.Manager", | |
693 | "SetPoolLimit", | |
694 | &error, | |
695 | NULL, | |
696 | "t", limit); | |
697 | ||
698 | if (r < 0) | |
699 | return log_error_errno(r, "Could not set limit: %s", bus_error_message(&error, r)); | |
700 | ||
701 | return 0; | |
702 | } | |
703 | ||
704 | static int is_image_attached(int argc, char *argv[], void *userdata) { | |
705 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
706 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
707 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
708 | _cleanup_free_ char *image = NULL; | |
709 | const char *state; | |
710 | int r; | |
711 | ||
712 | r = determine_image(argv[1], true, &image); | |
713 | if (r < 0) | |
714 | return r; | |
715 | ||
716 | r = acquire_bus(&bus); | |
717 | if (r < 0) | |
718 | return r; | |
719 | ||
720 | r = sd_bus_call_method( | |
721 | bus, | |
722 | "org.freedesktop.portable1", | |
723 | "/org/freedesktop/portable1", | |
724 | "org.freedesktop.portable1.Manager", | |
725 | "GetImageState", | |
726 | &error, | |
727 | &reply, | |
728 | "s", image); | |
729 | if (r < 0) | |
730 | return log_error_errno(r, "Failed to get image state: %s", bus_error_message(&error, r)); | |
731 | ||
732 | r = sd_bus_message_read(reply, "s", &state); | |
733 | if (r < 0) | |
734 | return r; | |
735 | ||
736 | if (!arg_quiet) | |
737 | puts(state); | |
738 | ||
739 | return streq(state, "detached"); | |
740 | } | |
741 | ||
742 | static int dump_profiles(void) { | |
743 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
744 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
745 | _cleanup_strv_free_ char **l = NULL; | |
61d0578b LP |
746 | char **i; |
747 | int r; | |
748 | ||
749 | r = acquire_bus(&bus); | |
750 | if (r < 0) | |
751 | return r; | |
752 | ||
753 | r = sd_bus_get_property_strv( | |
754 | bus, | |
755 | "org.freedesktop.portable1", | |
756 | "/org/freedesktop/portable1", | |
757 | "org.freedesktop.portable1.Manager", | |
758 | "Profiles", | |
759 | &error, | |
760 | &l); | |
761 | if (r < 0) | |
762 | return log_error_errno(r, "Failed to acquire list of profiles: %s", bus_error_message(&error, r)); | |
763 | ||
764 | if (arg_legend) | |
765 | log_info("Available unit profiles:"); | |
766 | ||
767 | STRV_FOREACH(i, l) { | |
768 | fputs(*i, stdout); | |
769 | fputc('\n', stdout); | |
770 | } | |
771 | ||
772 | return 0; | |
773 | } | |
774 | ||
775 | static int help(int argc, char *argv[], void *userdata) { | |
37ec0fdd LP |
776 | _cleanup_free_ char *link = NULL; |
777 | int r; | |
61d0578b | 778 | |
0221d68a | 779 | (void) pager_open(arg_pager_flags); |
61d0578b | 780 | |
37ec0fdd LP |
781 | r = terminal_urlify_man("portablectl", "1", &link); |
782 | if (r < 0) | |
783 | return log_oom(); | |
784 | ||
61d0578b LP |
785 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
786 | "Attach or detach portable services from the local system.\n\n" | |
787 | " -h --help Show this help\n" | |
788 | " --version Show package version\n" | |
789 | " --no-pager Do not pipe output into a pager\n" | |
790 | " --no-legend Do not show the headers and footers\n" | |
791 | " --no-ask-password Do not ask for system passwords\n" | |
792 | " -H --host=[USER@]HOST Operate on remote host\n" | |
793 | " -M --machine=CONTAINER Operate on local container\n" | |
794 | " -q --quiet Suppress informational messages\n" | |
795 | " -p --profile=PROFILE Pick security profile for portable service\n" | |
796 | " --copy=copy|auto|symlink Prefer copying or symlinks if possible\n" | |
797 | " --runtime Attach portable service until next reboot only\n" | |
798 | " --no-reload Don't reload the system and service manager\n" | |
799 | " --cat When inspecting include unit and os-release file\n" | |
800 | " contents\n\n" | |
801 | "Commands:\n" | |
802 | " list List available portable service images\n" | |
803 | " attach NAME|PATH [PREFIX...]\n" | |
804 | " Attach the specified portable service image\n" | |
805 | " detach NAME|PATH Detach the specified portable service image\n" | |
806 | " inspect NAME|PATH [PREFIX...]\n" | |
807 | " Show details of specified portable service image\n" | |
808 | " is-attached NAME|PATH Query if portable service image is attached\n" | |
809 | " read-only NAME|PATH [BOOL] Mark or unmark portable service image read-only\n" | |
810 | " remove NAME|PATH... Remove a portable service image\n" | |
811 | " set-limit [NAME|PATH] Set image or pool size limit (disk quota)\n" | |
37ec0fdd LP |
812 | "\nSee the %s for details.\n" |
813 | , program_invocation_short_name | |
814 | , link | |
815 | ); | |
61d0578b LP |
816 | |
817 | return 0; | |
818 | } | |
819 | ||
820 | static int parse_argv(int argc, char *argv[]) { | |
821 | ||
822 | enum { | |
823 | ARG_VERSION = 0x100, | |
824 | ARG_NO_PAGER, | |
825 | ARG_NO_LEGEND, | |
826 | ARG_NO_ASK_PASSWORD, | |
827 | ARG_COPY, | |
828 | ARG_RUNTIME, | |
829 | ARG_NO_RELOAD, | |
830 | ARG_CAT, | |
831 | }; | |
832 | ||
833 | static const struct option options[] = { | |
834 | { "help", no_argument, NULL, 'h' }, | |
835 | { "version", no_argument, NULL, ARG_VERSION }, | |
836 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
837 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
838 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
839 | { "host", required_argument, NULL, 'H' }, | |
840 | { "machine", required_argument, NULL, 'M' }, | |
841 | { "quiet", no_argument, NULL, 'q' }, | |
842 | { "profile", required_argument, NULL, 'p' }, | |
843 | { "copy", required_argument, NULL, ARG_COPY }, | |
844 | { "runtime", no_argument, NULL, ARG_RUNTIME }, | |
845 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, | |
846 | { "cat", no_argument, NULL, ARG_CAT }, | |
847 | {} | |
848 | }; | |
849 | ||
850 | assert(argc >= 0); | |
851 | assert(argv); | |
852 | ||
853 | for (;;) { | |
854 | int c; | |
855 | ||
856 | c = getopt_long(argc, argv, "hH:M:qp:", options, NULL); | |
857 | if (c < 0) | |
858 | break; | |
859 | ||
860 | switch (c) { | |
861 | ||
862 | case 'h': | |
37ec0fdd | 863 | return help(0, NULL, NULL); |
61d0578b LP |
864 | |
865 | case ARG_VERSION: | |
866 | return version(); | |
867 | ||
868 | case ARG_NO_PAGER: | |
0221d68a | 869 | arg_pager_flags |= PAGER_DISABLE; |
61d0578b LP |
870 | break; |
871 | ||
872 | case ARG_NO_LEGEND: | |
873 | arg_legend = false; | |
874 | break; | |
875 | ||
876 | case ARG_NO_ASK_PASSWORD: | |
877 | arg_ask_password = false; | |
878 | break; | |
879 | ||
880 | case 'H': | |
881 | arg_transport = BUS_TRANSPORT_REMOTE; | |
882 | arg_host = optarg; | |
883 | break; | |
884 | ||
885 | case 'M': | |
886 | arg_transport = BUS_TRANSPORT_MACHINE; | |
887 | arg_host = optarg; | |
888 | break; | |
889 | ||
890 | case 'q': | |
891 | arg_quiet = true; | |
892 | break; | |
893 | ||
894 | case 'p': | |
b3c66c44 LP |
895 | if (streq(optarg, "help")) |
896 | return dump_profiles(); | |
897 | ||
61d0578b LP |
898 | if (!filename_is_valid(optarg)) { |
899 | log_error("Unit profile name not valid: %s", optarg); | |
900 | return -EINVAL; | |
901 | } | |
902 | ||
61d0578b LP |
903 | arg_profile = optarg; |
904 | break; | |
905 | ||
906 | case ARG_COPY: | |
907 | if (streq(optarg, "auto")) | |
908 | arg_copy_mode = NULL; | |
909 | else if (STR_IN_SET(optarg, "copy", "symlink")) | |
910 | arg_copy_mode = optarg; | |
f524522b LP |
911 | else if (streq(optarg, "help")) { |
912 | puts("auto\n" | |
913 | "copy\n" | |
914 | "symlink"); | |
915 | return 0; | |
916 | } else { | |
61d0578b LP |
917 | log_error("Failed to parse --copy= argument: %s", optarg); |
918 | return -EINVAL; | |
919 | } | |
920 | ||
921 | break; | |
922 | ||
923 | case ARG_RUNTIME: | |
924 | arg_runtime = true; | |
925 | break; | |
926 | ||
927 | case ARG_NO_RELOAD: | |
928 | arg_reload = false; | |
929 | break; | |
930 | ||
931 | case ARG_CAT: | |
932 | arg_cat = true; | |
933 | break; | |
934 | ||
935 | case '?': | |
936 | return -EINVAL; | |
937 | ||
938 | default: | |
939 | assert_not_reached("Unhandled option"); | |
940 | } | |
941 | } | |
942 | ||
943 | return 1; | |
944 | } | |
945 | ||
946 | int main(int argc, char *argv[]) { | |
947 | ||
948 | static const Verb verbs[] = { | |
949 | { "help", VERB_ANY, VERB_ANY, 0, help }, | |
950 | { "list", VERB_ANY, 1, VERB_DEFAULT, list_images }, | |
951 | { "attach", 2, VERB_ANY, 0, attach_image }, | |
952 | { "detach", 2, 2, 0, detach_image }, | |
953 | { "inspect", 2, VERB_ANY, 0, inspect_image }, | |
954 | { "is-attached", 2, 2, 0, is_image_attached }, | |
955 | { "read-only", 2, 3, 0, read_only_image }, | |
956 | { "remove", 2, VERB_ANY, 0, remove_image }, | |
957 | { "set-limit", 3, 3, 0, set_limit }, | |
958 | {} | |
959 | }; | |
960 | ||
961 | int r; | |
962 | ||
963 | log_parse_environment(); | |
964 | log_open(); | |
965 | ||
966 | r = parse_argv(argc, argv); | |
967 | if (r <= 0) | |
968 | goto finish; | |
969 | ||
970 | r = dispatch_verb(argc, argv, verbs, NULL); | |
971 | ||
972 | finish: | |
973 | pager_close(); | |
974 | ||
975 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
976 | } |