[lxc-devel] [lxd/master] lxd/storage/ceph: Implement GetVolumeUsage

maran on Github lxc-bot at linuxcontainers.org
Thu Mar 5 07:46:31 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 455 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200304/ea2ee862/attachment.bin>
-------------- next part --------------
From e0c3a66dc37f4990f6a6e08e8edb078721760cb9 Mon Sep 17 00:00:00 2001
From: Maran <maran at protonmail.com>
Date: Thu, 5 Mar 2020 08:43:07 +0100
Subject: [PATCH] lxd/storage/ceph: Implement GetVolumeUsage

Signed-off-by: Maran <maran at 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.


More information about the lxc-devel mailing list