The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/6983

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
As discussed in #6953

I was unsure what the best way was to parse the JSON response, I decided to go for dedicated structs, I'm open to other options. 
From e0c3a66dc37f4990f6a6e08e8edb078721760cb9 Mon Sep 17 00:00:00 2001
From: Maran <ma...@protonmail.com>
Date: Thu, 5 Mar 2020 08:43:07 +0100
Subject: [PATCH] lxd/storage/ceph: Implement GetVolumeUsage

Signed-off-by: Maran <ma...@protonmail.com>
---
 lxd/storage/drivers/driver_ceph_volumes.go | 49 +++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/lxd/storage/drivers/driver_ceph_volumes.go 
b/lxd/storage/drivers/driver_ceph_volumes.go
index cd1f1581a7..40699785bc 100644
--- a/lxd/storage/drivers/driver_ceph_volumes.go
+++ b/lxd/storage/drivers/driver_ceph_volumes.go
@@ -602,9 +602,56 @@ func (d *ceph) UpdateVolume(vol Volume, changedConfig 
map[string]string) error {
        return nil
 }
 
+type cephDuInfo struct {
+       Images []cephDuLine `json:"images"`
+}
+
+type cephDuLine struct {
+       Name            string `json:"name"`
+       Snapshot        string `json:"snapshot"`
+       ProvisionedSize int64  `json:"provisioned_size"`
+       UsedSize        int64  `json:"used_size"`
+}
+
 // GetVolumeUsage returns the disk space used by the volume.
 func (d *ceph) GetVolumeUsage(vol Volume) (int64, error) {
-       return -1, ErrNotSupported
+       if vol.contentType == ContentTypeFS && 
shared.IsMountPoint(vol.MountPath()) {
+               var stat unix.Statfs_t
+
+               err := unix.Statfs(vol.MountPath(), &stat)
+               if err != nil {
+                       return -1, err
+               }
+
+               return int64(stat.Blocks-stat.Bfree) * int64(stat.Bsize), nil
+       } else {
+               jsonInfo, err := shared.TryRunCommand(
+                       "rbd",
+                       "du",
+                       "--format", "json",
+                       "--id", d.config["ceph.user.name"],
+                       "--cluster", d.config["ceph.cluster_name"],
+                       "--pool", d.config["ceph.osd.pool_name"],
+                       d.getRBDVolumeName(vol, "", false, false))
+
+               if err != nil {
+                       return -1, err
+               }
+
+               var usedSize int64
+               var result cephDuInfo
+               json.Unmarshal([]byte(jsonInfo), &result)
+
+               // rbd du gives the output of all related rbd images, snapshots 
included
+               // to get the total size of the image we use the result that 
does not include
+               // a snapshot name, this is the total image size.
+               for _, image := range result.Images {
+                       if image.Snapshot == "" {
+                               usedSize = image.UsedSize
+                       }
+               }
+               return usedSize, nil
+       }
 }
 
 // SetVolumeQuota applies a size limit on volume.
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to