chiark / gitweb /
machined: also set up /var/lib/machines as btrfs, if "machinectl set-limit" is called
[elogind.git] / src / machine / machinectl.c
index 053c8fbbe78d8181f908dd810652092673f3b54d..49e28ee52f21f5a4f3af21182507d558adc57134 100644 (file)
@@ -24,7 +24,6 @@
 #include <errno.h>
 #include <string.h>
 #include <getopt.h>
-#include <pwd.h>
 #include <locale.h>
 #include <fcntl.h>
 #include <netinet/in.h>
@@ -804,7 +803,7 @@ static void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) {
                 printf("\t   Limit: %s\n", s3);
 }
 
-static int show_image_info(const char *verb, sd_bus *bus, const char *path, bool *new_line) {
+static int show_image_info(sd_bus *bus, const char *path, bool *new_line) {
 
         static const struct bus_properties_map map[]  = {
                 { "Name",                  "s",  NULL, offsetof(ImageStatusInfo, name)            },
@@ -823,7 +822,6 @@ static int show_image_info(const char *verb, sd_bus *bus, const char *path, bool
         ImageStatusInfo info = {};
         int r;
 
-        assert(verb);
         assert(bus);
         assert(path);
         assert(new_line);
@@ -849,6 +847,59 @@ static int show_image_info(const char *verb, sd_bus *bus, const char *path, bool
         return r;
 }
 
+typedef struct PoolStatusInfo {
+        char *path;
+        uint64_t usage;
+        uint64_t limit;
+} PoolStatusInfo;
+
+static void print_pool_status_info(sd_bus *bus, PoolStatusInfo *i) {
+        char bs[FORMAT_BYTES_MAX], *s;
+
+        if (i->path)
+                printf("\t    Path: %s\n", i->path);
+
+        s = format_bytes(bs, sizeof(bs), i->usage);
+        if (s)
+                printf("\t   Usage: %s\n", s);
+
+        s = format_bytes(bs, sizeof(bs), i->limit);
+        if (s)
+                printf("\t   Limit: %s\n", s);
+}
+
+static int show_pool_info(sd_bus *bus) {
+
+        static const struct bus_properties_map map[]  = {
+                { "PoolPath",  "s",  NULL, offsetof(PoolStatusInfo, path)  },
+                { "PoolUsage", "t",  NULL, offsetof(PoolStatusInfo, usage) },
+                { "PoolLimit", "t",  NULL, offsetof(PoolStatusInfo, limit) },
+                {}
+        };
+
+        PoolStatusInfo info = {
+                .usage = (uint64_t) -1,
+                .limit = (uint64_t) -1,
+        };
+        int r;
+
+        assert(bus);
+
+        r = bus_map_all_properties(bus,
+                                   "org.freedesktop.machine1",
+                                   "/org/freedesktop/machine1",
+                                   map,
+                                   &info);
+        if (r < 0)
+                return log_error_errno(r, "Could not get properties: %m");
+
+        print_pool_status_info(bus, &info);
+
+        free(info.path);
+        return 0;
+}
+
+
 static int show_image_properties(sd_bus *bus, const char *path, bool *new_line) {
         int r;
 
@@ -882,11 +933,15 @@ static int show_image(int argc, char *argv[], void *userdata) {
 
         pager_open_if_enabled();
 
-        if (properties && argc <= 1) {
+        if (argc <= 1) {
 
                 /* If no argument is specified, inspect the manager
                  * itself */
-                r = show_image_properties(bus, "/org/freedesktop/machine1", &new_line);
+
+                if (properties)
+                        r = show_image_properties(bus, "/org/freedesktop/machine1", &new_line);
+                else
+                        r = show_pool_info(bus);
                 if (r < 0)
                         return r;
         }
@@ -915,7 +970,7 @@ static int show_image(int argc, char *argv[], void *userdata) {
                 if (properties)
                         r = show_image_properties(bus, path, &new_line);
                 else
-                        r = show_image_info(argv[0], bus, path, &new_line);
+                        r = show_image_info(bus, path, &new_line);
         }
 
         return r;
@@ -1150,7 +1205,7 @@ static int login_machine(int argc, char *argv[], void *userdata) {
         sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
         sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
 
-        r = pty_forward_new(event, master, true, &forward);
+        r = pty_forward_new(event, master, true, false, &forward);
         if (r < 0)
                 return log_error_errno(r, "Failed to create PTY forwarder: %m");
 
@@ -1906,6 +1961,56 @@ static int cancel_transfer(int argc, char *argv[], void *userdata) {
         return 0;
 }
 
+static int set_limit(int argc, char *argv[], void *userdata) {
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        sd_bus *bus = userdata;
+        uint64_t limit;
+        int r;
+
+        if (streq(argv[argc-1], "-"))
+                limit = (uint64_t) -1;
+        else {
+                off_t off;
+
+                r = parse_size(argv[argc-1], 1024, &off);
+                if (r < 0)
+                        return log_error("Failed to parse size: %s", argv[argc-1]);
+
+                limit = (uint64_t) off;
+        }
+
+        if (argc > 2)
+                /* With two arguments changes the quota limit of the
+                 * specified image */
+                r = sd_bus_call_method(
+                                bus,
+                                "org.freedesktop.machine1",
+                                "/org/freedesktop/machine1",
+                                "org.freedesktop.machine1.Manager",
+                                "SetImageLimit",
+                                &error,
+                                NULL,
+                                "st", argv[1], limit);
+        else
+                /* With one argument changes the pool quota limit */
+                r = sd_bus_call_method(
+                                bus,
+                                "org.freedesktop.machine1",
+                                "/org/freedesktop/machine1",
+                                "org.freedesktop.machine1.Manager",
+                                "SetPoolLimit",
+                                &error,
+                                NULL,
+                                "t", limit);
+
+        if (r < 0) {
+                log_error("Could not set limit: %s", bus_error_message(&error, -r));
+                return r;
+        }
+
+        return 0;
+}
+
 static int help(int argc, char *argv[], void *userdata) {
 
         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
@@ -1957,7 +2062,8 @@ static int help(int argc, char *argv[], void *userdata) {
                "  clone NAME NAME             Clone an image\n"
                "  rename NAME NAME            Rename an image\n"
                "  read-only NAME [BOOL]       Mark or unmark image read-only\n"
-               "  remove NAME...              Remove an image\n\n"
+               "  remove NAME...              Remove an image\n"
+               "  set-limit [NAME] BYTES      Set image or pool size limit (quota)\n\n"
                "Image Transfer Commands:\n"
                "  pull-tar URL [NAME]         Download a TAR container image\n"
                "  pull-raw URL [NAME]         Download a RAW container or VM image\n"
@@ -2143,7 +2249,7 @@ static int machinectl_main(int argc, char *argv[], sd_bus *bus) {
                 { "list",            VERB_ANY, 1,        VERB_DEFAULT, list_machines     },
                 { "list-images",     VERB_ANY, 1,        0,            list_images       },
                 { "status",          2,        VERB_ANY, 0,            show_machine      },
-                { "image-status",    2,        VERB_ANY, 0,            show_image        },
+                { "image-status",    VERB_ANY, VERB_ANY, 0,            show_image        },
                 { "show",            VERB_ANY, VERB_ANY, 0,            show_machine      },
                 { "show-image",      VERB_ANY, VERB_ANY, 0,            show_image        },
                 { "terminate",       2,        VERB_ANY, 0,            terminate_machine },
@@ -2166,6 +2272,7 @@ static int machinectl_main(int argc, char *argv[], sd_bus *bus) {
                 { "pull-dkr",        2,        3,        0,            pull_dkr          },
                 { "list-transfers",  VERB_ANY, 1,        0,            list_transfers    },
                 { "cancel-transfer", 2,        VERB_ANY, 0,            cancel_transfer   },
+                { "set-limit",       2,        3,        0,            set_limit         },
                 {}
         };