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: Use zero for unbound volumes' total size on the API #14837

Merged
merged 9 commits into from
Jan 29, 2025
8 changes: 4 additions & 4 deletions doc/rest-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2516,13 +2516,13 @@ definitions:
InstanceStateDisk:
properties:
total:
description: Total size in bytes. Uses -1 to convey that the instance has access to the entire pool's storage.
description: Total size in bytes. Uses 0 to convey that the instance has access to the entire pool's storage.
example: 502239232
format: int64
type: integer
x-go-name: Total
usage:
description: Disk usage in bytes. Uses -1 to indicate that the storage driver for the instance's pool does not support retrieving the disk usage.
description: Disk usage in bytes. Uses 0 to indicate that the storage driver for the pool does not support retrieving disk usage.
example: 502239232
format: int64
type: integer
Expand Down Expand Up @@ -6703,13 +6703,13 @@ definitions:
description: StorageVolumeStateUsage represents the disk usage of a volume
properties:
total:
description: Storage volume size in bytes
description: Storage volume size in bytes. Uses 0 to convey that the volume has access to the entire pool's storage.
example: 5189222192
format: int64
type: integer
x-go-name: Total
used:
description: Used space in bytes
description: Used space in bytes. Uses 0 to indicate that the storage driver for the pool does not support retrieving volume usage.
example: 1693552640
format: uint64
type: integer
Expand Down
5 changes: 4 additions & 1 deletion lxc/storage_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,10 @@ func (c *cmdStorageVolumeInfo) run(cmd *cobra.Command, args []string) error {
}

if volState != nil && volState.Usage != nil {
fmt.Printf(i18n.G("Usage: %s")+"\n", units.GetByteSizeStringIEC(int64(volState.Usage.Used), 2))
if volState.Usage.Used > 0 {
fmt.Printf(i18n.G("Usage: %s")+"\n", units.GetByteSizeStringIEC(int64(volState.Usage.Used), 2))
}

if volState.Usage.Total > 0 {
fmt.Printf(i18n.G("Total: %s")+"\n", units.GetByteSizeStringIEC(int64(volState.Usage.Total), 2))
}
Expand Down
25 changes: 11 additions & 14 deletions lxd/storage/backend_lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3264,13 +3264,15 @@ func (b *lxdBackend) GetInstanceUsage(inst instance.Instance) (*VolumeUsage, err

// Get the usage
// If storage driver does not support getting the volume usage, proceed getting the total.
size, err := b.driver.GetVolumeUsage(vol)
usedBytes, err := b.driver.GetVolumeUsage(vol)
if err != nil && !errors.Is(err, drivers.ErrNotSupported) {
return nil, err
}

// If driver does not support getting volume usage, this value would be -1.
val.Used = size
// If driver does not support getting volume usage, this value should be 0.
if usedBytes > 0 {
val.Used = usedBytes
}

// Get the total size.
_, rootDiskConf, err := instancetype.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
Expand All @@ -3290,14 +3292,6 @@ func (b *lxdBackend) GetInstanceUsage(inst instance.Instance) (*VolumeUsage, err
}
}

// If the instance volume is neither block based/typed nor bound by the device's size config key,
// this means it is only bound by the pool limits and has access to the entire pool storage.
// So instead of showing the entire pool size for each instance disk, we return -1 to signify the root disk
// is unbounded below the pool level.
if val.Total == 0 {
val.Total = -1
}

return &val, nil
}

Expand Down Expand Up @@ -6256,12 +6250,15 @@ func (b *lxdBackend) GetCustomVolumeUsage(projectName, volName string) (*VolumeU
vol := b.GetVolume(drivers.VolumeTypeCustom, drivers.ContentType(volume.ContentType), volStorageName, volume.Config)

// Get the usage.
size, err := b.driver.GetVolumeUsage(vol)
if err != nil {
usedBytes, err := b.driver.GetVolumeUsage(vol)
if err != nil && !errors.Is(err, drivers.ErrNotSupported) {
return nil, err
}

val.Used = size
// If retrieving usage is unsupported, Used should be 0.
if usedBytes > 0 {
val.Used = usedBytes
}

// Get the total size.
sizeStr, ok := vol.Config()["size"]
Expand Down
4 changes: 2 additions & 2 deletions shared/api/instance_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ type InstanceState struct {
//
// API extension: instances.
type InstanceStateDisk struct {
// Disk usage in bytes. Uses -1 to indicate that the storage driver for the instance's pool does not support retrieving the disk usage.
// Disk usage in bytes. Uses 0 to indicate that the storage driver for the pool does not support retrieving disk usage.
// Example: 502239232
Usage int64 `json:"usage" yaml:"usage"`

// Total size in bytes. Uses -1 to convey that the instance has access to the entire pool's storage.
// Total size in bytes. Uses 0 to convey that the instance has access to the entire pool's storage.
// Example: 502239232
//
// API extension: instances_state_total
Expand Down
6 changes: 3 additions & 3 deletions shared/api/storage_pool_volume_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type StorageVolumeState struct {
//
// API extension: storage_volume_state.
type StorageVolumeStateUsage struct {
// Used space in bytes
// Used space in bytes. Uses 0 to indicate that the storage driver for the pool does not support retrieving volume usage.
// Example: 1693552640
Used uint64 `json:"used,omitempty" yaml:"used,omitempty"`
Used uint64 `json:"used" yaml:"used"`

// Storage volume size in bytes
// Storage volume size in bytes. Uses 0 to convey that the volume has access to the entire pool's storage.
// Example: 5189222192
//
// API extension: storage_volume_state_total
Expand Down
11 changes: 9 additions & 2 deletions test/suites/storage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,19 @@ EOF

# Launch container.
lxc launch -s "${pool_name}" testimage c1
lxc storage volume create "${pool_name}" fsvol

# Disable quotas. The usage should be 0.
# shellcheck disable=SC2031
btrfs quota disable "${LXD_DIR}/storage-pools/${pool_name}"
# Usage -1 indicates the driver does not support getting instance usage.
[ "$(lxc query /1.0/instances/c1/state | jq '.disk.root.usage')" = "-1" ]
# Usage 0 indicates the driver does not support getting volume usage.
[ "$(lxc query /1.0/instances/c1/state | jq '.disk.root.usage')" = "0" ]
[ "$(lxc query "/1.0/storage-pools/${pool_name}/volumes/custom/fsvol/state" | jq '.usage.used')" = "0" ]
# Total 0 indicates the volume is unbound.
[ "$(lxc query /1.0/instances/c1/state | jq '.disk.root.total')" = "0" ]
[ "$(lxc query "/1.0/storage-pools/${pool_name}/volumes/custom/fsvol/state" | jq '.usage.total')" = "0" ]

lxc storage volume delete "${pool_name}" fsvol

# Enable quotas. The usage should then be > 0.
# shellcheck disable=SC2031
Expand Down
Loading