1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <sys/socket.h>
11 #include "sd-journal.h"
13 #include "alloc-util.h"
14 #include "ask-password-agent.h"
16 #include "build-path.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-message-util.h"
22 #include "bus-print-properties.h"
23 #include "bus-unit-procs.h"
24 #include "bus-unit-util.h"
26 #include "bus-wait-for-jobs.h"
27 #include "cgroup-show.h"
28 #include "cgroup-util.h"
29 #include "edit-util.h"
31 #include "format-ifname.h"
32 #include "format-table.h"
33 #include "format-util.h"
34 #include "hostname-util.h"
35 #include "import-util.h"
36 #include "in-addr-util.h"
37 #include "label-util.h"
39 #include "logs-show.h"
40 #include "machine-dbus.h"
41 #include "main-func.h"
42 #include "nulstr-util.h"
43 #include "osc-context.h"
44 #include "output-mode.h"
46 #include "parse-argument.h"
47 #include "parse-util.h"
48 #include "path-util.h"
50 #include "polkit-agent.h"
51 #include "pretty-print.h"
52 #include "process-util.h"
54 #include "runtime-scope.h"
55 #include "stdio-util.h"
56 #include "string-table.h"
57 #include "string-util.h"
59 #include "terminal-util.h"
60 #include "time-util.h"
61 #include "unit-name.h"
64 typedef enum MachineRunner
{
68 _RUNNER_INVALID
= -EINVAL
,
71 static const char* const machine_runner_table
[_RUNNER_MAX
] = {
72 [RUNNER_NSPAWN
] = "nspawn",
73 [RUNNER_VMSPAWN
] = "vmspawn",
76 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(machine_runner
, MachineRunner
);
78 static const char* const machine_runner_unit_prefix_table
[_RUNNER_MAX
] = {
79 [RUNNER_NSPAWN
] = "systemd-nspawn",
80 [RUNNER_VMSPAWN
] = "systemd-vmspawn",
83 static char **arg_property
= NULL
;
84 static bool arg_all
= false;
85 static BusPrintPropertyFlags arg_print_flags
= 0;
86 static bool arg_full
= false;
87 static PagerFlags arg_pager_flags
= 0;
88 static bool arg_legend
= true;
89 static const char *arg_kill_whom
= NULL
;
90 static int arg_signal
= SIGTERM
;
91 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
92 static const char *arg_host
= NULL
;
93 static bool arg_read_only
= false;
94 static bool arg_mkdir
= false;
95 static bool arg_quiet
= false;
96 static bool arg_ask_password
= true;
97 static unsigned arg_lines
= 10;
98 static OutputMode arg_output
= OUTPUT_SHORT
;
99 static bool arg_now
= false;
100 static bool arg_force
= false;
101 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
102 static MachineRunner arg_runner
= RUNNER_NSPAWN
;
103 static const char* arg_format
= NULL
;
104 static const char *arg_uid
= NULL
;
105 static char **arg_setenv
= NULL
;
106 static unsigned arg_max_addresses
= 1;
108 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
109 STATIC_DESTRUCTOR_REGISTER(arg_setenv
, strv_freep
);
111 static OutputFlags
get_output_flags(void) {
113 FLAGS_SET(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
) * OUTPUT_SHOW_ALL
|
114 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
115 colors_enabled() * OUTPUT_COLOR
|
116 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
119 static int call_get_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *query
, ...) {
120 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
121 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
130 r
= bus_call_method(bus
, bus_machine_mgr
, method
, &error
, &reply
, "s", name
);
132 return log_debug_errno(r
, "Failed to call '%s()': %s", method
, bus_error_message(&error
, r
));
134 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
136 return bus_log_parse_error(r
);
141 NULSTR_FOREACH(i
, query
)
144 res
= newa0(const char*, n_fields
);
147 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
149 NULSTR_FOREACH(i
, query
) {
158 return bus_log_parse_error(r
);
160 r
= sd_bus_message_exit_container(reply
);
162 return bus_log_parse_error(r
);
167 FOREACH_ARRAY(i
, res
, n_fields
) {
168 r
= strdup_to(va_arg(ap
, char**), *i
);
177 static int call_get_addresses(
185 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
186 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
187 _cleanup_free_
char *addresses
= NULL
;
196 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachineAddresses", NULL
, &reply
, "s", name
);
198 return log_debug_errno(r
, "Could not get addresses: %s", bus_error_message(&error
, r
));
200 addresses
= strdup(prefix
);
204 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
206 return bus_log_parse_error(r
);
209 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
213 char buf_ifi
[1 + DECIMAL_STR_MAX(int)] = "";
215 r
= sd_bus_message_read(reply
, "i", &family
);
217 return bus_log_parse_error(r
);
219 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
221 return bus_log_parse_error(r
);
223 if (family
== AF_INET6
&& ifi
> 0)
224 xsprintf(buf_ifi
, "%%%i", ifi
);
226 if (!strextend(&addresses
, prefix
, IN_ADDR_TO_STRING(family
, a
), buf_ifi
))
229 r
= sd_bus_message_exit_container(reply
);
231 return bus_log_parse_error(r
);
238 return bus_log_parse_error(r
);
240 r
= sd_bus_message_exit_container(reply
);
242 return bus_log_parse_error(r
);
244 *ret
= TAKE_PTR(addresses
);
248 static int show_table(Table
*table
, const char *word
) {
254 if (!table_isempty(table
) || OUTPUT_MODE_IS_JSON(arg_output
)) {
255 r
= table_set_sort(table
, (size_t) 0);
257 return table_log_sort_error(r
);
259 table_set_header(table
, arg_legend
);
261 if (OUTPUT_MODE_IS_JSON(arg_output
))
262 r
= table_print_json(table
, NULL
, output_mode_to_json_format_flags(arg_output
) | SD_JSON_FORMAT_COLOR_AUTO
);
264 r
= table_print(table
, NULL
);
266 return table_log_print_error(r
);
270 if (table_isempty(table
))
271 printf("No %s.\n", word
);
273 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
279 static int list_machines(int argc
, char *argv
[], void *userdata
) {
280 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
281 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
282 _cleanup_(table_unrefp
) Table
*table
= NULL
;
283 sd_bus
*bus
= ASSERT_PTR(userdata
);
286 pager_open(arg_pager_flags
);
288 r
= bus_call_method(bus
, bus_machine_mgr
, "ListMachines", &error
, &reply
, NULL
);
290 return log_error_errno(r
, "Could not get machines: %s", bus_error_message(&error
, r
));
292 table
= table_new("machine", "class", "service", "os", "version",
293 arg_max_addresses
> 0 ? "addresses" : NULL
);
297 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
298 if (!arg_full
&& arg_max_addresses
> 0 && arg_max_addresses
< UINT_MAX
)
299 table_set_cell_height_max(table
, arg_max_addresses
);
302 table_set_width(table
, 0);
304 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
306 return bus_log_parse_error(r
);
309 _cleanup_free_
char *os
= NULL
, *version_id
= NULL
, *addresses
= NULL
;
310 const char *name
, *class, *service
;
312 r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, NULL
);
314 return bus_log_parse_error(r
);
318 if (name
[0] == '.' && !arg_all
)
321 (void) call_get_os_release(
323 "GetMachineOSRelease",
330 r
= table_add_many(table
,
331 TABLE_STRING
, empty_to_null(name
),
332 TABLE_STRING
, empty_to_null(class),
333 TABLE_STRING
, empty_to_null(service
),
334 TABLE_STRING
, empty_to_null(os
),
335 TABLE_STRING
, empty_to_null(version_id
));
337 return table_log_add_error(r
);
339 if (arg_max_addresses
> 0) {
340 (void) call_get_addresses(bus
, name
, 0, "", "\n", &addresses
);
342 r
= table_add_many(table
,
343 TABLE_STRING
, empty_to_null(addresses
));
345 return table_log_add_error(r
);
349 r
= sd_bus_message_exit_container(reply
);
351 return bus_log_parse_error(r
);
353 return show_table(table
, "machines");
356 static int list_images(int argc
, char *argv
[], void *userdata
) {
358 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
359 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
360 _cleanup_(table_unrefp
) Table
*table
= NULL
;
361 sd_bus
*bus
= ASSERT_PTR(userdata
);
364 pager_open(arg_pager_flags
);
366 r
= bus_call_method(bus
, bus_machine_mgr
, "ListImages", &error
, &reply
, NULL
);
368 return log_error_errno(r
, "Could not get images: %s", bus_error_message(&error
, r
));
370 table
= table_new("name", "type", "ro", "usage", "created", "modified");
375 table_set_width(table
, 0);
377 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100);
379 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
381 return bus_log_parse_error(r
);
384 uint64_t crtime
, mtime
, size
;
385 const char *name
, *type
;
388 r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &size
, NULL
);
390 return bus_log_parse_error(r
);
394 if (name
[0] == '.' && !arg_all
)
397 r
= table_add_many(table
,
400 TABLE_BOOLEAN
, ro_int
,
401 TABLE_SET_COLOR
, ro_int
? ansi_highlight_red() : NULL
,
403 TABLE_TIMESTAMP
, crtime
,
404 TABLE_TIMESTAMP
, mtime
);
406 return table_log_add_error(r
);
409 r
= sd_bus_message_exit_container(reply
);
411 return bus_log_parse_error(r
);
413 return show_table(table
, "images");
416 static int show_unit_cgroup(
419 const char *subgroup
,
422 _cleanup_free_
char *cgroup
= NULL
;
423 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
424 OutputFlags extra_flags
= 0;
430 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
437 if (!empty_or_root(subgroup
)) {
438 if (!path_extend(&cgroup
, subgroup
))
441 /* If we have a subcgroup, then hide all processes outside of it */
442 extra_flags
|= OUTPUT_HIDE_EXTRA
;
445 unsigned c
= MAX(LESS_BY(columns(), 18U), 10U);
446 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags() | extra_flags
, &error
);
449 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
452 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
454 if (cg_is_empty(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
457 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
459 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
464 static int print_os_release(sd_bus
*bus
, const char *method
, const char *name
, const char *prefix
) {
465 _cleanup_free_
char *pretty
= NULL
;
472 r
= call_get_os_release(bus
, method
, name
, "PRETTY_NAME\0", &pretty
, NULL
);
477 printf("%s%s\n", prefix
, pretty
);
482 static int print_uid_shift(sd_bus
*bus
, const char *name
) {
483 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
484 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
491 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachineUIDShift", &error
, &reply
, "s", name
);
493 return log_debug_errno(r
, "Failed to query UID/GID shift: %s", bus_error_message(&error
, r
));
495 r
= sd_bus_message_read(reply
, "u", &shift
);
499 if (shift
== 0) /* Don't show trivial mappings */
502 printf("\tID Shift: %" PRIu32
"\n", shift
);
506 typedef struct MachineStatusInfo
{
512 const char *subgroup
;
513 const char *root_directory
;
515 uint64_t leader_pidfdid
;
517 uint64_t supervisor_pidfdid
;
518 struct dual_timestamp timestamp
;
524 static void machine_status_info_done(MachineStatusInfo
*info
) {
532 static void print_process_info(const char *field
, pid_t pid
, uint64_t pidfdid
) {
540 printf("%s: " PID_FMT
, field
, pid
);
542 _cleanup_(pidref_done
) PidRef pr
= PIDREF_NULL
;
543 r
= pidref_set_pid_and_pidfd_id(&pr
, pid
, pidfdid
);
545 log_debug_errno(r
, "Failed to acquire reference to process, ignoring: %m");
547 _cleanup_free_
char *t
= NULL
;
548 r
= pidref_get_comm(&pr
, &t
);
550 log_debug_errno(r
, "Failed to acquire name of process, ignoring: %m");
558 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
559 _cleanup_free_
char *addresses
= NULL
, *s1
= NULL
, *s2
= NULL
;
565 fputs(strna(i
->name
), stdout
);
567 if (!sd_id128_is_null(i
->id
))
568 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
572 s1
= strdup(strempty(FORMAT_TIMESTAMP_RELATIVE(i
->timestamp
.realtime
)));
573 s2
= strdup(strempty(FORMAT_TIMESTAMP(i
->timestamp
.realtime
)));
576 printf("\t Since: %s; %s\n", strna(s2
), s1
);
577 else if (!isempty(s2
))
578 printf("\t Since: %s\n", s2
);
580 print_process_info("\t Leader", i
->leader
, i
->leader_pidfdid
);
581 print_process_info("\t Superv.", i
->supervisor
, i
->supervisor_pidfdid
);
584 printf("\t Service: %s", i
->service
);
587 printf("; class %s", i
->class);
591 printf("\t Class: %s\n", i
->class);
594 printf("\t UID: " UID_FMT
"\n", i
->uid
);
596 if (i
->root_directory
)
597 printf("\t Root: %s\n", i
->root_directory
);
599 if (i
->n_netif
> 0) {
600 fputs("\t Iface:", stdout
);
602 for (size_t c
= 0; c
< i
->n_netif
; c
++) {
603 char name
[IF_NAMESIZE
];
605 if (format_ifname(i
->netif
[c
], name
) >= 0) {
614 printf(" %i", i
->netif
[c
]);
620 if (call_get_addresses(bus
, i
->name
, ifi
,
621 "\t Address: ", "\n\t ",
623 fputs(addresses
, stdout
);
627 print_os_release(bus
, "GetMachineOSRelease", i
->name
, "\t OS: ");
629 print_uid_shift(bus
, i
->name
);
632 printf("\t Unit: %s\n", i
->unit
);
634 if (!empty_or_root(i
->subgroup
))
635 printf("\tSubgroup: %s\n", i
->subgroup
);
637 show_unit_cgroup(bus
, i
->unit
, i
->subgroup
, i
->leader
);
639 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
641 show_journal_by_unit(
644 /* namespace = */ NULL
,
647 i
->timestamp
.monotonic
,
649 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
650 SD_JOURNAL_LOCAL_ONLY
,
651 /* system_unit = */ true,
652 /* ellipsized = */ NULL
);
656 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
657 MachineStatusInfo
*i
= userdata
;
662 assert_cc(sizeof(int32_t) == sizeof(int));
663 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
669 i
->n_netif
= l
/ sizeof(int32_t);
670 i
->netif
= memdup(v
, l
);
677 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
679 static const struct bus_properties_map map
[] = {
680 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
681 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
682 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
683 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
684 { "Subgroup", "s", NULL
, offsetof(MachineStatusInfo
, subgroup
) },
685 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
686 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
687 { "LeaderPIDFDId", "t", NULL
, offsetof(MachineStatusInfo
, leader_pidfdid
) },
688 { "Supervisor", "u", NULL
, offsetof(MachineStatusInfo
, supervisor
) },
689 { "SupervisorPIDFDId", "t", NULL
, offsetof(MachineStatusInfo
, supervisor_pidfdid
) },
690 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
691 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
692 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
693 { "NetworkInterfaces", "ai", map_netif
, 0 },
694 { "UID", "u", NULL
, offsetof(MachineStatusInfo
, uid
) },
698 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
699 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
700 _cleanup_(machine_status_info_done
) MachineStatusInfo info
= {};
708 r
= bus_map_all_properties(bus
,
709 "org.freedesktop.machine1",
717 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
723 print_machine_status_info(bus
, &info
);
728 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
740 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
742 log_error_errno(r
, "Could not get properties: %m");
747 static int show_machine(int argc
, char *argv
[], void *userdata
) {
748 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
749 bool properties
, new_line
= false;
750 sd_bus
*bus
= ASSERT_PTR(userdata
);
753 properties
= !strstr(argv
[0], "status");
755 pager_open(arg_pager_flags
);
757 if (properties
&& argc
<= 1) {
759 /* If no argument is specified, inspect the manager
761 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
766 for (int i
= 1; i
< argc
; i
++) {
767 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
768 const char *path
= NULL
;
770 r
= bus_call_method(bus
, bus_machine_mgr
, "GetMachine", &error
, &reply
, "s", argv
[i
]);
772 return log_error_errno(r
, "Could not get path to machine: %s", bus_error_message(&error
, r
));
774 r
= sd_bus_message_read(reply
, "o", &path
);
776 return bus_log_parse_error(r
);
779 r
= show_machine_properties(bus
, path
, &new_line
);
781 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
787 static int print_image_hostname(sd_bus
*bus
, const char *name
) {
788 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
792 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageHostname", NULL
, &reply
, "s", name
);
796 r
= sd_bus_message_read(reply
, "s", &hn
);
801 printf("\tHostname: %s\n", hn
);
806 static int print_image_machine_id(sd_bus
*bus
, const char *name
) {
807 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
811 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineID", NULL
, &reply
, "s", name
);
815 r
= bus_message_read_id128(reply
, &id
);
819 if (!sd_id128_is_null(id
))
820 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(id
));
825 static int print_image_machine_info(sd_bus
*bus
, const char *name
) {
826 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
829 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImageMachineInfo", NULL
, &reply
, "s", name
);
833 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
840 r
= sd_bus_message_read(reply
, "{ss}", &p
, &q
);
846 if (streq(p
, "DEPLOYMENT"))
847 printf(" Deployment: %s\n", q
);
850 r
= sd_bus_message_exit_container(reply
);
857 typedef struct ImageStatusInfo
{
866 uint64_t usage_exclusive
;
867 uint64_t limit_exclusive
;
870 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
875 fputs(i
->name
, stdout
);
880 printf("\t Type: %s\n", i
->type
);
883 printf("\t Path: %s\n", i
->path
);
885 (void) print_image_hostname(bus
, i
->name
);
886 (void) print_image_machine_id(bus
, i
->name
);
887 (void) print_image_machine_info(bus
, i
->name
);
889 print_os_release(bus
, "GetImageOSRelease", i
->name
, "\t OS: ");
891 printf("\t RO: %s%s%s\n",
892 i
->read_only
? ansi_highlight_red() : "",
893 i
->read_only
? "read-only" : "writable",
894 i
->read_only
? ansi_normal() : "");
896 if (timestamp_is_set(i
->crtime
))
897 printf("\t Created: %s; %s\n",
898 FORMAT_TIMESTAMP(i
->crtime
), FORMAT_TIMESTAMP_RELATIVE(i
->crtime
));
900 if (timestamp_is_set(i
->mtime
))
901 printf("\tModified: %s; %s\n",
902 FORMAT_TIMESTAMP(i
->mtime
), FORMAT_TIMESTAMP_RELATIVE(i
->mtime
));
904 if (i
->usage
!= UINT64_MAX
) {
905 if (i
->usage_exclusive
!= i
->usage
&& i
->usage_exclusive
!= UINT64_MAX
)
906 printf("\t Usage: %s (exclusive: %s)\n",
907 FORMAT_BYTES(i
->usage
), FORMAT_BYTES(i
->usage_exclusive
));
909 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
912 if (i
->limit
!= UINT64_MAX
) {
913 if (i
->limit_exclusive
!= i
->limit
&& i
->limit_exclusive
!= UINT64_MAX
)
914 printf("\t Limit: %s (exclusive: %s)\n",
915 FORMAT_BYTES(i
->limit
), FORMAT_BYTES(i
->limit_exclusive
));
917 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
921 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
923 static const struct bus_properties_map map
[] = {
924 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
925 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
926 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
927 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
928 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
929 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
930 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
931 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
932 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
933 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
937 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
938 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
939 ImageStatusInfo info
= {};
946 r
= bus_map_all_properties(bus
,
947 "org.freedesktop.machine1",
950 BUS_MAP_BOOLEAN_AS_BOOL
,
955 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
961 print_image_status_info(bus
, &info
);
966 typedef struct PoolStatusInfo
{
972 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
974 printf("\t Path: %s\n", i
->path
);
976 if (i
->usage
!= UINT64_MAX
)
977 printf("\t Usage: %s\n", FORMAT_BYTES(i
->usage
));
979 if (i
->limit
!= UINT64_MAX
)
980 printf("\t Limit: %s\n", FORMAT_BYTES(i
->limit
));
983 static int show_pool_info(sd_bus
*bus
) {
985 static const struct bus_properties_map map
[] = {
986 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
987 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
988 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
992 PoolStatusInfo info
= {
997 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
998 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1003 r
= bus_map_all_properties(bus
,
1004 "org.freedesktop.machine1",
1005 "/org/freedesktop/machine1",
1012 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
1014 print_pool_status_info(bus
, &info
);
1019 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
1031 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, NULL
, arg_property
, arg_print_flags
, NULL
);
1033 log_error_errno(r
, "Could not get properties: %m");
1038 static int show_image(int argc
, char *argv
[], void *userdata
) {
1039 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1040 bool properties
, new_line
= false;
1041 sd_bus
*bus
= ASSERT_PTR(userdata
);
1044 properties
= !strstr(argv
[0], "status");
1046 pager_open(arg_pager_flags
);
1050 /* If no argument is specified, inspect the manager
1054 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
1056 r
= show_pool_info(bus
);
1061 for (int i
= 1; i
< argc
; i
++) {
1062 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1063 const char *path
= NULL
;
1065 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, &reply
, "s", argv
[i
]);
1067 return log_error_errno(r
, "Could not get path to image: %s", bus_error_message(&error
, r
));
1069 r
= sd_bus_message_read(reply
, "o", &path
);
1071 return bus_log_parse_error(r
);
1074 r
= show_image_properties(bus
, path
, &new_line
);
1076 r
= show_image_info(bus
, path
, &new_line
);
1082 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1083 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1084 sd_bus
*bus
= ASSERT_PTR(userdata
);
1087 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1090 arg_kill_whom
= "all";
1092 for (int i
= 1; i
< argc
; i
++) {
1093 r
= bus_call_method(
1099 "ssi", argv
[i
], arg_kill_whom
, arg_signal
);
1101 return log_error_errno(r
, "Could not kill machine: %s", bus_error_message(&error
, r
));
1107 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1108 if (arg_runner
== RUNNER_VMSPAWN
)
1109 return log_error_errno(
1110 SYNTHETIC_ERRNO(EOPNOTSUPP
),
1111 "%s only support supported for --runner=nspawn",
1112 streq(argv
[0], "reboot") ? "Reboot" : "Restart");
1114 arg_kill_whom
= "leader";
1115 arg_signal
= SIGINT
; /* sysvinit + systemd */
1117 return kill_machine(argc
, argv
, userdata
);
1120 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1121 arg_kill_whom
= "leader";
1122 arg_signal
= SIGRTMIN
+4; /* only systemd */
1124 return kill_machine(argc
, argv
, userdata
);
1127 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1128 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1129 sd_bus
*bus
= ASSERT_PTR(userdata
);
1132 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1134 for (int i
= 1; i
< argc
; i
++) {
1135 r
= bus_call_method(bus
, bus_machine_mgr
, "TerminateMachine", &error
, NULL
, "s", argv
[i
]);
1137 return log_error_errno(r
, "Could not terminate machine: %s", bus_error_message(&error
, r
));
1143 static const char *select_copy_method(bool copy_from
, bool force
) {
1145 return copy_from
? "CopyFromMachineWithFlags" : "CopyToMachineWithFlags";
1147 return copy_from
? "CopyFromMachine" : "CopyToMachine";
1150 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1151 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1152 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1153 _cleanup_free_
char *abs_host_path
= NULL
;
1154 char *dest
, *host_path
, *container_path
;
1155 sd_bus
*bus
= ASSERT_PTR(userdata
);
1159 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1161 copy_from
= streq(argv
[0], "copy-from");
1162 dest
= argv
[3] ?: argv
[2];
1163 host_path
= copy_from
? dest
: argv
[2];
1164 container_path
= copy_from
? argv
[2] : dest
;
1166 if (!path_is_absolute(host_path
)) {
1167 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1169 return log_error_errno(r
, "Failed to make path absolute: %m");
1171 host_path
= abs_host_path
;
1174 r
= bus_message_new_method_call(
1178 select_copy_method(copy_from
, arg_force
));
1180 return bus_log_create_error(r
);
1182 r
= sd_bus_message_append(
1186 copy_from
? container_path
: host_path
,
1187 copy_from
? host_path
: container_path
);
1189 return bus_log_create_error(r
);
1192 r
= sd_bus_message_append(m
, "t", (uint64_t) MACHINE_COPY_REPLACE
);
1194 return bus_log_create_error(r
);
1197 /* This is a slow operation, hence turn off any method call timeouts */
1198 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1200 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1205 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1206 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1207 sd_bus
*bus
= ASSERT_PTR(userdata
);
1210 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1212 r
= bus_call_method(
1225 return log_error_errno(r
, "Failed to bind mount: %s", bus_error_message(&error
, r
));
1230 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1231 PTYForward
*forward
= ASSERT_PTR(userdata
);
1236 /* Tell the forwarder to exit on the next vhangup(), so that we still flush out what might be queued
1239 r
= pty_forward_set_ignore_vhangup(forward
, false);
1241 /* On error, quit immediately. */
1242 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1243 (void) sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1249 static int process_forward(sd_event
*event
, sd_bus_slot
*machine_removed_slot
, int master
, PTYForwardFlags flags
, const char *name
) {
1253 assert(machine_removed_slot
);
1254 assert(master
>= 0);
1258 if (streq(name
, ".host"))
1259 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1261 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1264 _cleanup_(osc_context_closep
) sd_id128_t osc_context_id
= SD_ID128_NULL
;
1265 if (!terminal_is_dumb()) {
1266 r
= osc_context_open_container(name
, /* ret_seq= */ NULL
, &osc_context_id
);
1271 r
= sd_event_set_signal_exit(event
, true);
1273 return log_error_errno(r
, "Failed to enable SIGINT/SITERM handling: %m");
1275 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1276 r
= pty_forward_new(event
, master
, flags
, &forward
);
1278 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1280 /* No userdata should not set previously. */
1281 assert_se(!sd_bus_slot_set_userdata(machine_removed_slot
, forward
));
1283 r
= sd_event_loop(event
);
1285 return log_error_errno(r
, "Failed to run event loop: %m");
1288 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1289 pty_forward_get_ignore_vhangup(forward
) == 0;
1293 log_info("Machine %s terminated.", name
);
1294 else if (streq(name
, ".host"))
1295 log_info("Connection to the local host terminated.");
1297 log_info("Connection to machine %s terminated.", name
);
1303 static int parse_machine_uid(const char *spec
, const char **machine
, char **uid
) {
1305 * Whatever is specified in the spec takes priority over global arguments.
1308 const char *_machine
= NULL
;
1313 at
= strchr(spec
, '@');
1316 /* Do the same as ssh and refuse "@host". */
1320 _uid
= strndup(spec
, at
- spec
);
1327 if (arg_uid
&& !_uid
) {
1328 _uid
= strdup(arg_uid
);
1334 *machine
= isempty(_machine
) ? ".host" : _machine
;
1338 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1339 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1340 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1341 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1342 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1344 sd_bus
*bus
= ASSERT_PTR(userdata
);
1345 const char *match
, *machine
;
1347 if (!strv_isempty(arg_setenv
) || arg_uid
)
1348 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1349 "--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1351 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1352 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1353 "Login only supported on local machines.");
1355 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1357 r
= sd_event_default(&event
);
1359 return log_error_errno(r
, "Failed to get event loop: %m");
1361 r
= sd_bus_attach_event(bus
, event
, 0);
1363 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1365 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1367 match
= strjoina("type='signal',"
1368 "sender='org.freedesktop.machine1',"
1369 "path='/org/freedesktop/machine1',",
1370 "interface='org.freedesktop.machine1.Manager',"
1371 "member='MachineRemoved',"
1372 "arg0='", machine
, "'");
1374 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, NULL
);
1376 return log_error_errno(r
, "Failed to request machine removal match: %m");
1378 r
= bus_call_method(bus
, bus_machine_mgr
, "OpenMachineLogin", &error
, &reply
, "s", machine
);
1380 return log_error_errno(r
, "Failed to get login PTY: %s", bus_error_message(&error
, r
));
1382 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1384 return bus_log_parse_error(r
);
1386 return process_forward(event
, slot
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1389 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1390 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1391 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1392 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1393 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1395 sd_bus
*bus
= ASSERT_PTR(userdata
);
1396 const char *match
, *machine
, *path
;
1397 _cleanup_free_
char *uid
= NULL
;
1399 if (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
))
1400 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1401 "Shell only supported on local machines.");
1403 /* Pass $TERM & Co. to shell session, if not explicitly specified. */
1404 FOREACH_STRING(v
, "TERM=", "COLORTERM=", "NO_COLOR=") {
1405 if (strv_find_prefix(arg_setenv
, v
))
1408 const char *t
= strv_find_prefix(environ
, v
);
1412 if (strv_extend(&arg_setenv
, t
) < 0)
1416 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1418 r
= sd_event_default(&event
);
1420 return log_error_errno(r
, "Failed to get event loop: %m");
1422 r
= sd_bus_attach_event(bus
, event
, 0);
1424 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1426 r
= parse_machine_uid(argc
>= 2 ? argv
[1] : NULL
, &machine
, &uid
);
1428 return log_error_errno(r
, "Failed to parse machine specification: %m");
1430 match
= strjoina("type='signal',"
1431 "sender='org.freedesktop.machine1',"
1432 "path='/org/freedesktop/machine1',",
1433 "interface='org.freedesktop.machine1.Manager',"
1434 "member='MachineRemoved',"
1435 "arg0='", machine
, "'");
1437 r
= sd_bus_add_match_async(bus
, &slot
, match
, on_machine_removed
, NULL
, NULL
);
1439 return log_error_errno(r
, "Failed to request machine removal match: %m");
1441 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "OpenMachineShell");
1443 return bus_log_create_error(r
);
1445 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1447 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1449 return bus_log_create_error(r
);
1451 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1453 return bus_log_create_error(r
);
1455 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1457 return bus_log_create_error(r
);
1459 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1461 return log_error_errno(r
, "Failed to get shell PTY: %s", bus_error_message(&error
, r
));
1463 r
= sd_bus_message_read(reply
, "hs", &master
, NULL
);
1465 return bus_log_parse_error(r
);
1467 return process_forward(event
, slot
, master
, /* flags = */ 0, machine
);
1470 static int normalize_nspawn_filename(const char *name
, char **ret_file
) {
1471 _cleanup_free_
char *file
= NULL
;
1476 if (!endswith(name
, ".nspawn"))
1477 file
= strjoin(name
, ".nspawn");
1479 file
= strdup(name
);
1483 if (!filename_is_valid(file
))
1484 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid settings file name '%s'.", file
);
1486 *ret_file
= TAKE_PTR(file
);
1490 static int get_settings_path(const char *name
, char **ret_path
) {
1494 FOREACH_STRING(i
, "/etc/systemd/nspawn", "/run/systemd/nspawn", "/var/lib/machines") {
1495 _cleanup_free_
char *path
= NULL
;
1497 path
= path_join(i
, name
);
1501 if (access(path
, F_OK
) >= 0) {
1502 *ret_path
= TAKE_PTR(path
);
1510 static int edit_settings(int argc
, char *argv
[], void *userdata
) {
1511 _cleanup_(edit_file_context_done
) EditFileContext context
= {};
1515 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Cannot edit machine settings if not on a tty.");
1517 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
1518 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1519 "Edit is only supported on the host machine.");
1521 if (arg_runner
== RUNNER_VMSPAWN
)
1522 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Edit is only supported for --runner=nspawn");
1528 STRV_FOREACH(name
, strv_skip(argv
, 1)) {
1529 _cleanup_free_
char *file
= NULL
, *path
= NULL
;
1531 if (path_is_absolute(*name
)) {
1532 if (!path_is_safe(*name
))
1533 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1534 "Invalid settings file path '%s'.",
1537 r
= edit_files_add(&context
, *name
, NULL
, NULL
);
1543 r
= normalize_nspawn_filename(*name
, &file
);
1547 r
= get_settings_path(file
, &path
);
1549 log_debug("No existing settings file for machine '%s' found, creating a new file.", *name
);
1551 path
= path_join("/etc/systemd/nspawn", file
);
1555 r
= edit_files_add(&context
, path
, NULL
, NULL
);
1561 return log_error_errno(r
, "Failed to get the path of the settings file: %m");
1563 if (path_startswith(path
, "/var/lib/machines")) {
1564 _cleanup_free_
char *new_path
= NULL
;
1566 new_path
= path_join("/etc/systemd/nspawn", file
);
1570 r
= edit_files_add(&context
, new_path
, path
, NULL
);
1572 r
= edit_files_add(&context
, path
, NULL
, NULL
);
1577 return do_edit_files_and_install(&context
);
1580 static int cat_settings(int argc
, char *argv
[], void *userdata
) {
1583 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
1584 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1585 "Cat is only supported on the host machine.");
1587 if (arg_runner
== RUNNER_VMSPAWN
)
1588 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Cat is only supported for --runner=nspawn");
1590 pager_open(arg_pager_flags
);
1592 STRV_FOREACH(name
, strv_skip(argv
, 1)) {
1593 _cleanup_free_
char *file
= NULL
, *path
= NULL
;
1596 if (path_is_absolute(*name
)) {
1597 if (!path_is_safe(*name
))
1598 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1599 "Invalid settings file path '%s'.",
1602 q
= cat_files(*name
, /* dropins = */ NULL
, /* flags = */ CAT_FORMAT_HAS_SECTIONS
);
1604 return r
< 0 ? r
: q
;
1608 q
= normalize_nspawn_filename(*name
, &file
);
1610 return r
< 0 ? r
: q
;
1612 q
= get_settings_path(file
, &path
);
1614 log_error_errno(q
, "No settings file found for machine '%s'.", *name
);
1619 log_error_errno(q
, "Failed to get the path of the settings file: %m");
1620 return r
< 0 ? r
: q
;
1623 q
= cat_files(path
, /* dropins = */ NULL
, /* flags = */ CAT_FORMAT_HAS_SECTIONS
);
1625 return r
< 0 ? r
: q
;
1631 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1632 sd_bus
*bus
= ASSERT_PTR(userdata
);
1635 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1637 for (int i
= 1; i
< argc
; i
++) {
1638 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1639 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1641 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "RemoveImage");
1643 return bus_log_create_error(r
);
1645 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1647 return bus_log_create_error(r
);
1649 /* This is a slow operation, hence turn off any method call timeouts */
1650 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1652 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1658 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1659 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1660 sd_bus
*bus
= ASSERT_PTR(userdata
);
1663 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1665 r
= bus_call_method(
1671 "ss", argv
[1], argv
[2]);
1673 return log_error_errno(r
, "Could not rename image: %s", bus_error_message(&error
, r
));
1678 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1679 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1680 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1681 sd_bus
*bus
= ASSERT_PTR(userdata
);
1684 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1686 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CloneImage");
1688 return bus_log_create_error(r
);
1690 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1692 return bus_log_create_error(r
);
1694 /* This is a slow operation, hence turn off any method call timeouts */
1695 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1697 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1702 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1703 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1704 sd_bus
*bus
= ASSERT_PTR(userdata
);
1708 b
= parse_boolean(argv
[2]);
1710 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1711 "Failed to parse boolean argument: %s",
1715 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1717 r
= bus_call_method(bus
, bus_machine_mgr
, "MarkImageReadOnly", &error
, NULL
, "sb", argv
[1], b
);
1719 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, r
));
1724 static int image_exists(sd_bus
*bus
, const char *name
) {
1725 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1731 r
= bus_call_method(bus
, bus_machine_mgr
, "GetImage", &error
, NULL
, "s", name
);
1733 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1736 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, r
));
1742 static int make_service_name(const char *name
, char **ret
) {
1747 assert(arg_runner
>= 0 && arg_runner
< _RUNNER_MAX
);
1749 if (!hostname_is_valid(name
, 0))
1750 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1751 "Invalid machine name %s.", name
);
1753 r
= unit_name_build(machine_runner_unit_prefix_table
[arg_runner
], name
, ".service", ret
);
1755 return log_error_errno(r
, "Failed to build unit name: %m");
1760 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1761 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1762 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1763 sd_bus
*bus
= ASSERT_PTR(userdata
);
1766 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1767 ask_password_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1769 r
= bus_wait_for_jobs_new(bus
, &w
);
1771 return log_error_errno(r
, "Could not watch jobs: %m");
1773 for (int i
= 1; i
< argc
; i
++) {
1774 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1775 _cleanup_free_
char *unit
= NULL
;
1778 r
= make_service_name(argv
[i
], &unit
);
1782 r
= image_exists(bus
, argv
[i
]);
1786 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1787 "Machine image '%s' does not exist.",
1790 r
= bus_call_method(
1796 "ss", unit
, "fail");
1798 return log_error_errno(r
, "Failed to start unit: %s", bus_error_message(&error
, r
));
1800 r
= sd_bus_message_read(reply
, "o", &object
);
1802 return bus_log_parse_error(r
);
1804 r
= bus_wait_for_jobs_add(w
, object
);
1809 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1816 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1817 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1818 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1820 sd_bus
*bus
= ASSERT_PTR(userdata
);
1824 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1826 enable
= streq(argv
[0], "enable");
1827 method
= enable
? "EnableUnitFiles" : "DisableUnitFiles";
1829 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, method
);
1831 return bus_log_create_error(r
);
1833 r
= sd_bus_message_open_container(m
, 'a', "s");
1835 return bus_log_create_error(r
);
1838 r
= sd_bus_message_append(m
, "s", "machines.target");
1840 return bus_log_create_error(r
);
1843 for (int i
= 1; i
< argc
; i
++) {
1844 _cleanup_free_
char *unit
= NULL
;
1846 r
= make_service_name(argv
[i
], &unit
);
1850 r
= image_exists(bus
, argv
[i
]);
1854 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
1855 "Machine image '%s' does not exist.",
1858 r
= sd_bus_message_append(m
, "s", unit
);
1860 return bus_log_create_error(r
);
1863 r
= sd_bus_message_close_container(m
);
1865 return bus_log_create_error(r
);
1868 r
= sd_bus_message_append(m
, "bb", false, false);
1870 r
= sd_bus_message_append(m
, "b", false);
1872 return bus_log_create_error(r
);
1874 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1876 return log_error_errno(r
, "Failed to enable or disable unit: %s", bus_error_message(&error
, r
));
1879 r
= sd_bus_message_read(reply
, "b", NULL
);
1881 return bus_log_parse_error(r
);
1884 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
);
1888 r
= bus_service_manager_reload(bus
);
1893 _cleanup_strv_free_
char **new_args
= NULL
;
1895 new_args
= strv_new(enable
? "start" : "poweroff");
1899 r
= strv_extend_strv(&new_args
, argv
+ 1, /* filter_duplicates = */ false);
1904 return start_machine(strv_length(new_args
), new_args
, userdata
);
1906 return poweroff_machine(strv_length(new_args
), new_args
, userdata
);
1912 static int set_limit(int argc
, char *argv
[], void *userdata
) {
1913 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1914 sd_bus
*bus
= userdata
;
1918 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1920 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
1923 r
= parse_size(argv
[argc
-1], 1024, &limit
);
1925 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
1929 /* With two arguments changes the quota limit of the
1930 * specified image */
1931 r
= bus_call_method(bus
, bus_machine_mgr
, "SetImageLimit", &error
, NULL
, "st", argv
[1], limit
);
1933 /* With one argument changes the pool quota limit */
1934 r
= bus_call_method(bus
, bus_machine_mgr
, "SetPoolLimit", &error
, NULL
, "t", limit
);
1937 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
1942 static int clean_images(int argc
, char *argv
[], void *userdata
) {
1943 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1944 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1945 uint64_t usage
, total
= 0;
1946 sd_bus
*bus
= userdata
;
1951 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1953 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CleanPool");
1955 return bus_log_create_error(r
);
1957 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
1959 return bus_log_create_error(r
);
1961 /* This is a slow operation, hence permit a longer time for completion. */
1962 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
1964 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
1966 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
1968 return bus_log_parse_error(r
);
1970 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
1971 if (usage
== UINT64_MAX
) {
1972 log_info("Removed image '%s'", name
);
1975 log_info("Removed image '%s'. Freed exclusive disk space: %s",
1976 name
, FORMAT_BYTES(usage
));
1977 if (total
!= UINT64_MAX
)
1983 r
= sd_bus_message_exit_container(reply
);
1985 return bus_log_parse_error(r
);
1987 if (total
== UINT64_MAX
)
1988 log_info("Removed %u images in total.", c
);
1990 log_info("Removed %u images in total. Total freed exclusive disk space: %s.",
1991 c
, FORMAT_BYTES(total
));
1996 static int chainload_importctl(int argc
, char *argv
[]) {
2000 log_notice("The 'machinectl %1$s' command has been replaced by 'importctl -m %1$s'. Redirecting invocation.", argv
[optind
]);
2002 _cleanup_strv_free_
char **c
=
2003 strv_new("importctl", "--class=machine");
2007 if (FLAGS_SET(arg_pager_flags
, PAGER_DISABLE
))
2008 if (strv_extend(&c
, "--no-pager") < 0)
2011 if (strv_extend(&c
, "--no-legend") < 0)
2014 if (strv_extend(&c
, "--read-only") < 0)
2017 if (strv_extend(&c
, "--force") < 0)
2020 if (strv_extend(&c
, "--quiet") < 0)
2022 if (!arg_ask_password
)
2023 if (strv_extend(&c
, "--no-ask-password") < 0)
2025 if (strv_extend_many(&c
, "--verify", import_verify_to_string(arg_verify
)) < 0)
2028 if (strv_extend_many(&c
, "--format", arg_format
) < 0)
2031 if (strv_extend_strv(&c
, argv
+ optind
, /* filter_duplicates= */ false) < 0)
2034 if (DEBUG_LOGGING
) {
2035 _cleanup_free_
char *joined
= strv_join(c
, " ");
2036 log_debug("Chainloading: %s", joined
);
2039 r
= invoke_callout_binary(BINDIR
"/importctl", c
);
2040 return log_error_errno(r
, "Failed to invoke 'importctl': %m");
2043 static int help(int argc
, char *argv
[], void *userdata
) {
2044 _cleanup_free_
char *link
= NULL
;
2047 pager_open(arg_pager_flags
);
2049 r
= terminal_urlify_man("machinectl", "1", &link
);
2053 printf("%1$s [OPTIONS...] COMMAND ...\n\n"
2054 "%5$sSend control commands to or query the virtual machine and container%6$s\n"
2055 "%5$sregistration manager.%6$s\n"
2056 "\n%3$sMachine Commands:%4$s\n"
2057 " list List running VMs and containers\n"
2058 " status NAME... Show VM/container details\n"
2059 " show [NAME...] Show properties of one or more VMs/containers\n"
2060 " start NAME... Start container as a service\n"
2061 " login [NAME] Get a login prompt in a container or on the\n"
2063 " shell [[USER@]NAME [COMMAND...]]\n"
2064 " Invoke a shell (or other command) in a container\n"
2065 " or on the local host\n"
2066 " enable NAME... Enable automatic container start at boot\n"
2067 " disable NAME... Disable automatic container start at boot\n"
2068 " poweroff NAME... Power off one or more containers\n"
2069 " reboot NAME... Reboot one or more containers\n"
2070 " terminate NAME... Terminate one or more VMs/containers\n"
2071 " kill NAME... Send signal to processes of a VM/container\n"
2072 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2073 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2074 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n"
2075 "\n%3$sImage Commands:%4$s\n"
2076 " list-images Show available container and VM images\n"
2077 " image-status [NAME...] Show image details\n"
2078 " show-image [NAME...] Show properties of image\n"
2079 " edit NAME|FILE... Edit settings of one or more VMs/containers\n"
2080 " cat NAME|FILE... Show settings of one or more VMs/containers\n"
2081 " clone NAME NAME Clone an image\n"
2082 " rename NAME NAME Rename an image\n"
2083 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2084 " remove NAME... Remove an image\n"
2085 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2086 " clean Remove hidden (or all) images\n"
2087 "\n%3$sOptions:%4$s\n"
2088 " -h --help Show this help\n"
2089 " --version Show package version\n"
2090 " --no-pager Do not pipe output into a pager\n"
2091 " --no-legend Do not show the headers and footers\n"
2092 " --no-ask-password Do not ask for system passwords\n"
2093 " -H --host=[USER@]HOST Operate on remote host\n"
2094 " -M --machine=CONTAINER Operate on local container\n"
2095 " -p --property=NAME Show only properties by this name\n"
2096 " --value When showing properties, only print the value\n"
2097 " -P NAME Equivalent to --value --property=NAME\n"
2098 " -q --quiet Suppress output\n"
2099 " -a --all Show all properties, including empty ones\n"
2100 " -l --full Do not ellipsize output\n"
2101 " --kill-whom=WHOM Whom to send signal to\n"
2102 " -s --signal=SIGNAL Which signal to send\n"
2103 " --uid=USER Specify user ID to invoke shell as\n"
2104 " -E --setenv=VAR[=VALUE] Add an environment variable for shell\n"
2105 " --read-only Create read-only bind mount or clone\n"
2106 " --mkdir Create directory before bind mounting, if missing\n"
2107 " -n --lines=INTEGER Number of journal entries to show\n"
2108 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
2109 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2110 " short-iso, short-iso-precise, short-full,\n"
2111 " short-monotonic, short-unix, short-delta,\n"
2112 " json, json-pretty, json-sse, json-seq, cat,\n"
2113 " verbose, export, with-unit)\n"
2114 " --force Replace target file when copying, if necessary\n"
2115 " --now Start or power off container after enabling or\n"
2117 " --runner=RUNNER Select between nspawn and vmspawn as the runner\n"
2118 " -V Short for --runner=vmspawn\n"
2119 "\nSee the %2$s for details.\n",
2120 program_invocation_short_name
,
2130 static int parse_argv(int argc
, char *argv
[]) {
2133 ARG_VERSION
= 0x100,
2140 ARG_NO_ASK_PASSWORD
,
2150 static const struct option options
[] = {
2151 { "help", no_argument
, NULL
, 'h' },
2152 { "version", no_argument
, NULL
, ARG_VERSION
},
2153 { "property", required_argument
, NULL
, 'p' },
2154 { "value", no_argument
, NULL
, ARG_VALUE
},
2155 { "all", no_argument
, NULL
, 'a' },
2156 { "full", no_argument
, NULL
, 'l' },
2157 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2158 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2159 { "kill-whom", required_argument
, NULL
, ARG_KILL_WHOM
},
2160 { "signal", required_argument
, NULL
, 's' },
2161 { "host", required_argument
, NULL
, 'H' },
2162 { "machine", required_argument
, NULL
, 'M' },
2163 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2164 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2165 { "quiet", no_argument
, NULL
, 'q' },
2166 { "lines", required_argument
, NULL
, 'n' },
2167 { "output", required_argument
, NULL
, 'o' },
2168 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2169 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2170 { "runner", required_argument
, NULL
, ARG_RUNNER
},
2171 { "now", no_argument
, NULL
, ARG_NOW
},
2172 { "force", no_argument
, NULL
, ARG_FORCE
},
2173 { "format", required_argument
, NULL
, ARG_FORMAT
},
2174 { "uid", required_argument
, NULL
, ARG_UID
},
2175 { "setenv", required_argument
, NULL
, 'E' },
2176 { "max-addresses", required_argument
, NULL
, ARG_MAX_ADDRESSES
},
2180 bool reorder
= false;
2181 int c
, r
, shell
= -1;
2186 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
2187 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
2191 static const char option_string
[] = "-hp:P:als:H:M:qn:o:E:V";
2193 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2199 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2200 * non-option argument was discovered. */
2204 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2205 * for switches after the main verb. However, for "shell" we really don't want that, since we
2206 * want that switches specified after the machine name are passed to the program to execute,
2207 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2208 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2209 * non-option parameter. If it's the verb "shell" we remember its position and continue
2210 * processing options. In this case, as soon as we hit the next non-option argument we found
2211 * the machine name, and stop further processing. If the first non-option argument is any other
2212 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2216 /* If we already found the "shell" verb on the command line, and now found the next
2217 * non-option argument, then this is the machine name and we should stop processing
2218 * further arguments. */
2219 optind
--; /* don't process this argument, go one step back */
2222 if (streq(optarg
, "shell"))
2223 /* Remember the position of the "shell" verb, and continue processing normally. */
2228 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2229 * processing normally. */
2232 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2233 * at least once with a reset option index. Hence, let's reset the option index here,
2234 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2235 * likely already processed it), and the bump the option index so that we read the
2236 * intended argument again. */
2237 saved_optind
= optind
;
2239 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2240 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2246 return help(0, NULL
, NULL
);
2253 r
= strv_extend(&arg_property
, optarg
);
2257 /* If the user asked for a particular property, show it to them, even if empty. */
2258 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2265 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
, true);
2269 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
2278 if (safe_atou(optarg
, &arg_lines
) < 0)
2279 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2280 "Failed to parse lines '%s'", optarg
);
2284 if (streq(optarg
, "help")) {
2285 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
2289 r
= output_mode_from_string(optarg
);
2291 return log_error_errno(r
, "Unknown output '%s'.", optarg
);
2294 if (OUTPUT_MODE_IS_JSON(arg_output
))
2299 arg_pager_flags
|= PAGER_DISABLE
;
2307 arg_kill_whom
= optarg
;
2311 r
= parse_signal_argument(optarg
, &arg_signal
);
2316 case ARG_NO_ASK_PASSWORD
:
2317 arg_ask_password
= false;
2321 arg_transport
= BUS_TRANSPORT_REMOTE
;
2326 arg_transport
= BUS_TRANSPORT_MACHINE
;
2331 arg_read_only
= true;
2343 if (streq(optarg
, "help")) {
2344 DUMP_STRING_TABLE(import_verify
, ImportVerify
, _IMPORT_VERIFY_MAX
);
2348 r
= import_verify_from_string(optarg
);
2350 return log_error_errno(r
, "Failed to parse --verify= setting: %s", optarg
);
2355 arg_runner
= RUNNER_VMSPAWN
;
2359 r
= machine_runner_from_string(optarg
);
2361 return log_error_errno(r
, "Failed to parse --runner= setting: %s", optarg
);
2375 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2", "zstd"))
2376 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2377 "Unknown format: %s", optarg
);
2379 arg_format
= optarg
;
2387 r
= strv_env_replace_strdup_passthrough(&arg_setenv
, optarg
);
2389 return log_error_errno(r
, "Cannot assign environment variable %s: %m", optarg
);
2392 case ARG_MAX_ADDRESSES
:
2393 if (streq(optarg
, "all"))
2394 arg_max_addresses
= UINT_MAX
;
2395 else if (safe_atou(optarg
, &arg_max_addresses
) < 0)
2396 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2397 "Invalid number of addresses: %s", optarg
);
2404 assert_not_reached();
2412 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2413 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2415 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2418 for (int i
= shell
; i
< optind
; i
++)
2419 argv
[i
] = argv
[i
+1];
2426 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
2428 static const Verb verbs
[] = {
2429 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
2430 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
2431 { "list-images", VERB_ANY
, 1, 0, list_images
},
2432 { "status", 2, VERB_ANY
, 0, show_machine
},
2433 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
2434 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
2435 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
2436 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
2437 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
2438 { "restart", 2, VERB_ANY
, 0, reboot_machine
}, /* Convenience alias */
2439 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
2440 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
2441 { "kill", 2, VERB_ANY
, 0, kill_machine
},
2442 { "login", VERB_ANY
, 2, 0, login_machine
},
2443 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
2444 { "bind", 3, 4, 0, bind_mount
},
2445 { "edit", 2, VERB_ANY
, 0, edit_settings
},
2446 { "cat", 2, VERB_ANY
, 0, cat_settings
},
2447 { "copy-to", 3, 4, 0, copy_files
},
2448 { "copy-from", 3, 4, 0, copy_files
},
2449 { "remove", 2, VERB_ANY
, 0, remove_image
},
2450 { "rename", 3, 3, 0, rename_image
},
2451 { "clone", 3, 3, 0, clone_image
},
2452 { "read-only", 2, 3, 0, read_only_image
},
2453 { "start", 2, VERB_ANY
, 0, start_machine
},
2454 { "enable", 2, VERB_ANY
, 0, enable_machine
},
2455 { "disable", 2, VERB_ANY
, 0, enable_machine
},
2456 { "set-limit", 2, 3, 0, set_limit
},
2457 { "clean", VERB_ANY
, 1, 0, clean_images
},
2461 return dispatch_verb(argc
, argv
, verbs
, bus
);
2464 static int run(int argc
, char *argv
[]) {
2465 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
2468 setlocale(LC_ALL
, "");
2471 r
= parse_argv(argc
, argv
);
2475 journal_browse_prepare();
2477 if (STRPTR_IN_SET(argv
[optind
],
2478 "import-tar", "import-raw", "import-fs",
2479 "export-tar", "export-raw",
2480 "pull-tar", "pull-raw",
2481 "list-transfers", "cancel-transfer"))
2482 return chainload_importctl(argc
, argv
);
2484 r
= bus_connect_transport(arg_transport
, arg_host
, RUNTIME_SCOPE_SYSTEM
, &bus
);
2486 return bus_log_connect_error(r
, arg_transport
, RUNTIME_SCOPE_SYSTEM
);
2488 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
2490 return machinectl_main(argc
, argv
, bus
);
2493 DEFINE_MAIN_FUNCTION(run
);