Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage: Avoid filling volume config on VolumeDBCreate` #14923

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6e4bca9
lxd/storage/drivers/btrfs: Remove unused parameters
hamistao Jan 16, 2025
c02f46a
lxd/storage/drivers/common: Remove stale return value from `[F|f]illV…
hamistao Feb 4, 2025
fe4bcdb
lxd/storage/drivers: Remove stale return value from `FillVolumeConfig`
hamistao Feb 4, 2025
c08ac7f
lxd/storage/backend_lxd: Update `FillVolumeConfig` usage
hamistao Feb 4, 2025
90e36f2
lxd/storage/utils: Update `FillVolumeConfig` usage
hamistao Feb 4, 2025
5ee733d
lxd/storage/drivers/ceph: Remove stale return value from `FillVolumeC…
hamistao Feb 4, 2025
300ddd4
lxd/storage/drivers/dir: Remove stale return value from `FillVolumeCo…
hamistao Feb 4, 2025
f5fa02c
lxd/storage/drivers/lvm: Remove stale return value from `FillVolumeCo…
hamistao Feb 4, 2025
6a5c520
lxd/storage/drivers/powerflex: Remove stale return value from `FillVo…
hamistao Feb 4, 2025
a780860
lxd/storage/drivers/zfs: Remove stale return value from `FillVolumeCo…
hamistao Feb 4, 2025
adc44a5
lxd/storage/drivers/pure: Remove stale return value from `FillVolumeC…
hamistao Feb 5, 2025
3c368f1
lxd/storage/utils: Don't fill up config on `VolumeDBCreate`
hamistao Feb 4, 2025
64afc6d
lxd/storage/backend_lxd: Fill volume configuration on `GetNewVolume`
hamistao Feb 4, 2025
9ff8f15
lxd/storage/backend_lxd: Add needed `FillVolumeConfig`
hamistao Feb 5, 2025
6e9d608
lxd/storage/backend_lxd: Update comments
hamistao Feb 5, 2025
c7ceccb
lxd/storage/drivers/ceph: Pre-allocate `ret` slice (prealloc)
hamistao Feb 5, 2025
1033993
lxd/storage/drivers/ceph: Avoid deprecated `RunCommand` (staticcheck)
hamistao Feb 5, 2025
e7c01f1
lxd/storage/drivers/common: Avoid deprecated `RunCommand` (staticcheck)
hamistao Feb 5, 2025
827bf2a
lxd/storage/drivers/zfs: Avoid deprecated `RunCommand` (staticcheck)
hamistao Feb 5, 2025
1497ea0
WIP
hamistao Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions lxd/storage/backend_lxd.go
tomponline marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2206,10 +2206,7 @@ func (b *lxdBackend) CreateInstanceFromMigration(inst instance.Instance, conn io
if args.MigrationType.FSType == migration.MigrationFSType_RSYNC || args.MigrationType.FSType == migration.MigrationFSType_BLOCK_AND_RSYNC {
vol.SetHasSource(false)

err = b.driver.FillVolumeConfig(vol)
if err != nil {
return fmt.Errorf("Failed filling volume config: %w", err)
}
b.driver.FillVolumeConfig(vol)
}

// Check if the volume exists on storage.
Expand Down Expand Up @@ -2465,10 +2462,7 @@ func (b *lxdBackend) CreateInstanceFromConversion(inst instance.Instance, conn i

// Ensure storage volume settings are honored when doing conversion.
vol.SetHasSource(false)
err = b.driver.FillVolumeConfig(vol)
if err != nil {
return fmt.Errorf("Failed filling volume config: %w", err)
}
b.driver.FillVolumeConfig(vol)

// Check if the volume exists in database
dbVol, err := VolumeDBGet(b, inst.Project().Name, inst.Name(), volType)
Expand Down Expand Up @@ -4102,10 +4096,7 @@ func (b *lxdBackend) EnsureImage(fingerprint string, op *operations.Operation) e
// Generate a temporary volume instance that represents how a new volume using pool defaults would
// be configured.
tmpImgVol := imgVol.Clone()
err := b.Driver().FillVolumeConfig(tmpImgVol)
if err != nil {
return err
}
b.Driver().FillVolumeConfig(tmpImgVol)

// Add existing image volume's config to imgVol.
imgVol = b.GetVolume(drivers.VolumeTypeImage, contentType, image.Fingerprint, imgDBVol.Config)
Expand Down Expand Up @@ -4251,17 +4242,11 @@ func (b *lxdBackend) shouldUseOptimizedImage(fingerprint string, contentType dri

// Create the image volume with the provided volume config.
newImgVol := b.GetVolume(drivers.VolumeTypeImage, contentType, fingerprint, volConfig)
err := b.Driver().FillVolumeConfig(newImgVol)
if err != nil {
return false, err
}
b.Driver().FillVolumeConfig(newImgVol)

// Create the image volume with pool's default settings.
poolDefaultImgVol := b.GetVolume(drivers.VolumeTypeImage, contentType, fingerprint, nil)
err = b.Driver().FillVolumeConfig(poolDefaultImgVol)
if err != nil {
return false, err
}
b.Driver().FillVolumeConfig(poolDefaultImgVol)

// If the new volume's config doesn't match the pool's default configuration, don't use an optimized image.
if !volumeConfigsMatch(newImgVol, poolDefaultImgVol) {
Expand Down Expand Up @@ -7338,10 +7323,7 @@ func (b *lxdBackend) detectUnknownCustomVolume(vol *drivers.Volume, projectVols

// This may not always be the correct thing to do, but seeing as we don't know what the volume's config
// was lets take a best guess that it was the default config.
err = b.driver.FillVolumeConfig(*vol)
if err != nil {
return fmt.Errorf("Failed filling custom volume default config: %w", err)
}
b.driver.FillVolumeConfig(*vol)

// Check the filesystem detected is valid for the storage driver.
err = b.driver.ValidateVolume(*vol, false)
Expand Down Expand Up @@ -7398,10 +7380,7 @@ func (b *lxdBackend) detectUnknownBuckets(vol *drivers.Volume, projectVols map[s

// This may not always be the correct thing to do, but seeing as we don't know what the volume's config
// was lets take a best guess that it was the default config.
err = b.driver.FillVolumeConfig(*vol)
if err != nil {
return fmt.Errorf("Failed filling bucket default config: %w", err)
}
b.driver.FillVolumeConfig(*vol)

// Check the detected filesystem is valid for the storage driver.
err = b.driver.ValidateVolume(*vol, false)
Expand Down
16 changes: 8 additions & 8 deletions lxd/storage/drivers/driver_btrfs_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (d *btrfs) CreateVolumeFromBackup(vol VolumeCopy, srcBackup backup.Info, sr

// createVolumeFromCopy creates a volume from copy by snapshotting the parent volume.
// It also copies the source volume's snapshots and supports refreshing an already existing volume.
func (d *btrfs) createVolumeFromCopy(vol VolumeCopy, srcVol VolumeCopy, allowInconsistent bool, refresh bool, op *operations.Operation) error {
func (d *btrfs) createVolumeFromCopy(vol VolumeCopy, srcVol VolumeCopy, refresh bool, op *operations.Operation) error {
revert := revert.New()
defer revert.Fail()

Expand Down Expand Up @@ -533,7 +533,7 @@ func (d *btrfs) createVolumeFromCopy(vol VolumeCopy, srcVol VolumeCopy, allowInc

// CreateVolumeFromCopy provides same-pool volume copying functionality.
func (d *btrfs) CreateVolumeFromCopy(vol VolumeCopy, srcVol VolumeCopy, allowInconsistent bool, op *operations.Operation) error {
return d.createVolumeFromCopy(vol, srcVol, allowInconsistent, false, op)
return d.createVolumeFromCopy(vol, srcVol, false, op)
}

// CreateVolumeFromMigration creates a volume being sent via a migration.
Expand Down Expand Up @@ -582,7 +582,7 @@ func (d *btrfs) CreateVolumeFromMigration(vol VolumeCopy, conn io.ReadWriteClose
}

if volTargetArgs.Refresh && shared.ValueInSlice(migration.BTRFSFeatureSubvolumeUUIDs, volTargetArgs.MigrationType.Features) {
snapshots, err := d.volumeSnapshotsSorted(vol.Volume, op)
snapshots, err := d.volumeSnapshotsSorted(vol.Volume)
if err != nil {
return err
}
Expand Down Expand Up @@ -641,10 +641,10 @@ func (d *btrfs) CreateVolumeFromMigration(vol VolumeCopy, conn io.ReadWriteClose
syncSubvolumes = migrationHeader.Subvolumes
}

return d.createVolumeFromMigrationOptimized(vol.Volume, conn, volTargetArgs, preFiller, syncSubvolumes, op)
return d.createVolumeFromMigrationOptimized(vol.Volume, conn, volTargetArgs, syncSubvolumes, op)
}

func (d *btrfs) createVolumeFromMigrationOptimized(vol Volume, conn io.ReadWriteCloser, volTargetArgs migration.VolumeTargetArgs, preFiller *VolumeFiller, subvolumes []BTRFSSubVolume, op *operations.Operation) error {
func (d *btrfs) createVolumeFromMigrationOptimized(vol Volume, conn io.ReadWriteCloser, volTargetArgs migration.VolumeTargetArgs, subvolumes []BTRFSSubVolume, op *operations.Operation) error {
revert := revert.New()
defer revert.Fail()

Expand Down Expand Up @@ -819,7 +819,7 @@ func (d *btrfs) createVolumeFromMigrationOptimized(vol Volume, conn io.ReadWrite

// RefreshVolume provides same-pool volume and specific snapshots syncing functionality.
func (d *btrfs) RefreshVolume(vol VolumeCopy, srcVol VolumeCopy, refreshSnapshots []string, allowInconsistent bool, op *operations.Operation) error {
return d.createVolumeFromCopy(vol, srcVol, allowInconsistent, true, op)
return d.createVolumeFromCopy(vol, srcVol, true, op)
}

// DeleteVolume deletes a volume of the storage device. If any snapshots of the volume remain then
Expand Down Expand Up @@ -1183,7 +1183,7 @@ func (d *btrfs) MigrateVolume(vol VolumeCopy, conn io.ReadWriteCloser, volSrcArg

if !volSrcArgs.VolumeOnly {
// Generate restoration header, containing info on the subvolumes and how they should be restored.
snapshots, err = d.volumeSnapshotsSorted(vol.Volume, op)
snapshots, err = d.volumeSnapshotsSorted(vol.Volume)
if err != nil {
return err
}
Expand Down Expand Up @@ -1778,7 +1778,7 @@ func (d *btrfs) VolumeSnapshots(vol Volume, op *operations.Operation) ([]string,

// volumeSnapshotsSorted returns a list of snapshots for the volume (ordered by subvolume ID).
// Since the subvolume ID is incremental, this also represents the order of creation.
func (d *btrfs) volumeSnapshotsSorted(vol Volume, op *operations.Operation) ([]string, error) {
func (d *btrfs) volumeSnapshotsSorted(vol Volume) ([]string, error) {
stdout := bytes.Buffer{}

err := shared.RunCommandWithFds(d.state.ShutdownCtx, nil, &stdout, "btrfs", "subvolume", "list", GetPoolMountPath(vol.pool))
Expand Down
9 changes: 2 additions & 7 deletions lxd/storage/drivers/driver_ceph_volumes.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm I'd be hesitant to start forcefully killing ongoing storage commands that are mutating the local or remote storage layers during a clean shutdown. This feels like its asking for trouble to me.

Was there a reason you didn't go with context.Background() here to ensure the commands safely completed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just failed to consider the risks of stopping these commands midway. Considering that doing so would simply leave the storage layers unharmed is far too optimistic.

Original file line number Diff line number Diff line change
Expand Up @@ -1116,14 +1116,11 @@ func (d *ceph) HasVolume(vol Volume) (bool, error) {
}

// FillVolumeConfig populate volume with default config.
func (d *ceph) FillVolumeConfig(vol Volume) error {
func (d *ceph) FillVolumeConfig(vol Volume) {
// Copy volume.* configuration options from pool.
// Exclude 'block.filesystem' and 'block.mount_options'
// as this ones are handled below in this function and depends from volume type
err := d.fillVolumeConfig(&vol, "block.filesystem", "block.mount_options")
if err != nil {
return err
}
d.fillVolumeConfig(&vol, "block.filesystem", "block.mount_options")

// Only validate filesystem config keys for filesystem volumes or VM block volumes (which have an
// associated filesystem volume).
Expand Down Expand Up @@ -1155,8 +1152,6 @@ func (d *ceph) FillVolumeConfig(vol Volume) error {
vol.config["block.mount_options"] = "discard"
}
}

return nil
}

// commonVolumeRules returns validation rules which are common for pool and volume.
Expand Down
8 changes: 3 additions & 5 deletions lxd/storage/drivers/driver_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (d *common) validatePool(config map[string]string, driverRules map[string]f
// excludeKeys allow exclude some keys from copying to volume config.
// Sometimes that can be useful when copying is dependant from specific conditions
// and shouldn't be done in generic way.
func (d *common) fillVolumeConfig(vol *Volume, excludedKeys ...string) error {
func (d *common) fillVolumeConfig(vol *Volume, excludedKeys ...string) {
for k := range d.config {
if !strings.HasPrefix(k, "volume.") {
continue
Expand Down Expand Up @@ -144,13 +144,11 @@ func (d *common) fillVolumeConfig(vol *Volume, excludedKeys ...string) error {
vol.config[volKey] = d.config[k]
}
}

return nil
}

// FillVolumeConfig populate volume with default config.
func (d *common) FillVolumeConfig(vol Volume) error {
return d.fillVolumeConfig(&vol)
func (d *common) FillVolumeConfig(vol Volume) {
d.fillVolumeConfig(&vol)
}

// validateVolume validates a volume config against common rules and optional driver specific rules.
Expand Down
9 changes: 2 additions & 7 deletions lxd/storage/drivers/driver_dir_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,16 @@ func (d *dir) HasVolume(vol Volume) (bool, error) {
}

// FillVolumeConfig populate volume with default config.
func (d *dir) FillVolumeConfig(vol Volume) error {
func (d *dir) FillVolumeConfig(vol Volume) {
initialSize := vol.config["size"]

err := d.fillVolumeConfig(&vol)
if err != nil {
return err
}
d.fillVolumeConfig(&vol)

// Buckets do not support default volume size.
// If size is specified manually, do not remove, so it triggers validation failure and an error to user.
if vol.volType == VolumeTypeBucket && initialSize == "" {
delete(vol.config, "size")
}

return nil
}

// ValidateVolume validates the supplied volume config. Optionally removes invalid keys from the volume's config.
Expand Down
9 changes: 2 additions & 7 deletions lxd/storage/drivers/driver_lvm_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,11 @@ func (d *lvm) HasVolume(vol Volume) (bool, error) {
}

// FillVolumeConfig populate volume with default config.
func (d *lvm) FillVolumeConfig(vol Volume) error {
func (d *lvm) FillVolumeConfig(vol Volume) {
// Copy volume.* configuration options from pool.
// Exclude "block.filesystem" and "block.mount_options" as they depend on volume type (handled below).
// Exclude "lvm.stripes", "lvm.stripes.size" as they only work on non-thin storage pools (handled below).
err := d.fillVolumeConfig(&vol, "block.filesystem", "block.mount_options", "lvm.stripes", "lvm.stripes.size")
if err != nil {
return err
}
d.fillVolumeConfig(&vol, "block.filesystem", "block.mount_options", "lvm.stripes", "lvm.stripes.size")

// Only validate filesystem config keys for filesystem volumes or VM block volumes (which have an
// associated filesystem volume).
Expand Down Expand Up @@ -315,8 +312,6 @@ func (d *lvm) FillVolumeConfig(vol Volume) error {
vol.config["lvm.stripes.size"] = d.config["lvm.stripes.size"]
}
}

return nil
}

// commonVolumeRules returns validation rules which are common for pool and volume.
Expand Down
9 changes: 2 additions & 7 deletions lxd/storage/drivers/driver_powerflex_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,11 @@ func (d *powerflex) HasVolume(vol Volume) (bool, error) {
}

// FillVolumeConfig populate volume with default config.
func (d *powerflex) FillVolumeConfig(vol Volume) error {
func (d *powerflex) FillVolumeConfig(vol Volume) {
// Copy volume.* configuration options from pool.
// Exclude 'block.filesystem' and 'block.mount_options'
// as these ones are handled below in this function and depend on the volume's type.
err := d.fillVolumeConfig(&vol, "block.filesystem", "block.mount_options")
if err != nil {
return err
}
d.fillVolumeConfig(&vol, "block.filesystem", "block.mount_options")

// Only validate filesystem config keys for filesystem volumes or VM block volumes (which have an
// associated filesystem volume).
Expand Down Expand Up @@ -415,8 +412,6 @@ func (d *powerflex) FillVolumeConfig(vol Volume) error {
vol.config["block.mount_options"] = "discard"
}
}

return nil
}

// commonVolumeRules returns validation rules which are common for pool and volume.
Expand Down
9 changes: 2 additions & 7 deletions lxd/storage/drivers/driver_pure_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,14 +671,11 @@ func (d *pure) HasVolume(vol Volume) (bool, error) {
}

// FillVolumeConfig populate volume with default config.
func (d *pure) FillVolumeConfig(vol Volume) error {
func (d *pure) FillVolumeConfig(vol Volume) {
// Copy volume.* configuration options from pool.
// Exclude 'block.filesystem' and 'block.mount_options'
// as these ones are handled below in this function and depend on the volume's type.
err := d.fillVolumeConfig(&vol, "block.filesystem", "block.mount_options")
if err != nil {
return err
}
d.fillVolumeConfig(&vol, "block.filesystem", "block.mount_options")

// Only validate filesystem config keys for filesystem volumes or VM block volumes (which have an
// associated filesystem volume).
Expand Down Expand Up @@ -710,8 +707,6 @@ func (d *pure) FillVolumeConfig(vol Volume) error {
vol.config["block.mount_options"] = "discard"
}
}

return nil
}

// ValidateVolume validates the supplied volume config.
Expand Down
9 changes: 2 additions & 7 deletions lxd/storage/drivers/driver_zfs_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3501,7 +3501,7 @@ func (d *zfs) RenameVolumeSnapshot(vol Volume, newSnapshotName string, op *opera
}

// FillVolumeConfig populate volume with default config.
func (d *zfs) FillVolumeConfig(vol Volume) error {
func (d *zfs) FillVolumeConfig(vol Volume) {
var excludedKeys []string

// Copy volume.* configuration options from pool.
Expand All @@ -3512,10 +3512,7 @@ func (d *zfs) FillVolumeConfig(vol Volume) error {
excludedKeys = []string{"block.filesystem", "block.mount_options"}
}

err := d.fillVolumeConfig(&vol, excludedKeys...)
if err != nil {
return err
}
d.fillVolumeConfig(&vol, excludedKeys...)

// Only validate filesystem config keys for filesystem volumes.
if d.isBlockBacked(vol) && vol.ContentType() == ContentTypeFS {
Expand Down Expand Up @@ -3546,8 +3543,6 @@ func (d *zfs) FillVolumeConfig(vol Volume) error {
vol.config["block.mount_options"] = "discard"
}
}

return nil
}

func (d *zfs) isBlockBacked(vol Volume) bool {
Expand Down
2 changes: 1 addition & 1 deletion lxd/storage/drivers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Driver interface {
DeleteBucketKey(bucket Volume, keyName string, op *operations.Operation) error

// Volumes.
FillVolumeConfig(vol Volume) error
FillVolumeConfig(vol Volume)
ValidateVolume(vol Volume, removeUnknownKeys bool) error
CreateVolume(vol Volume, filler *VolumeFiller, op *operations.Operation) error
CreateVolumeFromCopy(vol VolumeCopy, srcVol VolumeCopy, allowInconsistent bool, op *operations.Operation) error
Expand Down
14 changes: 2 additions & 12 deletions lxd/storage/utils.go
tomponline marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ func VolumeDBGet(pool Pool, projectName string, volumeName string, volumeType dr
}

// VolumeDBCreate creates a volume in the database.
// If volumeConfig is supplied, it is modified with any driver level default config options (if not set).
// If removeUnknownKeys is true, any unknown config keys are removed from volumeConfig rather than failing.
func VolumeDBCreate(pool Pool, projectName string, volumeName string, volumeDescription string, volumeType drivers.VolumeType, snapshot bool, volumeConfig map[string]string, creationDate time.Time, expiryDate time.Time, contentType drivers.ContentType, removeUnknownKeys bool, hasSource bool) error {
p, ok := pool.(*lxdBackend)
Expand Down Expand Up @@ -269,12 +268,6 @@ func VolumeDBCreate(pool Pool, projectName string, volumeName string, volumeDesc
// Set source indicator.
vol.SetHasSource(hasSource)

// Fill default config.
err = pool.Driver().FillVolumeConfig(vol)
if err != nil {
return err
}

// Validate config.
err = pool.Driver().ValidateVolume(vol, removeUnknownKeys)
if err != nil {
Expand Down Expand Up @@ -395,13 +388,10 @@ func BucketDBCreate(ctx context.Context, pool Pool, projectName string, memberSp
bucketVol := drivers.NewVolume(pool.Driver(), pool.Name(), drivers.VolumeTypeBucket, drivers.ContentTypeFS, bucketVolName, bucket.Config, pool.Driver().Config())

// Fill default config.
err := pool.Driver().FillVolumeConfig(bucketVol)
if err != nil {
return -1, err
}
pool.Driver().FillVolumeConfig(bucketVol)

// Validate bucket name.
err = pool.Driver().ValidateBucket(bucketVol)
err := pool.Driver().ValidateBucket(bucketVol)
if err != nil {
return -1, err
}
Expand Down