machinectl.c revision 56159e0d918e9a9be07988133bb2847779325de0
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool arg_all = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool arg_full = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool arg_no_pager = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool arg_legend = true;
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersenstatic const char *arg_kill_who = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool arg_read_only = false;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic bool arg_mkdir = false;
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poetteringstatic void pager_open_if_enabled(void) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering /* Cache result before we open the pager */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int list_machines(int argc, char *argv[], void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *name, *class, *service, *object;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned k = 0;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering "org.freedesktop.machine1",
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering "org.freedesktop.machine1.Manager",
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering "ListMachines",
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering log_error("Could not get machines: %s", bus_error_message(&error, -r));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering printf("%-32s %-9s %-16s\n", "MACHINE", "CONTAINER", "SERVICE");
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssso)");
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering while ((r = sd_bus_message_read(reply, "(ssso)", &name, &class, &service, &object)) > 0) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering printf("%-32s %-9s %-16s\n", name, class, service);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering r = sd_bus_message_exit_container(reply);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringtypedef struct ImageInfo {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic int compare_image_info(const void *a, const void *b) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const ImageInfo *x = a, *y = b;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic int list_images(int argc, char *argv[], void *userdata) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen size_t max_name = strlen("NAME"), max_type = strlen("TYPE");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "org.freedesktop.machine1",
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "org.freedesktop.machine1.Manager",
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering log_error("Could not get images: %s", bus_error_message(&error, -r));
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbo)");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while ((r = sd_bus_message_read(reply, "(ssbo)", &name, &type, &read_only, &object)) > 0) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!GREEDY_REALLOC(images, n_allocated, n_images + 1))
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering r = sd_bus_message_exit_container(reply);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering qsort_safe(images, n_images, sizeof(ImageInfo), compare_image_info);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering printf("%-*s %-*s %-3s\n", (int) max_name, "NAME", (int) max_type, "TYPE", "RO");
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering for (j = 0; j < n_images; j++) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering printf("\n%zu images listed.\n", n_images);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int show_unit_cgroup(sd_bus *bus, const char *unit, pid_t leader) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (arg_transport == BUS_TRANSPORT_REMOTE)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering endswith(unit, ".scope") ? "org.freedesktop.systemd1.Scope" : "org.freedesktop.systemd1.Service",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering "ControlGroup",
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering log_error("Failed to query ControlGroup: %s", bus_error_message(&error, -r));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = sd_bus_message_read(reply, "s", &cgroup);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup, false) != 0 && leader <= 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, false, &leader, leader > 0, output_flags);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poetteringstatic int print_addresses(sd_bus *bus, const char *name, int ifi, const char *prefix, const char *prefix2) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering "org.freedesktop.machine1",
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering "org.freedesktop.machine1.Manager",
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering "GetMachineAddresses",
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = sd_bus_message_enter_container(reply, 'a', "(iay)");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const void *a;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering char buffer[MAX(INET6_ADDRSTRLEN, INET_ADDRSTRLEN)];
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = sd_bus_message_read(reply, "i", &family);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering r = sd_bus_message_read_array(reply, 'y', &a, &sz);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering fputs(inet_ntop(family, a, buffer, sizeof(buffer)), stdout);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering r = sd_bus_message_exit_container(reply);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringstatic int print_os_release(sd_bus *bus, const char *name, const char *prefix) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering "org.freedesktop.machine1",
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering "org.freedesktop.machine1.Manager",
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering "GetMachineOSRelease",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = sd_bus_message_enter_container(reply, 'a', "{ss}");
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering while ((r = sd_bus_message_read(reply, "{ss}", &k, &v)) > 0) {
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering r = sd_bus_message_exit_container(reply);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!sd_id128_equal(i->id, SD_ID128_NULL))
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering printf("(" SD_ID128_FORMAT_STR ")\n", SD_ID128_FORMAT_VAL(i->id));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering s1 = format_timestamp_relative(since1, sizeof(since1), i->timestamp);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering s2 = format_timestamp(since2, sizeof(since2), i->timestamp);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering printf("\t Leader: %u", (unsigned) i->leader);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering } else if (i->class)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering printf("\t Root: %s\n", i->root_directory);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering for (c = 0; c < i->n_netif; c++) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering "\t Address: ",
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering print_os_release(bus, i->name, "\t OS: ");
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering show_unit_cgroup(bus, i->unit, i->leader);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poetteringstatic int map_netif(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering const void *v;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert_cc(sizeof(int32_t) == sizeof(int));
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = sd_bus_message_read_array(m, SD_BUS_TYPE_INT32, &v, &l);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int show_info(const char *verb, sd_bus *bus, const char *path, bool *new_line) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering static const struct bus_properties_map map[] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "Name", "s", NULL, offsetof(MachineStatusInfo, name) },
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering { "Class", "s", NULL, offsetof(MachineStatusInfo, class) },
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering { "Service", "s", NULL, offsetof(MachineStatusInfo, service) },
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering { "Unit", "s", NULL, offsetof(MachineStatusInfo, unit) },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "RootDirectory", "s", NULL, offsetof(MachineStatusInfo, root_directory) },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "Leader", "u", NULL, offsetof(MachineStatusInfo, leader) },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "Timestamp", "t", NULL, offsetof(MachineStatusInfo, timestamp) },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "Id", "ay", bus_map_id128, offsetof(MachineStatusInfo, id) },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "NetworkInterfaces", "ai", map_netif, 0 },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.machine1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Could not get properties: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int show_properties(sd_bus *bus, const char *path, bool *new_line) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_all);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_error_errno(r, "Could not get properties: %m");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic int show(int argc, char *argv[], void *userdata) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering /* If no argument is specified, inspect the manager
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = show_properties(bus, "/org/freedesktop/machine1", &new_line);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering "org.freedesktop.machine1",
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering "org.freedesktop.machine1.Manager",
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = sd_bus_message_read(reply, "o", &path);
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek r = show_properties(bus, path, &new_line);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = show_info(argv[0], bus, path, &new_line);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic int kill_machine(int argc, char *argv[], void *userdata) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers "org.freedesktop.machine1",
a6c616024db23fef34152c1432892824a07799ccLennart Poettering "org.freedesktop.machine1.Manager",
a6c616024db23fef34152c1432892824a07799ccLennart Poettering "KillMachine",
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering "ssi", argv[i], arg_kill_who, arg_signal);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Could not kill machine: %s", bus_error_message(&error, -r));
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidtstatic int reboot_machine(int argc, char *argv[], void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering arg_signal = SIGINT; /* sysvinit + systemd */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return kill_machine(argc, argv, userdata);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversstatic int poweroff_machine(int argc, char *argv[], void *userdata) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic int terminate_machine(int argc, char *argv[], void *userdata) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers "org.freedesktop.machine1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.machine1.Manager",
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers "TerminateMachine",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Could not terminate machine: %s", bus_error_message(&error, -r));
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversstatic int machine_get_leader(sd_bus *bus, const char *name, pid_t *ret) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *reply2 = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "org.freedesktop.machine1",
8c841f21f5042b11acc91cc1b039cb162cbbe8f4Djalal Harouni "org.freedesktop.machine1.Manager",
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers "GetMachine",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "org.freedesktop.machine1",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen "org.freedesktop.machine1.Machine",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return log_error_errno(r, "Failed to retrieve PID of leader: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = sd_bus_message_read(reply2, "u", &leader);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringstatic int copy_files(int argc, char *argv[], void *userdata) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char *dest, *host_path, *container_path, *host_dirname, *host_basename, *container_dirname, *container_basename, *t;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering host_path = strdupa(copy_from ? dest : argv[2]);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering container_path = strdupa(copy_from ? argv[2] : dest);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering log_error("Container path not absolute.");
9a14fb6285bdb089d4fc195410de3362cb4f586fThomas Hindoe Paaboel Andersen container_dirname = dirname(container_path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = machine_get_leader(bus, argv[1], &leader);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering hostfd = open(host_dirname, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_DIRECTORY);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return log_error_errno(errno, "Failed to open source directory: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return log_error_errno(errno, "Failed to fork(): %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering const char *q;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering q = procfs_file_alloca(leader, "ns/mnt");
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering mntfd = open(q, O_RDONLY|O_NOCTTY|O_CLOEXEC);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering log_error_errno(errno, "Failed to open mount namespace of leader: %m");
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering log_error_errno(errno, "Failed to join namespace of leader: %m");
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering containerfd = open(container_dirname, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_DIRECTORY);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering log_error_errno(errno, "Failed top open destination directory: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = copy_tree_at(containerfd, container_basename, hostfd, host_basename, true);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = copy_tree_at(hostfd, host_basename, containerfd, container_basename, true);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering log_error_errno(errno, "Failed to copy tree: %m");
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering return log_error_errno(r, "Failed to wait for client: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringstatic int bind_mount(int argc, char *argv[], void *userdata) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering char mount_slave[] = "/tmp/propagate.XXXXXX", *mount_tmp, *mount_outside, *p;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering bool mount_slave_created = false, mount_slave_mounted = false,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering mount_tmp_created = false, mount_tmp_mounted = false,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering mount_outside_created = false, mount_outside_mounted = false;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* One day, when bind mounting /proc/self/fd/n works across
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * namespace boundaries we should rework this logic to make
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * use of it... */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering log_error("Destination path not absolute.");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering p = strappenda("/run/systemd/nspawn/propagate/", argv[1], "/");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering log_error("Container does not allow propagation of mount points.");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = machine_get_leader(bus, argv[1], &leader);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering /* Our goal is to install a new bind mount into the container,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering possibly read-only. This is irritatingly complex
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering unfortunately, currently.
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering First, we start by creating a private playground in /tmp,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering that we can mount MS_SLAVE. (Which is necessary, since
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering MS_MOUNT cannot be applied to mounts with MS_SHARED parent
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return log_error_errno(errno, "Failed to create playground: %m");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (mount(mount_slave, mount_slave, NULL, MS_BIND, NULL) < 0) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = log_error_errno(errno, "Failed to make bind mount: %m");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (mount(NULL, mount_slave, NULL, MS_SLAVE, NULL) < 0) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = log_error_errno(errno, "Failed to remount slave: %m");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering /* Second, we mount the source directory to a directory inside
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering of our MS_SLAVE playground. */
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering mount_tmp = strappenda(mount_slave, "/mount");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = log_error_errno(errno, "Failed to create temporary mount: %m");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (mount(argv[2], mount_tmp, NULL, MS_BIND, NULL) < 0) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = log_error_errno(errno, "Failed to overmount: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Third, we remount the new bind mount read-only if requested. */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (mount(NULL, mount_tmp, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL) < 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = log_error_errno(errno, "Failed to mark read-only: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Fourth, we move the new bind mount into the propagation
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * directory. This way it will appear there read-only
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * right-away. */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering mount_outside = strappenda("/run/systemd/nspawn/propagate/", argv[1], "/XXXXXX");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = log_error_errno(errno, "Cannot create propagation directory: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (mount(mount_tmp, mount_outside, NULL, MS_MOVE, NULL) < 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering r = log_error_errno(errno, "Failed to move: %m");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = log_error_errno(errno, "Failed to fork(): %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering const char *q;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering q = procfs_file_alloca(leader, "ns/mnt");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering mntfd = open(q, O_RDONLY|O_NOCTTY|O_CLOEXEC);
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering log_error_errno(errno, "Failed to open mount namespace of leader: %m");
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering log_error_errno(errno, "Failed to join namespace of leader: %m");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* Fifth, move the mount to the right place inside */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering mount_inside = strappenda("/run/systemd/nspawn/incoming/", basename(mount_outside));
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (mount(mount_inside, dest, NULL, MS_MOVE, NULL) < 0) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering log_error_errno(errno, "Failed to mount: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(r, "Failed to wait for client: %m");
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poetteringstatic int openpt_in_namespace(pid_t pid, int flags) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_close_pair_ int pair[2] = { -1, -1 };
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &rootfd);
923d8fd381bced1c2d90ca53d18629d61a0f454aLennart Poettering if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
1dba654b27918c22e413ac5b3c19301f1ff86ad2Lennart Poettering r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering memcpy(CMSG_DATA(cmsg), &master, sizeof(int));
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering if (sendmsg(pair[1], &mh, MSG_NOSIGNAL) < 0)
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering if (recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poetteringstatic int login_machine(int argc, char *argv[], void *userdata) {
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering _cleanup_bus_close_unref_ sd_bus *container_bus = NULL;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering const char *pty, *p;
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering if (arg_transport != BUS_TRANSPORT_LOCAL) {
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering log_error("Login only supported on local machines.");
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return log_error_errno(r, "Failed to get event loop: %m");
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering return log_error_errno(r, "Failed to attach bus to event loop: %m");
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering r = machine_get_leader(bus, argv[1], &leader);
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering master = openpt_in_namespace(leader, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return log_error_errno(master, "Failed to acquire pseudo tty: %m");
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return log_error_errno(errno, "Failed to get pty name: %m");
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering r = sd_bus_open_system_container(&container_bus, argv[1]);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return log_error_errno(r, "Failed to get container bus: %m");
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering getty = strjoin("container-getty@", p, ".service", NULL);
de33fc625725d199629ed074d6278504deb23debLennart Poettering return log_error_errno(errno, "Failed to unlock tty: %m");
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering "org.freedesktop.systemd1.Manager",
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering log_error("Failed to start getty service: %s", bus_error_message(&error, r));
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering container_bus = sd_bus_unref(container_bus);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering sigset_add_many(&mask, SIGWINCH, SIGTERM, SIGINT, -1);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering assert_se(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering log_info("Connected to container %s. Press ^] three times within 1s to exit session.", argv[1]);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering r = pty_forward_new(event, master, &forward);
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering return log_error_errno(r, "Failed to create PTY forwarder: %m");
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering log_info("Connection to container %s terminated.", argv[1]);
40205d706e1210763ff4c98a317556375bd04bcdLennart Poetteringstatic int help(int argc, char *argv[], void *userdata) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering printf("%s [OPTIONS...] {COMMAND} ...\n\n"
813c65c34beae2eed1f93d9317f97d7e806389f5Lennart Poettering "Send control commands to or query the virtual machine and container\n"
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "registration manager.\n\n"
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering " -h --help Show this help\n"
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering " --version Show package version\n"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering " --no-pager Do not pipe output into a pager\n"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering " --no-legend Do not show the headers and footers\n"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering " -H --host=[USER@]HOST Operate on remote host\n"
9c857b9d160c10b4454fc9f83442c1878343422fLennart Poettering " -M --machine=CONTAINER Operate on local container\n"
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt " -p --property=NAME Show only properties by this name\n"
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt " -a --all Show all properties, including empty ones\n"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering " -l --full Do not ellipsize output\n"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering " --kill-who=WHO Who to send signal to\n"
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt " -s --signal=SIGNAL Which signal to send\n"
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt " --read-only Create read-only bind mount\n"
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering " --mkdir Create directory before bind mounting, if missing\n\n"
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering "Machine Commands:\n"
da054c3782f25b3b18243f6c76dcfcf90ba70274Lennart Poettering " list List running VMs and containers\n"
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering " status NAME... Show VM/container status\n"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering " show NAME... Show properties of one or more VMs/containers\n"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering " login NAME Get a login prompt on a container\n"
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering " poweroff NAME... Power off one or more containers\n"
c7b7d4493aa03e9ef5fb1e670b8969a48aa494ddLennart Poettering " reboot NAME... Reboot one or more containers\n"
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering " kill NAME... Send signal to processes of a VM/container\n"
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering " terminate NAME... Terminate one or more VMs/containers\n"
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering " bind NAME PATH [PATH] Bind mount a path from the host into a container\n"
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
0ec5543c4c0318552a4dcdd83210793347b93081Lennart Poettering " copy-from NAME PATH [PATH] Copy files from a container to the host\n\n"
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering "Image commands:\n"
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering " list-images Show available images\n",
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "version", no_argument, NULL, ARG_VERSION },
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "property", required_argument, NULL, 'p' },
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "no-pager", no_argument, NULL, ARG_NO_PAGER },
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "kill-who", required_argument, NULL, ARG_KILL_WHO },
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "signal", required_argument, NULL, 's' },
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "host", required_argument, NULL, 'H' },
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "machine", required_argument, NULL, 'M' },
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "read-only", no_argument, NULL, ARG_READ_ONLY },
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering { "mkdir", no_argument, NULL, ARG_MKDIR },
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering while ((c = getopt_long(argc, argv, "hp:als:H:M:", options, NULL)) >= 0)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering /* If the user asked for a particular
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering * property, show it to him, even if it is
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering arg_signal = signal_from_string_try_harder(optarg);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_error("Failed to parse signal string %s.", optarg);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poetteringstatic int machinectl_main(int argc, char *argv[], sd_bus *bus) {
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering { "list", VERB_ANY, 1, VERB_DEFAULT, list_machines },
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering { "list-images", VERB_ANY, 1, 0, list_images },
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering { "terminate", 2, VERB_ANY, 0, terminate_machine },
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering { "reboot", 2, VERB_ANY, 0, reboot_machine },
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering { "poweroff", 2, VERB_ANY, 0, poweroff_machine },
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering { "kill", 2, VERB_ANY, 0, kill_machine },
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering return dispatch_verb(argc, argv, verbs, bus);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering _cleanup_bus_close_unref_ sd_bus *bus = NULL;
2723b3b51d409340558e46e37e90525d4f880fe1Lennart Poettering r = bus_open_transport(arg_transport, arg_host, false, &bus);
ebd011d95b61a86258dece9864f65b7c4af721c0Lennart Poettering log_error_errno(r, "Failed to create bus connection: %m");