1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <netinet/in.h>
12 #include <sys/mount.h>
13 #include <sys/socket.h>
18 #include "alloc-util.h"
19 #include "bus-common-errors.h"
20 #include "bus-error.h"
21 #include "bus-unit-procs.h"
22 #include "bus-unit-util.h"
24 #include "bus-wait-for-jobs.h"
25 #include "cgroup-show.h"
26 #include "cgroup-util.h"
31 #include "format-table.h"
32 #include "hostname-util.h"
33 #include "import-util.h"
34 #include "locale-util.h"
36 #include "logs-show.h"
38 #include "main-func.h"
40 #include "nulstr-util.h"
42 #include "parse-util.h"
43 #include "path-util.h"
44 #include "pretty-print.h"
45 #include "process-util.h"
47 #include "rlimit-util.h"
49 #include "signal-util.h"
50 #include "sort-util.h"
51 #include "spawn-polkit-agent.h"
52 #include "stdio-util.h"
53 #include "string-table.h"
55 #include "terminal-util.h"
56 #include "unit-name.h"
60 #define ALL_IP_ADDRESSES -1
62 static char **arg_property
= NULL
;
63 static bool arg_all
= false;
64 static bool arg_value
= false;
65 static bool arg_full
= false;
66 static PagerFlags arg_pager_flags
= 0;
67 static bool arg_legend
= true;
68 static const char *arg_kill_who
= NULL
;
69 static int arg_signal
= SIGTERM
;
70 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
71 static const char *arg_host
= NULL
;
72 static bool arg_read_only
= false;
73 static bool arg_mkdir
= false;
74 static bool arg_quiet
= false;
75 static bool arg_ask_password
= true;
76 static unsigned arg_lines
= 10;
77 static OutputMode arg_output
= OUTPUT_SHORT
;
78 static bool arg_force
= false;
79 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
80 static const char* arg_format
= NULL
;
81 static const char *arg_uid
= NULL
;
82 static char **arg_setenv
= NULL
;
83 static int arg_addrs
= 1;
85 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
86 STATIC_DESTRUCTOR_REGISTER(arg_setenv
, strv_freep
);
88 static OutputFlags
get_output_flags(void) {
90 arg_all
* OUTPUT_SHOW_ALL
|
91 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
92 colors_enabled() * OUTPUT_COLOR
|
93 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
96 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
97 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
98 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
99 const char *k
, *v
, *iter
, **query_res
= NULL
;
100 size_t count
= 0, awaited_args
= 0;
108 NULSTR_FOREACH(iter
, query
)
110 query_res
= newa0(const char *, awaited_args
);
112 r
= sd_bus_call_method(
114 "org.freedesktop.machine1",
115 "/org/freedesktop/machine1",
116 "org.freedesktop.machine1.Manager",
121 return log_debug_errno(r
, "Failed to call '%s()': %s", method
, bus_error_message(&error
, r
));
123 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
125 return bus_log_parse_error(r
);
127 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
129 NULSTR_FOREACH(iter
, query
) {
130 if (streq(k
, iter
)) {
131 query_res
[count
] = v
;
138 return bus_log_parse_error(r
);
140 r
= sd_bus_message_exit_container(reply
);
142 return bus_log_parse_error(r
);
145 for (count
= 0; count
< awaited_args
; count
++) {
148 out
= va_arg(ap
, char **);
150 if (query_res
[count
]) {
151 val
= strdup(query_res
[count
]);
164 static int call_get_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
, int n_addr
, char **ret
) {
166 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
167 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
168 _cleanup_free_
char *addresses
= NULL
;
169 bool truncate
= false;
178 r
= sd_bus_call_method(bus
,
179 "org.freedesktop.machine1",
180 "/org/freedesktop/machine1",
181 "org.freedesktop.machine1.Manager",
182 "GetMachineAddresses",
187 return log_debug_errno(r
, "Could not get addresses: %s", bus_error_message(&error
, r
));
189 addresses
= strdup(prefix
);
194 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
196 return bus_log_parse_error(r
);
198 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
202 char buf_ifi
[DECIMAL_STR_MAX(int) + 2], buffer
[MAX(INET6_ADDRSTRLEN
, INET_ADDRSTRLEN
)];
204 r
= sd_bus_message_read(reply
, "i", &family
);
206 return bus_log_parse_error(r
);
208 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
210 return bus_log_parse_error(r
);
213 if (family
== AF_INET6
&& ifi
> 0)
214 xsprintf(buf_ifi
, "%%%i", ifi
);
218 if (!strextend(&addresses
, prefix
, inet_ntop(family
, a
, buffer
, sizeof(buffer
)), buf_ifi
, NULL
))
223 r
= sd_bus_message_exit_container(reply
);
225 return bus_log_parse_error(r
);
235 return bus_log_parse_error(r
);
237 r
= sd_bus_message_exit_container(reply
);
239 return bus_log_parse_error(r
);
243 if (!strextend(&addresses
, special_glyph(SPECIAL_GLYPH_ELLIPSIS
), NULL
))
248 *ret
= TAKE_PTR(addresses
);
252 static int show_table(Table
*table
, const char *word
) {
258 if (table_get_rows(table
) > 1) {
259 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
261 return log_error_errno(r
, "Failed to sort table: %m");
263 table_set_header(table
, arg_legend
);
265 if (OUTPUT_MODE_IS_JSON(arg_output
))
266 r
= table_print_json(table
, NULL
, output_mode_to_json_format_flags(arg_output
) | JSON_FORMAT_COLOR_AUTO
);
268 r
= table_print(table
, NULL
);
270 return log_error_errno(r
, "Failed to show table: %m");
274 if (table_get_rows(table
) > 1)
275 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
277 printf("No %s.\n", word
);
283 static int list_machines(int argc
, char *argv
[], void *userdata
) {
285 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
286 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
287 _cleanup_(table_unrefp
) Table
*table
= NULL
;
288 sd_bus
*bus
= userdata
;
293 (void) pager_open(arg_pager_flags
);
295 r
= sd_bus_call_method(bus
,
296 "org.freedesktop.machine1",
297 "/org/freedesktop/machine1",
298 "org.freedesktop.machine1.Manager",
304 return log_error_errno(r
, "Could not get machines: %s", bus_error_message(&error
, r
));
306 table
= table_new("machine", "class", "service", "os", "version", "addresses");
310 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
312 return bus_log_parse_error(r
);
315 _cleanup_free_
char *os
= NULL
, *version_id
= NULL
, *addresses
= NULL
;
316 const char *name
, *class, *service
;
318 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, NULL
);
320 return bus_log_parse_error(r
);
324 if (name
[0] == '.' && !arg_all
)
327 (void) call_get_os_release(
329 "GetMachineOSRelease",
336 (void) call_get_addresses(
345 r
= table_add_many(table
,
348 TABLE_STRING
, empty_to_dash(service
),
349 TABLE_STRING
, empty_to_dash(os
),
350 TABLE_STRING
, empty_to_dash(version_id
),
351 TABLE_STRING
, empty_to_dash(addresses
));
353 return log_error_errno(r
, "Failed to add table row: %m");
356 r
= sd_bus_message_exit_container(reply
);
358 return bus_log_parse_error(r
);
360 return show_table(table
, "machines");
363 static int list_images(int argc
, char *argv
[], void *userdata
) {
365 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
366 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
367 _cleanup_(table_unrefp
) Table
*table
= NULL
;
368 sd_bus
*bus
= userdata
;
373 (void) pager_open(arg_pager_flags
);
375 r
= sd_bus_call_method(bus
,
376 "org.freedesktop.machine1",
377 "/org/freedesktop/machine1",
378 "org.freedesktop.machine1.Manager",
384 return log_error_errno(r
, "Could not get images: %s", bus_error_message(&error
, r
));
386 table
= table_new("name", "type", "ro", "usage", "created", "modified");
390 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100);
392 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
394 return bus_log_parse_error(r
);
397 uint64_t crtime
, mtime
, size
;
398 const char *name
, *type
;
403 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, NULL
);
405 return bus_log_parse_error(r
);
409 if (name
[0] == '.' && !arg_all
)
412 r
= table_add_many(table
,
416 return log_error_errno(r
, "Failed to add table row: %m");
419 r
= table_add_cell(table
, &cell
, TABLE_BOOLEAN
, &ro_bool
);
421 return log_error_errno(r
, "Failed to add table cell: %m");
424 r
= table_set_color(table
, cell
, ansi_highlight_red());
426 return log_error_errno(r
, "Failed to set table cell color: %m");
429 r
= table_add_many(table
,
431 TABLE_TIMESTAMP
, crtime
,
432 TABLE_TIMESTAMP
, mtime
);
434 return log_error_errno(r
, "Failed to add table row: %m");
437 r
= sd_bus_message_exit_container(reply
);
439 return bus_log_parse_error(r
);
441 return show_table(table
, "images");
444 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
445 _cleanup_free_
char *cgroup
= NULL
;
446 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
453 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
466 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
469 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
472 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
474 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
477 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
479 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
484 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
485 _cleanup_free_
char *pretty
= NULL
;
492 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
497 printf("%s%s\n", prefix
, pretty
);
502 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
503 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
504 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
511 r
= sd_bus_call_method(bus
,
512 "org.freedesktop.machine1",
513 "/org/freedesktop/machine1",
514 "org.freedesktop.machine1.Manager",
515 "GetMachineUIDShift",
520 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
522 r
= sd_bus_message_read(reply
, "u", &shift
);
526 if (shift
== 0) /* Don't show trivial mappings */
529 printf(" UID Shift: %" PRIu32
"\n", shift
);
533 typedef struct MachineStatusInfo
{
539 const char *root_directory
;
541 struct dual_timestamp timestamp
;
546 static void machine_status_info_clear(MachineStatusInfo
*info
) {
553 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
554 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
555 char since2
[FORMAT_TIMESTAMP_MAX
];
556 _cleanup_free_
char *addresses
= NULL
;
563 fputs(strna(i
->name
), stdout
);
565 if (!sd_id128_is_null(i
->id
))
566 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
570 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
->timestamp
.realtime
);
571 s2
= format_timestamp(since2
, sizeof(since2
), i
->timestamp
.realtime
);
574 printf("\t Since: %s; %s\n", s2
, s1
);
576 printf("\t Since: %s\n", s2
);
579 _cleanup_free_
char *t
= NULL
;
581 printf("\t Leader: %u", (unsigned) i
->leader
);
583 get_process_comm(i
->leader
, &t
);
591 printf("\t Service: %s", i
->service
);
594 printf("; class %s", i
->class);
598 printf("\t Class: %s\n", i
->class);
600 if (i
->root_directory
)
601 printf("\t Root: %s\n", i
->root_directory
);
603 if (i
->n_netif
> 0) {
606 fputs("\t Iface:", stdout
);
608 for (c
= 0; c
< i
->n_netif
; c
++) {
609 char name
[IF_NAMESIZE
+1] = "";
611 if (if_indextoname(i
->netif
[c
], name
)) {
620 printf(" %i", i
->netif
[c
]);
626 if (call_get_addresses(bus
, i
->name
, ifi
,
627 "\t Address: ", "\n\t ", ALL_IP_ADDRESSES
,
629 fputs(addresses
, stdout
);
633 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
635 print_uid_shift(bus
, i
->name
);
638 printf("\t Unit: %s\n", i
->unit
);
639 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
641 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
643 show_journal_by_unit(
648 i
->timestamp
.monotonic
,
651 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
652 SD_JOURNAL_LOCAL_ONLY
,
658 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
659 MachineStatusInfo
*i
= userdata
;
664 assert_cc(sizeof(int32_t) == sizeof(int));
665 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
671 i
->n_netif
= l
/ sizeof(int32_t);
672 i
->netif
= memdup(v
, l
);
679 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
681 static const struct bus_properties_map map
[] = {
682 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
683 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
684 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
685 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
686 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
687 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
688 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
689 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
690 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
691 { "NetworkInterfaces", "ai", map_netif
, 0 },
695 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
696 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
697 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
705 r
= bus_map_all_properties(bus
,
706 "org.freedesktop.machine1",
714 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
720 print_machine_status_info(bus
, &info
);
725 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
737 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_value
, arg_all
, NULL
);
739 log_error_errno(r
, "Could not get properties: %m");
744 static int show_machine(int argc
, char *argv
[], void *userdata
) {
746 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
747 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
748 bool properties
, new_line
= false;
749 sd_bus
*bus
= userdata
;
754 properties
= !strstr(argv
[0], "status");
756 (void) pager_open(arg_pager_flags
);
758 if (properties
&& argc
<= 1) {
760 /* If no argument is specified, inspect the manager
762 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
767 for (i
= 1; i
< argc
; i
++) {
768 const char *path
= NULL
;
770 r
= sd_bus_call_method(bus
,
771 "org.freedesktop.machine1",
772 "/org/freedesktop/machine1",
773 "org.freedesktop.machine1.Manager",
779 return log_error_errno(r
, "Could not get path to machine: %s", bus_error_message(&error
, -r
));
781 r
= sd_bus_message_read(reply
, "o", &path
);
783 return bus_log_parse_error(r
);
786 r
= show_machine_properties(bus
, path
, &new_line
);
788 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
794 static int print_image_hostname(sd_bus
*bus
, const char *name
) {
795 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
799 r
= sd_bus_call_method(
801 "org.freedesktop.machine1",
802 "/org/freedesktop/machine1",
803 "org.freedesktop.machine1.Manager",
805 NULL
, &reply
, "s", name
);
809 r
= sd_bus_message_read(reply
, "s", &hn
);
814 printf("\tHostname: %s\n", hn
);
819 static int print_image_machine_id(sd_bus
*bus
, const char *name
) {
820 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
821 sd_id128_t id
= SD_ID128_NULL
;
826 r
= sd_bus_call_method(
828 "org.freedesktop.machine1",
829 "/org/freedesktop/machine1",
830 "org.freedesktop.machine1.Manager",
832 NULL
, &reply
, "s", name
);
836 r
= sd_bus_message_read_array(reply
, 'y', &p
, &size
);
840 if (size
== sizeof(sd_id128_t
))
841 memcpy(&id
, p
, size
);
843 if (!sd_id128_is_null(id
))
844 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(id
));
849 static int print_image_machine_info(sd_bus
*bus
, const char *name
) {
850 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
853 r
= sd_bus_call_method(
855 "org.freedesktop.machine1",
856 "/org/freedesktop/machine1",
857 "org.freedesktop.machine1.Manager",
858 "GetImageMachineInfo",
859 NULL
, &reply
, "s", name
);
863 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
870 r
= sd_bus_message_read(reply
, "{ss}", &p
, &q
);
876 if (streq(p
, "DEPLOYMENT"))
877 printf(" Deployment: %s\n", q
);
880 r
= sd_bus_message_exit_container(reply
);
887 typedef struct ImageStatusInfo
{
896 uint64_t usage_exclusive
;
897 uint64_t limit_exclusive
;
900 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
901 char ts_relative
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
902 char ts_absolute
[FORMAT_TIMESTAMP_MAX
];
903 char bs
[FORMAT_BYTES_MAX
];
904 char bs_exclusive
[FORMAT_BYTES_MAX
];
905 const char *s1
, *s2
, *s3
, *s4
;
911 fputs(i
->name
, stdout
);
916 printf("\t Type: %s\n", i
->type
);
919 printf("\t Path: %s\n", i
->path
);
921 (void) print_image_hostname(bus
, i
->name
);
922 (void) print_image_machine_id(bus
, i
->name
);
923 (void) print_image_machine_info(bus
, i
->name
);
925 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
927 printf("\t RO: %s%s%s\n",
928 i
->read_only
? ansi_highlight_red() : "",
929 i
->read_only
? "read-only" : "writable",
930 i
->read_only
? ansi_normal() : "");
932 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->crtime
);
933 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->crtime
);
935 printf("\t Created: %s; %s\n", s2
, s1
);
937 printf("\t Created: %s\n", s2
);
939 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->mtime
);
940 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->mtime
);
942 printf("\tModified: %s; %s\n", s2
, s1
);
944 printf("\tModified: %s\n", s2
);
946 s3
= format_bytes(bs
, sizeof(bs
), i
->usage
);
947 s4
= i
->usage_exclusive
!= i
->usage
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->usage_exclusive
) : NULL
;
949 printf("\t Usage: %s (exclusive: %s)\n", s3
, s4
);
951 printf("\t Usage: %s\n", s3
);
953 s3
= format_bytes(bs
, sizeof(bs
), i
->limit
);
954 s4
= i
->limit_exclusive
!= i
->limit
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->limit_exclusive
) : NULL
;
956 printf("\t Limit: %s (exclusive: %s)\n", s3
, s4
);
958 printf("\t Limit: %s\n", s3
);
961 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
963 static const struct bus_properties_map map
[] = {
964 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
965 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
966 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
967 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
968 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
969 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
970 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
971 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
972 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
973 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
977 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
978 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
979 ImageStatusInfo info
= {};
986 r
= bus_map_all_properties(bus
,
987 "org.freedesktop.machine1",
990 BUS_MAP_BOOLEAN_AS_BOOL
,
995 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1001 print_image_status_info(bus
, &info
);
1006 typedef struct PoolStatusInfo
{
1012 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
1013 char bs
[FORMAT_BYTES_MAX
], *s
;
1016 printf("\t Path: %s\n", i
->path
);
1018 s
= format_bytes(bs
, sizeof(bs
), i
->usage
);
1020 printf("\t Usage: %s\n", s
);
1022 s
= format_bytes(bs
, sizeof(bs
), i
->limit
);
1024 printf("\t Limit: %s\n", s
);
1027 static int show_pool_info(sd_bus
*bus
) {
1029 static const struct bus_properties_map map
[] = {
1030 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
1031 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
1032 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
1036 PoolStatusInfo info
= {
1037 .usage
= (uint64_t) -1,
1038 .limit
= (uint64_t) -1,
1041 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1042 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1047 r
= bus_map_all_properties(bus
,
1048 "org.freedesktop.machine1",
1049 "/org/freedesktop/machine1",
1056 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1058 print_pool_status_info(bus
, &info
);
1063 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
1075 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_value
, arg_all
, NULL
);
1077 log_error_errno(r
, "Could not get properties: %m");
1082 static int show_image(int argc
, char *argv
[], void *userdata
) {
1084 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1085 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1086 bool properties
, new_line
= false;
1087 sd_bus
*bus
= userdata
;
1092 properties
= !strstr(argv
[0], "status");
1094 (void) pager_open(arg_pager_flags
);
1098 /* If no argument is specified, inspect the manager
1102 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1104 r
= show_pool_info(bus
);
1109 for (i
= 1; i
< argc
; i
++) {
1110 const char *path
= NULL
;
1112 r
= sd_bus_call_method(
1114 "org.freedesktop.machine1",
1115 "/org/freedesktop/machine1",
1116 "org.freedesktop.machine1.Manager",
1122 return log_error_errno(r
, "Could not get path to image: %s", bus_error_message(&error
, -r
));
1124 r
= sd_bus_message_read(reply
, "o", &path
);
1126 return bus_log_parse_error(r
);
1129 r
= show_image_properties(bus
, path
, &new_line
);
1131 r
= show_image_info(bus
, path
, &new_line
);
1137 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1138 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1139 sd_bus
*bus
= userdata
;
1144 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1147 arg_kill_who
= "all";
1149 for (i
= 1; i
< argc
; i
++) {
1150 r
= sd_bus_call_method(
1152 "org.freedesktop.machine1",
1153 "/org/freedesktop/machine1",
1154 "org.freedesktop.machine1.Manager",
1158 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1160 return log_error_errno(r
, "Could not kill machine: %s", bus_error_message(&error
, -r
));
1166 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1167 arg_kill_who
= "leader";
1168 arg_signal
= SIGINT
; /* sysvinit + systemd */
1170 return kill_machine(argc
, argv
, userdata
);
1173 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1174 arg_kill_who
= "leader";
1175 arg_signal
= SIGRTMIN
+4; /* only systemd */
1177 return kill_machine(argc
, argv
, userdata
);
1180 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1181 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1182 sd_bus
*bus
= userdata
;
1187 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1189 for (i
= 1; i
< argc
; i
++) {
1190 r
= sd_bus_call_method(
1192 "org.freedesktop.machine1",
1193 "/org/freedesktop/machine1",
1194 "org.freedesktop.machine1.Manager",
1200 return log_error_errno(r
, "Could not terminate machine: %s", bus_error_message(&error
, -r
));
1206 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1207 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1208 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1209 _cleanup_free_
char *abs_host_path
= NULL
;
1210 char *dest
, *host_path
, *container_path
;
1211 sd_bus
*bus
= userdata
;
1217 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1219 copy_from
= streq(argv
[0], "copy-from");
1220 dest
= argv
[3] ?: argv
[2];
1221 host_path
= copy_from
? dest
: argv
[2];
1222 container_path
= copy_from
? argv
[2] : dest
;
1224 if (!path_is_absolute(host_path
)) {
1225 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1227 return log_error_errno(r
, "Failed to make path absolute: %m");
1229 host_path
= abs_host_path
;
1232 r
= sd_bus_message_new_method_call(
1235 "org.freedesktop.machine1",
1236 "/org/freedesktop/machine1",
1237 "org.freedesktop.machine1.Manager",
1238 copy_from
? "CopyFromMachine" : "CopyToMachine");
1240 return bus_log_create_error(r
);
1242 r
= sd_bus_message_append(
1246 copy_from
? container_path
: host_path
,
1247 copy_from
? host_path
: container_path
);
1249 return bus_log_create_error(r
);
1251 /* This is a slow operation, hence turn off any method call timeouts */
1252 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1254 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1259 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1260 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1261 sd_bus
*bus
= userdata
;
1266 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1268 r
= sd_bus_call_method(
1270 "org.freedesktop.machine1",
1271 "/org/freedesktop/machine1",
1272 "org.freedesktop.machine1.Manager",
1283 return log_error_errno(r
, "Failed to bind mount: %s", bus_error_message(&error
, -r
));
1288 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1289 PTYForward
** forward
= (PTYForward
**) userdata
;
1296 /* If the forwarder is already initialized, tell it to
1297 * exit on the next vhangup(), so that we still flush
1298 * out what might be queued and exit then. */
1300 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1304 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1307 /* On error, or when the forwarder is not initialized yet, quit immediately */
1308 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1312 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1318 assert(master
>= 0);
1321 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1324 if (streq(name
, ".host"))
1325 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1327 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1330 (void) sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1331 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1333 r
= pty_forward_new(event
, master
, flags
, forward
);
1335 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1337 r
= sd_event_loop(event
);
1339 return log_error_errno(r
, "Failed to run event loop: %m");
1341 pty_forward_get_last_char(*forward
, &last_char
);
1344 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1345 pty_forward_get_ignore_vhangup(*forward
) == 0;
1347 *forward
= pty_forward_free(*forward
);
1349 if (last_char
!= '\n')
1350 fputc('\n', stdout
);
1354 log_info("Machine %s terminated.", name
);
1355 else if (streq(name
, ".host"))
1356 log_info("Connection to the local host terminated.");
1358 log_info("Connection to machine %s terminated.", name
);
1364 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1366 * Whatever is specified in the spec takes priority over global arguments.
1369 const char *_machine
= NULL
;
1374 at
= strchr(spec
, '@');
1377 /* Do the same as ssh and refuse "@host". */
1381 _uid
= strndup(spec
, at
- spec
);
1388 if (arg_uid
&& !_uid
) {
1389 _uid
= strdup(arg_uid
);
1395 *machine
= isempty(_machine
) ? ".host" : _machine
;
1399 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1400 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1401 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1402 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1403 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1404 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1406 sd_bus
*bus
= userdata
;
1407 const char *match
, *machine
;
1411 if (!strv_isempty(arg_setenv
) || arg_uid
) {
1412 log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1416 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1417 log_error("Login only supported on local machines.");
1421 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1423 r
= sd_event_default(&event
);
1425 return log_error_errno(r
, "Failed to get event loop: %m");
1427 r
= sd_bus_attach_event(bus
, event
, 0);
1429 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1431 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1433 match
= strjoina("type='signal',"
1434 "sender='org.freedesktop.machine1',"
1435 "path='/org/freedesktop/machine1',",
1436 "interface='org.freedesktop.machine1.Manager',"
1437 "member='MachineRemoved',"
1438 "arg0='", machine
, "'");
1440 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1442 return log_error_errno(r
, "Failed to request machine removal match: %m");
1444 r
= sd_bus_call_method(
1446 "org.freedesktop.machine1",
1447 "/org/freedesktop/machine1",
1448 "org.freedesktop.machine1.Manager",
1454 return log_error_errno(r
, "Failed to get login PTY: %s", bus_error_message(&error
, -r
));
1456 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1458 return bus_log_parse_error(r
);
1460 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1463 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1464 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1465 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1466 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1467 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1468 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1470 sd_bus
*bus
= userdata
;
1471 const char *match
, *machine
, *path
;
1472 _cleanup_free_
char *uid
= NULL
;
1476 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
)) {
1477 log_error("Shell only supported on local machines.");
1481 /* Pass $TERM to shell session, if not explicitly specified. */
1482 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1485 t
= strv_find_prefix(environ
, "TERM=");
1487 if (strv_extend(&arg_setenv
, t
) < 0)
1492 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1494 r
= sd_event_default(&event
);
1496 return log_error_errno(r
, "Failed to get event loop: %m");
1498 r
= sd_bus_attach_event(bus
, event
, 0);
1500 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1502 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1504 return log_error_errno(r
, "Failed to parse machine specification: %m");
1506 match
= strjoina("type='signal',"
1507 "sender='org.freedesktop.machine1',"
1508 "path='/org/freedesktop/machine1',",
1509 "interface='org.freedesktop.machine1.Manager',"
1510 "member='MachineRemoved',"
1511 "arg0='", machine
, "'");
1513 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1515 return log_error_errno(r
, "Failed to request machine removal match: %m");
1517 r
= sd_bus_message_new_method_call(
1520 "org.freedesktop.machine1",
1521 "/org/freedesktop/machine1",
1522 "org.freedesktop.machine1.Manager",
1523 "OpenMachineShell");
1525 return bus_log_create_error(r
);
1527 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1529 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1531 return bus_log_create_error(r
);
1533 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1535 return bus_log_create_error(r
);
1537 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1539 return bus_log_create_error(r
);
1541 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1543 return log_error_errno(r
, "Failed to get shell PTY: %s", bus_error_message(&error
, -r
));
1545 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1547 return bus_log_parse_error(r
);
1549 return process_forward(event
, &forward
, master
, 0, machine
);
1552 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1553 sd_bus
*bus
= userdata
;
1558 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1560 for (i
= 1; i
< argc
; i
++) {
1561 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1562 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1564 r
= sd_bus_message_new_method_call(
1567 "org.freedesktop.machine1",
1568 "/org/freedesktop/machine1",
1569 "org.freedesktop.machine1.Manager",
1572 return bus_log_create_error(r
);
1574 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1576 return bus_log_create_error(r
);
1578 /* This is a slow operation, hence turn off any method call timeouts */
1579 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1581 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1587 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1588 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1589 sd_bus
*bus
= userdata
;
1594 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1596 r
= sd_bus_call_method(
1598 "org.freedesktop.machine1",
1599 "/org/freedesktop/machine1",
1600 "org.freedesktop.machine1.Manager",
1604 "ss", argv
[1], argv
[2]);
1606 return log_error_errno(r
, "Could not rename image: %s", bus_error_message(&error
, -r
));
1611 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1612 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1613 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1614 sd_bus
*bus
= userdata
;
1619 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1621 r
= sd_bus_message_new_method_call(
1624 "org.freedesktop.machine1",
1625 "/org/freedesktop/machine1",
1626 "org.freedesktop.machine1.Manager",
1629 return bus_log_create_error(r
);
1631 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1633 return bus_log_create_error(r
);
1635 /* This is a slow operation, hence turn off any method call timeouts */
1636 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1638 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1643 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1644 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1645 sd_bus
*bus
= userdata
;
1651 b
= parse_boolean(argv
[2]);
1653 log_error("Failed to parse boolean argument: %s", argv
[2]);
1658 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1660 r
= sd_bus_call_method(
1662 "org.freedesktop.machine1",
1663 "/org/freedesktop/machine1",
1664 "org.freedesktop.machine1.Manager",
1665 "MarkImageReadOnly",
1670 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, -r
));
1675 static int image_exists(sd_bus
*bus
, const char *name
) {
1676 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1682 r
= sd_bus_call_method(
1684 "org.freedesktop.machine1",
1685 "/org/freedesktop/machine1",
1686 "org.freedesktop.machine1.Manager",
1692 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1695 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, -r
));
1701 static int make_service_name(const char *name
, char **ret
) {
1707 if (!machine_name_is_valid(name
))
1708 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1709 "Invalid machine name %s.", name
);
1711 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1713 return log_error_errno(r
, "Failed to build unit name: %m");
1718 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1719 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1720 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1721 sd_bus
*bus
= userdata
;
1726 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1728 r
= bus_wait_for_jobs_new(bus
, &w
);
1732 for (i
= 1; i
< argc
; i
++) {
1733 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1734 _cleanup_free_
char *unit
= NULL
;
1737 r
= make_service_name(argv
[i
], &unit
);
1741 r
= image_exists(bus
, argv
[i
]);
1745 log_error("Machine image '%s' does not exist.", argv
[i
]);
1749 r
= sd_bus_call_method(
1751 "org.freedesktop.systemd1",
1752 "/org/freedesktop/systemd1",
1753 "org.freedesktop.systemd1.Manager",
1757 "ss", unit
, "fail");
1759 return log_error_errno(r
, "Failed to start unit: %s", bus_error_message(&error
, -r
));
1761 r
= sd_bus_message_read(reply
, "o", &object
);
1763 return bus_log_parse_error(r
);
1765 r
= bus_wait_for_jobs_add(w
, object
);
1770 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1777 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1778 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1779 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1780 UnitFileChange
*changes
= NULL
;
1781 size_t n_changes
= 0;
1782 const char *method
= NULL
;
1783 sd_bus
*bus
= userdata
;
1788 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1790 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1792 r
= sd_bus_message_new_method_call(
1795 "org.freedesktop.systemd1",
1796 "/org/freedesktop/systemd1",
1797 "org.freedesktop.systemd1.Manager",
1800 return bus_log_create_error(r
);
1802 r
= sd_bus_message_open_container(m
, 'a', "s");
1804 return bus_log_create_error(r
);
1806 for (i
= 1; i
< argc
; i
++) {
1807 _cleanup_free_
char *unit
= NULL
;
1809 r
= make_service_name(argv
[i
], &unit
);
1813 r
= image_exists(bus
, argv
[i
]);
1817 log_error("Machine image '%s' does not exist.", argv
[i
]);
1821 r
= sd_bus_message_append(m
, "s", unit
);
1823 return bus_log_create_error(r
);
1826 r
= sd_bus_message_close_container(m
);
1828 return bus_log_create_error(r
);
1830 if (streq(argv
[0], "enable"))
1831 r
= sd_bus_message_append(m
, "bb", false, false);
1833 r
= sd_bus_message_append(m
, "b", false);
1835 return bus_log_create_error(r
);
1837 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1839 return log_error_errno(r
, "Failed to enable or disable unit: %s", bus_error_message(&error
, -r
));
1841 if (streq(argv
[0], "enable")) {
1842 r
= sd_bus_message_read(reply
, "b", NULL
);
1844 return bus_log_parse_error(r
);
1847 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1851 r
= sd_bus_call_method(
1853 "org.freedesktop.systemd1",
1854 "/org/freedesktop/systemd1",
1855 "org.freedesktop.systemd1.Manager",
1861 log_error("Failed to reload daemon: %s", bus_error_message(&error
, -r
));
1868 unit_file_changes_free(changes
, n_changes
);
1873 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1874 const char **our_path
= userdata
, *line
;
1881 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1883 bus_log_parse_error(r
);
1887 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1890 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1893 log_full(priority
, "%s", line
);
1897 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1898 const char **our_path
= userdata
, *path
, *result
;
1905 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1907 bus_log_parse_error(r
);
1911 if (!streq_ptr(*our_path
, path
))
1914 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1918 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1923 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1925 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1929 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1930 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1931 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1932 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1933 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1934 const char *path
= NULL
;
1941 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1943 r
= sd_event_default(&event
);
1945 return log_error_errno(r
, "Failed to get event loop: %m");
1947 r
= sd_bus_attach_event(bus
, event
, 0);
1949 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1951 r
= sd_bus_match_signal_async(
1954 "org.freedesktop.import1",
1955 "/org/freedesktop/import1",
1956 "org.freedesktop.import1.Manager",
1958 match_transfer_removed
, NULL
, &path
);
1960 return log_error_errno(r
, "Failed to request match: %m");
1962 r
= sd_bus_match_signal_async(
1965 "org.freedesktop.import1",
1967 "org.freedesktop.import1.Transfer",
1969 match_log_message
, NULL
, &path
);
1971 return log_error_errno(r
, "Failed to request match: %m");
1973 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1975 return log_error_errno(r
, "Failed to transfer image: %s", bus_error_message(&error
, -r
));
1977 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1979 return bus_log_parse_error(r
);
1981 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1984 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
1986 (void) sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1987 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1989 r
= sd_event_loop(event
);
1991 return log_error_errno(r
, "Failed to run event loop: %m");
1996 static int import_tar(int argc
, char *argv
[], void *userdata
) {
1997 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1998 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
1999 const char *local
= NULL
, *path
= NULL
;
2000 _cleanup_close_
int fd
= -1;
2001 sd_bus
*bus
= userdata
;
2007 path
= empty_or_dash_to_null(argv
[1]);
2010 local
= empty_or_dash_to_null(argv
[2]);
2012 r
= path_extract_filename(path
, &fn
);
2014 return log_error_errno(r
, "Cannot extract container name from filename: %m");
2019 log_error("Need either path or local name.");
2023 r
= tar_strip_suffixes(local
, &ll
);
2029 if (!machine_name_is_valid(local
)) {
2030 log_error("Local name %s is not a suitable machine name.", local
);
2035 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2037 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2040 r
= sd_bus_message_new_method_call(
2043 "org.freedesktop.import1",
2044 "/org/freedesktop/import1",
2045 "org.freedesktop.import1.Manager",
2048 return bus_log_create_error(r
);
2050 r
= sd_bus_message_append(
2053 fd
>= 0 ? fd
: STDIN_FILENO
,
2058 return bus_log_create_error(r
);
2060 return transfer_image_common(bus
, m
);
2063 static int import_raw(int argc
, char *argv
[], void *userdata
) {
2064 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2065 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
2066 const char *local
= NULL
, *path
= NULL
;
2067 _cleanup_close_
int fd
= -1;
2068 sd_bus
*bus
= userdata
;
2074 path
= empty_or_dash_to_null(argv
[1]);
2077 local
= empty_or_dash_to_null(argv
[2]);
2079 r
= path_extract_filename(path
, &fn
);
2081 return log_error_errno(r
, "Cannot extract container name from filename: %m");
2086 log_error("Need either path or local name.");
2090 r
= raw_strip_suffixes(local
, &ll
);
2096 if (!machine_name_is_valid(local
)) {
2097 log_error("Local name %s is not a suitable machine name.", local
);
2102 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2104 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2107 r
= sd_bus_message_new_method_call(
2110 "org.freedesktop.import1",
2111 "/org/freedesktop/import1",
2112 "org.freedesktop.import1.Manager",
2115 return bus_log_create_error(r
);
2117 r
= sd_bus_message_append(
2120 fd
>= 0 ? fd
: STDIN_FILENO
,
2125 return bus_log_create_error(r
);
2127 return transfer_image_common(bus
, m
);
2130 static int import_fs(int argc
, char *argv
[], void *userdata
) {
2131 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2132 const char *local
= NULL
, *path
= NULL
;
2133 _cleanup_free_
char *fn
= NULL
;
2134 _cleanup_close_
int fd
= -1;
2135 sd_bus
*bus
= userdata
;
2141 path
= empty_or_dash_to_null(argv
[1]);
2144 local
= empty_or_dash_to_null(argv
[2]);
2146 r
= path_extract_filename(path
, &fn
);
2148 return log_error_errno(r
, "Cannot extract container name from filename: %m");
2153 log_error("Need either path or local name.");
2157 if (!machine_name_is_valid(local
)) {
2158 log_error("Local name %s is not a suitable machine name.", local
);
2163 fd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
);
2165 return log_error_errno(errno
, "Failed to open directory '%s': %m", path
);
2168 r
= sd_bus_message_new_method_call(
2171 "org.freedesktop.import1",
2172 "/org/freedesktop/import1",
2173 "org.freedesktop.import1.Manager",
2174 "ImportFileSystem");
2176 return bus_log_create_error(r
);
2178 r
= sd_bus_message_append(
2181 fd
>= 0 ? fd
: STDIN_FILENO
,
2186 return bus_log_create_error(r
);
2188 return transfer_image_common(bus
, m
);
2191 static void determine_compression_from_filename(const char *p
) {
2198 if (endswith(p
, ".xz"))
2200 else if (endswith(p
, ".gz"))
2201 arg_format
= "gzip";
2202 else if (endswith(p
, ".bz2"))
2203 arg_format
= "bzip2";
2206 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2207 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2208 _cleanup_close_
int fd
= -1;
2209 const char *local
= NULL
, *path
= NULL
;
2210 sd_bus
*bus
= userdata
;
2216 if (!machine_name_is_valid(local
)) {
2217 log_error("Machine name %s is not valid.", local
);
2223 path
= empty_or_dash_to_null(path
);
2226 determine_compression_from_filename(path
);
2228 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2230 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2233 r
= sd_bus_message_new_method_call(
2236 "org.freedesktop.import1",
2237 "/org/freedesktop/import1",
2238 "org.freedesktop.import1.Manager",
2241 return bus_log_create_error(r
);
2243 r
= sd_bus_message_append(
2247 fd
>= 0 ? fd
: STDOUT_FILENO
,
2250 return bus_log_create_error(r
);
2252 return transfer_image_common(bus
, m
);
2255 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2256 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2257 _cleanup_close_
int fd
= -1;
2258 const char *local
= NULL
, *path
= NULL
;
2259 sd_bus
*bus
= userdata
;
2265 if (!machine_name_is_valid(local
)) {
2266 log_error("Machine name %s is not valid.", local
);
2272 path
= empty_or_dash_to_null(path
);
2275 determine_compression_from_filename(path
);
2277 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2279 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2282 r
= sd_bus_message_new_method_call(
2285 "org.freedesktop.import1",
2286 "/org/freedesktop/import1",
2287 "org.freedesktop.import1.Manager",
2290 return bus_log_create_error(r
);
2292 r
= sd_bus_message_append(
2296 fd
>= 0 ? fd
: STDOUT_FILENO
,
2299 return bus_log_create_error(r
);
2301 return transfer_image_common(bus
, m
);
2304 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2305 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2306 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2307 const char *local
, *remote
;
2308 sd_bus
*bus
= userdata
;
2314 if (!http_url_is_valid(remote
)) {
2315 log_error("URL '%s' is not valid.", remote
);
2322 r
= import_url_last_component(remote
, &l
);
2324 return log_error_errno(r
, "Failed to get final component of URL: %m");
2329 local
= empty_or_dash_to_null(local
);
2332 r
= tar_strip_suffixes(local
, &ll
);
2338 if (!machine_name_is_valid(local
)) {
2339 log_error("Local name %s is not a suitable machine name.", local
);
2344 r
= sd_bus_message_new_method_call(
2347 "org.freedesktop.import1",
2348 "/org/freedesktop/import1",
2349 "org.freedesktop.import1.Manager",
2352 return bus_log_create_error(r
);
2354 r
= sd_bus_message_append(
2359 import_verify_to_string(arg_verify
),
2362 return bus_log_create_error(r
);
2364 return transfer_image_common(bus
, m
);
2367 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2368 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2369 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2370 const char *local
, *remote
;
2371 sd_bus
*bus
= userdata
;
2377 if (!http_url_is_valid(remote
)) {
2378 log_error("URL '%s' is not valid.", remote
);
2385 r
= import_url_last_component(remote
, &l
);
2387 return log_error_errno(r
, "Failed to get final component of URL: %m");
2392 local
= empty_or_dash_to_null(local
);
2395 r
= raw_strip_suffixes(local
, &ll
);
2401 if (!machine_name_is_valid(local
)) {
2402 log_error("Local name %s is not a suitable machine name.", local
);
2407 r
= sd_bus_message_new_method_call(
2410 "org.freedesktop.import1",
2411 "/org/freedesktop/import1",
2412 "org.freedesktop.import1.Manager",
2415 return bus_log_create_error(r
);
2417 r
= sd_bus_message_append(
2422 import_verify_to_string(arg_verify
),
2425 return bus_log_create_error(r
);
2427 return transfer_image_common(bus
, m
);
2430 typedef struct TransferInfo
{
2438 static int compare_transfer_info(const TransferInfo
*a
, const TransferInfo
*b
) {
2439 return strcmp(a
->local
, b
->local
);
2442 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2443 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2444 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2445 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2446 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2447 size_t n_transfers
= 0, n_allocated
= 0, j
;
2448 const char *type
, *remote
, *local
;
2449 sd_bus
*bus
= userdata
;
2450 uint32_t id
, max_id
= 0;
2454 (void) pager_open(arg_pager_flags
);
2456 r
= sd_bus_call_method(bus
,
2457 "org.freedesktop.import1",
2458 "/org/freedesktop/import1",
2459 "org.freedesktop.import1.Manager",
2465 return log_error_errno(r
, "Could not get transfers: %s", bus_error_message(&error
, -r
));
2467 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2469 return bus_log_parse_error(r
);
2471 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, NULL
)) > 0) {
2474 if (!GREEDY_REALLOC(transfers
, n_allocated
, n_transfers
+ 1))
2477 transfers
[n_transfers
].id
= id
;
2478 transfers
[n_transfers
].type
= type
;
2479 transfers
[n_transfers
].remote
= remote
;
2480 transfers
[n_transfers
].local
= local
;
2481 transfers
[n_transfers
].progress
= progress
;
2501 return bus_log_parse_error(r
);
2503 r
= sd_bus_message_exit_container(reply
);
2505 return bus_log_parse_error(r
);
2507 typesafe_qsort(transfers
, n_transfers
, compare_transfer_info
);
2509 if (arg_legend
&& n_transfers
> 0)
2510 printf("%-*s %-*s %-*s %-*s %-*s\n",
2511 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2513 (int) max_type
, "TYPE",
2514 (int) max_local
, "LOCAL",
2515 (int) max_remote
, "REMOTE");
2517 for (j
= 0; j
< n_transfers
; j
++)
2519 if (transfers
[j
].progress
< 0)
2520 printf("%*" PRIu32
" %*s %-*s %-*s %-*s\n",
2521 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2523 (int) max_type
, transfers
[j
].type
,
2524 (int) max_local
, transfers
[j
].local
,
2525 (int) max_remote
, transfers
[j
].remote
);
2527 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2528 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2529 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2530 (int) max_type
, transfers
[j
].type
,
2531 (int) max_local
, transfers
[j
].local
,
2532 (int) max_remote
, transfers
[j
].remote
);
2535 if (n_transfers
> 0)
2536 printf("\n%zu transfers listed.\n", n_transfers
);
2538 printf("No transfers.\n");
2544 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2545 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2546 sd_bus
*bus
= userdata
;
2551 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2553 for (i
= 1; i
< argc
; i
++) {
2556 r
= safe_atou32(argv
[i
], &id
);
2558 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2560 r
= sd_bus_call_method(
2562 "org.freedesktop.import1",
2563 "/org/freedesktop/import1",
2564 "org.freedesktop.import1.Manager",
2570 return log_error_errno(r
, "Could not cancel transfer: %s", bus_error_message(&error
, -r
));
2576 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2577 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2578 sd_bus
*bus
= userdata
;
2582 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2584 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2585 limit
= (uint64_t) -1;
2587 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2589 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2593 /* With two arguments changes the quota limit of the
2594 * specified image */
2595 r
= sd_bus_call_method(
2597 "org.freedesktop.machine1",
2598 "/org/freedesktop/machine1",
2599 "org.freedesktop.machine1.Manager",
2603 "st", argv
[1], limit
);
2605 /* With one argument changes the pool quota limit */
2606 r
= sd_bus_call_method(
2608 "org.freedesktop.machine1",
2609 "/org/freedesktop/machine1",
2610 "org.freedesktop.machine1.Manager",
2617 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2622 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2623 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2624 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2625 uint64_t usage
, total
= 0;
2626 char fb
[FORMAT_BYTES_MAX
];
2627 sd_bus
*bus
= userdata
;
2632 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2634 r
= sd_bus_message_new_method_call(
2637 "org.freedesktop.machine1",
2638 "/org/freedesktop/machine1",
2639 "org.freedesktop.machine1.Manager",
2642 return bus_log_create_error(r
);
2644 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2646 return bus_log_create_error(r
);
2648 /* This is a slow operation, hence permit a longer time for completion. */
2649 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2651 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2653 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2655 return bus_log_parse_error(r
);
2657 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2658 if (usage
== UINT64_MAX
) {
2659 log_info("Removed image '%s'", name
);
2662 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2663 name
, format_bytes(fb
, sizeof(fb
), usage
));
2664 if (total
!= UINT64_MAX
)
2670 r
= sd_bus_message_exit_container(reply
);
2672 return bus_log_parse_error(r
);
2674 if (total
== UINT64_MAX
)
2675 log_info("Removed %u images in total.", c
);
2677 log_info("Removed %u images in total. Total freed exclusive disk space: %s.",
2678 c
, format_bytes(fb
, sizeof(fb
), total
));
2683 static int help(int argc
, char *argv
[], void *userdata
) {
2684 _cleanup_free_
char *link
= NULL
;
2687 (void) pager_open(arg_pager_flags
);
2689 r
= terminal_urlify_man("machinectl", "1", &link
);
2693 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2694 "Send control commands to or query the virtual machine and container\n"
2695 "registration manager.\n\n"
2696 " -h --help Show this help\n"
2697 " --version Show package version\n"
2698 " --no-pager Do not pipe output into a pager\n"
2699 " --no-legend Do not show the headers and footers\n"
2700 " --no-ask-password Do not ask for system passwords\n"
2701 " -H --host=[USER@]HOST Operate on remote host\n"
2702 " -M --machine=CONTAINER Operate on local container\n"
2703 " -p --property=NAME Show only properties by this name\n"
2704 " -q --quiet Suppress output\n"
2705 " -a --all Show all properties, including empty ones\n"
2706 " --value When showing properties, only print the value\n"
2707 " -l --full Do not ellipsize output\n"
2708 " --kill-who=WHO Who to send signal to\n"
2709 " -s --signal=SIGNAL Which signal to send\n"
2710 " --uid=USER Specify user ID to invoke shell as\n"
2711 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2712 " --read-only Create read-only bind mount\n"
2713 " --mkdir Create directory before bind mounting, if missing\n"
2714 " -n --lines=INTEGER Number of journal entries to show\n"
2715 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2716 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2717 " short-iso, short-iso-precise, short-full,\n"
2718 " short-monotonic, short-unix, verbose, export,\n"
2719 " json, json-pretty, json-sse, json-seq, cat,\n"
2721 " --verify=MODE Verification mode for downloaded images (no,\n"
2722 " checksum, signature)\n"
2723 " --force Download image even if already exists\n\n"
2724 "Machine Commands:\n"
2725 " list List running VMs and containers\n"
2726 " status NAME... Show VM/container details\n"
2727 " show [NAME...] Show properties of one or more VMs/containers\n"
2728 " start NAME... Start container as a service\n"
2729 " login [NAME] Get a login prompt in a container or on the\n"
2731 " shell [[USER@]NAME [COMMAND...]]\n"
2732 " Invoke a shell (or other command) in a container\n"
2733 " or on the local host\n"
2734 " enable NAME... Enable automatic container start at boot\n"
2735 " disable NAME... Disable automatic container start at boot\n"
2736 " poweroff NAME... Power off one or more containers\n"
2737 " reboot NAME... Reboot one or more containers\n"
2738 " terminate NAME... Terminate one or more VMs/containers\n"
2739 " kill NAME... Send signal to processes of a VM/container\n"
2740 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2741 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2742 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2744 " list-images Show available container and VM images\n"
2745 " image-status [NAME...] Show image details\n"
2746 " show-image [NAME...] Show properties of image\n"
2747 " clone NAME NAME Clone an image\n"
2748 " rename NAME NAME Rename an image\n"
2749 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2750 " remove NAME... Remove an image\n"
2751 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2752 " clean Remove hidden (or all) images\n\n"
2753 "Image Transfer Commands:\n"
2754 " pull-tar URL [NAME] Download a TAR container image\n"
2755 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2756 " import-tar FILE [NAME] Import a local TAR container image\n"
2757 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2758 " import-fs DIRECTORY [NAME] Import a local directory container image\n"
2759 " export-tar NAME [FILE] Export a TAR container image locally\n"
2760 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2761 " list-transfers Show list of downloads in progress\n"
2762 " cancel-transfer Cancel a download\n"
2763 "\nSee the %s for details.\n"
2764 , program_invocation_short_name
2771 static int parse_argv(int argc
, char *argv
[]) {
2774 ARG_VERSION
= 0x100,
2781 ARG_NO_ASK_PASSWORD
,
2789 static const struct option options
[] = {
2790 { "help", no_argument
, NULL
, 'h' },
2791 { "version", no_argument
, NULL
, ARG_VERSION
},
2792 { "property", required_argument
, NULL
, 'p' },
2793 { "all", no_argument
, NULL
, 'a' },
2794 { "value", no_argument
, NULL
, ARG_VALUE
},
2795 { "full", no_argument
, NULL
, 'l' },
2796 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2797 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2798 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2799 { "signal", required_argument
, NULL
, 's' },
2800 { "host", required_argument
, NULL
, 'H' },
2801 { "machine", required_argument
, NULL
, 'M' },
2802 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2803 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2804 { "quiet", no_argument
, NULL
, 'q' },
2805 { "lines", required_argument
, NULL
, 'n' },
2806 { "output", required_argument
, NULL
, 'o' },
2807 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2808 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2809 { "force", no_argument
, NULL
, ARG_FORCE
},
2810 { "format", required_argument
, NULL
, ARG_FORMAT
},
2811 { "uid", required_argument
, NULL
, ARG_UID
},
2812 { "setenv", required_argument
, NULL
, 'E' },
2813 { "max-addresses", required_argument
, NULL
, ARG_NUMBER_IPS
},
2817 bool reorder
= false;
2818 int c
, r
, shell
= -1;
2824 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2826 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2832 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2833 * non-option argument was discovered. */
2837 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2838 * for switches after the main verb. However, for "shell" we really don't want that, since we
2839 * want that switches specified after the machine name are passed to the program to execute,
2840 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2841 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2842 * non-option parameter. If it's the verb "shell" we remember its position and continue
2843 * processing options. In this case, as soon as we hit the next non-option argument we found
2844 * the machine name, and stop further processing. If the first non-option argument is any other
2845 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2849 /* If we already found the "shell" verb on the command line, and now found the next
2850 * non-option argument, then this is the machine name and we should stop processing
2851 * further arguments. */
2852 optind
--; /* don't process this argument, go one step back */
2855 if (streq(optarg
, "shell"))
2856 /* Remember the position of the "shell" verb, and continue processing normally. */
2861 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2862 * processing normally. */
2865 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2866 * at least once with a reset option index. Hence, let's reset the option index here,
2867 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2868 * likely already processed it), and the bump the option index so that we read the
2869 * intended argument again. */
2870 saved_optind
= optind
;
2872 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2873 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2879 return help(0, NULL
, NULL
);
2885 r
= strv_extend(&arg_property
, optarg
);
2889 /* If the user asked for a particular
2890 * property, show it to him, even if it is
2908 if (safe_atou(optarg
, &arg_lines
) < 0)
2909 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2910 "Failed to parse lines '%s'", optarg
);
2914 if (streq(optarg
, "help")) {
2915 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2919 arg_output
= output_mode_from_string(optarg
);
2921 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2922 "Unknown output '%s'.", optarg
);
2924 if (OUTPUT_MODE_IS_JSON(arg_output
))
2929 arg_pager_flags
|= PAGER_DISABLE
;
2937 arg_kill_who
= optarg
;
2941 if (streq(optarg
, "help")) {
2942 DUMP_STRING_TABLE(signal
, int, _NSIG
);
2946 arg_signal
= signal_from_string(optarg
);
2948 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2949 "Failed to parse signal string %s.", optarg
);
2952 case ARG_NO_ASK_PASSWORD
:
2953 arg_ask_password
= false;
2957 arg_transport
= BUS_TRANSPORT_REMOTE
;
2962 arg_transport
= BUS_TRANSPORT_MACHINE
;
2967 arg_read_only
= true;
2979 if (streq(optarg
, "help")) {
2980 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2984 arg_verify
= import_verify_from_string(optarg
);
2986 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2987 "Failed to parse --verify= setting: %s", optarg
);
2995 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2"))
2996 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2997 "Unknown format: %s", optarg
);
2999 arg_format
= optarg
;
3007 if (!env_assignment_is_valid(optarg
))
3008 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3009 "Environment assignment invalid: %s", optarg
);
3011 r
= strv_extend(&arg_setenv
, optarg
);
3016 case ARG_NUMBER_IPS
:
3017 if (streq(optarg
, "all"))
3018 arg_addrs
= ALL_IP_ADDRESSES
;
3019 else if (safe_atoi(optarg
, &arg_addrs
) < 0)
3020 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3021 "Invalid number of IPs");
3022 else if (arg_addrs
< 0)
3023 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3024 "Number of IPs cannot be negative");
3031 assert_not_reached("Unhandled option");
3040 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
3041 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
3043 optind
-= 1; /* place the option index where the "shell" verb will be placed */
3046 for (i
= shell
; i
< optind
; i
++)
3047 argv
[i
] = argv
[i
+1];
3054 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
3056 static const Verb verbs
[] = {
3057 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
3058 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
3059 { "list-images", VERB_ANY
, 1, 0, list_images
},
3060 { "status", 2, VERB_ANY
, 0, show_machine
},
3061 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
3062 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
3063 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
3064 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
3065 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
3066 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
3067 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
3068 { "kill", 2, VERB_ANY
, 0, kill_machine
},
3069 { "login", VERB_ANY
, 2, 0, login_machine
},
3070 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
3071 { "bind", 3, 4, 0, bind_mount
},
3072 { "copy-to", 3, 4, 0, copy_files
},
3073 { "copy-from", 3, 4, 0, copy_files
},
3074 { "remove", 2, VERB_ANY
, 0, remove_image
},
3075 { "rename", 3, 3, 0, rename_image
},
3076 { "clone", 3, 3, 0, clone_image
},
3077 { "read-only", 2, 3, 0, read_only_image
},
3078 { "start", 2, VERB_ANY
, 0, start_machine
},
3079 { "enable", 2, VERB_ANY
, 0, enable_machine
},
3080 { "disable", 2, VERB_ANY
, 0, enable_machine
},
3081 { "import-tar", 2, 3, 0, import_tar
},
3082 { "import-raw", 2, 3, 0, import_raw
},
3083 { "import-fs", 2, 3, 0, import_fs
},
3084 { "export-tar", 2, 3, 0, export_tar
},
3085 { "export-raw", 2, 3, 0, export_raw
},
3086 { "pull-tar", 2, 3, 0, pull_tar
},
3087 { "pull-raw", 2, 3, 0, pull_raw
},
3088 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
3089 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
3090 { "set-limit", 2, 3, 0, set_limit
},
3091 { "clean", VERB_ANY
, 1, 0, clean_images
},
3095 return dispatch_verb(argc
, argv
, verbs
, bus
);
3098 static int run(int argc
, char *argv
[]) {
3099 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
3102 setlocale(LC_ALL
, "");
3103 log_parse_environment();
3106 /* The journal merging logic potentially needs a lot of fds. */
3107 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
3111 r
= parse_argv(argc
, argv
);
3115 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
3117 return log_error_errno(r
, "Failed to create bus connection: %m");
3119 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
3121 return machinectl_main(argc
, argv
, bus
);
3124 DEFINE_MAIN_FUNCTION(run
);