From 49588706816867c9e269bf94a7e2f50aa0e3f5b4 Mon Sep 17 00:00:00 2001 From: Max Asnaashari Date: Wed, 11 Dec 2024 23:23:02 +0000 Subject: [PATCH 1/3] lxd/storage/drivers: Add helpers for fetching osd pool default size Signed-off-by: Max Asnaashari --- lxd/storage/drivers/driver_ceph_utils.go | 25 ++++++++++++++++++++ lxd/storage/drivers/driver_cephfs_utils.go | 27 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lxd/storage/drivers/driver_ceph_utils.go b/lxd/storage/drivers/driver_ceph_utils.go index f8474dd4e33d..d5ca7336b5d0 100644 --- a/lxd/storage/drivers/driver_ceph_utils.go +++ b/lxd/storage/drivers/driver_ceph_utils.go @@ -589,6 +589,31 @@ func (d *ceph) rbdListVolumeSnapshots(vol Volume) ([]string, error) { return snapshots, nil } +// getOSDPoolDefaultSize gets the global OSD default pool size that is used for +// all pools created without an explicit OSD pool size. +func (d *ceph) getOSDPoolDefaultSize() (int, error) { + defaultSize, err := shared.TryRunCommand("ceph", + "--name", "client."+d.config["ceph.user.name"], + "--cluster", d.config["ceph.cluster_name"], + "config", + "get", + "mon", + "osd_pool_default_size", + "--format", + "json") + if err != nil { + return -1, err + } + + var defaultSizeInt int + err = json.Unmarshal([]byte(defaultSize), &defaultSizeInt) + if err != nil { + return -1, err + } + + return defaultSizeInt, nil +} + // copyVolumeDiff creates a sparse copy of a volume by exporting and importing the diff // between `sourceVolumeName` and its optional `sourceParentSnapshot` onto `targetVolumeName`. // This does not introduce a dependency relation between the source RBD storage diff --git a/lxd/storage/drivers/driver_cephfs_utils.go b/lxd/storage/drivers/driver_cephfs_utils.go index 1dad43d550d3..90cd5ef428f7 100644 --- a/lxd/storage/drivers/driver_cephfs_utils.go +++ b/lxd/storage/drivers/driver_cephfs_utils.go @@ -1,6 +1,8 @@ package drivers import ( + "encoding/json" + "github.com/canonical/lxd/shared" ) @@ -42,6 +44,31 @@ func (d *cephfs) osdPoolExists(clusterName string, userName string, osdPoolName return true, nil } +// getOSDPoolDefaultSize gets the global OSD default pool size that is used for +// all pools created without an explicit OSD pool size. +func (d *cephfs) getOSDPoolDefaultSize() (int, error) { + size, err := shared.TryRunCommand("ceph", + "--name", "client."+d.config["cephfs.user.name"], + "--cluster", d.config["cephfs.cluster_name"], + "config", + "get", + "mon", + "osd_pool_default_size", + "--format", + "json") + if err != nil { + return -1, err + } + + var sizeInt int + err = json.Unmarshal([]byte(size), &sizeInt) + if err != nil { + return -1, err + } + + return sizeInt, nil +} + // getConfig parses the Ceph configuration file and returns the list of monitors and secret key. func (d *cephfs) getConfig(clusterName string, userName string) ([]string, string, error) { // Get the monitor list. From 36c34cd24c0a3ffcf4b233f24127b6948f1f0b58 Mon Sep 17 00:00:00 2001 From: Max Asnaashari Date: Wed, 11 Dec 2024 23:26:34 +0000 Subject: [PATCH 2/3] lxd/storage/drivers: Use helper to get default pool size in FIllConfig Signed-off-by: Max Asnaashari --- lxd/storage/drivers/driver_ceph.go | 18 ++---------------- lxd/storage/drivers/driver_cephfs.go | 18 ++---------------- 2 files changed, 4 insertions(+), 32 deletions(-) diff --git a/lxd/storage/drivers/driver_ceph.go b/lxd/storage/drivers/driver_ceph.go index c3de16f42a27..89dd3c512b53 100644 --- a/lxd/storage/drivers/driver_ceph.go +++ b/lxd/storage/drivers/driver_ceph.go @@ -115,26 +115,12 @@ func (d *ceph) FillConfig() error { } if d.config["ceph.osd.pool_size"] == "" { - size, err := shared.TryRunCommand("ceph", - "--name", "client."+d.config["ceph.user.name"], - "--cluster", d.config["ceph.cluster_name"], - "config", - "get", - "mon", - "osd_pool_default_size", - "--format", - "json") - if err != nil { - return err - } - - var sizeInt int - err = json.Unmarshal([]byte(size), &sizeInt) + defaultSize, err := d.getOSDPoolDefaultSize() if err != nil { return err } - d.config["ceph.osd.pool_size"] = strconv.Itoa(sizeInt) + d.config["ceph.osd.pool_size"] = strconv.Itoa(defaultSize) } return nil diff --git a/lxd/storage/drivers/driver_cephfs.go b/lxd/storage/drivers/driver_cephfs.go index 21a9e125a6a5..38f343384ba7 100644 --- a/lxd/storage/drivers/driver_cephfs.go +++ b/lxd/storage/drivers/driver_cephfs.go @@ -105,26 +105,12 @@ func (d *cephfs) FillConfig() error { } if d.config["cephfs.osd_pool_size"] == "" { - size, err := shared.TryRunCommand("ceph", - "--name", "client."+d.config["cephfs.user.name"], - "--cluster", d.config["cephfs.cluster_name"], - "config", - "get", - "mon", - "osd_pool_default_size", - "--format", - "json") - if err != nil { - return err - } - - var sizeInt int - err = json.Unmarshal([]byte(size), &sizeInt) + defaultSize, err := d.getOSDPoolDefaultSize() if err != nil { return err } - d.config["cephfs.osd_pool_size"] = strconv.Itoa(sizeInt) + d.config["cephfs.osd_pool_size"] = strconv.Itoa(defaultSize) } return nil From 1f8765b5f38ec6fb7510238f6e54fe0a1a914bc5 Mon Sep 17 00:00:00 2001 From: Max Asnaashari Date: Wed, 11 Dec 2024 23:26:56 +0000 Subject: [PATCH 3/3] lxd/storage/drivers: Only set pool size on create if not default Signed-off-by: Max Asnaashari --- lxd/storage/drivers/driver_ceph.go | 29 ++++++++++++++++++---------- lxd/storage/drivers/driver_cephfs.go | 28 ++++++++++++++++----------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/lxd/storage/drivers/driver_ceph.go b/lxd/storage/drivers/driver_ceph.go index 89dd3c512b53..04ecbfcab163 100644 --- a/lxd/storage/drivers/driver_ceph.go +++ b/lxd/storage/drivers/driver_ceph.go @@ -182,20 +182,29 @@ func (d *ceph) Create() error { revert.Add(func() { _ = d.osdDeletePool() }) - _, err = shared.TryRunCommand("ceph", - "--name", "client."+d.config["ceph.user.name"], - "--cluster", d.config["ceph.cluster_name"], - "osd", - "pool", - "set", - d.config["ceph.osd.pool_name"], - "size", - d.config["ceph.osd.pool_size"], - "--yes-i-really-mean-it") + // Fetch the default OSD pool size. + defaultSize, err := d.getOSDPoolDefaultSize() if err != nil { return err } + // If the OSD pool size in the config for this pool is different than the default OSD pool size, then set the pool size for the pool. + if d.config["ceph.osd.pool_size"] != strconv.Itoa(defaultSize) { + _, err = shared.TryRunCommand("ceph", + "--name", "client."+d.config["ceph.user.name"], + "--cluster", d.config["ceph.cluster_name"], + "osd", + "pool", + "set", + d.config["ceph.osd.pool_name"], + "size", + d.config["ceph.osd.pool_size"], + "--yes-i-really-mean-it") + if err != nil { + return err + } + } + // Initialize the pool. This is not necessary but allows the pool to be monitored. _, err = shared.TryRunCommand("rbd", "--id", d.config["ceph.user.name"], diff --git a/lxd/storage/drivers/driver_cephfs.go b/lxd/storage/drivers/driver_cephfs.go index 38f343384ba7..750fab8f0f39 100644 --- a/lxd/storage/drivers/driver_cephfs.go +++ b/lxd/storage/drivers/driver_cephfs.go @@ -1,7 +1,6 @@ package drivers import ( - "encoding/json" "fmt" "os" "os/exec" @@ -213,19 +212,26 @@ func (d *cephfs) Create() error { ) }) - _, err = shared.TryRunCommand("ceph", - "--name", "client."+d.config["cephfs.user.name"], - "--cluster", d.config["cephfs.cluster_name"], - "osd", - "pool", - "set", - pool, - "size", - d.config["cephfs.osd_pool_size"], - "--yes-i-really-mean-it") + defaultSize, err := d.getOSDPoolDefaultSize() if err != nil { return err } + + if strconv.Itoa(defaultSize) != d.config["cephfs.osd_pool_size"] { + _, err = shared.TryRunCommand("ceph", + "--name", "client."+d.config["cephfs.user.name"], + "--cluster", d.config["cephfs.cluster_name"], + "osd", + "pool", + "set", + pool, + "size", + d.config["cephfs.osd_pool_size"], + "--yes-i-really-mean-it") + if err != nil { + return err + } + } } }