[systemd-commits] 3 commits - src/import src/machine src/shared units/var-lib-machines.mount

Lennart Poettering lennart at kemper.freedesktop.org
Tue Feb 24 09:46:54 PST 2015


 src/import/importd.c         |    9 ++++
 src/machine/machinectl.c     |   68 +++++++++++++++++++++++++++++---
 src/machine/machined-dbus.c  |   91 +++++++++++++++++++++++++++++++++++++++++++
 src/shared/btrfs-util.c      |   23 ++++++++++
 src/shared/btrfs-util.h      |    3 +
 units/var-lib-machines.mount |   16 +++++++
 6 files changed, 204 insertions(+), 6 deletions(-)

New commits:
commit 4f3c1682029450f5d93cb282343035d0cf3dd407
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Feb 24 18:46:33 2015 +0100

    units: add missing unit file

diff --git a/units/var-lib-machines.mount b/units/var-lib-machines.mount
new file mode 100644
index 0000000..7eba68f
--- /dev/null
+++ b/units/var-lib-machines.mount
@@ -0,0 +1,16 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Virtual Machine and Container Storage
+ConditionPathExists=/var/lib/machines.raw
+
+[Mount]
+What=/var/lib/machines.raw
+Where=/var/lib/machines
+Type=btrfs
+Options=loop

commit 754061ce7173fd8cb66ade1a48381e2cead35522
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Feb 24 18:43:37 2015 +0100

    importd: enable btrfs quota in /var/lib/machines, if necessary

diff --git a/src/import/importd.c b/src/import/importd.c
index 25d9ab2..f315212 100644
--- a/src/import/importd.c
+++ b/src/import/importd.c
@@ -792,6 +792,11 @@ static int setup_machine_directory(sd_bus_error *error) {
                 return sd_bus_error_set_errnof(error, r, "Failed to determine whether /var/lib/machines is located on btrfs: %m");
         if (r > 0) {
                 (void) btrfs_subvol_make_label("/var/lib/machines");
+
+                r = btrfs_quota_enable("/var/lib/machines", true);
+                if (r < 0)
+                        log_warning_errno(r, "Failed to enable quota, ignoring: %m");
+
                 return 0;
         }
 
@@ -858,6 +863,10 @@ static int setup_machine_directory(sd_bus_error *error) {
         }
         mntdir_mounted = true;
 
+        r = btrfs_quota_enable(mntdir, true);
+        if (r < 0)
+                log_warning_errno(r, "Failed to enable quota, ignoring: %m");
+
         if (chmod(mntdir, 0700) < 0) {
                 r = sd_bus_error_set_errnof(error, errno, "Failed to fix owner: %m");
                 goto fail;
diff --git a/src/shared/btrfs-util.c b/src/shared/btrfs-util.c
index 2a70dfe..980963b 100644
--- a/src/shared/btrfs-util.c
+++ b/src/shared/btrfs-util.c
@@ -646,3 +646,26 @@ int btrfs_defrag(const char *p) {
 
         return btrfs_defrag_fd(fd);
 }
+
+int btrfs_quota_enable_fd(int fd, bool b) {
+        struct btrfs_ioctl_quota_ctl_args args = {
+                .cmd = b ? BTRFS_QUOTA_CTL_ENABLE : BTRFS_QUOTA_CTL_DISABLE,
+        };
+
+        assert(fd >= 0);
+
+        if (ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args) < 0)
+                return -errno;
+
+        return 0;
+}
+
+int btrfs_quota_enable(const char *path, bool b) {
+        _cleanup_close_ int fd = -1;
+
+        fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
+        if (fd < 0)
+                return -errno;
+
+        return btrfs_quota_enable_fd(fd, b);
+}
diff --git a/src/shared/btrfs-util.h b/src/shared/btrfs-util.h
index 1b9c142..93c3f13 100644
--- a/src/shared/btrfs-util.h
+++ b/src/shared/btrfs-util.h
@@ -64,3 +64,6 @@ int btrfs_get_block_device(const char *path, dev_t *dev);
 
 int btrfs_defrag_fd(int fd);
 int btrfs_defrag(const char *p);
+
+int btrfs_quota_enable_fd(int fd, bool b);
+int btrfs_quota_enable(const char *path, bool b);

commit 160e3793adf2da2bd9ae3fe6b8881bb937e6e71b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Feb 24 18:23:40 2015 +0100

    machined/machinectl: when "machinectl image-status" is used without arguments show statistics about pool

diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index 61183a6..ddd2a4a 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -803,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)            },
@@ -822,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);
@@ -848,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;
 
@@ -881,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;
         }
@@ -914,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;
@@ -2142,7 +2198,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 },
diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c
index 0e5a494..c4f60b5 100644
--- a/src/machine/machined-dbus.c
+++ b/src/machine/machined-dbus.c
@@ -29,11 +29,99 @@
 #include "bus-util.h"
 #include "bus-common-errors.h"
 #include "cgroup-util.h"
+#include "btrfs-util.h"
 #include "machine-image.h"
 #include "image-dbus.h"
 #include "machined.h"
 #include "machine-dbus.h"
 
+static int property_get_pool_path(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        assert(bus);
+        assert(reply);
+
+        return sd_bus_message_append(reply, "s", "/var/lib/machines");
+}
+
+static int property_get_pool_usage(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        _cleanup_close_ int fd = -1;
+        uint64_t usage = (uint64_t) -1;
+        struct stat st;
+
+        assert(bus);
+        assert(reply);
+
+        /* We try to read the quota info from /var/lib/machines, as
+         * well as the usage of the loopback file
+         * /var/lib/machines.raw, and pick the larger value. */
+
+        fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
+        if (fd >= 0) {
+                BtrfsQuotaInfo q;
+
+                if (btrfs_subvol_get_quota_fd(fd, &q) >= 0)
+                        usage = q.referred;
+        }
+
+        if (stat("/var/lib/machines.raw", &st) >= 0) {
+                if (usage == (uint64_t) -1 || st.st_blocks * 512ULL > usage)
+                        usage = st.st_blocks * 512ULL;
+        }
+
+        return sd_bus_message_append(reply, "t", usage);
+}
+
+static int property_get_pool_limit(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        _cleanup_close_ int fd = -1;
+        uint64_t size = (uint64_t) -1;
+        struct stat st;
+
+        assert(bus);
+        assert(reply);
+
+        /* We try to read the quota limit from /var/lib/machines, as
+         * well as the size of the loopback file
+         * /var/lib/machines.raw, and pick the smaller value. */
+
+        fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
+        if (fd >= 0) {
+                BtrfsQuotaInfo q;
+
+                if (btrfs_subvol_get_quota_fd(fd, &q) >= 0)
+                        size = q.referred_max;
+        }
+
+        if (stat("/var/lib/machines.raw", &st) >= 0) {
+                if (size == (uint64_t) -1 || (uint64_t) st.st_size < size)
+                        size = st.st_size;
+        }
+
+        return sd_bus_message_append(reply, "t", size);
+}
+
 static int method_get_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
         _cleanup_free_ char *p = NULL;
         Manager *m = userdata;
@@ -690,6 +778,9 @@ static int method_mark_image_read_only(sd_bus *bus, sd_bus_message *message, voi
 
 const sd_bus_vtable manager_vtable[] = {
         SD_BUS_VTABLE_START(0),
+        SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0),
+        SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0),
+        SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0),
         SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("GetImage", "s", "o", method_get_image, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),



More information about the systemd-commits mailing list