| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <locale.h> |
| 4 |
#include <net/if.h> |
| 5 |
#include <sys/socket.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "sd-bus.h" |
| 9 |
#include "sd-event.h" |
| 10 |
#include "sd-journal.h" |
| 11 |
#include "sd-varlink.h" |
| 12 |
|
| 13 |
#include "alloc-util.h" |
| 14 |
#include "ask-password-agent.h" |
| 15 |
#include "build.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" |
| 25 |
#include "bus-util.h" |
| 26 |
#include "bus-wait-for-jobs.h" |
| 27 |
#include "cgroup-show.h" |
| 28 |
#include "cgroup-util.h" |
| 29 |
#include "edit-util.h" |
| 30 |
#include "env-util.h" |
| 31 |
#include "fd-util.h" |
| 32 |
#include "format-ifname.h" |
| 33 |
#include "format-table.h" |
| 34 |
#include "format-util.h" |
| 35 |
#include "help-util.h" |
| 36 |
#include "hostname-util.h" |
| 37 |
#include "import-util.h" |
| 38 |
#include "in-addr-util.h" |
| 39 |
#include "json-util.h" |
| 40 |
#include "label-util.h" |
| 41 |
#include "log.h" |
| 42 |
#include "logs-show.h" |
| 43 |
#include "machine-dbus.h" |
| 44 |
#include "machine-util.h" |
| 45 |
#include "main-func.h" |
| 46 |
#include "nulstr-util.h" |
| 47 |
#include "options.h" |
| 48 |
#include "osc-context.h" |
| 49 |
#include "output-mode.h" |
| 50 |
#include "pager.h" |
| 51 |
#include "parse-argument.h" |
| 52 |
#include "parse-util.h" |
| 53 |
#include "path-lookup.h" |
| 54 |
#include "path-util.h" |
| 55 |
#include "pidref.h" |
| 56 |
#include "polkit-agent.h" |
| 57 |
#include "pretty-print.h" |
| 58 |
#include "process-util.h" |
| 59 |
#include "ptyfwd.h" |
| 60 |
#include "runtime-scope.h" |
| 61 |
#include "stdio-util.h" |
| 62 |
#include "storage-util.h" |
| 63 |
#include "string-table.h" |
| 64 |
#include "string-util.h" |
| 65 |
#include "strv.h" |
| 66 |
#include "terminal-util.h" |
| 67 |
#include "time-util.h" |
| 68 |
#include "unit-name.h" |
| 69 |
#include "verbs.h" |
| 70 |
|
| 71 |
typedef enum MachineRunner { |
| 72 |
RUNNER_NSPAWN, |
| 73 |
RUNNER_VMSPAWN, |
| 74 |
_RUNNER_MAX, |
| 75 |
_RUNNER_INVALID = -EINVAL, |
| 76 |
} MachineRunner; |
| 77 |
|
| 78 |
static const char* const machine_runner_table[_RUNNER_MAX] = { |
| 79 |
[RUNNER_NSPAWN] = "nspawn", |
| 80 |
[RUNNER_VMSPAWN] = "vmspawn", |
| 81 |
}; |
| 82 |
|
| 83 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(machine_runner, MachineRunner); |
| 84 |
|
| 85 |
static const char* const machine_runner_unit_prefix_table[_RUNNER_MAX] = { |
| 86 |
[RUNNER_NSPAWN] = "systemd-nspawn", |
| 87 |
[RUNNER_VMSPAWN] = "systemd-vmspawn", |
| 88 |
}; |
| 89 |
|
| 90 |
static char **arg_property = NULL; |
| 91 |
static bool arg_all = false; |
| 92 |
static BusPrintPropertyFlags arg_print_flags = 0; |
| 93 |
static bool arg_full = false; |
| 94 |
static PagerFlags arg_pager_flags = 0; |
| 95 |
static bool arg_legend = true; |
| 96 |
static const char *arg_kill_whom = NULL; |
| 97 |
static int arg_signal = SIGTERM; |
| 98 |
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
| 99 |
static const char *arg_host = NULL; |
| 100 |
static bool arg_read_only = false; |
| 101 |
static bool arg_mkdir = false; |
| 102 |
static bool arg_quiet = false; |
| 103 |
static bool arg_ask_password = true; |
| 104 |
static unsigned arg_lines = 10; |
| 105 |
static OutputMode arg_output = OUTPUT_SHORT; |
| 106 |
static bool arg_now = false; |
| 107 |
static bool arg_force = false; |
| 108 |
static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE; |
| 109 |
static MachineRunner arg_runner = RUNNER_NSPAWN; |
| 110 |
static const char* arg_format = NULL; |
| 111 |
static const char *arg_uid = NULL; |
| 112 |
static char **arg_setenv = NULL; |
| 113 |
static unsigned arg_max_addresses = 1; |
| 114 |
static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
| 115 |
|
| 116 |
STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep); |
| 117 |
STATIC_DESTRUCTOR_REGISTER(arg_setenv, strv_freep); |
| 118 |
|
| 119 |
static OutputFlags get_output_flags(void) { |
| 120 |
return |
| 121 |
FLAGS_SET(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) * OUTPUT_SHOW_ALL | |
| 122 |
(arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | |
| 123 |
colors_enabled() * OUTPUT_COLOR | |
| 124 |
!arg_quiet * OUTPUT_WARN_CUTOFF; |
| 125 |
} |
| 126 |
|
| 127 |
static int call_get_os_release(sd_bus *bus, const char *method, const char *name, const char *query, ...) { |
| 128 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 129 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 130 |
va_list ap; |
| 131 |
int r; |
| 132 |
|
| 133 |
assert(bus); |
| 134 |
assert(method); |
| 135 |
assert(name); |
| 136 |
assert(query); |
| 137 |
|
| 138 |
r = bus_call_method(bus, bus_machine_mgr, method, &error, &reply, "s", name); |
| 139 |
if (r < 0) |
| 140 |
return log_debug_errno(r, "Failed to call '%s()': %s", method, bus_error_message(&error, r)); |
| 141 |
|
| 142 |
r = sd_bus_message_enter_container(reply, 'a', "{ss}"); |
| 143 |
if (r < 0) |
| 144 |
return bus_log_parse_error(r); |
| 145 |
|
| 146 |
const char **res; |
| 147 |
size_t n_fields = 0; |
| 148 |
|
| 149 |
NULSTR_FOREACH(i, query) |
| 150 |
n_fields++; |
| 151 |
|
| 152 |
res = newa0(const char*, n_fields); |
| 153 |
|
| 154 |
const char *k, *v; |
| 155 |
while ((r = sd_bus_message_read(reply, "{ss}", &k, &v)) > 0) { |
| 156 |
size_t c = 0; |
| 157 |
NULSTR_FOREACH(i, query) { |
| 158 |
if (streq(i, k)) { |
| 159 |
res[c] = v; |
| 160 |
break; |
| 161 |
} |
| 162 |
c++; |
| 163 |
} |
| 164 |
} |
| 165 |
if (r < 0) |
| 166 |
return bus_log_parse_error(r); |
| 167 |
|
| 168 |
r = sd_bus_message_exit_container(reply); |
| 169 |
if (r < 0) |
| 170 |
return bus_log_parse_error(r); |
| 171 |
|
| 172 |
r = 0; |
| 173 |
|
| 174 |
va_start(ap, query); |
| 175 |
FOREACH_ARRAY(i, res, n_fields) { |
| 176 |
r = strdup_to(va_arg(ap, char**), *i); |
| 177 |
if (r < 0) |
| 178 |
break; |
| 179 |
} |
| 180 |
va_end(ap); |
| 181 |
|
| 182 |
return r; |
| 183 |
} |
| 184 |
|
| 185 |
static int call_get_addresses( |
| 186 |
sd_bus *bus, |
| 187 |
const char *name, |
| 188 |
int ifi, |
| 189 |
const char *prefix, |
| 190 |
const char *prefix2, |
| 191 |
char **ret) { |
| 192 |
|
| 193 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 194 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 195 |
_cleanup_free_ char *addresses = NULL; |
| 196 |
unsigned n = 0; |
| 197 |
int r; |
| 198 |
|
| 199 |
assert(bus); |
| 200 |
assert(name); |
| 201 |
assert(prefix); |
| 202 |
assert(prefix2); |
| 203 |
assert(ret); |
| 204 |
|
| 205 |
r = bus_call_method(bus, bus_machine_mgr, "GetMachineAddresses", NULL, &reply, "s", name); |
| 206 |
if (r < 0) |
| 207 |
return log_debug_errno(r, "Could not get addresses: %s", bus_error_message(&error, r)); |
| 208 |
|
| 209 |
addresses = strdup(prefix); |
| 210 |
if (!addresses) |
| 211 |
return log_oom(); |
| 212 |
|
| 213 |
r = sd_bus_message_enter_container(reply, 'a', "(iay)"); |
| 214 |
if (r < 0) |
| 215 |
return bus_log_parse_error(r); |
| 216 |
|
| 217 |
prefix = ""; |
| 218 |
while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) { |
| 219 |
int family; |
| 220 |
const void *a; |
| 221 |
size_t sz; |
| 222 |
char buf_ifi[1 + DECIMAL_STR_MAX(int)] = ""; |
| 223 |
|
| 224 |
r = sd_bus_message_read(reply, "i", &family); |
| 225 |
if (r < 0) |
| 226 |
return bus_log_parse_error(r); |
| 227 |
|
| 228 |
r = sd_bus_message_read_array(reply, 'y', &a, &sz); |
| 229 |
if (r < 0) |
| 230 |
return bus_log_parse_error(r); |
| 231 |
|
| 232 |
if (family == AF_INET6 && ifi > 0) |
| 233 |
xsprintf(buf_ifi, "%%%i", ifi); |
| 234 |
|
| 235 |
if (!strextend(&addresses, prefix, IN_ADDR_TO_STRING(family, a), buf_ifi)) |
| 236 |
return log_oom(); |
| 237 |
|
| 238 |
r = sd_bus_message_exit_container(reply); |
| 239 |
if (r < 0) |
| 240 |
return bus_log_parse_error(r); |
| 241 |
|
| 242 |
prefix = prefix2; |
| 243 |
|
| 244 |
n++; |
| 245 |
} |
| 246 |
if (r < 0) |
| 247 |
return bus_log_parse_error(r); |
| 248 |
|
| 249 |
r = sd_bus_message_exit_container(reply); |
| 250 |
if (r < 0) |
| 251 |
return bus_log_parse_error(r); |
| 252 |
|
| 253 |
*ret = TAKE_PTR(addresses); |
| 254 |
return (int) n; |
| 255 |
} |
| 256 |
|
| 257 |
static int show_table(Table *table, const char *word) { |
| 258 |
int r; |
| 259 |
|
| 260 |
assert(table); |
| 261 |
assert(word); |
| 262 |
|
| 263 |
if (!table_isempty(table) || OUTPUT_MODE_IS_JSON(arg_output)) { |
| 264 |
r = table_set_sort(table, (size_t) 0); |
| 265 |
if (r < 0) |
| 266 |
return table_log_sort_error(r); |
| 267 |
|
| 268 |
table_set_header(table, arg_legend); |
| 269 |
|
| 270 |
if (OUTPUT_MODE_IS_JSON(arg_output)) |
| 271 |
r = table_print_json(table, NULL, output_mode_to_json_format_flags(arg_output) | SD_JSON_FORMAT_COLOR_AUTO); |
| 272 |
else |
| 273 |
r = table_print(table); |
| 274 |
if (r < 0) |
| 275 |
return table_log_print_error(r); |
| 276 |
} |
| 277 |
|
| 278 |
if (arg_legend) { |
| 279 |
if (table_isempty(table)) |
| 280 |
printf("No %s.\n", word); |
| 281 |
else |
| 282 |
printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word); |
| 283 |
} |
| 284 |
|
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
VERB_GROUP("Machine Commands"); |
| 289 |
|
| 290 |
VERB_DEFAULT_NOARG(verb_list_machines, "list", "List running VMs and containers"); |
| 291 |
static int verb_list_machines(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 292 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 293 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 294 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 295 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 296 |
int r; |
| 297 |
|
| 298 |
pager_open(arg_pager_flags); |
| 299 |
|
| 300 |
r = bus_call_method(bus, bus_machine_mgr, "ListMachines", &error, &reply, NULL); |
| 301 |
if (r < 0) |
| 302 |
return log_error_errno(r, "Could not get machines: %s", bus_error_message(&error, r)); |
| 303 |
|
| 304 |
table = table_new("machine", "class", "service", "os", "version", |
| 305 |
arg_max_addresses > 0 ? "addresses" : NULL); |
| 306 |
if (!table) |
| 307 |
return log_oom(); |
| 308 |
|
| 309 |
table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
| 310 |
if (!arg_full && arg_max_addresses > 0 && arg_max_addresses < UINT_MAX) |
| 311 |
table_set_cell_height_max(table, arg_max_addresses); |
| 312 |
|
| 313 |
if (arg_full) |
| 314 |
table_set_width(table, 0); |
| 315 |
|
| 316 |
r = sd_bus_message_enter_container(reply, 'a', "(ssso)"); |
| 317 |
if (r < 0) |
| 318 |
return bus_log_parse_error(r); |
| 319 |
|
| 320 |
for (;;) { |
| 321 |
_cleanup_free_ char *os = NULL, *version_id = NULL, *addresses = NULL; |
| 322 |
const char *name, *class, *service; |
| 323 |
|
| 324 |
r = sd_bus_message_read(reply, "(ssso)", &name, &class, &service, NULL); |
| 325 |
if (r < 0) |
| 326 |
return bus_log_parse_error(r); |
| 327 |
if (r == 0) |
| 328 |
break; |
| 329 |
|
| 330 |
if (name[0] == '.' && !arg_all) |
| 331 |
continue; |
| 332 |
|
| 333 |
(void) call_get_os_release( |
| 334 |
bus, |
| 335 |
"GetMachineOSRelease", |
| 336 |
name, |
| 337 |
"ID\0" |
| 338 |
"VERSION_ID\0", |
| 339 |
&os, |
| 340 |
&version_id); |
| 341 |
|
| 342 |
r = table_add_many(table, |
| 343 |
TABLE_STRING, empty_to_null(name), |
| 344 |
TABLE_STRING, empty_to_null(class), |
| 345 |
TABLE_STRING, empty_to_null(service), |
| 346 |
TABLE_STRING, empty_to_null(os), |
| 347 |
TABLE_STRING, empty_to_null(version_id)); |
| 348 |
if (r < 0) |
| 349 |
return table_log_add_error(r); |
| 350 |
|
| 351 |
if (arg_max_addresses > 0) { |
| 352 |
(void) call_get_addresses(bus, name, 0, "", "\n", &addresses); |
| 353 |
|
| 354 |
r = table_add_many(table, |
| 355 |
TABLE_STRING, empty_to_null(addresses)); |
| 356 |
if (r < 0) |
| 357 |
return table_log_add_error(r); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
r = sd_bus_message_exit_container(reply); |
| 362 |
if (r < 0) |
| 363 |
return bus_log_parse_error(r); |
| 364 |
|
| 365 |
return show_table(table, "machines"); |
| 366 |
} |
| 367 |
|
| 368 |
static int show_unit_cgroup( |
| 369 |
sd_bus *bus, |
| 370 |
const char *unit, |
| 371 |
const char *subgroup, |
| 372 |
pid_t leader) { |
| 373 |
|
| 374 |
_cleanup_free_ char *cgroup = NULL; |
| 375 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 376 |
OutputFlags extra_flags = 0; |
| 377 |
int r; |
| 378 |
|
| 379 |
assert(bus); |
| 380 |
assert(unit); |
| 381 |
|
| 382 |
r = show_cgroup_get_unit_path_and_warn(bus, unit, &cgroup); |
| 383 |
if (r < 0) |
| 384 |
return r; |
| 385 |
|
| 386 |
if (isempty(cgroup)) |
| 387 |
return 0; |
| 388 |
|
| 389 |
if (!empty_or_root(subgroup)) { |
| 390 |
if (!path_extend(&cgroup, subgroup)) |
| 391 |
return log_oom(); |
| 392 |
|
| 393 |
/* If we have a subcgroup, then hide all processes outside of it */ |
| 394 |
extra_flags |= OUTPUT_HIDE_EXTRA; |
| 395 |
} |
| 396 |
|
| 397 |
unsigned c = MAX(LESS_BY(columns(), 18U), 10U); |
| 398 |
r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags() | extra_flags, &error); |
| 399 |
if (r == -EBADR) { |
| 400 |
|
| 401 |
if (arg_transport == BUS_TRANSPORT_REMOTE) |
| 402 |
return 0; |
| 403 |
|
| 404 |
/* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */ |
| 405 |
|
| 406 |
if (cg_is_empty(cgroup) != 0 && leader <= 0) |
| 407 |
return 0; |
| 408 |
|
| 409 |
show_cgroup_and_extra(cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags()); |
| 410 |
} else if (r < 0) |
| 411 |
return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r)); |
| 412 |
|
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
static int print_os_release(sd_bus *bus, const char *method, const char *name, const char *prefix) { |
| 417 |
_cleanup_free_ char *pretty = NULL; |
| 418 |
int r; |
| 419 |
|
| 420 |
assert(bus); |
| 421 |
assert(name); |
| 422 |
assert(prefix); |
| 423 |
|
| 424 |
r = call_get_os_release(bus, method, name, "PRETTY_NAME\0", &pretty, NULL); |
| 425 |
if (r < 0) |
| 426 |
return r; |
| 427 |
|
| 428 |
if (pretty) |
| 429 |
printf("%s%s\n", prefix, pretty); |
| 430 |
|
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
static int print_uid_shift(sd_bus *bus, const char *name) { |
| 435 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 436 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 437 |
uint32_t shift; |
| 438 |
int r; |
| 439 |
|
| 440 |
assert(bus); |
| 441 |
assert(name); |
| 442 |
|
| 443 |
r = bus_call_method(bus, bus_machine_mgr, "GetMachineUIDShift", &error, &reply, "s", name); |
| 444 |
if (r < 0) |
| 445 |
return log_debug_errno(r, "Failed to query UID/GID shift: %s", bus_error_message(&error, r)); |
| 446 |
|
| 447 |
r = sd_bus_message_read(reply, "u", &shift); |
| 448 |
if (r < 0) |
| 449 |
return r; |
| 450 |
|
| 451 |
if (shift == 0) /* Don't show trivial mappings */ |
| 452 |
return 0; |
| 453 |
|
| 454 |
printf("\tID Shift: %" PRIu32 "\n", shift); |
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
typedef struct MachineStatusInfo { |
| 459 |
const char *name; |
| 460 |
sd_id128_t id; |
| 461 |
const char *class; |
| 462 |
const char *service; |
| 463 |
const char *unit; |
| 464 |
const char *subgroup; |
| 465 |
const char *root_directory; |
| 466 |
pid_t leader; |
| 467 |
uint64_t leader_pidfdid; |
| 468 |
pid_t supervisor; |
| 469 |
uint64_t supervisor_pidfdid; |
| 470 |
struct dual_timestamp timestamp; |
| 471 |
int *netif; |
| 472 |
size_t n_netif; |
| 473 |
uid_t uid; |
| 474 |
} MachineStatusInfo; |
| 475 |
|
| 476 |
static void machine_status_info_done(MachineStatusInfo *info) { |
| 477 |
if (!info) |
| 478 |
return; |
| 479 |
|
| 480 |
free(info->netif); |
| 481 |
zero(*info); |
| 482 |
} |
| 483 |
|
| 484 |
static void print_process_info(const char *field, pid_t pid, uint64_t pidfdid) { |
| 485 |
int r; |
| 486 |
|
| 487 |
assert(field); |
| 488 |
|
| 489 |
if (pid <= 0) |
| 490 |
return; |
| 491 |
|
| 492 |
printf("%s: " PID_FMT, field, pid); |
| 493 |
|
| 494 |
_cleanup_(pidref_done) PidRef pr = PIDREF_NULL; |
| 495 |
r = pidref_set_pid_and_pidfd_id(&pr, pid, pidfdid); |
| 496 |
if (r < 0) |
| 497 |
log_debug_errno(r, "Failed to acquire reference to process, ignoring: %m"); |
| 498 |
else { |
| 499 |
_cleanup_free_ char *t = NULL; |
| 500 |
r = pidref_get_comm(&pr, &t); |
| 501 |
if (r < 0) |
| 502 |
log_debug_errno(r, "Failed to acquire name of process, ignoring: %m"); |
| 503 |
else |
| 504 |
printf(" (%s)", t); |
| 505 |
} |
| 506 |
|
| 507 |
putchar('\n'); |
| 508 |
} |
| 509 |
|
| 510 |
static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) { |
| 511 |
_cleanup_free_ char *addresses = NULL, *s1 = NULL, *s2 = NULL; |
| 512 |
int ifi = -1; |
| 513 |
|
| 514 |
assert(bus); |
| 515 |
assert(i); |
| 516 |
|
| 517 |
fputs(strna(i->name), stdout); |
| 518 |
|
| 519 |
if (!sd_id128_is_null(i->id)) |
| 520 |
printf("(" SD_ID128_FORMAT_STR ")\n", SD_ID128_FORMAT_VAL(i->id)); |
| 521 |
else |
| 522 |
putchar('\n'); |
| 523 |
|
| 524 |
s1 = strdup(strempty(FORMAT_TIMESTAMP_RELATIVE(i->timestamp.realtime))); |
| 525 |
s2 = strdup(strempty(FORMAT_TIMESTAMP(i->timestamp.realtime))); |
| 526 |
|
| 527 |
if (!isempty(s1)) |
| 528 |
printf("\t Since: %s; %s\n", strna(s2), s1); |
| 529 |
else if (!isempty(s2)) |
| 530 |
printf("\t Since: %s\n", s2); |
| 531 |
|
| 532 |
print_process_info("\t Leader", i->leader, i->leader_pidfdid); |
| 533 |
print_process_info("\t Superv.", i->supervisor, i->supervisor_pidfdid); |
| 534 |
|
| 535 |
if (i->service) { |
| 536 |
printf("\t Service: %s", i->service); |
| 537 |
|
| 538 |
if (i->class) |
| 539 |
printf("; class %s", i->class); |
| 540 |
|
| 541 |
putchar('\n'); |
| 542 |
} else if (i->class) |
| 543 |
printf("\t Class: %s\n", i->class); |
| 544 |
|
| 545 |
if (i->uid != 0) |
| 546 |
printf("\t UID: " UID_FMT "\n", i->uid); |
| 547 |
|
| 548 |
if (i->root_directory) |
| 549 |
printf("\t Root: %s\n", i->root_directory); |
| 550 |
|
| 551 |
if (i->n_netif > 0) { |
| 552 |
fputs("\t Iface:", stdout); |
| 553 |
|
| 554 |
for (size_t c = 0; c < i->n_netif; c++) { |
| 555 |
char name[IF_NAMESIZE]; |
| 556 |
|
| 557 |
if (format_ifname(i->netif[c], name) >= 0) { |
| 558 |
fputc(' ', stdout); |
| 559 |
fputs(name, stdout); |
| 560 |
|
| 561 |
if (ifi < 0) |
| 562 |
ifi = i->netif[c]; |
| 563 |
else |
| 564 |
ifi = 0; |
| 565 |
} else |
| 566 |
printf(" %i", i->netif[c]); |
| 567 |
} |
| 568 |
|
| 569 |
fputc('\n', stdout); |
| 570 |
} |
| 571 |
|
| 572 |
if (call_get_addresses(bus, i->name, ifi, |
| 573 |
"\t Address: ", "\n\t ", |
| 574 |
&addresses) > 0) { |
| 575 |
fputs(addresses, stdout); |
| 576 |
fputc('\n', stdout); |
| 577 |
} |
| 578 |
|
| 579 |
print_os_release(bus, "GetMachineOSRelease", i->name, "\t OS: "); |
| 580 |
|
| 581 |
print_uid_shift(bus, i->name); |
| 582 |
|
| 583 |
if (i->unit) { |
| 584 |
printf("\t Unit: %s\n", i->unit); |
| 585 |
|
| 586 |
if (!empty_or_root(i->subgroup)) |
| 587 |
printf("\tSubgroup: %s\n", i->subgroup); |
| 588 |
|
| 589 |
show_unit_cgroup(bus, i->unit, i->subgroup, i->leader); |
| 590 |
|
| 591 |
if (arg_transport == BUS_TRANSPORT_LOCAL) |
| 592 |
|
| 593 |
show_journal_by_unit( |
| 594 |
stdout, |
| 595 |
i->unit, |
| 596 |
/* namespace= */ NULL, |
| 597 |
arg_output, |
| 598 |
/* n_columns= */ 0, |
| 599 |
i->timestamp.monotonic, |
| 600 |
arg_lines, |
| 601 |
get_output_flags() | OUTPUT_BEGIN_NEWLINE, |
| 602 |
SD_JOURNAL_LOCAL_ONLY, |
| 603 |
/* system_unit= */ true, |
| 604 |
/* ellipsized= */ NULL); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
static int map_netif(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
| 609 |
MachineStatusInfo *i = userdata; |
| 610 |
size_t l; |
| 611 |
const void *v; |
| 612 |
int r; |
| 613 |
|
| 614 |
assert_cc(sizeof(int32_t) == sizeof(int)); |
| 615 |
r = sd_bus_message_read_array(m, SD_BUS_TYPE_INT32, &v, &l); |
| 616 |
if (r < 0) |
| 617 |
return r; |
| 618 |
if (r == 0) |
| 619 |
return -EBADMSG; |
| 620 |
|
| 621 |
i->n_netif = l / sizeof(int32_t); |
| 622 |
i->netif = memdup(v, l); |
| 623 |
if (!i->netif) |
| 624 |
return -ENOMEM; |
| 625 |
|
| 626 |
return 0; |
| 627 |
} |
| 628 |
|
| 629 |
static int show_machine_info(const char *verb, sd_bus *bus, const char *path, bool *new_line) { |
| 630 |
static const struct bus_properties_map map[] = { |
| 631 |
{ "Name", "s", NULL, offsetof(MachineStatusInfo, name) }, |
| 632 |
{ "Class", "s", NULL, offsetof(MachineStatusInfo, class) }, |
| 633 |
{ "Service", "s", NULL, offsetof(MachineStatusInfo, service) }, |
| 634 |
{ "Unit", "s", NULL, offsetof(MachineStatusInfo, unit) }, |
| 635 |
{ "Subgroup", "s", NULL, offsetof(MachineStatusInfo, subgroup) }, |
| 636 |
{ "RootDirectory", "s", NULL, offsetof(MachineStatusInfo, root_directory) }, |
| 637 |
{ "Leader", "u", NULL, offsetof(MachineStatusInfo, leader) }, |
| 638 |
{ "LeaderPIDFDId", "t", NULL, offsetof(MachineStatusInfo, leader_pidfdid) }, |
| 639 |
{ "Supervisor", "u", NULL, offsetof(MachineStatusInfo, supervisor) }, |
| 640 |
{ "SupervisorPIDFDId", "t", NULL, offsetof(MachineStatusInfo, supervisor_pidfdid) }, |
| 641 |
{ "Timestamp", "t", NULL, offsetof(MachineStatusInfo, timestamp.realtime) }, |
| 642 |
{ "TimestampMonotonic", "t", NULL, offsetof(MachineStatusInfo, timestamp.monotonic) }, |
| 643 |
{ "Id", "ay", bus_map_id128, offsetof(MachineStatusInfo, id) }, |
| 644 |
{ "NetworkInterfaces", "ai", map_netif, 0 }, |
| 645 |
{ "UID", "u", NULL, offsetof(MachineStatusInfo, uid) }, |
| 646 |
{} |
| 647 |
}; |
| 648 |
|
| 649 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 650 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 651 |
_cleanup_(machine_status_info_done) MachineStatusInfo info = {}; |
| 652 |
int r; |
| 653 |
|
| 654 |
assert(verb); |
| 655 |
assert(bus); |
| 656 |
assert(path); |
| 657 |
assert(new_line); |
| 658 |
|
| 659 |
r = bus_map_all_properties(bus, |
| 660 |
"org.freedesktop.machine1", |
| 661 |
path, |
| 662 |
map, |
| 663 |
0, |
| 664 |
&error, |
| 665 |
&m, |
| 666 |
&info); |
| 667 |
if (r < 0) |
| 668 |
return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r)); |
| 669 |
|
| 670 |
if (*new_line) |
| 671 |
printf("\n"); |
| 672 |
*new_line = true; |
| 673 |
|
| 674 |
print_machine_status_info(bus, &info); |
| 675 |
|
| 676 |
return r; |
| 677 |
} |
| 678 |
|
| 679 |
static int show_machine_properties(sd_bus *bus, const char *path, bool *new_line) { |
| 680 |
int r; |
| 681 |
|
| 682 |
assert(bus); |
| 683 |
assert(path); |
| 684 |
assert(new_line); |
| 685 |
|
| 686 |
if (*new_line) |
| 687 |
printf("\n"); |
| 688 |
|
| 689 |
*new_line = true; |
| 690 |
|
| 691 |
r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, NULL, arg_property, arg_print_flags, NULL); |
| 692 |
if (r < 0) |
| 693 |
log_error_errno(r, "Could not get properties: %m"); |
| 694 |
|
| 695 |
return r; |
| 696 |
} |
| 697 |
|
| 698 |
VERB(verb_show_machine, "status", "NAME…", 2, VERB_ANY, 0, |
| 699 |
"Show VM/container details"); |
| 700 |
VERB(verb_show_machine, "show", "[NAME…]", VERB_ANY, VERB_ANY, 0, |
| 701 |
"Show properties of one or more VMs/containers"); |
| 702 |
static int verb_show_machine(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 703 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 704 |
bool properties, new_line = false; |
| 705 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 706 |
int r = 0; |
| 707 |
|
| 708 |
properties = !strstr(argv[0], "status"); |
| 709 |
|
| 710 |
pager_open(arg_pager_flags); |
| 711 |
|
| 712 |
if (properties && argc <= 1) { |
| 713 |
|
| 714 |
/* If no argument is specified, inspect the manager |
| 715 |
* itself */ |
| 716 |
r = show_machine_properties(bus, "/org/freedesktop/machine1", &new_line); |
| 717 |
if (r < 0) |
| 718 |
return r; |
| 719 |
} |
| 720 |
|
| 721 |
for (int i = 1; i < argc; i++) { |
| 722 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 723 |
const char *path = NULL; |
| 724 |
|
| 725 |
r = bus_call_method(bus, bus_machine_mgr, "GetMachine", &error, &reply, "s", argv[i]); |
| 726 |
if (r < 0) |
| 727 |
return log_error_errno(r, "Could not get path to machine: %s", bus_error_message(&error, r)); |
| 728 |
|
| 729 |
r = sd_bus_message_read(reply, "o", &path); |
| 730 |
if (r < 0) |
| 731 |
return bus_log_parse_error(r); |
| 732 |
|
| 733 |
if (properties) |
| 734 |
r = show_machine_properties(bus, path, &new_line); |
| 735 |
else |
| 736 |
r = show_machine_info(argv[0], bus, path, &new_line); |
| 737 |
} |
| 738 |
|
| 739 |
return r; |
| 740 |
} |
| 741 |
|
| 742 |
static int make_service_name(const char *name, char **ret) { |
| 743 |
int r; |
| 744 |
|
| 745 |
assert(name); |
| 746 |
assert(ret); |
| 747 |
assert(arg_runner >= 0 && arg_runner < _RUNNER_MAX); |
| 748 |
|
| 749 |
if (!hostname_is_valid(name, 0)) |
| 750 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 751 |
"Invalid machine name %s.", name); |
| 752 |
|
| 753 |
r = unit_name_build(machine_runner_unit_prefix_table[arg_runner], name, ".service", ret); |
| 754 |
if (r < 0) |
| 755 |
return log_error_errno(r, "Failed to build unit name: %m"); |
| 756 |
|
| 757 |
return 0; |
| 758 |
} |
| 759 |
|
| 760 |
static int image_exists(sd_bus *bus, const char *name) { |
| 761 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 762 |
int r; |
| 763 |
|
| 764 |
assert(bus); |
| 765 |
assert(name); |
| 766 |
|
| 767 |
r = bus_call_method(bus, bus_machine_mgr, "GetImage", &error, NULL, "s", name); |
| 768 |
if (r < 0) { |
| 769 |
if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_IMAGE)) |
| 770 |
return 0; |
| 771 |
|
| 772 |
return log_error_errno(r, "Failed to check whether image %s exists: %s", name, bus_error_message(&error, r)); |
| 773 |
} |
| 774 |
|
| 775 |
return 1; |
| 776 |
} |
| 777 |
|
| 778 |
VERB(verb_start_machine, "start", "NAME…", 2, VERB_ANY, 0, |
| 779 |
"Start container as a service"); |
| 780 |
static int verb_start_machine(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 781 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 782 |
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
| 783 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 784 |
int r; |
| 785 |
|
| 786 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 787 |
ask_password_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 788 |
|
| 789 |
r = bus_wait_for_jobs_new(bus, &w); |
| 790 |
if (r < 0) |
| 791 |
return log_error_errno(r, "Could not watch jobs: %m"); |
| 792 |
|
| 793 |
for (int i = 1; i < argc; i++) { |
| 794 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 795 |
_cleanup_free_ char *unit = NULL; |
| 796 |
const char *object; |
| 797 |
|
| 798 |
r = make_service_name(argv[i], &unit); |
| 799 |
if (r < 0) |
| 800 |
return r; |
| 801 |
|
| 802 |
r = image_exists(bus, argv[i]); |
| 803 |
if (r < 0) |
| 804 |
return r; |
| 805 |
if (r == 0) |
| 806 |
return log_error_errno(SYNTHETIC_ERRNO(ENXIO), |
| 807 |
"Machine image '%s' does not exist.", |
| 808 |
argv[i]); |
| 809 |
|
| 810 |
r = bus_call_method( |
| 811 |
bus, |
| 812 |
bus_systemd_mgr, |
| 813 |
"StartUnit", |
| 814 |
&error, |
| 815 |
&reply, |
| 816 |
"ss", unit, "fail"); |
| 817 |
if (r < 0) |
| 818 |
return log_error_errno(r, "Failed to start unit: %s", bus_error_message(&error, r)); |
| 819 |
|
| 820 |
r = sd_bus_message_read(reply, "o", &object); |
| 821 |
if (r < 0) |
| 822 |
return bus_log_parse_error(r); |
| 823 |
|
| 824 |
r = bus_wait_for_jobs_add(w, object); |
| 825 |
if (r < 0) |
| 826 |
return log_oom(); |
| 827 |
} |
| 828 |
|
| 829 |
r = bus_wait_for_jobs(w, arg_quiet, NULL); |
| 830 |
if (r < 0) |
| 831 |
return r; |
| 832 |
|
| 833 |
return 0; |
| 834 |
} |
| 835 |
|
| 836 |
static int parse_machine_uid(const char *spec, const char **machine, char **uid) { |
| 837 |
/* |
| 838 |
* Whatever is specified in the spec takes priority over global arguments. |
| 839 |
*/ |
| 840 |
char *_uid = NULL; |
| 841 |
const char *_machine = NULL; |
| 842 |
|
| 843 |
assert(uid); |
| 844 |
assert(machine); |
| 845 |
|
| 846 |
if (spec) { |
| 847 |
const char *at; |
| 848 |
|
| 849 |
at = strchr(spec, '@'); |
| 850 |
if (at) { |
| 851 |
if (at == spec) |
| 852 |
/* Do the same as ssh and refuse "@host". */ |
| 853 |
return -EINVAL; |
| 854 |
|
| 855 |
_machine = at + 1; |
| 856 |
_uid = strndup(spec, at - spec); |
| 857 |
if (!_uid) |
| 858 |
return -ENOMEM; |
| 859 |
} else |
| 860 |
_machine = spec; |
| 861 |
}; |
| 862 |
|
| 863 |
if (arg_uid && !_uid) { |
| 864 |
_uid = strdup(arg_uid); |
| 865 |
if (!_uid) |
| 866 |
return -ENOMEM; |
| 867 |
} |
| 868 |
|
| 869 |
*uid = _uid; |
| 870 |
*machine = isempty(_machine) ? ".host" : _machine; |
| 871 |
return 0; |
| 872 |
} |
| 873 |
|
| 874 |
static int process_forward(sd_event *event, sd_bus_slot *machine_removed_slot, int master, PTYForwardFlags flags, const char *name) { |
| 875 |
int r; |
| 876 |
|
| 877 |
assert(event); |
| 878 |
assert(machine_removed_slot); |
| 879 |
assert(master >= 0); |
| 880 |
assert(name); |
| 881 |
|
| 882 |
if (!arg_quiet) { |
| 883 |
if (streq(name, ".host")) |
| 884 |
log_info("Connected to the local host. Press ^] three times within 1s to exit session."); |
| 885 |
else |
| 886 |
log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name); |
| 887 |
} |
| 888 |
|
| 889 |
_cleanup_(osc_context_closep) sd_id128_t osc_context_id = SD_ID128_NULL; |
| 890 |
if (!terminal_is_dumb()) { |
| 891 |
r = osc_context_open_container(name, /* ret_seq= */ NULL, &osc_context_id); |
| 892 |
if (r < 0) |
| 893 |
return r; |
| 894 |
} |
| 895 |
|
| 896 |
r = sd_event_set_signal_exit(event, true); |
| 897 |
if (r < 0) |
| 898 |
return log_error_errno(r, "Failed to enable SIGINT/SITERM handling: %m"); |
| 899 |
|
| 900 |
_cleanup_(pty_forward_freep) PTYForward *forward = NULL; |
| 901 |
r = pty_forward_new(event, master, flags, &forward); |
| 902 |
if (r < 0) |
| 903 |
return log_error_errno(r, "Failed to create PTY forwarder: %m"); |
| 904 |
|
| 905 |
/* No userdata should not set previously. */ |
| 906 |
assert_se(!sd_bus_slot_set_userdata(machine_removed_slot, forward)); |
| 907 |
|
| 908 |
r = sd_event_loop(event); |
| 909 |
if (r < 0) |
| 910 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 911 |
|
| 912 |
bool machine_died = |
| 913 |
FLAGS_SET(flags, PTY_FORWARD_IGNORE_VHANGUP) && |
| 914 |
!pty_forward_vhangup_honored(forward); |
| 915 |
|
| 916 |
if (!arg_quiet) { |
| 917 |
if (machine_died) |
| 918 |
log_info("Machine %s terminated.", name); |
| 919 |
else if (streq(name, ".host")) |
| 920 |
log_info("Connection to the local host terminated."); |
| 921 |
else |
| 922 |
log_info("Connection to machine %s terminated.", name); |
| 923 |
} |
| 924 |
|
| 925 |
return 0; |
| 926 |
} |
| 927 |
|
| 928 |
static int on_machine_removed(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
| 929 |
PTYForward *forward = ASSERT_PTR(userdata); |
| 930 |
int r; |
| 931 |
|
| 932 |
assert(m); |
| 933 |
|
| 934 |
/* Tell the forwarder to exit on the next vhangup(), so that we still flush out what might be queued |
| 935 |
* and exit then. */ |
| 936 |
|
| 937 |
r = pty_forward_honor_vhangup(forward); |
| 938 |
if (r < 0) { |
| 939 |
/* On error, quit immediately. */ |
| 940 |
log_error_errno(r, "Failed to make PTY forwarder honor vhangup(): %m"); |
| 941 |
(void) sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), EXIT_FAILURE); |
| 942 |
} |
| 943 |
|
| 944 |
return 0; |
| 945 |
} |
| 946 |
|
| 947 |
VERB(verb_login_machine, "login", "[NAME]", VERB_ANY, 2, 0, |
| 948 |
"Get a login prompt in a container or on the local host"); |
| 949 |
static int verb_login_machine(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 950 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 951 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 952 |
_cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL; |
| 953 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 954 |
int master = -1, r; |
| 955 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 956 |
const char *match, *machine; |
| 957 |
|
| 958 |
if (!strv_isempty(arg_setenv) || arg_uid) |
| 959 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 960 |
"--setenv= and --uid= are not supported for 'login'. Use 'shell' instead."); |
| 961 |
|
| 962 |
if (!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) |
| 963 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 964 |
"Login only supported on local machines."); |
| 965 |
|
| 966 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 967 |
|
| 968 |
r = sd_event_default(&event); |
| 969 |
if (r < 0) |
| 970 |
return log_error_errno(r, "Failed to get event loop: %m"); |
| 971 |
|
| 972 |
r = sd_bus_attach_event(bus, event, 0); |
| 973 |
if (r < 0) |
| 974 |
return log_error_errno(r, "Failed to attach bus to event loop: %m"); |
| 975 |
|
| 976 |
machine = argc < 2 || isempty(argv[1]) ? ".host" : argv[1]; |
| 977 |
|
| 978 |
match = strjoina("type='signal'," |
| 979 |
"sender='org.freedesktop.machine1'," |
| 980 |
"path='/org/freedesktop/machine1',", |
| 981 |
"interface='org.freedesktop.machine1.Manager'," |
| 982 |
"member='MachineRemoved'," |
| 983 |
"arg0='", machine, "'"); |
| 984 |
|
| 985 |
r = sd_bus_add_match_async(bus, &slot, match, on_machine_removed, NULL, NULL); |
| 986 |
if (r < 0) |
| 987 |
return log_error_errno(r, "Failed to request machine removal match: %m"); |
| 988 |
|
| 989 |
r = bus_call_method(bus, bus_machine_mgr, "OpenMachineLogin", &error, &reply, "s", machine); |
| 990 |
if (r < 0) |
| 991 |
return log_error_errno(r, "Failed to get login PTY: %s", bus_error_message(&error, r)); |
| 992 |
|
| 993 |
r = sd_bus_message_read(reply, "hs", &master, NULL); |
| 994 |
if (r < 0) |
| 995 |
return bus_log_parse_error(r); |
| 996 |
|
| 997 |
return process_forward(event, slot, master, PTY_FORWARD_IGNORE_VHANGUP, machine); |
| 998 |
} |
| 999 |
|
| 1000 |
VERB(verb_shell_machine, "shell", "[[USER@]NAME [COMMAND…]]", VERB_ANY, VERB_ANY, 0, |
| 1001 |
"Invoke a shell (or other command) in a container or on the local host"); |
| 1002 |
static int verb_shell_machine(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1003 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
| 1004 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1005 |
_cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL; |
| 1006 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 1007 |
int master = -1, r; |
| 1008 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1009 |
const char *match, *machine, *path; |
| 1010 |
_cleanup_free_ char *uid = NULL; |
| 1011 |
|
| 1012 |
if (!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) |
| 1013 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 1014 |
"Shell only supported on local machines."); |
| 1015 |
|
| 1016 |
if (terminal_is_dumb()) { |
| 1017 |
/* Set TERM=dumb if we are running on a dumb terminal or with a pipe. |
| 1018 |
* Otherwise, we will get unwanted OSC sequences. */ |
| 1019 |
if (!strv_find_prefix(arg_setenv, "TERM=")) |
| 1020 |
if (strv_extend(&arg_setenv, "TERM=dumb") < 0) |
| 1021 |
return log_oom(); |
| 1022 |
} else |
| 1023 |
/* Pass $TERM & Co. to shell session, if not explicitly specified. */ |
| 1024 |
FOREACH_STRING(v, "TERM=", "COLORTERM=", "NO_COLOR=") { |
| 1025 |
if (strv_find_prefix(arg_setenv, v)) |
| 1026 |
continue; |
| 1027 |
|
| 1028 |
const char *t = strv_find_prefix(environ, v); |
| 1029 |
if (!t) |
| 1030 |
continue; |
| 1031 |
|
| 1032 |
if (strv_extend(&arg_setenv, t) < 0) |
| 1033 |
return log_oom(); |
| 1034 |
} |
| 1035 |
|
| 1036 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 1037 |
|
| 1038 |
r = sd_event_default(&event); |
| 1039 |
if (r < 0) |
| 1040 |
return log_error_errno(r, "Failed to get event loop: %m"); |
| 1041 |
|
| 1042 |
r = sd_bus_attach_event(bus, event, 0); |
| 1043 |
if (r < 0) |
| 1044 |
return log_error_errno(r, "Failed to attach bus to event loop: %m"); |
| 1045 |
|
| 1046 |
r = parse_machine_uid(argc >= 2 ? argv[1] : NULL, &machine, &uid); |
| 1047 |
if (r < 0) |
| 1048 |
return log_error_errno(r, "Failed to parse machine specification: %m"); |
| 1049 |
|
| 1050 |
match = strjoina("type='signal'," |
| 1051 |
"sender='org.freedesktop.machine1'," |
| 1052 |
"path='/org/freedesktop/machine1',", |
| 1053 |
"interface='org.freedesktop.machine1.Manager'," |
| 1054 |
"member='MachineRemoved'," |
| 1055 |
"arg0='", machine, "'"); |
| 1056 |
|
| 1057 |
r = sd_bus_add_match_async(bus, &slot, match, on_machine_removed, NULL, NULL); |
| 1058 |
if (r < 0) |
| 1059 |
return log_error_errno(r, "Failed to request machine removal match: %m"); |
| 1060 |
|
| 1061 |
r = bus_message_new_method_call(bus, &m, bus_machine_mgr, "OpenMachineShell"); |
| 1062 |
if (r < 0) |
| 1063 |
return bus_log_create_error(r); |
| 1064 |
|
| 1065 |
path = argc < 3 || isempty(argv[2]) ? NULL : argv[2]; |
| 1066 |
|
| 1067 |
r = sd_bus_message_append(m, "sss", machine, uid, path); |
| 1068 |
if (r < 0) |
| 1069 |
return bus_log_create_error(r); |
| 1070 |
|
| 1071 |
r = sd_bus_message_append_strv(m, strv_length(argv) <= 3 ? NULL : argv + 2); |
| 1072 |
if (r < 0) |
| 1073 |
return bus_log_create_error(r); |
| 1074 |
|
| 1075 |
r = sd_bus_message_append_strv(m, arg_setenv); |
| 1076 |
if (r < 0) |
| 1077 |
return bus_log_create_error(r); |
| 1078 |
|
| 1079 |
r = sd_bus_call(bus, m, 0, &error, &reply); |
| 1080 |
if (r < 0) |
| 1081 |
return log_error_errno(r, "Failed to get shell PTY: %s", bus_error_message(&error, r)); |
| 1082 |
|
| 1083 |
r = sd_bus_message_read(reply, "hs", &master, NULL); |
| 1084 |
if (r < 0) |
| 1085 |
return bus_log_parse_error(r); |
| 1086 |
|
| 1087 |
return process_forward(event, slot, master, /* flags= */ 0, machine); |
| 1088 |
} |
| 1089 |
|
| 1090 |
static int verb_poweroff_machine(int argc, char *argv[], uintptr_t data, void *userdata); |
| 1091 |
|
| 1092 |
VERB(verb_enable_machine, "enable", "NAME…", 2, VERB_ANY, 0, |
| 1093 |
"Enable automatic container start at boot"); |
| 1094 |
VERB(verb_enable_machine, "disable", "NAME…", 2, VERB_ANY, 0, |
| 1095 |
"Disable automatic container start at boot"); |
| 1096 |
static int verb_enable_machine(int argc, char *argv[], uintptr_t data, void *userdata) { |
| 1097 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
| 1098 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1099 |
const char *method; |
| 1100 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1101 |
int r; |
| 1102 |
bool enable; |
| 1103 |
|
| 1104 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 1105 |
|
| 1106 |
enable = streq(argv[0], "enable"); |
| 1107 |
method = enable ? "EnableUnitFiles" : "DisableUnitFiles"; |
| 1108 |
|
| 1109 |
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
| 1110 |
if (r < 0) |
| 1111 |
return bus_log_create_error(r); |
| 1112 |
|
| 1113 |
r = sd_bus_message_open_container(m, 'a', "s"); |
| 1114 |
if (r < 0) |
| 1115 |
return bus_log_create_error(r); |
| 1116 |
|
| 1117 |
if (enable) { |
| 1118 |
r = sd_bus_message_append(m, "s", "machines.target"); |
| 1119 |
if (r < 0) |
| 1120 |
return bus_log_create_error(r); |
| 1121 |
} |
| 1122 |
|
| 1123 |
for (int i = 1; i < argc; i++) { |
| 1124 |
_cleanup_free_ char *unit = NULL; |
| 1125 |
|
| 1126 |
r = make_service_name(argv[i], &unit); |
| 1127 |
if (r < 0) |
| 1128 |
return r; |
| 1129 |
|
| 1130 |
r = image_exists(bus, argv[i]); |
| 1131 |
if (r < 0) |
| 1132 |
return r; |
| 1133 |
if (r == 0) |
| 1134 |
return log_error_errno(SYNTHETIC_ERRNO(ENXIO), |
| 1135 |
"Machine image '%s' does not exist.", |
| 1136 |
argv[i]); |
| 1137 |
|
| 1138 |
r = sd_bus_message_append(m, "s", unit); |
| 1139 |
if (r < 0) |
| 1140 |
return bus_log_create_error(r); |
| 1141 |
} |
| 1142 |
|
| 1143 |
r = sd_bus_message_close_container(m); |
| 1144 |
if (r < 0) |
| 1145 |
return bus_log_create_error(r); |
| 1146 |
|
| 1147 |
if (enable) |
| 1148 |
r = sd_bus_message_append(m, "bb", false, false); |
| 1149 |
else |
| 1150 |
r = sd_bus_message_append(m, "b", false); |
| 1151 |
if (r < 0) |
| 1152 |
return bus_log_create_error(r); |
| 1153 |
|
| 1154 |
r = sd_bus_call(bus, m, 0, &error, &reply); |
| 1155 |
if (r < 0) |
| 1156 |
return log_error_errno(r, "Failed to enable or disable unit: %s", bus_error_message(&error, r)); |
| 1157 |
|
| 1158 |
if (enable) { |
| 1159 |
r = sd_bus_message_read(reply, "b", NULL); |
| 1160 |
if (r < 0) |
| 1161 |
return bus_log_parse_error(r); |
| 1162 |
} |
| 1163 |
|
| 1164 |
r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet); |
| 1165 |
if (r < 0) |
| 1166 |
return r; |
| 1167 |
|
| 1168 |
r = bus_service_manager_reload(bus); |
| 1169 |
if (r < 0) |
| 1170 |
return r; |
| 1171 |
|
| 1172 |
if (arg_now) { |
| 1173 |
_cleanup_strv_free_ char **new_args = NULL; |
| 1174 |
|
| 1175 |
new_args = strv_new(enable ? "start" : "poweroff"); |
| 1176 |
if (!new_args) |
| 1177 |
return log_oom(); |
| 1178 |
|
| 1179 |
r = strv_extend_strv(&new_args, argv + 1, /* filter_duplicates= */ false); |
| 1180 |
if (r < 0) |
| 1181 |
return log_oom(); |
| 1182 |
|
| 1183 |
if (enable) |
| 1184 |
return verb_start_machine(strv_length(new_args), new_args, data, userdata); |
| 1185 |
|
| 1186 |
return verb_poweroff_machine(strv_length(new_args), new_args, data, userdata); |
| 1187 |
} |
| 1188 |
|
| 1189 |
return 0; |
| 1190 |
} |
| 1191 |
|
| 1192 |
/* Look up the controlAddress of a machine by calling machined's Machine.List varlink interface. */ |
| 1193 |
static int machine_get_control_address(const char *machine_name, char **ret) { |
| 1194 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
| 1195 |
sd_json_variant *reply = NULL; |
| 1196 |
const char *error_id = NULL; |
| 1197 |
int r; |
| 1198 |
|
| 1199 |
assert(machine_name); |
| 1200 |
assert(ret); |
| 1201 |
|
| 1202 |
if (arg_transport != BUS_TRANSPORT_LOCAL) |
| 1203 |
return -EOPNOTSUPP; |
| 1204 |
|
| 1205 |
_cleanup_free_ char *p = NULL; |
| 1206 |
r = runtime_directory_generic(arg_runtime_scope, "systemd/machine/io.systemd.Machine", &p); |
| 1207 |
if (r < 0) |
| 1208 |
return log_error_errno(r, "Failed to determine Machine varlink socket path: %m"); |
| 1209 |
|
| 1210 |
r = sd_varlink_connect_address(&vl, p); |
| 1211 |
if (r < 0) |
| 1212 |
return log_error_errno(r, "Failed to connect to machined varlink: %m"); |
| 1213 |
|
| 1214 |
r = sd_varlink_callbo( |
| 1215 |
vl, |
| 1216 |
"io.systemd.Machine.List", |
| 1217 |
&reply, |
| 1218 |
&error_id, |
| 1219 |
SD_JSON_BUILD_PAIR_STRING("name", machine_name)); |
| 1220 |
if (r < 0) |
| 1221 |
return log_error_errno(r, "Failed to list machine: %m"); |
| 1222 |
if (error_id) |
| 1223 |
return log_error_errno(sd_varlink_error_to_errno(error_id, reply), |
| 1224 |
"Failed to look up machine '%s': %s", machine_name, error_id); |
| 1225 |
|
| 1226 |
sd_json_variant *addr = sd_json_variant_by_key(reply, "controlAddress"); |
| 1227 |
if (!addr || !sd_json_variant_is_string(addr)) |
| 1228 |
return -EOPNOTSUPP; /* No varlink control socket, caller decides whether to log */ |
| 1229 |
|
| 1230 |
char *a = strdup(sd_json_variant_string(addr)); |
| 1231 |
if (!a) |
| 1232 |
return log_oom(); |
| 1233 |
|
| 1234 |
*ret = a; |
| 1235 |
return 0; |
| 1236 |
} |
| 1237 |
|
| 1238 |
static int verb_machine_control_one(const char *machine_name, const char *method) { |
| 1239 |
_cleanup_free_ char *address = NULL; |
| 1240 |
int r; |
| 1241 |
|
| 1242 |
r = machine_get_control_address(machine_name, &address); |
| 1243 |
if (r < 0) |
| 1244 |
return r; |
| 1245 |
|
| 1246 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
| 1247 |
r = sd_varlink_connect_address(&vl, address); |
| 1248 |
if (r < 0) |
| 1249 |
return log_error_errno(r, "Failed to connect to machine control socket: %m"); |
| 1250 |
|
| 1251 |
sd_json_variant *reply = NULL; |
| 1252 |
const char *error_id = NULL; |
| 1253 |
r = sd_varlink_call(vl, method, /* parameters= */ NULL, &reply, &error_id); |
| 1254 |
if (r < 0) |
| 1255 |
return log_error_errno(r, "Failed to call %s: %m", method); |
| 1256 |
if (error_id) |
| 1257 |
return log_error_errno(sd_varlink_error_to_errno(error_id, reply), |
| 1258 |
"Machine control call failed: %s", error_id); |
| 1259 |
|
| 1260 |
return 0; |
| 1261 |
} |
| 1262 |
|
| 1263 |
VERB(verb_poweroff_machine, "poweroff", "NAME…", 2, VERB_ANY, 0, |
| 1264 |
"Power off one or more machines"); |
| 1265 |
VERB(verb_poweroff_machine, "stop", "NAME…", 2, VERB_ANY, 0, |
| 1266 |
/* help= */ NULL); /* Convenience alias */ |
| 1267 |
static int verb_poweroff_machine(int argc, char *argv[], uintptr_t data, void *userdata) { |
| 1268 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1269 |
int r; |
| 1270 |
|
| 1271 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 1272 |
|
| 1273 |
for (int i = 1; i < argc; i++) { |
| 1274 |
/* VM with varlink control socket: QMP graceful powerdown */ |
| 1275 |
r = verb_machine_control_one(argv[i], "io.systemd.MachineInstance.PowerOff"); |
| 1276 |
if (r >= 0) |
| 1277 |
continue; |
| 1278 |
if (r != -EOPNOTSUPP) |
| 1279 |
return r; |
| 1280 |
|
| 1281 |
/* Not a VM: signal-based poweroff */ |
| 1282 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1283 |
r = bus_call_method(bus, bus_machine_mgr, "KillMachine", &error, NULL, |
| 1284 |
"ssi", argv[i], "leader", (int32_t) (SIGRTMIN+4)); |
| 1285 |
if (r < 0) |
| 1286 |
return log_error_errno(r, "Could not kill machine: %s", bus_error_message(&error, r)); |
| 1287 |
} |
| 1288 |
|
| 1289 |
return 0; |
| 1290 |
} |
| 1291 |
|
| 1292 |
VERB(verb_reboot_machine, "reboot", "NAME…", 2, VERB_ANY, 0, |
| 1293 |
"Reboot one or more machines"); |
| 1294 |
VERB(verb_reboot_machine, "restart", "NAME…", 2, VERB_ANY, 0, |
| 1295 |
/* help= */ NULL); /* Convenience alias */ |
| 1296 |
static int verb_reboot_machine(int argc, char *argv[], uintptr_t data, void *userdata) { |
| 1297 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1298 |
int r; |
| 1299 |
|
| 1300 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 1301 |
|
| 1302 |
for (int i = 1; i < argc; i++) { |
| 1303 |
r = verb_machine_control_one(argv[i], "io.systemd.MachineInstance.Reboot"); |
| 1304 |
if (r >= 0) |
| 1305 |
continue; |
| 1306 |
if (r != -EOPNOTSUPP) |
| 1307 |
return r; |
| 1308 |
|
| 1309 |
/* Container fallback: SIGINT to init (sysvinit + systemd compatible) */ |
| 1310 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1311 |
r = bus_call_method(bus, bus_machine_mgr, "KillMachine", &error, NULL, |
| 1312 |
"ssi", argv[i], "leader", (int32_t) SIGINT); |
| 1313 |
if (r < 0) |
| 1314 |
return log_error_errno(r, "Could not reboot machine '%s': %s", argv[i], bus_error_message(&error, r)); |
| 1315 |
} |
| 1316 |
|
| 1317 |
return 0; |
| 1318 |
} |
| 1319 |
|
| 1320 |
static int verb_vm_control(int argc, char *argv[], const char *method) { |
| 1321 |
int r; |
| 1322 |
|
| 1323 |
for (int i = 1; i < argc; i++) { |
| 1324 |
r = verb_machine_control_one(argv[i], method); |
| 1325 |
if (r == -EOPNOTSUPP) |
| 1326 |
return log_error_errno(r, "Machine '%s' does not expose a varlink control socket.", argv[i]); |
| 1327 |
if (r < 0) |
| 1328 |
return r; |
| 1329 |
} |
| 1330 |
|
| 1331 |
return 0; |
| 1332 |
} |
| 1333 |
|
| 1334 |
VERB(verb_pause, "pause", "NAME…", 2, VERB_ANY, 0, |
| 1335 |
"Pause one or more machines"); |
| 1336 |
static int verb_pause(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1337 |
return verb_vm_control(argc, argv, "io.systemd.MachineInstance.Pause"); |
| 1338 |
} |
| 1339 |
|
| 1340 |
VERB(verb_resume, "resume", "NAME…", 2, VERB_ANY, 0, |
| 1341 |
"Resume one or more paused machines"); |
| 1342 |
static int verb_resume(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1343 |
return verb_vm_control(argc, argv, "io.systemd.MachineInstance.Resume"); |
| 1344 |
} |
| 1345 |
|
| 1346 |
VERB(verb_terminate_machine, "terminate", "NAME…", 2, VERB_ANY, 0, |
| 1347 |
"Terminate one or more machines"); |
| 1348 |
static int verb_terminate_machine(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1349 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1350 |
int r; |
| 1351 |
|
| 1352 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 1353 |
|
| 1354 |
for (int i = 1; i < argc; i++) { |
| 1355 |
/* VM with varlink control socket: QMP quit (immediate termination) */ |
| 1356 |
r = verb_machine_control_one(argv[i], "io.systemd.MachineInstance.Terminate"); |
| 1357 |
if (r >= 0) |
| 1358 |
continue; |
| 1359 |
if (r != -EOPNOTSUPP) |
| 1360 |
return r; |
| 1361 |
|
| 1362 |
/* Not a VM or no varlink socket: fall back to machined */ |
| 1363 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1364 |
r = bus_call_method(bus, bus_machine_mgr, "TerminateMachine", &error, NULL, "s", argv[i]); |
| 1365 |
if (r < 0) |
| 1366 |
return log_error_errno(r, "Could not terminate machine: %s", bus_error_message(&error, r)); |
| 1367 |
} |
| 1368 |
|
| 1369 |
return 0; |
| 1370 |
} |
| 1371 |
|
| 1372 |
VERB(verb_kill_machine, "kill", "NAME…", 2, VERB_ANY, 0, |
| 1373 |
"Send signal to processes of a machine"); |
| 1374 |
static int verb_kill_machine(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1375 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1376 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1377 |
int r; |
| 1378 |
|
| 1379 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 1380 |
|
| 1381 |
if (!arg_kill_whom) |
| 1382 |
arg_kill_whom = "all"; |
| 1383 |
|
| 1384 |
for (int i = 1; i < argc; i++) { |
| 1385 |
r = bus_call_method( |
| 1386 |
bus, |
| 1387 |
bus_machine_mgr, |
| 1388 |
"KillMachine", |
| 1389 |
&error, |
| 1390 |
NULL, |
| 1391 |
"ssi", argv[i], arg_kill_whom, arg_signal); |
| 1392 |
if (r < 0) |
| 1393 |
return log_error_errno(r, "Could not kill machine: %s", bus_error_message(&error, r)); |
| 1394 |
} |
| 1395 |
|
| 1396 |
return 0; |
| 1397 |
} |
| 1398 |
|
| 1399 |
static const char* select_copy_method(bool copy_from, bool force) { |
| 1400 |
if (force) |
| 1401 |
return copy_from ? "CopyFromMachineWithFlags" : "CopyToMachineWithFlags"; |
| 1402 |
else |
| 1403 |
return copy_from ? "CopyFromMachine" : "CopyToMachine"; |
| 1404 |
} |
| 1405 |
|
| 1406 |
VERB(verb_copy_files, "copy-to", "NAME PATH [PATH]", 3, 4, 0, |
| 1407 |
"Copy files from the host to a container"); |
| 1408 |
VERB(verb_copy_files, "copy-from", "NAME PATH [PATH]", 3, 4, 0, |
| 1409 |
"Copy files from a container to the host"); |
| 1410 |
static int verb_copy_files(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1411 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1412 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 1413 |
_cleanup_free_ char *abs_host_path = NULL; |
| 1414 |
char *dest, *host_path, *container_path; |
| 1415 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1416 |
bool copy_from; |
| 1417 |
int r; |
| 1418 |
|
| 1419 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 1420 |
|
| 1421 |
copy_from = streq(argv[0], "copy-from"); |
| 1422 |
dest = argv[3] ?: argv[2]; |
| 1423 |
host_path = copy_from ? dest : argv[2]; |
| 1424 |
container_path = copy_from ? argv[2] : dest; |
| 1425 |
|
| 1426 |
if (!path_is_absolute(host_path)) { |
| 1427 |
r = path_make_absolute_cwd(host_path, &abs_host_path); |
| 1428 |
if (r < 0) |
| 1429 |
return log_error_errno(r, "Failed to make path absolute: %m"); |
| 1430 |
|
| 1431 |
host_path = abs_host_path; |
| 1432 |
} |
| 1433 |
|
| 1434 |
r = bus_message_new_method_call( |
| 1435 |
bus, |
| 1436 |
&m, |
| 1437 |
bus_machine_mgr, |
| 1438 |
select_copy_method(copy_from, arg_force)); |
| 1439 |
if (r < 0) |
| 1440 |
return bus_log_create_error(r); |
| 1441 |
|
| 1442 |
r = sd_bus_message_append( |
| 1443 |
m, |
| 1444 |
"sss", |
| 1445 |
argv[1], |
| 1446 |
copy_from ? container_path : host_path, |
| 1447 |
copy_from ? host_path : container_path); |
| 1448 |
if (r < 0) |
| 1449 |
return bus_log_create_error(r); |
| 1450 |
|
| 1451 |
if (arg_force) { |
| 1452 |
r = sd_bus_message_append(m, "t", (uint64_t) MACHINE_COPY_REPLACE); |
| 1453 |
if (r < 0) |
| 1454 |
return bus_log_create_error(r); |
| 1455 |
} |
| 1456 |
|
| 1457 |
/* This is a slow operation, hence turn off any method call timeouts */ |
| 1458 |
r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL); |
| 1459 |
if (r < 0) |
| 1460 |
return log_error_errno(r, "Failed to copy: %s", bus_error_message(&error, r)); |
| 1461 |
|
| 1462 |
return 0; |
| 1463 |
} |
| 1464 |
|
| 1465 |
VERB(verb_bind_mount, "bind", "NAME PATH [PATH]", 3, 4, 0, |
| 1466 |
"Bind mount a path from the host into a container"); |
| 1467 |
static int verb_bind_mount(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1468 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1469 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1470 |
int r; |
| 1471 |
|
| 1472 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 1473 |
|
| 1474 |
r = bus_call_method( |
| 1475 |
bus, |
| 1476 |
bus_machine_mgr, |
| 1477 |
"BindMountMachine", |
| 1478 |
&error, |
| 1479 |
NULL, |
| 1480 |
"sssbb", |
| 1481 |
argv[1], |
| 1482 |
argv[2], |
| 1483 |
argv[3], |
| 1484 |
arg_read_only, |
| 1485 |
arg_mkdir); |
| 1486 |
if (r < 0) |
| 1487 |
return log_error_errno(r, "Failed to bind mount: %s", bus_error_message(&error, r)); |
| 1488 |
|
| 1489 |
return 0; |
| 1490 |
} |
| 1491 |
|
| 1492 |
VERB_GROUP("Image Commands"); |
| 1493 |
|
| 1494 |
VERB(verb_list_images, "list-images", /* argspec= */ NULL, VERB_ANY, 1, 0, |
| 1495 |
"Show available container and VM images"); |
| 1496 |
static int verb_list_images(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1497 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1498 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1499 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 1500 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1501 |
int r; |
| 1502 |
|
| 1503 |
pager_open(arg_pager_flags); |
| 1504 |
|
| 1505 |
r = bus_call_method(bus, bus_machine_mgr, "ListImages", &error, &reply, NULL); |
| 1506 |
if (r < 0) |
| 1507 |
return log_error_errno(r, "Could not get images: %s", bus_error_message(&error, r)); |
| 1508 |
|
| 1509 |
table = table_new("name", "type", "ro", "usage", "created", "modified"); |
| 1510 |
if (!table) |
| 1511 |
return log_oom(); |
| 1512 |
|
| 1513 |
if (arg_full) |
| 1514 |
table_set_width(table, 0); |
| 1515 |
|
| 1516 |
(void) table_set_align_percent(table, TABLE_HEADER_CELL(3), 100); |
| 1517 |
|
| 1518 |
r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbttto)"); |
| 1519 |
if (r < 0) |
| 1520 |
return bus_log_parse_error(r); |
| 1521 |
|
| 1522 |
for (;;) { |
| 1523 |
uint64_t crtime, mtime, size; |
| 1524 |
const char *name, *type; |
| 1525 |
int ro_int; |
| 1526 |
|
| 1527 |
r = sd_bus_message_read(reply, "(ssbttto)", &name, &type, &ro_int, &crtime, &mtime, &size, NULL); |
| 1528 |
if (r < 0) |
| 1529 |
return bus_log_parse_error(r); |
| 1530 |
if (r == 0) |
| 1531 |
break; |
| 1532 |
|
| 1533 |
if (name[0] == '.' && !arg_all) |
| 1534 |
continue; |
| 1535 |
|
| 1536 |
r = table_add_many(table, |
| 1537 |
TABLE_STRING, name, |
| 1538 |
TABLE_STRING, type, |
| 1539 |
TABLE_BOOLEAN, ro_int, |
| 1540 |
TABLE_SET_COLOR, ro_int ? ansi_highlight_red() : NULL, |
| 1541 |
TABLE_SIZE, size, |
| 1542 |
TABLE_TIMESTAMP, crtime, |
| 1543 |
TABLE_TIMESTAMP, mtime); |
| 1544 |
if (r < 0) |
| 1545 |
return table_log_add_error(r); |
| 1546 |
} |
| 1547 |
|
| 1548 |
r = sd_bus_message_exit_container(reply); |
| 1549 |
if (r < 0) |
| 1550 |
return bus_log_parse_error(r); |
| 1551 |
|
| 1552 |
return show_table(table, "images"); |
| 1553 |
} |
| 1554 |
|
| 1555 |
static int print_image_hostname(sd_bus *bus, const char *name) { |
| 1556 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1557 |
const char *hn; |
| 1558 |
int r; |
| 1559 |
|
| 1560 |
r = bus_call_method(bus, bus_machine_mgr, "GetImageHostname", NULL, &reply, "s", name); |
| 1561 |
if (r < 0) |
| 1562 |
return r; |
| 1563 |
|
| 1564 |
r = sd_bus_message_read(reply, "s", &hn); |
| 1565 |
if (r < 0) |
| 1566 |
return r; |
| 1567 |
|
| 1568 |
if (!isempty(hn)) |
| 1569 |
printf("\tHostname: %s\n", hn); |
| 1570 |
|
| 1571 |
return 0; |
| 1572 |
} |
| 1573 |
|
| 1574 |
static int print_image_machine_id(sd_bus *bus, const char *name) { |
| 1575 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1576 |
sd_id128_t id; |
| 1577 |
int r; |
| 1578 |
|
| 1579 |
r = bus_call_method(bus, bus_machine_mgr, "GetImageMachineID", NULL, &reply, "s", name); |
| 1580 |
if (r < 0) |
| 1581 |
return r; |
| 1582 |
|
| 1583 |
r = bus_message_read_id128(reply, &id); |
| 1584 |
if (r < 0) |
| 1585 |
return r; |
| 1586 |
|
| 1587 |
if (!sd_id128_is_null(id)) |
| 1588 |
printf(" Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(id)); |
| 1589 |
|
| 1590 |
return 0; |
| 1591 |
} |
| 1592 |
|
| 1593 |
static int print_image_machine_info(sd_bus *bus, const char *name) { |
| 1594 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1595 |
int r; |
| 1596 |
|
| 1597 |
r = bus_call_method(bus, bus_machine_mgr, "GetImageMachineInfo", NULL, &reply, "s", name); |
| 1598 |
if (r < 0) |
| 1599 |
return r; |
| 1600 |
|
| 1601 |
r = sd_bus_message_enter_container(reply, 'a', "{ss}"); |
| 1602 |
if (r < 0) |
| 1603 |
return r; |
| 1604 |
|
| 1605 |
for (;;) { |
| 1606 |
const char *p, *q; |
| 1607 |
|
| 1608 |
r = sd_bus_message_read(reply, "{ss}", &p, &q); |
| 1609 |
if (r < 0) |
| 1610 |
return r; |
| 1611 |
if (r == 0) |
| 1612 |
break; |
| 1613 |
|
| 1614 |
if (streq(p, "DEPLOYMENT")) |
| 1615 |
printf(" Deployment: %s\n", q); |
| 1616 |
} |
| 1617 |
|
| 1618 |
r = sd_bus_message_exit_container(reply); |
| 1619 |
if (r < 0) |
| 1620 |
return r; |
| 1621 |
|
| 1622 |
return 0; |
| 1623 |
} |
| 1624 |
|
| 1625 |
typedef struct ImageStatusInfo { |
| 1626 |
const char *name; |
| 1627 |
const char *path; |
| 1628 |
const char *type; |
| 1629 |
bool read_only; |
| 1630 |
usec_t crtime; |
| 1631 |
usec_t mtime; |
| 1632 |
uint64_t usage; |
| 1633 |
uint64_t limit; |
| 1634 |
uint64_t usage_exclusive; |
| 1635 |
uint64_t limit_exclusive; |
| 1636 |
} ImageStatusInfo; |
| 1637 |
|
| 1638 |
static void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) { |
| 1639 |
assert(bus); |
| 1640 |
assert(i); |
| 1641 |
|
| 1642 |
if (i->name) { |
| 1643 |
fputs(i->name, stdout); |
| 1644 |
putchar('\n'); |
| 1645 |
} |
| 1646 |
|
| 1647 |
if (i->type) |
| 1648 |
printf("\t Type: %s\n", i->type); |
| 1649 |
|
| 1650 |
if (i->path) |
| 1651 |
printf("\t Path: %s\n", i->path); |
| 1652 |
|
| 1653 |
(void) print_image_hostname(bus, i->name); |
| 1654 |
(void) print_image_machine_id(bus, i->name); |
| 1655 |
(void) print_image_machine_info(bus, i->name); |
| 1656 |
|
| 1657 |
print_os_release(bus, "GetImageOSRelease", i->name, "\t OS: "); |
| 1658 |
|
| 1659 |
printf("\t RO: %s%s%s\n", |
| 1660 |
i->read_only ? ansi_highlight_red() : "", |
| 1661 |
i->read_only ? "read-only" : "writable", |
| 1662 |
i->read_only ? ansi_normal() : ""); |
| 1663 |
|
| 1664 |
if (timestamp_is_set(i->crtime)) |
| 1665 |
printf("\t Created: %s; %s\n", |
| 1666 |
FORMAT_TIMESTAMP(i->crtime), FORMAT_TIMESTAMP_RELATIVE(i->crtime)); |
| 1667 |
|
| 1668 |
if (timestamp_is_set(i->mtime)) |
| 1669 |
printf("\tModified: %s; %s\n", |
| 1670 |
FORMAT_TIMESTAMP(i->mtime), FORMAT_TIMESTAMP_RELATIVE(i->mtime)); |
| 1671 |
|
| 1672 |
if (i->usage != UINT64_MAX) { |
| 1673 |
if (i->usage_exclusive != i->usage && i->usage_exclusive != UINT64_MAX) |
| 1674 |
printf("\t Usage: %s (exclusive: %s)\n", |
| 1675 |
FORMAT_BYTES(i->usage), FORMAT_BYTES(i->usage_exclusive)); |
| 1676 |
else |
| 1677 |
printf("\t Usage: %s\n", FORMAT_BYTES(i->usage)); |
| 1678 |
} |
| 1679 |
|
| 1680 |
if (i->limit != UINT64_MAX) { |
| 1681 |
if (i->limit_exclusive != i->limit && i->limit_exclusive != UINT64_MAX) |
| 1682 |
printf("\t Limit: %s (exclusive: %s)\n", |
| 1683 |
FORMAT_BYTES(i->limit), FORMAT_BYTES(i->limit_exclusive)); |
| 1684 |
else |
| 1685 |
printf("\t Limit: %s\n", FORMAT_BYTES(i->limit)); |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
static int show_image_info(sd_bus *bus, const char *path, bool *new_line) { |
| 1690 |
static const struct bus_properties_map map[] = { |
| 1691 |
{ "Name", "s", NULL, offsetof(ImageStatusInfo, name) }, |
| 1692 |
{ "Path", "s", NULL, offsetof(ImageStatusInfo, path) }, |
| 1693 |
{ "Type", "s", NULL, offsetof(ImageStatusInfo, type) }, |
| 1694 |
{ "ReadOnly", "b", NULL, offsetof(ImageStatusInfo, read_only) }, |
| 1695 |
{ "CreationTimestamp", "t", NULL, offsetof(ImageStatusInfo, crtime) }, |
| 1696 |
{ "ModificationTimestamp", "t", NULL, offsetof(ImageStatusInfo, mtime) }, |
| 1697 |
{ "Usage", "t", NULL, offsetof(ImageStatusInfo, usage) }, |
| 1698 |
{ "Limit", "t", NULL, offsetof(ImageStatusInfo, limit) }, |
| 1699 |
{ "UsageExclusive", "t", NULL, offsetof(ImageStatusInfo, usage_exclusive) }, |
| 1700 |
{ "LimitExclusive", "t", NULL, offsetof(ImageStatusInfo, limit_exclusive) }, |
| 1701 |
{} |
| 1702 |
}; |
| 1703 |
|
| 1704 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1705 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 1706 |
ImageStatusInfo info = {}; |
| 1707 |
int r; |
| 1708 |
|
| 1709 |
assert(bus); |
| 1710 |
assert(path); |
| 1711 |
assert(new_line); |
| 1712 |
|
| 1713 |
r = bus_map_all_properties(bus, |
| 1714 |
"org.freedesktop.machine1", |
| 1715 |
path, |
| 1716 |
map, |
| 1717 |
BUS_MAP_BOOLEAN_AS_BOOL, |
| 1718 |
&error, |
| 1719 |
&m, |
| 1720 |
&info); |
| 1721 |
if (r < 0) |
| 1722 |
return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r)); |
| 1723 |
|
| 1724 |
if (*new_line) |
| 1725 |
printf("\n"); |
| 1726 |
*new_line = true; |
| 1727 |
|
| 1728 |
print_image_status_info(bus, &info); |
| 1729 |
|
| 1730 |
return r; |
| 1731 |
} |
| 1732 |
|
| 1733 |
typedef struct PoolStatusInfo { |
| 1734 |
const char *path; |
| 1735 |
uint64_t usage; |
| 1736 |
uint64_t limit; |
| 1737 |
} PoolStatusInfo; |
| 1738 |
|
| 1739 |
static void print_pool_status_info(sd_bus *bus, PoolStatusInfo *i) { |
| 1740 |
if (i->path) |
| 1741 |
printf("\t Path: %s\n", i->path); |
| 1742 |
|
| 1743 |
if (i->usage != UINT64_MAX) |
| 1744 |
printf("\t Usage: %s\n", FORMAT_BYTES(i->usage)); |
| 1745 |
|
| 1746 |
if (i->limit != UINT64_MAX) |
| 1747 |
printf("\t Limit: %s\n", FORMAT_BYTES(i->limit)); |
| 1748 |
} |
| 1749 |
|
| 1750 |
static int show_pool_info(sd_bus *bus) { |
| 1751 |
static const struct bus_properties_map map[] = { |
| 1752 |
{ "PoolPath", "s", NULL, offsetof(PoolStatusInfo, path) }, |
| 1753 |
{ "PoolUsage", "t", NULL, offsetof(PoolStatusInfo, usage) }, |
| 1754 |
{ "PoolLimit", "t", NULL, offsetof(PoolStatusInfo, limit) }, |
| 1755 |
{} |
| 1756 |
}; |
| 1757 |
|
| 1758 |
PoolStatusInfo info = { |
| 1759 |
.usage = UINT64_MAX, |
| 1760 |
.limit = UINT64_MAX, |
| 1761 |
}; |
| 1762 |
|
| 1763 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1764 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 1765 |
int r; |
| 1766 |
|
| 1767 |
assert(bus); |
| 1768 |
|
| 1769 |
r = bus_map_all_properties(bus, |
| 1770 |
"org.freedesktop.machine1", |
| 1771 |
"/org/freedesktop/machine1", |
| 1772 |
map, |
| 1773 |
0, |
| 1774 |
&error, |
| 1775 |
&m, |
| 1776 |
&info); |
| 1777 |
if (r < 0) |
| 1778 |
return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r)); |
| 1779 |
|
| 1780 |
print_pool_status_info(bus, &info); |
| 1781 |
|
| 1782 |
return 0; |
| 1783 |
} |
| 1784 |
|
| 1785 |
static int show_image_properties(sd_bus *bus, const char *path, bool *new_line) { |
| 1786 |
int r; |
| 1787 |
|
| 1788 |
assert(bus); |
| 1789 |
assert(path); |
| 1790 |
assert(new_line); |
| 1791 |
|
| 1792 |
if (*new_line) |
| 1793 |
printf("\n"); |
| 1794 |
|
| 1795 |
*new_line = true; |
| 1796 |
|
| 1797 |
r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, NULL, arg_property, arg_print_flags, NULL); |
| 1798 |
if (r < 0) |
| 1799 |
log_error_errno(r, "Could not get properties: %m"); |
| 1800 |
|
| 1801 |
return r; |
| 1802 |
} |
| 1803 |
|
| 1804 |
VERB(verb_show_image, "image-status", "[NAME…]", VERB_ANY, VERB_ANY, 0, |
| 1805 |
"Show image details"); |
| 1806 |
VERB(verb_show_image, "show-image", "[NAME…]", VERB_ANY, VERB_ANY, 0, |
| 1807 |
"Show properties of image"); |
| 1808 |
static int verb_show_image(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1809 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1810 |
bool properties, new_line = false; |
| 1811 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 1812 |
int r = 0; |
| 1813 |
|
| 1814 |
properties = !strstr(argv[0], "status"); |
| 1815 |
|
| 1816 |
pager_open(arg_pager_flags); |
| 1817 |
|
| 1818 |
if (argc <= 1) { |
| 1819 |
|
| 1820 |
/* If no argument is specified, inspect the manager |
| 1821 |
* itself */ |
| 1822 |
|
| 1823 |
if (properties) |
| 1824 |
r = show_image_properties(bus, "/org/freedesktop/machine1", &new_line); |
| 1825 |
else |
| 1826 |
r = show_pool_info(bus); |
| 1827 |
if (r < 0) |
| 1828 |
return r; |
| 1829 |
} |
| 1830 |
|
| 1831 |
for (int i = 1; i < argc; i++) { |
| 1832 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1833 |
const char *path = NULL; |
| 1834 |
|
| 1835 |
r = bus_call_method(bus, bus_machine_mgr, "GetImage", &error, &reply, "s", argv[i]); |
| 1836 |
if (r < 0) |
| 1837 |
return log_error_errno(r, "Could not get path to image: %s", bus_error_message(&error, r)); |
| 1838 |
|
| 1839 |
r = sd_bus_message_read(reply, "o", &path); |
| 1840 |
if (r < 0) |
| 1841 |
return bus_log_parse_error(r); |
| 1842 |
|
| 1843 |
if (properties) |
| 1844 |
r = show_image_properties(bus, path, &new_line); |
| 1845 |
else |
| 1846 |
r = show_image_info(bus, path, &new_line); |
| 1847 |
} |
| 1848 |
|
| 1849 |
return r; |
| 1850 |
} |
| 1851 |
|
| 1852 |
static int normalize_nspawn_filename(const char *name, char **ret_file) { |
| 1853 |
_cleanup_free_ char *file = NULL; |
| 1854 |
|
| 1855 |
assert(name); |
| 1856 |
assert(ret_file); |
| 1857 |
|
| 1858 |
if (!endswith(name, ".nspawn")) |
| 1859 |
file = strjoin(name, ".nspawn"); |
| 1860 |
else |
| 1861 |
file = strdup(name); |
| 1862 |
if (!file) |
| 1863 |
return log_oom(); |
| 1864 |
|
| 1865 |
if (!filename_is_valid(file)) |
| 1866 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid settings file name '%s'.", file); |
| 1867 |
|
| 1868 |
*ret_file = TAKE_PTR(file); |
| 1869 |
return 0; |
| 1870 |
} |
| 1871 |
|
| 1872 |
static int get_settings_path(const char *name, char **ret_path) { |
| 1873 |
assert(name); |
| 1874 |
assert(ret_path); |
| 1875 |
|
| 1876 |
FOREACH_STRING(i, "/etc/systemd/nspawn", "/run/systemd/nspawn", "/var/lib/machines") { |
| 1877 |
_cleanup_free_ char *path = NULL; |
| 1878 |
|
| 1879 |
path = path_join(i, name); |
| 1880 |
if (!path) |
| 1881 |
return -ENOMEM; |
| 1882 |
|
| 1883 |
if (access(path, F_OK) >= 0) { |
| 1884 |
*ret_path = TAKE_PTR(path); |
| 1885 |
return 0; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
return -ENOENT; |
| 1890 |
} |
| 1891 |
|
| 1892 |
VERB(verb_edit_settings, "edit", "NAME|FILE…", 2, VERB_ANY, 0, |
| 1893 |
"Edit settings of one or more VMs/containers"); |
| 1894 |
static int verb_edit_settings(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1895 |
_cleanup_(edit_file_context_done) EditFileContext context = {}; |
| 1896 |
int r; |
| 1897 |
|
| 1898 |
if (!on_tty()) |
| 1899 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit machine settings if not on a tty."); |
| 1900 |
|
| 1901 |
if (arg_transport != BUS_TRANSPORT_LOCAL) |
| 1902 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 1903 |
"Edit is only supported on the host machine."); |
| 1904 |
|
| 1905 |
if (arg_runner == RUNNER_VMSPAWN) |
| 1906 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Edit is only supported for --runner=nspawn"); |
| 1907 |
|
| 1908 |
r = mac_init(); |
| 1909 |
if (r < 0) |
| 1910 |
return r; |
| 1911 |
|
| 1912 |
STRV_FOREACH(name, strv_skip(argv, 1)) { |
| 1913 |
_cleanup_free_ char *file = NULL, *path = NULL; |
| 1914 |
|
| 1915 |
if (path_is_absolute(*name)) { |
| 1916 |
if (!path_is_safe(*name)) |
| 1917 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1918 |
"Invalid settings file path '%s'.", |
| 1919 |
*name); |
| 1920 |
|
| 1921 |
r = edit_files_add(&context, *name, NULL, NULL); |
| 1922 |
if (r < 0) |
| 1923 |
return r; |
| 1924 |
continue; |
| 1925 |
} |
| 1926 |
|
| 1927 |
r = normalize_nspawn_filename(*name, &file); |
| 1928 |
if (r < 0) |
| 1929 |
return r; |
| 1930 |
|
| 1931 |
r = get_settings_path(file, &path); |
| 1932 |
if (r == -ENOENT) { |
| 1933 |
log_debug("No existing settings file for machine '%s' found, creating a new file.", *name); |
| 1934 |
|
| 1935 |
path = path_join("/etc/systemd/nspawn", file); |
| 1936 |
if (!path) |
| 1937 |
return log_oom(); |
| 1938 |
|
| 1939 |
r = edit_files_add(&context, path, NULL, NULL); |
| 1940 |
if (r < 0) |
| 1941 |
return r; |
| 1942 |
continue; |
| 1943 |
} |
| 1944 |
if (r < 0) |
| 1945 |
return log_error_errno(r, "Failed to get the path of the settings file: %m"); |
| 1946 |
|
| 1947 |
if (path_startswith(path, "/var/lib/machines")) { |
| 1948 |
_cleanup_free_ char *new_path = NULL; |
| 1949 |
|
| 1950 |
new_path = path_join("/etc/systemd/nspawn", file); |
| 1951 |
if (!new_path) |
| 1952 |
return log_oom(); |
| 1953 |
|
| 1954 |
r = edit_files_add(&context, new_path, path, NULL); |
| 1955 |
} else |
| 1956 |
r = edit_files_add(&context, path, NULL, NULL); |
| 1957 |
if (r < 0) |
| 1958 |
return r; |
| 1959 |
} |
| 1960 |
|
| 1961 |
return do_edit_files_and_install(&context); |
| 1962 |
} |
| 1963 |
|
| 1964 |
VERB(verb_cat_settings, "cat", "NAME|FILE…", 2, VERB_ANY, 0, |
| 1965 |
"Show settings of one or more VMs/containers"); |
| 1966 |
static int verb_cat_settings(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 1967 |
int r = 0; |
| 1968 |
|
| 1969 |
if (arg_transport != BUS_TRANSPORT_LOCAL) |
| 1970 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 1971 |
"Cat is only supported on the host machine."); |
| 1972 |
|
| 1973 |
if (arg_runner == RUNNER_VMSPAWN) |
| 1974 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Cat is only supported for --runner=nspawn"); |
| 1975 |
|
| 1976 |
pager_open(arg_pager_flags); |
| 1977 |
|
| 1978 |
STRV_FOREACH(name, strv_skip(argv, 1)) { |
| 1979 |
_cleanup_free_ char *file = NULL, *path = NULL; |
| 1980 |
int q; |
| 1981 |
|
| 1982 |
if (path_is_absolute(*name)) { |
| 1983 |
if (!path_is_safe(*name)) |
| 1984 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 1985 |
"Invalid settings file path '%s'.", |
| 1986 |
*name); |
| 1987 |
|
| 1988 |
q = cat_files(*name, /* dropins= */ NULL, /* flags= */ CAT_FORMAT_HAS_SECTIONS); |
| 1989 |
if (q < 0) |
| 1990 |
return r < 0 ? r : q; |
| 1991 |
continue; |
| 1992 |
} |
| 1993 |
|
| 1994 |
q = normalize_nspawn_filename(*name, &file); |
| 1995 |
if (q < 0) |
| 1996 |
return r < 0 ? r : q; |
| 1997 |
|
| 1998 |
q = get_settings_path(file, &path); |
| 1999 |
if (q == -ENOENT) { |
| 2000 |
log_error_errno(q, "No settings file found for machine '%s'.", *name); |
| 2001 |
r = r < 0 ? r : q; |
| 2002 |
continue; |
| 2003 |
} |
| 2004 |
if (q < 0) { |
| 2005 |
log_error_errno(q, "Failed to get the path of the settings file: %m"); |
| 2006 |
return r < 0 ? r : q; |
| 2007 |
} |
| 2008 |
|
| 2009 |
q = cat_files(path, /* dropins= */ NULL, /* flags= */ CAT_FORMAT_HAS_SECTIONS); |
| 2010 |
if (q < 0) |
| 2011 |
return r < 0 ? r : q; |
| 2012 |
} |
| 2013 |
|
| 2014 |
return r; |
| 2015 |
} |
| 2016 |
|
| 2017 |
VERB(verb_clone_image, "clone", "NAME NAME", 3, 3, 0, |
| 2018 |
"Clone an image"); |
| 2019 |
static int verb_clone_image(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 2020 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2021 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 2022 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 2023 |
int r; |
| 2024 |
|
| 2025 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 2026 |
|
| 2027 |
r = bus_message_new_method_call(bus, &m, bus_machine_mgr, "CloneImage"); |
| 2028 |
if (r < 0) |
| 2029 |
return bus_log_create_error(r); |
| 2030 |
|
| 2031 |
r = sd_bus_message_append(m, "ssb", argv[1], argv[2], arg_read_only); |
| 2032 |
if (r < 0) |
| 2033 |
return bus_log_create_error(r); |
| 2034 |
|
| 2035 |
/* This is a slow operation, hence turn off any method call timeouts */ |
| 2036 |
r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL); |
| 2037 |
if (r < 0) |
| 2038 |
return log_error_errno(r, "Could not clone image: %s", bus_error_message(&error, r)); |
| 2039 |
|
| 2040 |
return 0; |
| 2041 |
} |
| 2042 |
|
| 2043 |
VERB(verb_rename_image, "rename", "NAME NAME", 3, 3, 0, |
| 2044 |
"Rename an image"); |
| 2045 |
static int verb_rename_image(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 2046 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2047 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 2048 |
int r; |
| 2049 |
|
| 2050 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 2051 |
|
| 2052 |
r = bus_call_method( |
| 2053 |
bus, |
| 2054 |
bus_machine_mgr, |
| 2055 |
"RenameImage", |
| 2056 |
&error, |
| 2057 |
NULL, |
| 2058 |
"ss", argv[1], argv[2]); |
| 2059 |
if (r < 0) |
| 2060 |
return log_error_errno(r, "Could not rename image: %s", bus_error_message(&error, r)); |
| 2061 |
|
| 2062 |
return 0; |
| 2063 |
} |
| 2064 |
|
| 2065 |
VERB(verb_read_only_image, "read-only", "NAME [BOOL]", 2, 3, 0, |
| 2066 |
"Mark or unmark image read-only"); |
| 2067 |
static int verb_read_only_image(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 2068 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2069 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 2070 |
int b = true, r; |
| 2071 |
|
| 2072 |
if (argc > 2) { |
| 2073 |
b = parse_boolean(argv[2]); |
| 2074 |
if (b < 0) |
| 2075 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 2076 |
"Failed to parse boolean argument: %s", |
| 2077 |
argv[2]); |
| 2078 |
} |
| 2079 |
|
| 2080 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 2081 |
|
| 2082 |
r = bus_call_method(bus, bus_machine_mgr, "MarkImageReadOnly", &error, NULL, "sb", argv[1], b); |
| 2083 |
if (r < 0) |
| 2084 |
return log_error_errno(r, "Could not mark image read-only: %s", bus_error_message(&error, r)); |
| 2085 |
|
| 2086 |
return 0; |
| 2087 |
} |
| 2088 |
|
| 2089 |
VERB(verb_remove_image, "remove", "NAME…", 2, VERB_ANY, 0, |
| 2090 |
"Remove an image"); |
| 2091 |
static int verb_remove_image(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 2092 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 2093 |
int r; |
| 2094 |
|
| 2095 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 2096 |
|
| 2097 |
for (int i = 1; i < argc; i++) { |
| 2098 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2099 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 2100 |
|
| 2101 |
r = bus_message_new_method_call(bus, &m, bus_machine_mgr, "RemoveImage"); |
| 2102 |
if (r < 0) |
| 2103 |
return bus_log_create_error(r); |
| 2104 |
|
| 2105 |
r = sd_bus_message_append(m, "s", argv[i]); |
| 2106 |
if (r < 0) |
| 2107 |
return bus_log_create_error(r); |
| 2108 |
|
| 2109 |
/* This is a slow operation, hence turn off any method call timeouts */ |
| 2110 |
r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL); |
| 2111 |
if (r < 0) |
| 2112 |
return log_error_errno(r, "Could not remove image: %s", bus_error_message(&error, r)); |
| 2113 |
} |
| 2114 |
|
| 2115 |
return 0; |
| 2116 |
} |
| 2117 |
|
| 2118 |
VERB(verb_set_limit, "set-limit", "[NAME] BYTES", 2, 3, 0, |
| 2119 |
"Set image or pool size limit (disk quota)"); |
| 2120 |
static int verb_set_limit(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 2121 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2122 |
sd_bus *bus = userdata; |
| 2123 |
uint64_t limit; |
| 2124 |
int r; |
| 2125 |
|
| 2126 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 2127 |
|
| 2128 |
if (STR_IN_SET(argv[argc-1], "-", "none", "infinity")) |
| 2129 |
limit = UINT64_MAX; |
| 2130 |
else { |
| 2131 |
r = parse_size(argv[argc-1], 1024, &limit); |
| 2132 |
if (r < 0) |
| 2133 |
return log_error_errno(r, "Failed to parse size: %s", argv[argc-1]); |
| 2134 |
} |
| 2135 |
|
| 2136 |
if (argc > 2) |
| 2137 |
/* With two arguments changes the quota limit of the |
| 2138 |
* specified image */ |
| 2139 |
r = bus_call_method(bus, bus_machine_mgr, "SetImageLimit", &error, NULL, "st", argv[1], limit); |
| 2140 |
else |
| 2141 |
/* With one argument changes the pool quota limit */ |
| 2142 |
r = bus_call_method(bus, bus_machine_mgr, "SetPoolLimit", &error, NULL, "t", limit); |
| 2143 |
|
| 2144 |
if (r < 0) |
| 2145 |
return log_error_errno(r, "Could not set limit: %s", bus_error_message(&error, r)); |
| 2146 |
|
| 2147 |
return 0; |
| 2148 |
} |
| 2149 |
|
| 2150 |
VERB(verb_clean_images, "clean", /* argspec= */ NULL, VERB_ANY, 1, 0, |
| 2151 |
"Remove hidden (or all) images"); |
| 2152 |
static int verb_clean_images(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 2153 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
| 2154 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2155 |
uint64_t usage, total = 0; |
| 2156 |
sd_bus *bus = userdata; |
| 2157 |
const char *name; |
| 2158 |
unsigned c = 0; |
| 2159 |
int r; |
| 2160 |
|
| 2161 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 2162 |
|
| 2163 |
r = bus_message_new_method_call(bus, &m, bus_machine_mgr, "CleanPool"); |
| 2164 |
if (r < 0) |
| 2165 |
return bus_log_create_error(r); |
| 2166 |
|
| 2167 |
r = sd_bus_message_append(m, "s", arg_all ? "all" : "hidden"); |
| 2168 |
if (r < 0) |
| 2169 |
return bus_log_create_error(r); |
| 2170 |
|
| 2171 |
/* This is a slow operation, hence permit a longer time for completion. */ |
| 2172 |
r = sd_bus_call(bus, m, USEC_INFINITY, &error, &reply); |
| 2173 |
if (r < 0) |
| 2174 |
return log_error_errno(r, "Could not clean pool: %s", bus_error_message(&error, r)); |
| 2175 |
|
| 2176 |
r = sd_bus_message_enter_container(reply, 'a', "(st)"); |
| 2177 |
if (r < 0) |
| 2178 |
return bus_log_parse_error(r); |
| 2179 |
|
| 2180 |
while ((r = sd_bus_message_read(reply, "(st)", &name, &usage)) > 0) { |
| 2181 |
if (usage == UINT64_MAX) { |
| 2182 |
log_info("Removed image '%s'", name); |
| 2183 |
total = UINT64_MAX; |
| 2184 |
} else { |
| 2185 |
log_info("Removed image '%s'. Freed exclusive disk space: %s", |
| 2186 |
name, FORMAT_BYTES(usage)); |
| 2187 |
if (total != UINT64_MAX) |
| 2188 |
total += usage; |
| 2189 |
} |
| 2190 |
c++; |
| 2191 |
} |
| 2192 |
|
| 2193 |
r = sd_bus_message_exit_container(reply); |
| 2194 |
if (r < 0) |
| 2195 |
return bus_log_parse_error(r); |
| 2196 |
|
| 2197 |
if (total == UINT64_MAX) |
| 2198 |
log_info("Removed %u images in total.", c); |
| 2199 |
else |
| 2200 |
log_info("Removed %u images in total. Total freed exclusive disk space: %s.", |
| 2201 |
c, FORMAT_BYTES(total)); |
| 2202 |
|
| 2203 |
return 0; |
| 2204 |
} |
| 2205 |
|
| 2206 |
VERB(verb_bind_volume, "bind-volume", "MACHINE PROVIDER:VOLUME[:…]", 3, 3, 0, |
| 2207 |
"Attach a volume to a running machine"); |
| 2208 |
static int verb_bind_volume(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 2209 |
int r; |
| 2210 |
|
| 2211 |
if (arg_transport != BUS_TRANSPORT_LOCAL) |
| 2212 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 2213 |
"bind-volume is only supported on the local transport."); |
| 2214 |
|
| 2215 |
_cleanup_(bind_volume_freep) BindVolume *bv = NULL; |
| 2216 |
r = bind_volume_parse(argv[2], &bv); |
| 2217 |
if (r < 0) |
| 2218 |
return log_error_errno(r, "Failed to parse bind-volume argument '%s': %m", argv[2]); |
| 2219 |
|
| 2220 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 2221 |
|
| 2222 |
/* Locate and connect to the target machine before acquiring storage, so a missing |
| 2223 |
* machine doesn't trigger 'create=new' side effects on the StorageProvider. */ |
| 2224 |
_cleanup_free_ char *address = NULL; |
| 2225 |
r = machine_get_control_address(argv[1], &address); |
| 2226 |
if (r == -EOPNOTSUPP) |
| 2227 |
return log_error_errno(r, "Machine '%s' does not expose a varlink control socket.", argv[1]); |
| 2228 |
if (r < 0) |
| 2229 |
return r; |
| 2230 |
|
| 2231 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
| 2232 |
r = sd_varlink_connect_address(&vl, address); |
| 2233 |
if (r < 0) |
| 2234 |
return log_error_errno(r, "Failed to connect to machine control socket %s: %m", address); |
| 2235 |
|
| 2236 |
r = sd_varlink_set_allow_fd_passing_output(vl, true); |
| 2237 |
if (r < 0) |
| 2238 |
return log_error_errno(r, "Failed to enable fd passing on varlink connection: %m"); |
| 2239 |
|
| 2240 |
_cleanup_(storage_acquire_reply_done) StorageAcquireReply reply = STORAGE_ACQUIRE_REPLY_INIT; |
| 2241 |
_cleanup_free_ char *acquire_error_id = NULL; |
| 2242 |
r = storage_acquire_volume(arg_runtime_scope, bv, arg_ask_password, &acquire_error_id, &reply); |
| 2243 |
if (r < 0) { |
| 2244 |
if (acquire_error_id) |
| 2245 |
return log_error_errno(r, "Failed to acquire storage volume '%s:%s' from provider: %s", |
| 2246 |
bv->provider, bv->volume, acquire_error_id); |
| 2247 |
return log_error_errno(r, "Failed to acquire storage volume '%s:%s' from provider: %m", |
| 2248 |
bv->provider, bv->volume); |
| 2249 |
} |
| 2250 |
|
| 2251 |
int fd_index = sd_varlink_push_fd(vl, reply.fd); |
| 2252 |
if (fd_index < 0) |
| 2253 |
return log_error_errno(fd_index, "Failed to push storage fd onto varlink connection: %m"); |
| 2254 |
TAKE_FD(reply.fd); |
| 2255 |
|
| 2256 |
_cleanup_free_ char *name = strjoin(bv->provider, ":", bv->volume); |
| 2257 |
if (!name) |
| 2258 |
return log_oom(); |
| 2259 |
|
| 2260 |
sd_json_variant *vl_reply = NULL; |
| 2261 |
const char *error_id = NULL; |
| 2262 |
r = sd_varlink_callbo( |
| 2263 |
vl, |
| 2264 |
"io.systemd.MachineInstance.AddStorage", |
| 2265 |
&vl_reply, &error_id, |
| 2266 |
SD_JSON_BUILD_PAIR_INTEGER("fileDescriptorIndex", fd_index), |
| 2267 |
SD_JSON_BUILD_PAIR_STRING("name", name), |
| 2268 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("config", bv->config)); |
| 2269 |
if (r < 0) |
| 2270 |
return log_error_errno(r, "Failed to call io.systemd.MachineInstance.AddStorage: %m"); |
| 2271 |
if (error_id) |
| 2272 |
return log_error_errno(sd_varlink_error_to_errno(error_id, vl_reply), |
| 2273 |
"AddStorage failed for '%s': %s", name, error_id); |
| 2274 |
|
| 2275 |
return 0; |
| 2276 |
} |
| 2277 |
|
| 2278 |
VERB(verb_unbind_volume, "unbind-volume", "MACHINE PROVIDER:VOLUME", 3, 3, 0, |
| 2279 |
"Detach a volume from a running machine"); |
| 2280 |
static int verb_unbind_volume(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 2281 |
int r; |
| 2282 |
|
| 2283 |
if (arg_transport != BUS_TRANSPORT_LOCAL) |
| 2284 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 2285 |
"unbind-volume is only supported on the local transport."); |
| 2286 |
|
| 2287 |
r = machine_storage_name_split(argv[2], /* ret_provider= */ NULL, /* ret_volume= */ NULL); |
| 2288 |
if (r == -ENOMEM) |
| 2289 |
return log_oom(); |
| 2290 |
if (r < 0) |
| 2291 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 2292 |
"Invalid unbind-volume name '%s', expected '<provider>:<volume>'.", argv[2]); |
| 2293 |
|
| 2294 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 2295 |
|
| 2296 |
_cleanup_free_ char *address = NULL; |
| 2297 |
r = machine_get_control_address(argv[1], &address); |
| 2298 |
if (r == -EOPNOTSUPP) |
| 2299 |
return log_error_errno(r, "Machine '%s' does not expose a varlink control socket.", argv[1]); |
| 2300 |
if (r < 0) |
| 2301 |
return r; |
| 2302 |
|
| 2303 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
| 2304 |
r = sd_varlink_connect_address(&vl, address); |
| 2305 |
if (r < 0) |
| 2306 |
return log_error_errno(r, "Failed to connect to machine control socket %s: %m", address); |
| 2307 |
|
| 2308 |
sd_json_variant *reply = NULL; |
| 2309 |
const char *error_id = NULL; |
| 2310 |
r = sd_varlink_callbo( |
| 2311 |
vl, |
| 2312 |
"io.systemd.MachineInstance.RemoveStorage", |
| 2313 |
&reply, &error_id, |
| 2314 |
SD_JSON_BUILD_PAIR_STRING("name", argv[2])); |
| 2315 |
if (r < 0) |
| 2316 |
return log_error_errno(r, "Failed to call io.systemd.MachineInstance.RemoveStorage: %m"); |
| 2317 |
if (error_id) |
| 2318 |
return log_error_errno(sd_varlink_error_to_errno(error_id, reply), |
| 2319 |
"RemoveStorage failed for '%s': %s", argv[2], error_id); |
| 2320 |
|
| 2321 |
return 0; |
| 2322 |
} |
| 2323 |
|
| 2324 |
static int chainload_importctl(char **args) { |
| 2325 |
int r; |
| 2326 |
|
| 2327 |
if (!arg_quiet) |
| 2328 |
log_notice("The 'machinectl %1$s' command has been replaced by 'importctl -m %1$s'. " |
| 2329 |
"Redirecting invocation.", args[0]); |
| 2330 |
|
| 2331 |
_cleanup_strv_free_ char **c = |
| 2332 |
strv_new("importctl", "--class=machine"); |
| 2333 |
if (!c) |
| 2334 |
return log_oom(); |
| 2335 |
|
| 2336 |
if (FLAGS_SET(arg_pager_flags, PAGER_DISABLE)) |
| 2337 |
if (strv_extend(&c, "--no-pager") < 0) |
| 2338 |
return log_oom(); |
| 2339 |
if (!arg_legend) |
| 2340 |
if (strv_extend(&c, "--no-legend") < 0) |
| 2341 |
return log_oom(); |
| 2342 |
if (arg_read_only) |
| 2343 |
if (strv_extend(&c, "--read-only") < 0) |
| 2344 |
return log_oom(); |
| 2345 |
if (arg_force) |
| 2346 |
if (strv_extend(&c, "--force") < 0) |
| 2347 |
return log_oom(); |
| 2348 |
if (arg_quiet) |
| 2349 |
if (strv_extend(&c, "--quiet") < 0) |
| 2350 |
return log_oom(); |
| 2351 |
if (!arg_ask_password) |
| 2352 |
if (strv_extend(&c, "--no-ask-password") < 0) |
| 2353 |
return log_oom(); |
| 2354 |
if (strv_extend_many(&c, "--verify", import_verify_to_string(arg_verify)) < 0) |
| 2355 |
return log_oom(); |
| 2356 |
if (arg_format) |
| 2357 |
if (strv_extend_many(&c, "--format", arg_format) < 0) |
| 2358 |
return log_oom(); |
| 2359 |
|
| 2360 |
if (strv_extend_strv(&c, args, /* filter_duplicates= */ false) < 0) |
| 2361 |
return log_oom(); |
| 2362 |
|
| 2363 |
if (DEBUG_LOGGING) { |
| 2364 |
_cleanup_free_ char *joined = strv_join(c, " "); |
| 2365 |
log_debug("Chainloading: %s", joined); |
| 2366 |
} |
| 2367 |
|
| 2368 |
r = invoke_callout_binary(BINDIR "/importctl", c); |
| 2369 |
return log_error_errno(r, "Failed to invoke 'importctl': %m"); |
| 2370 |
} |
| 2371 |
|
| 2372 |
static int help(void) { |
| 2373 |
static const char* const vgroups[] = { |
| 2374 |
"Machine Commands", |
| 2375 |
"Image Commands", |
| 2376 |
}; |
| 2377 |
|
| 2378 |
Table *vtables[ELEMENTSOF(vgroups)] = {}; |
| 2379 |
CLEANUP_ELEMENTS(vtables, table_unref_array_clear); |
| 2380 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 2381 |
int r; |
| 2382 |
|
| 2383 |
for (size_t i = 0; i < ELEMENTSOF(vgroups); i++) { |
| 2384 |
r = verbs_get_help_table_group(vgroups[i], &vtables[i]); |
| 2385 |
if (r < 0) |
| 2386 |
return r; |
| 2387 |
} |
| 2388 |
|
| 2389 |
r = option_parser_get_help_table(&options); |
| 2390 |
if (r < 0) |
| 2391 |
return r; |
| 2392 |
|
| 2393 |
assert_cc(ELEMENTSOF(vtables) == 2); |
| 2394 |
(void) table_sync_column_widths(0, options, vtables[0], vtables[1]); |
| 2395 |
|
| 2396 |
pager_open(arg_pager_flags); |
| 2397 |
|
| 2398 |
help_cmdline("[OPTIONS…] COMMAND …"); |
| 2399 |
|
| 2400 |
printf("\n%1$s%2$sSend control commands to or query the virtual machine and container%3$s\n" |
| 2401 |
"%1$s%2$sregistration manager.%3$s\n", |
| 2402 |
ansi_highlight(), |
| 2403 |
ansi_add_italics(), |
| 2404 |
ansi_normal()); |
| 2405 |
|
| 2406 |
for (size_t i = 0; i < ELEMENTSOF(vgroups); i++) { |
| 2407 |
help_section(vgroups[i]); |
| 2408 |
r = table_print_or_warn(vtables[i]); |
| 2409 |
if (r < 0) |
| 2410 |
return r; |
| 2411 |
} |
| 2412 |
|
| 2413 |
help_section("Options"); |
| 2414 |
r = table_print_or_warn(options); |
| 2415 |
if (r < 0) |
| 2416 |
return r; |
| 2417 |
|
| 2418 |
help_man_page_reference("machinectl", "1"); |
| 2419 |
|
| 2420 |
return 0; |
| 2421 |
} |
| 2422 |
|
| 2423 |
VERB_COMMON_HELP_HIDDEN(help); |
| 2424 |
|
| 2425 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 2426 |
assert(argc >= 0); |
| 2427 |
assert(argv); |
| 2428 |
assert(ret_args); |
| 2429 |
|
| 2430 |
/* For "shell" we don't want option reordering; options specified after the command should be passed |
| 2431 |
* to the program to execute, and not processed by us. For other verbs, we consume all options as |
| 2432 |
* usual. To make this work, start with OPTION_PARSER_RETURN_POSITIONAL_ARGS and switch to either |
| 2433 |
* OPTION_PARSER_STOP_AT_FIRST_NONOPTION or OPTION_PARSER_NORMAL after we've seen the verb and one |
| 2434 |
* more argument after that. */ |
| 2435 |
OptionParser opts = { argc, argv, OPTION_PARSER_RETURN_POSITIONAL_ARGS }; |
| 2436 |
_cleanup_strv_free_ char **args = NULL; |
| 2437 |
int r; |
| 2438 |
|
| 2439 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 2440 |
switch (c) { |
| 2441 |
|
| 2442 |
OPTION_POSITIONAL: |
| 2443 |
assert(opts.mode == OPTION_PARSER_RETURN_POSITIONAL_ARGS); |
| 2444 |
|
| 2445 |
if (!args && !streq(opts.arg, "shell")) |
| 2446 |
opts.mode = OPTION_PARSER_NORMAL; |
| 2447 |
else if (args) |
| 2448 |
opts.mode = OPTION_PARSER_STOP_AT_FIRST_NONOPTION; |
| 2449 |
|
| 2450 |
r = strv_extend(&args, opts.arg); |
| 2451 |
if (r < 0) |
| 2452 |
return log_oom(); |
| 2453 |
break; |
| 2454 |
|
| 2455 |
OPTION_COMMON_HELP: |
| 2456 |
return help(); |
| 2457 |
|
| 2458 |
OPTION_COMMON_VERSION: |
| 2459 |
return version(); |
| 2460 |
|
| 2461 |
OPTION_COMMON_HOST: |
| 2462 |
arg_transport = BUS_TRANSPORT_REMOTE; |
| 2463 |
arg_host = opts.arg; |
| 2464 |
break; |
| 2465 |
|
| 2466 |
OPTION_COMMON_MACHINE: |
| 2467 |
arg_transport = BUS_TRANSPORT_MACHINE; |
| 2468 |
arg_host = opts.arg; |
| 2469 |
break; |
| 2470 |
|
| 2471 |
OPTION_LONG("system", NULL, "Connect to system machine manager"): |
| 2472 |
arg_runtime_scope = RUNTIME_SCOPE_SYSTEM; |
| 2473 |
break; |
| 2474 |
|
| 2475 |
OPTION_LONG("user", NULL, "Connect to user machine manager"): |
| 2476 |
arg_runtime_scope = RUNTIME_SCOPE_USER; |
| 2477 |
break; |
| 2478 |
|
| 2479 |
OPTION_LONG("value", NULL, "When showing properties, only print the value"): |
| 2480 |
SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true); |
| 2481 |
break; |
| 2482 |
|
| 2483 |
OPTION('p', "property", "NAME", "Show only properties by this name"): {} |
| 2484 |
OPTION_SHORT('P', "NAME", "Equivalent to --value --property=NAME"): |
| 2485 |
r = strv_extend(&arg_property, opts.arg); |
| 2486 |
if (r < 0) |
| 2487 |
return log_oom(); |
| 2488 |
|
| 2489 |
/* If the user asked for a particular property, show it to them, even if empty. */ |
| 2490 |
SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true); |
| 2491 |
|
| 2492 |
if (opts.opt->short_code == 'P') |
| 2493 |
SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true); |
| 2494 |
|
| 2495 |
break; |
| 2496 |
|
| 2497 |
OPTION('a', "all", NULL, "Show all properties, including empty ones"): |
| 2498 |
SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true); |
| 2499 |
arg_all = true; |
| 2500 |
break; |
| 2501 |
|
| 2502 |
OPTION('l', "full", NULL, "Do not ellipsize output"): |
| 2503 |
arg_full = true; |
| 2504 |
break; |
| 2505 |
|
| 2506 |
OPTION_LONG("kill-whom", "WHOM", "Whom to send signal to"): |
| 2507 |
arg_kill_whom = opts.arg; |
| 2508 |
break; |
| 2509 |
|
| 2510 |
OPTION('s', "signal", "SIGNAL", "Which signal to send"): |
| 2511 |
r = parse_signal_argument(opts.arg, &arg_signal); |
| 2512 |
if (r <= 0) |
| 2513 |
return r; |
| 2514 |
break; |
| 2515 |
|
| 2516 |
OPTION_LONG("uid", "USER", "Specify user ID to invoke shell as"): |
| 2517 |
arg_uid = opts.arg; |
| 2518 |
break; |
| 2519 |
|
| 2520 |
OPTION('E', "setenv", "VAR[=VALUE]", "Add an environment variable for shell"): |
| 2521 |
r = strv_env_replace_strdup_passthrough(&arg_setenv, opts.arg); |
| 2522 |
if (r < 0) |
| 2523 |
return log_error_errno(r, "Cannot assign environment variable %s: %m", opts.arg); |
| 2524 |
break; |
| 2525 |
|
| 2526 |
OPTION_LONG("read-only", NULL, "Create read-only bind mount or clone"): |
| 2527 |
arg_read_only = true; |
| 2528 |
break; |
| 2529 |
|
| 2530 |
OPTION_LONG("mkdir", NULL, "Create directory before bind mounting, if missing"): |
| 2531 |
arg_mkdir = true; |
| 2532 |
break; |
| 2533 |
|
| 2534 |
OPTION('n', "lines", "INTEGER", "Number of journal entries to show"): |
| 2535 |
if (safe_atou(opts.arg, &arg_lines) < 0) |
| 2536 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 2537 |
"Failed to parse lines '%s'", opts.arg); |
| 2538 |
break; |
| 2539 |
|
| 2540 |
OPTION_LONG("max-addresses", "INTEGER", |
| 2541 |
"Number of internet addresses to show at most"): |
| 2542 |
if (streq(opts.arg, "all")) |
| 2543 |
arg_max_addresses = UINT_MAX; |
| 2544 |
else if (safe_atou(opts.arg, &arg_max_addresses) < 0) |
| 2545 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 2546 |
"Invalid number of addresses: %s", opts.arg); |
| 2547 |
break; |
| 2548 |
|
| 2549 |
OPTION('o', "output", "STRING", |
| 2550 |
"Change journal output mode (short, short-precise, short-iso, " |
| 2551 |
"short-iso-precise, short-full, short-monotonic, short-unix, short-delta, " |
| 2552 |
"json, json-pretty, json-sse, json-seq, cat, verbose, export, with-unit)"): |
| 2553 |
if (streq(opts.arg, "help")) |
| 2554 |
return DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX); |
| 2555 |
|
| 2556 |
r = output_mode_from_string(opts.arg); |
| 2557 |
if (r < 0) |
| 2558 |
return log_error_errno(r, "Unknown output '%s'.", opts.arg); |
| 2559 |
arg_output = r; |
| 2560 |
|
| 2561 |
if (OUTPUT_MODE_IS_JSON(arg_output)) |
| 2562 |
arg_legend = false; |
| 2563 |
break; |
| 2564 |
|
| 2565 |
OPTION_LONG("force", NULL, "Replace target file when copying, if necessary"): |
| 2566 |
arg_force = true; |
| 2567 |
break; |
| 2568 |
|
| 2569 |
OPTION_LONG("now", NULL, |
| 2570 |
"Start or power off container after enabling or disabling it"): |
| 2571 |
arg_now = true; |
| 2572 |
break; |
| 2573 |
|
| 2574 |
OPTION_LONG("runner", "RUNNER", |
| 2575 |
"Select between nspawn and vmspawn as the runner"): |
| 2576 |
r = machine_runner_from_string(opts.arg); |
| 2577 |
if (r < 0) |
| 2578 |
return log_error_errno(r, "Failed to parse --runner= setting: %s", opts.arg); |
| 2579 |
|
| 2580 |
arg_runner = r; |
| 2581 |
break; |
| 2582 |
|
| 2583 |
OPTION_SHORT('V', NULL, "Short for --runner=vmspawn"): |
| 2584 |
arg_runner = RUNNER_VMSPAWN; |
| 2585 |
break; |
| 2586 |
|
| 2587 |
/* Hidden options below */ |
| 2588 |
|
| 2589 |
OPTION_LONG("verify", "MODE", /* help= */ NULL): |
| 2590 |
if (streq(opts.arg, "help")) |
| 2591 |
return DUMP_STRING_TABLE(import_verify, ImportVerify, _IMPORT_VERIFY_MAX); |
| 2592 |
|
| 2593 |
r = import_verify_from_string(opts.arg); |
| 2594 |
if (r < 0) |
| 2595 |
return log_error_errno(r, "Failed to parse --verify= setting: %s", opts.arg); |
| 2596 |
arg_verify = r; |
| 2597 |
break; |
| 2598 |
|
| 2599 |
OPTION_LONG("format", "FORMAT", /* help= */ NULL): |
| 2600 |
if (!STR_IN_SET(opts.arg, "uncompressed", "xz", "gzip", "bzip2", "zstd")) |
| 2601 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 2602 |
"Unknown format: %s", opts.arg); |
| 2603 |
|
| 2604 |
arg_format = opts.arg; |
| 2605 |
break; |
| 2606 |
|
| 2607 |
OPTION_COMMON_NO_PAGER: |
| 2608 |
arg_pager_flags |= PAGER_DISABLE; |
| 2609 |
break; |
| 2610 |
|
| 2611 |
OPTION_COMMON_NO_LEGEND: |
| 2612 |
arg_legend = false; |
| 2613 |
break; |
| 2614 |
|
| 2615 |
OPTION_COMMON_NO_ASK_PASSWORD: |
| 2616 |
arg_ask_password = false; |
| 2617 |
break; |
| 2618 |
|
| 2619 |
OPTION('q', "quiet", NULL, "Suppress output"): |
| 2620 |
arg_quiet = true; |
| 2621 |
break; |
| 2622 |
} |
| 2623 |
|
| 2624 |
/* We gathered some positional args in 'args' ourselves. Append the remaining ones. */ |
| 2625 |
if (strv_extend_strv(&args, option_parser_get_args(&opts), /* filter_duplicates= */ false) < 0) |
| 2626 |
return log_oom(); |
| 2627 |
|
| 2628 |
*ret_args = TAKE_PTR(args); |
| 2629 |
return 1; |
| 2630 |
} |
| 2631 |
|
| 2632 |
static int run(int argc, char *argv[]) { |
| 2633 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 2634 |
_cleanup_strv_free_ char **args = NULL; |
| 2635 |
int r; |
| 2636 |
|
| 2637 |
setlocale(LC_ALL, ""); |
| 2638 |
log_setup(); |
| 2639 |
|
| 2640 |
r = parse_argv(argc, argv, &args); |
| 2641 |
if (r <= 0) |
| 2642 |
return r; |
| 2643 |
|
| 2644 |
journal_browse_prepare(); |
| 2645 |
|
| 2646 |
if (args && STR_IN_SET(args[0], |
| 2647 |
"import-tar", "import-raw", "import-fs", |
| 2648 |
"export-tar", "export-raw", |
| 2649 |
"pull-tar", "pull-raw", |
| 2650 |
"list-transfers", "cancel-transfer")) |
| 2651 |
return chainload_importctl(args); |
| 2652 |
|
| 2653 |
r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, &bus); |
| 2654 |
if (r < 0) |
| 2655 |
return bus_log_connect_error(r, arg_transport, arg_runtime_scope); |
| 2656 |
|
| 2657 |
(void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password); |
| 2658 |
|
| 2659 |
return dispatch_verb(args, bus); |
| 2660 |
} |
| 2661 |
|
| 2662 |
DEFINE_MAIN_FUNCTION(run); |