diff --git a/lxd/storage/drivers/driver_ceph.go b/lxd/storage/drivers/driver_ceph.go index c3de16f42a27..04ecbfcab163 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 @@ -196,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_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.go b/lxd/storage/drivers/driver_cephfs.go index 21a9e125a6a5..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" @@ -105,26 +104,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") + defaultSize, err := d.getOSDPoolDefaultSize() if err != nil { return err } - var sizeInt int - err = json.Unmarshal([]byte(size), &sizeInt) - 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 @@ -227,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 + } + } } } 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.