diff --git a/lxd/storage/connectors/utils.go b/lxd/storage/connectors/utils.go index 133172fcc13f..2d6ae36aaa5d 100644 --- a/lxd/storage/connectors/utils.go +++ b/lxd/storage/connectors/utils.go @@ -13,6 +13,7 @@ import ( "github.com/canonical/lxd/lxd/locking" "github.com/canonical/lxd/lxd/resources" + "github.com/canonical/lxd/lxd/storage/block" "github.com/canonical/lxd/shared" "github.com/canonical/lxd/shared/logger" "github.com/canonical/lxd/shared/revert" @@ -132,6 +133,29 @@ func WaitDiskDeviceGone(ctx context.Context, diskPath string) bool { } } +// WaitDiskDeviceResize waits until the disk device reflects the new size. +func WaitDiskDeviceResize(ctx context.Context, diskPath string, newSizeBytes int64) error { + ctx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + + for { + sizeBytes, err := block.DiskSizeBytes(diskPath) + if err != nil { + return fmt.Errorf("Error getting disk size: %w", err) + } + + if sizeBytes == newSizeBytes { + return nil + } + + if ctx.Err() != nil { + return ctx.Err() + } + + time.Sleep(500 * time.Millisecond) + } +} + // connectFunc is invoked by "connect" for each provided address. // It receives a session and a target address. A non-nil session indicates // an existing session for the target.