1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include <netinet/in.h>
10 #include <sys/mount.h>
11 #include <sys/socket.h>
16 #include "alloc-util.h"
17 #include "bus-common-errors.h"
18 #include "bus-error.h"
19 #include "bus-locator.h"
20 #include "bus-map-properties.h"
21 #include "bus-print-properties.h"
22 #include "bus-unit-procs.h"
23 #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"
37 #include "machine-dbus.h"
39 #include "main-func.h"
41 #include "nulstr-util.h"
43 #include "parse-argument.h"
44 #include "parse-util.h"
45 #include "path-util.h"
46 #include "pretty-print.h"
47 #include "process-util.h"
49 #include "rlimit-util.h"
51 #include "signal-util.h"
52 #include "sort-util.h"
53 #include "spawn-ask-password-agent.h"
54 #include "spawn-polkit-agent.h"
55 #include "stdio-util.h"
56 #include "string-table.h"
58 #include "terminal-util.h"
59 #include "unit-name.h"
63 #define ALL_ADDRESSES -1
65 static char **arg_property
= NULL
;
66 static bool arg_all
= false;
67 static BusPrintPropertyFlags arg_print_flags
= 0;
68 static bool arg_full
= false;
69 static PagerFlags arg_pager_flags
= 0;
70 static bool arg_legend
= true;
71 static const char *arg_kill_whom
= NULL
;
72 static int arg_signal
= SIGTERM
;
73 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
74 static const char *arg_host
= NULL
;
75 static bool arg_read_only
= false;
76 static bool arg_mkdir
= false;
77 static bool arg_quiet
= false;
78 static bool arg_ask_password
= true;
79 static unsigned arg_lines
= 10;
80 static OutputMode arg_output
= OUTPUT_SHORT
;
81 static bool arg_force
= false;
82 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
83 static const char* arg_format
= NULL
;
84 static const char *arg_uid
= NULL
;
85 static char **arg_setenv
= NULL
;
86 static int arg_max_addresses
= 1;
88 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
89 STATIC_DESTRUCTOR_REGISTER(arg_setenv
, strv_freep
);
91 static OutputFlags
get_output_flags(void) {
93 FLAGS_SET(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
) * OUTPUT_SHOW_ALL
|
94 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
95 colors_enabled() * OUTPUT_COLOR
|
96 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
99 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
100 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
101 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
102 const char *k
, *v
, *iter
, **query_res
= NULL
;
103 size_t count
= 0, awaited_args
= 0;
111 NULSTR_FOREACH(iter
, query
)
113 query_res
= newa0(const char *, awaited_args
);
115 r
= bus_call_method(bus
, bus_machine_mgr
, method
, &error
, &reply
, "s", name
);
117 return log_debug_errno(r
, "Failed to call '%s()': %s", method
, bus_error_message(&error
, r
));
119 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
121 return bus_log_parse_error(r
);
123 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
125 NULSTR_FOREACH(iter
, query
) {
126 if (streq(k
, iter
)) {
127 query_res
[count
] = v
;
134 return bus_log_parse_error(r
);
136 r
= sd_bus_message_exit_container(reply
);
138 return bus_log_parse_error(r
);
141 for (count
= 0; count
< awaited_args
; count
++) {
144 out
= va_arg(ap
, char **);
146 if (query_res
[count
]) {
147 val
= strdup(query_res
[count
]);
160 static int call_get_addresses(
168 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
169 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
170 _cleanup_free_
char *addresses
= NULL
;
179 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachineAddresses", NULL
, &reply
, "s", name
);
181 return log_debug_errno(r
, "Could not get addresses: %s", bus_error_message(&error
, r
));
183 addresses
= strdup(prefix
);
188 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
190 return bus_log_parse_error(r
);
192 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
196 char buf_ifi
[1 + DECIMAL_STR_MAX(int)] = "";
198 r
= sd_bus_message_read(reply
, "i", &family
);
200 return bus_log_parse_error(r
);
202 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
204 return bus_log_parse_error(r
);
206 if (family
== AF_INET6
&& ifi
> 0)
207 xsprintf(buf_ifi
, "%%%i", ifi
);
209 if (!strextend(&addresses
, prefix
, IN_ADDR_TO_STRING(family
, a
), buf_ifi
))
212 r
= sd_bus_message_exit_container(reply
);
214 return bus_log_parse_error(r
);
221 return bus_log_parse_error(r
);
223 r
= sd_bus_message_exit_container(reply
);
225 return bus_log_parse_error(r
);
227 *ret
= TAKE_PTR(addresses
);
231 static int show_table(Table
*table
, const char *word
) {
237 if (table_get_rows(table
) > 1 || OUTPUT_MODE_IS_JSON(arg_output
)) {
238 r
= table_set_sort(table
, (size_t) 0);
240 return table_log_sort_error(r
);
242 table_set_header(table
, arg_legend
);
244 if (OUTPUT_MODE_IS_JSON(arg_output
))
245 r
= table_print_json(table
, NULL
, output_mode_to_json_format_flags(arg_output
) | JSON_FORMAT_COLOR_AUTO
);
247 r
= table_print(table
, NULL
);
249 return table_log_print_error(r
);
253 if (table_get_rows(table
) > 1)
254 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
256 printf("No %s.\n", word
);
262 static int list_machines(int argc
, char *argv
[], void *userdata
) {
264 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
265 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
266 _cleanup_(table_unrefp
) Table
*table
= NULL
;
267 sd_bus
*bus
= ASSERT_PTR(userdata
);
270 pager_open(arg_pager_flags
);
272 r
= bus_call_method(bus
, bus_machine_mgr
, "ListMachines", &error
, &reply
, NULL
);
274 return log_error_errno(r
, "Could not get machines: %s", bus_error_message(&error
, r
));
276 table
= table_new("machine", "class", "service", "os", "version", "addresses");
280 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
281 if (!arg_full
&& arg_max_addresses
!= ALL_ADDRESSES
)
282 table_set_cell_height_max(table
, arg_max_addresses
);
285 table_set_width(table
, 0);
287 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
289 return bus_log_parse_error(r
);
292 _cleanup_free_
char *os
= NULL
, *version_id
= NULL
, *addresses
= NULL
;
293 const char *name
, *class, *service
;
295 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, NULL
);
297 return bus_log_parse_error(r
);
301 if (name
[0] == '.' && !arg_all
)
304 (void) call_get_os_release(
306 "GetMachineOSRelease",
313 (void) call_get_addresses(
321 r
= table_add_many(table
,
322 TABLE_STRING
, empty_to_null(name
),
323 TABLE_STRING
, empty_to_null(class),
324 TABLE_STRING
, empty_to_null(service
),
325 TABLE_STRING
, empty_to_null(os
),
326 TABLE_STRING
, empty_to_null(version_id
),
327 TABLE_STRING
, empty_to_null(addresses
));
329 return table_log_add_error(r
);
332 r
= sd_bus_message_exit_container(reply
);
334 return bus_log_parse_error(r
);
336 return show_table(table
, "machines");
339 static int list_images(int argc
, char *argv
[], void *userdata
) {
341 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
342 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
343 _cleanup_(table_unrefp
) Table
*table
= NULL
;
344 sd_bus
*bus
= ASSERT_PTR(userdata
);
347 pager_open(arg_pager_flags
);
349 r
= bus_call_method(bus
, bus_machine_mgr
, "ListImages", &error
, &reply
, NULL
);
351 return log_error_errno(r
, "Could not get images: %s", bus_error_message(&error
, r
));
353 table
= table_new("name", "type", "ro", "usage", "created", "modified");
358 table_set_width(table
, 0);
360 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100);
362 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
364 return bus_log_parse_error(r
);
367 uint64_t crtime
, mtime
, size
;
368 const char *name
, *type
;
371 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, NULL
);
373 return bus_log_parse_error(r
);
377 if (name
[0] == '.' && !arg_all
)
380 r
= table_add_many(table
,
383 TABLE_BOOLEAN
, ro_int
,
384 TABLE_SET_COLOR
, ro_int
? ansi_highlight_red() : NULL
,
386 TABLE_TIMESTAMP
, crtime
,
387 TABLE_TIMESTAMP
, mtime
);
389 return table_log_add_error(r
);
392 r
= sd_bus_message_exit_container(reply
);
394 return bus_log_parse_error(r
);
396 return show_table(table
, "images");
399 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
400 _cleanup_free_
char *cgroup
= NULL
;
401 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
408 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
421 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
424 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
427 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
429 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
432 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
434 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
439 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
440 _cleanup_free_
char *pretty
= NULL
;
447 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
452 printf("%s%s\n", prefix
, pretty
);
457 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
458 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
459 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
466 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachineUIDShift", &error
, &reply
, "s", name
);
468 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
470 r
= sd_bus_message_read(reply
, "u", &shift
);
474 if (shift
== 0) /* Don't show trivial mappings */
477 printf(" UID Shift: %" PRIu32
"\n", shift
);
481 typedef struct MachineStatusInfo
{
487 const char *root_directory
;
489 struct dual_timestamp timestamp
;
494 static void machine_status_info_clear(MachineStatusInfo
*info
) {
501 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
502 _cleanup_free_
char *addresses
= NULL
, *s1
= NULL
, *s2
= NULL
;
508 fputs(strna(i
->name
), stdout
);
510 if (!sd_id128_is_null(i
->id
))
511 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
515 s1
= strdup(strempty(FORMAT_TIMESTAMP_RELATIVE(i
->timestamp
.realtime
)));
516 s2
= strdup(strempty(FORMAT_TIMESTAMP(i
->timestamp
.realtime
)));
519 printf("\t Since: %s; %s\n", strna(s2
), s1
);
520 else if (!isempty(s2
))
521 printf("\t Since: %s\n", s2
);
524 _cleanup_free_
char *t
= NULL
;
526 printf("\t Leader: %u", (unsigned) i
->leader
);
528 (void) get_process_comm(i
->leader
, &t
);
536 printf("\t Service: %s", i
->service
);
539 printf("; class %s", i
->class);
543 printf("\t Class: %s\n", i
->class);
545 if (i
->root_directory
)
546 printf("\t Root: %s\n", i
->root_directory
);
548 if (i
->n_netif
> 0) {
549 fputs("\t Iface:", stdout
);
551 for (size_t c
= 0; c
< i
->n_netif
; c
++) {
552 char name
[IF_NAMESIZE
];
554 if (format_ifname(i
->netif
[c
], name
) >= 0) {
563 printf(" %i", i
->netif
[c
]);
569 if (call_get_addresses(bus
, i
->name
, ifi
,
570 "\t Address: ", "\n\t ",
572 fputs(addresses
, stdout
);
576 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
578 print_uid_shift(bus
, i
->name
);
581 printf("\t Unit: %s\n", i
->unit
);
582 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
584 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
586 show_journal_by_unit(
592 i
->timestamp
.monotonic
,
595 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
596 SD_JOURNAL_LOCAL_ONLY
,
602 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
603 MachineStatusInfo
*i
= userdata
;
608 assert_cc(sizeof(int32_t) == sizeof(int));
609 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
615 i
->n_netif
= l
/ sizeof(int32_t);
616 i
->netif
= memdup(v
, l
);
623 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
625 static const struct bus_properties_map map
[] = {
626 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
627 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
628 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
629 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
630 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
631 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
632 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
633 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
634 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
635 { "NetworkInterfaces", "ai", map_netif
, 0 },
639 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
640 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
641 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
649 r
= bus_map_all_properties(bus
,
650 "org.freedesktop.machine1",
658 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
664 print_machine_status_info(bus
, &info
);
669 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
681 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
683 log_error_errno(r
, "Could not get properties: %m");
688 static int show_machine(int argc
, char *argv
[], void *userdata
) {
690 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
691 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
692 bool properties
, new_line
= false;
693 sd_bus
*bus
= ASSERT_PTR(userdata
);
696 properties
= !strstr(argv
[0], "status");
698 pager_open(arg_pager_flags
);
700 if (properties
&& argc
<= 1) {
702 /* If no argument is specified, inspect the manager
704 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
709 for (int i
= 1; i
< argc
; i
++) {
710 const char *path
= NULL
;
712 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachine", &error
, &reply
, "s", argv
[i
]);
714 return log_error_errno(r
, "Could not get path to machine: %s", bus_error_message(&error
, r
));
716 r
= sd_bus_message_read(reply
, "o", &path
);
718 return bus_log_parse_error(r
);
721 r
= show_machine_properties(bus
, path
, &new_line
);
723 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
729 static int print_image_hostname(sd_bus
*bus
, const char *name
) {
730 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
734 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageHostname", NULL
, &reply
, "s", name
);
738 r
= sd_bus_message_read(reply
, "s", &hn
);
743 printf("\tHostname: %s\n", hn
);
748 static int print_image_machine_id(sd_bus
*bus
, const char *name
) {
749 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
750 sd_id128_t id
= SD_ID128_NULL
;
755 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineID", NULL
, &reply
, "s", name
);
759 r
= sd_bus_message_read_array(reply
, 'y', &p
, &size
);
763 if (size
== sizeof(sd_id128_t
))
764 memcpy(&id
, p
, size
);
766 if (!sd_id128_is_null(id
))
767 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(id
));
772 static int print_image_machine_info(sd_bus
*bus
, const char *name
) {
773 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
776 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineInfo", NULL
, &reply
, "s", name
);
780 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
787 r
= sd_bus_message_read(reply
, "{ss}", &p
, &q
);
793 if (streq(p
, "DEPLOYMENT"))
794 printf(" Deployment: %s\n", q
);
797 r
= sd_bus_message_exit_container(reply
);
804 typedef struct ImageStatusInfo
{
813 uint64_t usage_exclusive
;
814 uint64_t limit_exclusive
;
817 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
822 fputs(i
->name
, stdout
);
827 printf("\t Type: %s\n", i
->type
);
830 printf("\t Path: %s\n", i
->path
);
832 (void) print_image_hostname(bus
, i
->name
);
833 (void) print_image_machine_id(bus
, i
->name
);
834 (void) print_image_machine_info(bus
, i
->name
);
836 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
838 printf("\t RO: %s%s%s\n",
839 i
->read_only
? ansi_highlight_red() : "",
840 i
->read_only
? "read-only" : "writable",
841 i
->read_only
? ansi_normal() : "");
843 if (timestamp_is_set(i
->crtime
))
844 printf("\t Created: %s; %s\n",
845 FORMAT_TIMESTAMP(i
->crtime
), FORMAT_TIMESTAMP_RELATIVE(i
->crtime
));
847 if (timestamp_is_set(i
->mtime
))
848 printf("\tModified: %s; %s\n",
849 FORMAT_TIMESTAMP(i
->mtime
), FORMAT_TIMESTAMP_RELATIVE(i
->mtime
));
851 if (i
->usage
!= UINT64_MAX
) {
852 if (i
->usage_exclusive
!= i
->usage
&& i
->usage_exclusive
!= UINT64_MAX
)
853 printf("\t Usage: %s (exclusive: %s)\n",
854 FORMAT_BYTES(i
->usage
), FORMAT_BYTES(i
->usage_exclusive
));
856 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
859 if (i
->limit
!= UINT64_MAX
) {
860 if (i
->limit_exclusive
!= i
->limit
&& i
->limit_exclusive
!= UINT64_MAX
)
861 printf("\t Limit: %s (exclusive: %s)\n",
862 FORMAT_BYTES(i
->limit
), FORMAT_BYTES(i
->limit_exclusive
));
864 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
868 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
870 static const struct bus_properties_map map
[] = {
871 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
872 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
873 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
874 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
875 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
876 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
877 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
878 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
879 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
880 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
884 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
885 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
886 ImageStatusInfo info
= {};
893 r
= bus_map_all_properties(bus
,
894 "org.freedesktop.machine1",
897 BUS_MAP_BOOLEAN_AS_BOOL
,
902 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
908 print_image_status_info(bus
, &info
);
913 typedef struct PoolStatusInfo
{
919 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
921 printf("\t Path: %s\n", i
->path
);
923 if (i
->usage
!= UINT64_MAX
)
924 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
926 if (i
->limit
!= UINT64_MAX
)
927 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
930 static int show_pool_info(sd_bus
*bus
) {
932 static const struct bus_properties_map map
[] = {
933 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
934 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
935 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
939 PoolStatusInfo info
= {
944 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
945 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
950 r
= bus_map_all_properties(bus
,
951 "org.freedesktop.machine1",
952 "/org/freedesktop/machine1",
959 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
961 print_pool_status_info(bus
, &info
);
966 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
978 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
980 log_error_errno(r
, "Could not get properties: %m");
985 static int show_image(int argc
, char *argv
[], void *userdata
) {
987 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
988 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
989 bool properties
, new_line
= false;
990 sd_bus
*bus
= ASSERT_PTR(userdata
);
993 properties
= !strstr(argv
[0], "status");
995 pager_open(arg_pager_flags
);
999 /* If no argument is specified, inspect the manager
1003 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1005 r
= show_pool_info(bus
);
1010 for (int i
= 1; i
< argc
; i
++) {
1011 const char *path
= NULL
;
1013 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, &reply
, "s", argv
[i
]);
1015 return log_error_errno(r
, "Could not get path to image: %s", bus_error_message(&error
, r
));
1017 r
= sd_bus_message_read(reply
, "o", &path
);
1019 return bus_log_parse_error(r
);
1022 r
= show_image_properties(bus
, path
, &new_line
);
1024 r
= show_image_info(bus
, path
, &new_line
);
1030 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1031 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1032 sd_bus
*bus
= ASSERT_PTR(userdata
);
1035 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1038 arg_kill_whom
= "all";
1040 for (int i
= 1; i
< argc
; i
++) {
1041 r
= bus_call_method(
1047 "ssi", argv
[i
], arg_kill_whom
, arg_signal
);
1049 return log_error_errno(r
, "Could not kill machine: %s", bus_error_message(&error
, r
));
1055 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1056 arg_kill_whom
= "leader";
1057 arg_signal
= SIGINT
; /* sysvinit + systemd */
1059 return kill_machine(argc
, argv
, userdata
);
1062 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1063 arg_kill_whom
= "leader";
1064 arg_signal
= SIGRTMIN
+4; /* only systemd */
1066 return kill_machine(argc
, argv
, userdata
);
1069 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1070 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1071 sd_bus
*bus
= ASSERT_PTR(userdata
);
1074 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1076 for (int i
= 1; i
< argc
; i
++) {
1077 r
= bus_call_method(bus
, bus_machine_mgr
, "TerminateMachine", &error
, NULL
, "s", argv
[i
]);
1079 return log_error_errno(r
, "Could not terminate machine: %s", bus_error_message(&error
, r
));
1085 static const char *select_copy_method(bool copy_from
, bool force
) {
1087 return copy_from
? "CopyFromMachineWithFlags" : "CopyToMachineWithFlags";
1089 return copy_from
? "CopyFromMachine" : "CopyToMachine";
1092 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1093 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1094 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1095 _cleanup_free_
char *abs_host_path
= NULL
;
1096 char *dest
, *host_path
, *container_path
;
1097 sd_bus
*bus
= ASSERT_PTR(userdata
);
1101 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1103 copy_from
= streq(argv
[0], "copy-from");
1104 dest
= argv
[3] ?: argv
[2];
1105 host_path
= copy_from
? dest
: argv
[2];
1106 container_path
= copy_from
? argv
[2] : dest
;
1108 if (!path_is_absolute(host_path
)) {
1109 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1111 return log_error_errno(r
, "Failed to make path absolute: %m");
1113 host_path
= abs_host_path
;
1116 r
= bus_message_new_method_call(
1120 select_copy_method(copy_from
, arg_force
));
1122 return bus_log_create_error(r
);
1124 r
= sd_bus_message_append(
1128 copy_from
? container_path
: host_path
,
1129 copy_from
? host_path
: container_path
);
1131 return bus_log_create_error(r
);
1134 r
= sd_bus_message_append(m
, "t", MACHINE_COPY_REPLACE
);
1136 return bus_log_create_error(r
);
1139 /* This is a slow operation, hence turn off any method call timeouts */
1140 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1142 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1147 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1148 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1149 sd_bus
*bus
= ASSERT_PTR(userdata
);
1152 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1154 r
= bus_call_method(
1167 return log_error_errno(r
, "Failed to bind mount: %s", bus_error_message(&error
, r
));
1172 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1173 PTYForward
** forward
= (PTYForward
**) userdata
;
1180 /* If the forwarder is already initialized, tell it to
1181 * exit on the next vhangup(), so that we still flush
1182 * out what might be queued and exit then. */
1184 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1188 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1191 /* On error, or when the forwarder is not initialized yet, quit immediately */
1192 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1196 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1202 assert(master
>= 0);
1205 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1208 if (streq(name
, ".host"))
1209 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1211 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1214 (void) sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1215 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1217 r
= pty_forward_new(event
, master
, flags
, forward
);
1219 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1221 r
= sd_event_loop(event
);
1223 return log_error_errno(r
, "Failed to run event loop: %m");
1225 pty_forward_get_last_char(*forward
, &last_char
);
1228 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1229 pty_forward_get_ignore_vhangup(*forward
) == 0;
1231 *forward
= pty_forward_free(*forward
);
1233 if (last_char
!= '\n')
1234 fputc('\n', stdout
);
1238 log_info("Machine %s terminated.", name
);
1239 else if (streq(name
, ".host"))
1240 log_info("Connection to the local host terminated.");
1242 log_info("Connection to machine %s terminated.", name
);
1248 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1250 * Whatever is specified in the spec takes priority over global arguments.
1253 const char *_machine
= NULL
;
1258 at
= strchr(spec
, '@');
1261 /* Do the same as ssh and refuse "@host". */
1265 _uid
= strndup(spec
, at
- spec
);
1272 if (arg_uid
&& !_uid
) {
1273 _uid
= strdup(arg_uid
);
1279 *machine
= isempty(_machine
) ? ".host" : _machine
;
1283 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1284 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1285 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1286 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1287 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1288 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1290 sd_bus
*bus
= ASSERT_PTR(userdata
);
1291 const char *match
, *machine
;
1293 if (!strv_isempty(arg_setenv
) || arg_uid
)
1294 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1295 "--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1297 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1298 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1299 "Login only supported on local machines.");
1301 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1303 r
= sd_event_default(&event
);
1305 return log_error_errno(r
, "Failed to get event loop: %m");
1307 r
= sd_bus_attach_event(bus
, event
, 0);
1309 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1311 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1313 match
= strjoina("type='signal',"
1314 "sender='org.freedesktop.machine1',"
1315 "path='/org/freedesktop/machine1',",
1316 "interface='org.freedesktop.machine1.Manager',"
1317 "member='MachineRemoved',"
1318 "arg0='", machine
, "'");
1320 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1322 return log_error_errno(r
, "Failed to request machine removal match: %m");
1324 r
= bus_call_method(bus
, bus_machine_mgr
, "OpenMachineLogin", &error
, &reply
, "s", machine
);
1326 return log_error_errno(r
, "Failed to get login PTY: %s", bus_error_message(&error
, r
));
1328 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1330 return bus_log_parse_error(r
);
1332 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1335 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1336 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1337 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1338 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1339 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1340 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1342 sd_bus
*bus
= ASSERT_PTR(userdata
);
1343 const char *match
, *machine
, *path
;
1344 _cleanup_free_
char *uid
= NULL
;
1346 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1347 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1348 "Shell only supported on local machines.");
1350 /* Pass $TERM to shell session, if not explicitly specified. */
1351 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1354 t
= strv_find_prefix(environ
, "TERM=");
1356 if (strv_extend(&arg_setenv
, t
) < 0)
1361 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1363 r
= sd_event_default(&event
);
1365 return log_error_errno(r
, "Failed to get event loop: %m");
1367 r
= sd_bus_attach_event(bus
, event
, 0);
1369 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1371 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1373 return log_error_errno(r
, "Failed to parse machine specification: %m");
1375 match
= strjoina("type='signal',"
1376 "sender='org.freedesktop.machine1',"
1377 "path='/org/freedesktop/machine1',",
1378 "interface='org.freedesktop.machine1.Manager',"
1379 "member='MachineRemoved',"
1380 "arg0='", machine
, "'");
1382 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, &forward
);
1384 return log_error_errno(r
, "Failed to request machine removal match: %m");
1386 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "OpenMachineShell");
1388 return bus_log_create_error(r
);
1390 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1392 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1394 return bus_log_create_error(r
);
1396 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1398 return bus_log_create_error(r
);
1400 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1402 return bus_log_create_error(r
);
1404 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1406 return log_error_errno(r
, "Failed to get shell PTY: %s", bus_error_message(&error
, r
));
1408 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1410 return bus_log_parse_error(r
);
1412 return process_forward(event
, &forward
, master
, 0, machine
);
1415 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1416 sd_bus
*bus
= ASSERT_PTR(userdata
);
1419 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1421 for (int i
= 1; i
< argc
; i
++) {
1422 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1423 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1425 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "RemoveImage");
1427 return bus_log_create_error(r
);
1429 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1431 return bus_log_create_error(r
);
1433 /* This is a slow operation, hence turn off any method call timeouts */
1434 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1436 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1442 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1443 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1444 sd_bus
*bus
= ASSERT_PTR(userdata
);
1447 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1449 r
= bus_call_method(
1455 "ss", argv
[1], argv
[2]);
1457 return log_error_errno(r
, "Could not rename image: %s", bus_error_message(&error
, r
));
1462 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1463 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1464 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1465 sd_bus
*bus
= ASSERT_PTR(userdata
);
1468 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1470 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CloneImage");
1472 return bus_log_create_error(r
);
1474 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1476 return bus_log_create_error(r
);
1478 /* This is a slow operation, hence turn off any method call timeouts */
1479 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1481 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1486 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1487 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1488 sd_bus
*bus
= ASSERT_PTR(userdata
);
1492 b
= parse_boolean(argv
[2]);
1494 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1495 "Failed to parse boolean argument: %s",
1499 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1501 r
= bus_call_method(bus
, bus_machine_mgr
, "MarkImageReadOnly", &error
, NULL
, "sb", argv
[1], b
);
1503 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, r
));
1508 static int image_exists(sd_bus
*bus
, const char *name
) {
1509 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1515 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, NULL
, "s", name
);
1517 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1520 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, r
));
1526 static int make_service_name(const char *name
, char **ret
) {
1532 if (!hostname_is_valid(name
, 0))
1533 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1534 "Invalid machine name %s.", name
);
1536 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1538 return log_error_errno(r
, "Failed to build unit name: %m");
1543 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1544 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1545 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1546 sd_bus
*bus
= ASSERT_PTR(userdata
);
1549 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1550 ask_password_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1552 r
= bus_wait_for_jobs_new(bus
, &w
);
1556 for (int i
= 1; i
< argc
; i
++) {
1557 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1558 _cleanup_free_
char *unit
= NULL
;
1561 r
= make_service_name(argv
[i
], &unit
);
1565 r
= image_exists(bus
, argv
[i
]);
1569 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1570 "Machine image '%s' does not exist.",
1573 r
= bus_call_method(
1579 "ss", unit
, "fail");
1581 return log_error_errno(r
, "Failed to start unit: %s", bus_error_message(&error
, r
));
1583 r
= sd_bus_message_read(reply
, "o", &object
);
1585 return bus_log_parse_error(r
);
1587 r
= bus_wait_for_jobs_add(w
, object
);
1592 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1599 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1600 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1601 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1602 InstallChange
*changes
= NULL
;
1603 size_t n_changes
= 0;
1604 const char *method
= NULL
;
1605 sd_bus
*bus
= ASSERT_PTR(userdata
);
1608 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1610 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1612 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, method
);
1614 return bus_log_create_error(r
);
1616 r
= sd_bus_message_open_container(m
, 'a', "s");
1618 return bus_log_create_error(r
);
1620 for (int i
= 1; i
< argc
; i
++) {
1621 _cleanup_free_
char *unit
= NULL
;
1623 r
= make_service_name(argv
[i
], &unit
);
1627 r
= image_exists(bus
, argv
[i
]);
1631 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1632 "Machine image '%s' does not exist.",
1635 r
= sd_bus_message_append(m
, "s", unit
);
1637 return bus_log_create_error(r
);
1640 r
= sd_bus_message_close_container(m
);
1642 return bus_log_create_error(r
);
1644 if (streq(argv
[0], "enable"))
1645 r
= sd_bus_message_append(m
, "bb", false, false);
1647 r
= sd_bus_message_append(m
, "b", false);
1649 return bus_log_create_error(r
);
1651 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1653 return log_error_errno(r
, "Failed to enable or disable unit: %s", bus_error_message(&error
, r
));
1655 if (streq(argv
[0], "enable")) {
1656 r
= sd_bus_message_read(reply
, "b", NULL
);
1658 return bus_log_parse_error(r
);
1661 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1665 r
= bus_call_method(bus
, bus_systemd_mgr
, "Reload", &error
, NULL
, NULL
);
1667 log_error("Failed to reload daemon: %s", bus_error_message(&error
, r
));
1674 install_changes_free(changes
, n_changes
);
1679 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1680 const char **our_path
= userdata
, *line
;
1687 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1689 bus_log_parse_error(r
);
1693 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1696 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1699 log_full(priority
, "%s", line
);
1703 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1704 const char **our_path
= userdata
, *path
, *result
;
1711 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1713 bus_log_parse_error(r
);
1717 if (!streq_ptr(*our_path
, path
))
1720 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1724 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1729 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1731 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1735 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1736 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1737 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1738 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1739 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1740 const char *path
= NULL
;
1747 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1749 r
= sd_event_default(&event
);
1751 return log_error_errno(r
, "Failed to get event loop: %m");
1753 r
= sd_bus_attach_event(bus
, event
, 0);
1755 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1757 r
= bus_match_signal_async(
1762 match_transfer_removed
, NULL
, &path
);
1764 return log_error_errno(r
, "Failed to request match: %m");
1766 r
= sd_bus_match_signal_async(
1769 "org.freedesktop.import1",
1771 "org.freedesktop.import1.Transfer",
1773 match_log_message
, NULL
, &path
);
1775 return log_error_errno(r
, "Failed to request match: %m");
1777 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1779 return log_error_errno(r
, "Failed to transfer image: %s", bus_error_message(&error
, r
));
1781 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1783 return bus_log_parse_error(r
);
1785 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1788 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
1790 (void) sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1791 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1793 r
= sd_event_loop(event
);
1795 return log_error_errno(r
, "Failed to run event loop: %m");
1800 static int import_tar(int argc
, char *argv
[], void *userdata
) {
1801 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1802 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
1803 const char *local
= NULL
, *path
= NULL
;
1804 _cleanup_close_
int fd
= -1;
1805 sd_bus
*bus
= ASSERT_PTR(userdata
);
1809 path
= empty_or_dash_to_null(argv
[1]);
1812 local
= empty_or_dash_to_null(argv
[2]);
1814 r
= path_extract_filename(path
, &fn
);
1816 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1817 if (r
== O_DIRECTORY
)
1818 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
1819 "Path '%s' refers to directory, but we need a regular file: %m", path
);
1824 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1825 "Need either path or local name.");
1827 r
= tar_strip_suffixes(local
, &ll
);
1833 if (!hostname_is_valid(local
, 0))
1834 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1835 "Local name %s is not a suitable machine name.",
1839 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1841 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1844 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportTar");
1846 return bus_log_create_error(r
);
1848 r
= sd_bus_message_append(
1851 fd
>= 0 ? fd
: STDIN_FILENO
,
1856 return bus_log_create_error(r
);
1858 return transfer_image_common(bus
, m
);
1861 static int import_raw(int argc
, char *argv
[], void *userdata
) {
1862 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1863 _cleanup_free_
char *ll
= NULL
, *fn
= NULL
;
1864 const char *local
= NULL
, *path
= NULL
;
1865 _cleanup_close_
int fd
= -1;
1866 sd_bus
*bus
= ASSERT_PTR(userdata
);
1870 path
= empty_or_dash_to_null(argv
[1]);
1873 local
= empty_or_dash_to_null(argv
[2]);
1875 r
= path_extract_filename(path
, &fn
);
1877 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1878 if (r
== O_DIRECTORY
)
1879 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
1880 "Path '%s' refers to directory, but we need a regular file: %m", path
);
1885 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1886 "Need either path or local name.");
1888 r
= raw_strip_suffixes(local
, &ll
);
1894 if (!hostname_is_valid(local
, 0))
1895 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1896 "Local name %s is not a suitable machine name.",
1900 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1902 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1905 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportRaw");
1907 return bus_log_create_error(r
);
1909 r
= sd_bus_message_append(
1912 fd
>= 0 ? fd
: STDIN_FILENO
,
1917 return bus_log_create_error(r
);
1919 return transfer_image_common(bus
, m
);
1922 static int import_fs(int argc
, char *argv
[], void *userdata
) {
1923 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1924 const char *local
= NULL
, *path
= NULL
;
1925 _cleanup_free_
char *fn
= NULL
;
1926 _cleanup_close_
int fd
= -1;
1927 sd_bus
*bus
= ASSERT_PTR(userdata
);
1931 path
= empty_or_dash_to_null(argv
[1]);
1934 local
= empty_or_dash_to_null(argv
[2]);
1936 r
= path_extract_filename(path
, &fn
);
1938 return log_error_errno(r
, "Cannot extract container name from filename: %m");
1943 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1944 "Need either path or local name.");
1946 if (!hostname_is_valid(local
, 0))
1947 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1948 "Local name %s is not a suitable machine name.",
1952 fd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
);
1954 return log_error_errno(errno
, "Failed to open directory '%s': %m", path
);
1957 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ImportFileSystem");
1959 return bus_log_create_error(r
);
1961 r
= sd_bus_message_append(
1964 fd
>= 0 ? fd
: STDIN_FILENO
,
1969 return bus_log_create_error(r
);
1971 return transfer_image_common(bus
, m
);
1974 static void determine_compression_from_filename(const char *p
) {
1981 if (endswith(p
, ".xz"))
1983 else if (endswith(p
, ".gz"))
1984 arg_format
= "gzip";
1985 else if (endswith(p
, ".bz2"))
1986 arg_format
= "bzip2";
1989 static int export_tar(int argc
, char *argv
[], void *userdata
) {
1990 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1991 _cleanup_close_
int fd
= -1;
1992 const char *local
= NULL
, *path
= NULL
;
1993 sd_bus
*bus
= ASSERT_PTR(userdata
);
1997 if (!hostname_is_valid(local
, 0))
1998 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1999 "Machine name %s is not valid.", local
);
2003 path
= empty_or_dash_to_null(path
);
2006 determine_compression_from_filename(path
);
2008 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2010 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2013 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ExportTar");
2015 return bus_log_create_error(r
);
2017 r
= sd_bus_message_append(
2021 fd
>= 0 ? fd
: STDOUT_FILENO
,
2024 return bus_log_create_error(r
);
2026 return transfer_image_common(bus
, m
);
2029 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2030 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2031 _cleanup_close_
int fd
= -1;
2032 const char *local
= NULL
, *path
= NULL
;
2033 sd_bus
*bus
= ASSERT_PTR(userdata
);
2037 if (!hostname_is_valid(local
, 0))
2038 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2039 "Machine name %s is not valid.", local
);
2043 path
= empty_or_dash_to_null(path
);
2046 determine_compression_from_filename(path
);
2048 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2050 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2053 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "ExportRaw");
2055 return bus_log_create_error(r
);
2057 r
= sd_bus_message_append(
2061 fd
>= 0 ? fd
: STDOUT_FILENO
,
2064 return bus_log_create_error(r
);
2066 return transfer_image_common(bus
, m
);
2069 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2070 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2071 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2072 const char *local
, *remote
;
2073 sd_bus
*bus
= ASSERT_PTR(userdata
);
2077 if (!http_url_is_valid(remote
) && !file_url_is_valid(remote
))
2078 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2079 "URL '%s' is not valid.", remote
);
2084 r
= import_url_last_component(remote
, &l
);
2086 return log_error_errno(r
, "Failed to get final component of URL: %m");
2091 local
= empty_or_dash_to_null(local
);
2094 r
= tar_strip_suffixes(local
, &ll
);
2100 if (!hostname_is_valid(local
, 0))
2101 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2102 "Local name %s is not a suitable machine name.",
2106 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "PullTar");
2108 return bus_log_create_error(r
);
2110 r
= sd_bus_message_append(
2115 import_verify_to_string(arg_verify
),
2118 return bus_log_create_error(r
);
2120 return transfer_image_common(bus
, m
);
2123 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2124 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2125 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2126 const char *local
, *remote
;
2127 sd_bus
*bus
= ASSERT_PTR(userdata
);
2131 if (!http_url_is_valid(remote
) && !file_url_is_valid(remote
))
2132 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2133 "URL '%s' is not valid.", remote
);
2138 r
= import_url_last_component(remote
, &l
);
2140 return log_error_errno(r
, "Failed to get final component of URL: %m");
2145 local
= empty_or_dash_to_null(local
);
2148 r
= raw_strip_suffixes(local
, &ll
);
2154 if (!hostname_is_valid(local
, 0))
2155 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2156 "Local name %s is not a suitable machine name.",
2160 r
= bus_message_new_method_call(bus
, &m
, bus_import_mgr
, "PullRaw");
2162 return bus_log_create_error(r
);
2164 r
= sd_bus_message_append(
2169 import_verify_to_string(arg_verify
),
2172 return bus_log_create_error(r
);
2174 return transfer_image_common(bus
, m
);
2177 typedef struct TransferInfo
{
2185 static int compare_transfer_info(const TransferInfo
*a
, const TransferInfo
*b
) {
2186 return strcmp(a
->local
, b
->local
);
2189 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2190 size_t max_type
= STRLEN("TYPE"), max_local
= STRLEN("LOCAL"), max_remote
= STRLEN("REMOTE");
2191 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2192 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2193 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2194 const char *type
, *remote
, *local
;
2195 sd_bus
*bus
= userdata
;
2196 uint32_t id
, max_id
= 0;
2197 size_t n_transfers
= 0;
2201 pager_open(arg_pager_flags
);
2203 r
= bus_call_method(bus
, bus_import_mgr
, "ListTransfers", &error
, &reply
, NULL
);
2205 return log_error_errno(r
, "Could not get transfers: %s", bus_error_message(&error
, r
));
2207 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2209 return bus_log_parse_error(r
);
2211 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, NULL
)) > 0) {
2214 if (!GREEDY_REALLOC(transfers
, n_transfers
+ 1))
2217 transfers
[n_transfers
].id
= id
;
2218 transfers
[n_transfers
].type
= type
;
2219 transfers
[n_transfers
].remote
= remote
;
2220 transfers
[n_transfers
].local
= local
;
2221 transfers
[n_transfers
].progress
= progress
;
2241 return bus_log_parse_error(r
);
2243 r
= sd_bus_message_exit_container(reply
);
2245 return bus_log_parse_error(r
);
2247 typesafe_qsort(transfers
, n_transfers
, compare_transfer_info
);
2249 if (arg_legend
&& n_transfers
> 0)
2250 printf("%-*s %-*s %-*s %-*s %-*s\n",
2251 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2253 (int) max_type
, "TYPE",
2254 (int) max_local
, "LOCAL",
2255 (int) max_remote
, "REMOTE");
2257 for (size_t j
= 0; j
< n_transfers
; j
++)
2259 if (transfers
[j
].progress
< 0)
2260 printf("%*" PRIu32
" %*s %-*s %-*s %-*s\n",
2261 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2263 (int) max_type
, transfers
[j
].type
,
2264 (int) max_local
, transfers
[j
].local
,
2265 (int) max_remote
, transfers
[j
].remote
);
2267 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2268 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2269 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2270 (int) max_type
, transfers
[j
].type
,
2271 (int) max_local
, transfers
[j
].local
,
2272 (int) max_remote
, transfers
[j
].remote
);
2275 if (n_transfers
> 0)
2276 printf("\n%zu transfers listed.\n", n_transfers
);
2278 printf("No transfers.\n");
2284 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2285 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2286 sd_bus
*bus
= ASSERT_PTR(userdata
);
2289 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2291 for (int i
= 1; i
< argc
; i
++) {
2294 r
= safe_atou32(argv
[i
], &id
);
2296 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2298 r
= bus_call_method(bus
, bus_import_mgr
, "CancelTransfer", &error
, NULL
, "u", id
);
2300 return log_error_errno(r
, "Could not cancel transfer: %s", bus_error_message(&error
, r
));
2306 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2307 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2308 sd_bus
*bus
= userdata
;
2312 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2314 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2317 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2319 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
2323 /* With two arguments changes the quota limit of the
2324 * specified image */
2325 r
= bus_call_method(bus
, bus_machine_mgr
, "SetImageLimit", &error
, NULL
, "st", argv
[1], limit
);
2327 /* With one argument changes the pool quota limit */
2328 r
= bus_call_method(bus
, bus_machine_mgr
, "SetPoolLimit", &error
, NULL
, "t", limit
);
2331 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
2336 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2337 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2338 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2339 uint64_t usage
, total
= 0;
2340 sd_bus
*bus
= userdata
;
2345 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
2347 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CleanPool");
2349 return bus_log_create_error(r
);
2351 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2353 return bus_log_create_error(r
);
2355 /* This is a slow operation, hence permit a longer time for completion. */
2356 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2358 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2360 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2362 return bus_log_parse_error(r
);
2364 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2365 if (usage
== UINT64_MAX
) {
2366 log_info("Removed image '%s'", name
);
2369 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2370 name
, FORMAT_BYTES(usage
));
2371 if (total
!= UINT64_MAX
)
2377 r
= sd_bus_message_exit_container(reply
);
2379 return bus_log_parse_error(r
);
2381 if (total
== UINT64_MAX
)
2382 log_info("Removed %u images in total.", c
);
2384 log_info("Removed %u images in total. Total freed exclusive disk space: %s.",
2385 c
, FORMAT_BYTES(total
));
2390 static int help(int argc
, char *argv
[], void *userdata
) {
2391 _cleanup_free_
char *link
= NULL
;
2394 pager_open(arg_pager_flags
);
2396 r
= terminal_urlify_man("machinectl", "1", &link
);
2400 printf("%s [OPTIONS...] COMMAND ...\n\n"
2401 "%sSend control commands to or query the virtual machine and container%s\n"
2402 "%sregistration manager.%s\n"
2403 "\nMachine Commands:\n"
2404 " list List running VMs and containers\n"
2405 " status NAME... Show VM/container details\n"
2406 " show [NAME...] Show properties of one or more VMs/containers\n"
2407 " start NAME... Start container as a service\n"
2408 " login [NAME] Get a login prompt in a container or on the\n"
2410 " shell [[USER@]NAME [COMMAND...]]\n"
2411 " Invoke a shell (or other command) in a container\n"
2412 " or on the local host\n"
2413 " enable NAME... Enable automatic container start at boot\n"
2414 " disable NAME... Disable automatic container start at boot\n"
2415 " poweroff NAME... Power off one or more containers\n"
2416 " reboot NAME... Reboot one or more containers\n"
2417 " terminate NAME... Terminate one or more VMs/containers\n"
2418 " kill NAME... Send signal to processes of a VM/container\n"
2419 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2420 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2421 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2423 " list-images Show available container and VM images\n"
2424 " image-status [NAME...] Show image details\n"
2425 " show-image [NAME...] Show properties of image\n"
2426 " clone NAME NAME Clone an image\n"
2427 " rename NAME NAME Rename an image\n"
2428 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2429 " remove NAME... Remove an image\n"
2430 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2431 " clean Remove hidden (or all) images\n\n"
2432 "Image Transfer Commands:\n"
2433 " pull-tar URL [NAME] Download a TAR container image\n"
2434 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2435 " import-tar FILE [NAME] Import a local TAR container image\n"
2436 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2437 " import-fs DIRECTORY [NAME] Import a local directory container image\n"
2438 " export-tar NAME [FILE] Export a TAR container image locally\n"
2439 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2440 " list-transfers Show list of downloads in progress\n"
2441 " cancel-transfer Cancel a download\n"
2443 " -h --help Show this help\n"
2444 " --version Show package version\n"
2445 " --no-pager Do not pipe output into a pager\n"
2446 " --no-legend Do not show the headers and footers\n"
2447 " --no-ask-password Do not ask for system passwords\n"
2448 " -H --host=[USER@]HOST Operate on remote host\n"
2449 " -M --machine=CONTAINER Operate on local container\n"
2450 " -p --property=NAME Show only properties by this name\n"
2451 " -q --quiet Suppress output\n"
2452 " -a --all Show all properties, including empty ones\n"
2453 " --value When showing properties, only print the value\n"
2454 " -l --full Do not ellipsize output\n"
2455 " --kill-whom=WHOM Whom to send signal to\n"
2456 " -s --signal=SIGNAL Which signal to send\n"
2457 " --uid=USER Specify user ID to invoke shell as\n"
2458 " -E --setenv=VAR[=VALUE] Add an environment variable for shell\n"
2459 " --read-only Create read-only bind mount\n"
2460 " --mkdir Create directory before bind mounting, if missing\n"
2461 " -n --lines=INTEGER Number of journal entries to show\n"
2462 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2463 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2464 " short-iso, short-iso-precise, short-full,\n"
2465 " short-monotonic, short-unix, short-delta,\n"
2466 " json, json-pretty, json-sse, json-seq, cat,\n"
2467 " verbose, export, with-unit)\n"
2468 " --verify=MODE Verification mode for downloaded images (no,\n"
2469 " checksum, signature)\n"
2470 " --force Download image even if already exists\n"
2471 "\nSee the %s for details.\n",
2472 program_invocation_short_name
,
2482 static int parse_argv(int argc
, char *argv
[]) {
2485 ARG_VERSION
= 0x100,
2492 ARG_NO_ASK_PASSWORD
,
2500 static const struct option options
[] = {
2501 { "help", no_argument
, NULL
, 'h' },
2502 { "version", no_argument
, NULL
, ARG_VERSION
},
2503 { "property", required_argument
, NULL
, 'p' },
2504 { "all", no_argument
, NULL
, 'a' },
2505 { "value", no_argument
, NULL
, ARG_VALUE
},
2506 { "full", no_argument
, NULL
, 'l' },
2507 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2508 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2509 { "kill-whom", required_argument
, NULL
, ARG_KILL_WHOM
},
2510 { "signal", required_argument
, NULL
, 's' },
2511 { "host", required_argument
, NULL
, 'H' },
2512 { "machine", required_argument
, NULL
, 'M' },
2513 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2514 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2515 { "quiet", no_argument
, NULL
, 'q' },
2516 { "lines", required_argument
, NULL
, 'n' },
2517 { "output", required_argument
, NULL
, 'o' },
2518 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2519 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2520 { "force", no_argument
, NULL
, ARG_FORCE
},
2521 { "format", required_argument
, NULL
, ARG_FORMAT
},
2522 { "uid", required_argument
, NULL
, ARG_UID
},
2523 { "setenv", required_argument
, NULL
, 'E' },
2524 { "max-addresses", required_argument
, NULL
, ARG_MAX_ADDRESSES
},
2528 bool reorder
= false;
2529 int c
, r
, shell
= -1;
2535 static const char option_string
[] = "-hp:als:H:M:qn:o:E:";
2537 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2543 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2544 * non-option argument was discovered. */
2548 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2549 * for switches after the main verb. However, for "shell" we really don't want that, since we
2550 * want that switches specified after the machine name are passed to the program to execute,
2551 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2552 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2553 * non-option parameter. If it's the verb "shell" we remember its position and continue
2554 * processing options. In this case, as soon as we hit the next non-option argument we found
2555 * the machine name, and stop further processing. If the first non-option argument is any other
2556 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2560 /* If we already found the "shell" verb on the command line, and now found the next
2561 * non-option argument, then this is the machine name and we should stop processing
2562 * further arguments. */
2563 optind
--; /* don't process this argument, go one step back */
2566 if (streq(optarg
, "shell"))
2567 /* Remember the position of the "shell" verb, and continue processing normally. */
2572 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2573 * processing normally. */
2576 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2577 * at least once with a reset option index. Hence, let's reset the option index here,
2578 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2579 * likely already processed it), and the bump the option index so that we read the
2580 * intended argument again. */
2581 saved_optind
= optind
;
2583 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2584 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2590 return help(0, NULL
, NULL
);
2596 r
= strv_extend(&arg_property
, optarg
);
2600 /* If the user asked for a particular
2601 * property, show it to them, even if it is
2603 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2607 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2612 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
, true);
2620 if (safe_atou(optarg
, &arg_lines
) < 0)
2621 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2622 "Failed to parse lines '%s'", optarg
);
2626 if (streq(optarg
, "help")) {
2627 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2631 r
= output_mode_from_string(optarg
);
2633 return log_error_errno(r
, "Unknown output '%s'.", optarg
);
2636 if (OUTPUT_MODE_IS_JSON(arg_output
))
2641 arg_pager_flags
|= PAGER_DISABLE
;
2649 arg_kill_whom
= optarg
;
2653 r
= parse_signal_argument(optarg
, &arg_signal
);
2658 case ARG_NO_ASK_PASSWORD
:
2659 arg_ask_password
= false;
2663 arg_transport
= BUS_TRANSPORT_REMOTE
;
2668 arg_transport
= BUS_TRANSPORT_MACHINE
;
2673 arg_read_only
= true;
2685 if (streq(optarg
, "help")) {
2686 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2690 r
= import_verify_from_string(optarg
);
2692 return log_error_errno(r
, "Failed to parse --verify= setting: %s", optarg
);
2701 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2"))
2702 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2703 "Unknown format: %s", optarg
);
2705 arg_format
= optarg
;
2713 r
= strv_env_replace_strdup_passthrough(&arg_setenv
, optarg
);
2715 return log_error_errno(r
, "Cannot assign environment variable %s: %m", optarg
);
2718 case ARG_MAX_ADDRESSES
:
2719 if (streq(optarg
, "all"))
2720 arg_max_addresses
= ALL_ADDRESSES
;
2721 else if (safe_atoi(optarg
, &arg_max_addresses
) < 0)
2722 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2723 "Invalid number of addresses: %s", optarg
);
2724 else if (arg_max_addresses
<= 0)
2725 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2726 "Number of IPs cannot be negative or zero: %s", optarg
);
2733 assert_not_reached();
2741 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2742 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2744 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2747 for (int i
= shell
; i
< optind
; i
++)
2748 argv
[i
] = argv
[i
+1];
2755 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
2757 static const Verb verbs
[] = {
2758 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
2759 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
2760 { "list-images", VERB_ANY
, 1, 0, list_images
},
2761 { "status", 2, VERB_ANY
, 0, show_machine
},
2762 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
2763 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
2764 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
2765 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
2766 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
2767 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
2768 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
2769 { "kill", 2, VERB_ANY
, 0, kill_machine
},
2770 { "login", VERB_ANY
, 2, 0, login_machine
},
2771 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
2772 { "bind", 3, 4, 0, bind_mount
},
2773 { "copy-to", 3, 4, 0, copy_files
},
2774 { "copy-from", 3, 4, 0, copy_files
},
2775 { "remove", 2, VERB_ANY
, 0, remove_image
},
2776 { "rename", 3, 3, 0, rename_image
},
2777 { "clone", 3, 3, 0, clone_image
},
2778 { "read-only", 2, 3, 0, read_only_image
},
2779 { "start", 2, VERB_ANY
, 0, start_machine
},
2780 { "enable", 2, VERB_ANY
, 0, enable_machine
},
2781 { "disable", 2, VERB_ANY
, 0, enable_machine
},
2782 { "import-tar", 2, 3, 0, import_tar
},
2783 { "import-raw", 2, 3, 0, import_raw
},
2784 { "import-fs", 2, 3, 0, import_fs
},
2785 { "export-tar", 2, 3, 0, export_tar
},
2786 { "export-raw", 2, 3, 0, export_raw
},
2787 { "pull-tar", 2, 3, 0, pull_tar
},
2788 { "pull-raw", 2, 3, 0, pull_raw
},
2789 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
2790 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
2791 { "set-limit", 2, 3, 0, set_limit
},
2792 { "clean", VERB_ANY
, 1, 0, clean_images
},
2796 return dispatch_verb(argc
, argv
, verbs
, bus
);
2799 static int run(int argc
, char *argv
[]) {
2800 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2803 setlocale(LC_ALL
, "");
2806 /* The journal merging logic potentially needs a lot of fds. */
2807 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
2811 r
= parse_argv(argc
, argv
);
2815 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
2817 return bus_log_connect_error(r
, arg_transport
);
2819 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
2821 return machinectl_main(argc
, argv
, bus
);
2824 DEFINE_MAIN_FUNCTION(run
);