Skip to content

Commit

Permalink
fix: refine GetFreeSpace call on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Apr 14, 2024
1 parent 3674a7f commit 4a1656c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 38 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
go.uber.org/mock v0.4.0
golang.org/x/net v0.22.0
golang.org/x/sync v0.6.0
golang.org/x/sys v0.18.0
google.golang.org/grpc v1.61.1
google.golang.org/protobuf v1.33.0
k8s.io/api v0.29.3
Expand Down Expand Up @@ -134,7 +135,6 @@ require (
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions pkg/azuredisk/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ func (d *Driver) NodeExpandVolume(_ context.Context, req *csi.NodeExpandVolumeRe
klog.Errorf("%v, will continue checking whether the volume has been resized", retErr)
}

if runtime.GOOS == "windows" && d.enableWindowsHostProcess {
// in windows host process mode, this driver could get the volume size from the volume path
devicePath = volumePath
}
gotBlockSizeBytes, err := getBlockSizeBytes(devicePath, d.mounter)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("could not get size of block volume at path %s: %v", devicePath, err))
Expand Down
33 changes: 20 additions & 13 deletions pkg/mounter/safe_mounter_host_process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"strings"

"github.com/container-storage-interface/spec/lib/go/csi"
"golang.org/x/sys/windows"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
mount "k8s.io/mount-utils"

Expand Down Expand Up @@ -224,32 +227,36 @@ func (mounter *winMounter) GetDeviceNameFromMount(mountPath, pluginMountDir stri
}

// GetVolumeSizeInBytes returns the size of the volume in bytes.
func (mounter *winMounter) GetVolumeSizeInBytes(devicePath string) (int64, error) {
volumeSize, _, err := volume.GetVolumeStats(devicePath)
return volumeSize, err
func (mounter *winMounter) GetVolumeSizeInBytes(volumePath string) (int64, error) {
_, totalBytes, _, err := GetFreeSpace(volumePath)
return totalBytes, err
}

// ResizeVolume resizes the volume to the maximum available size.
func (mounter *winMounter) ResizeVolume(devicePath string) error {
return volume.ResizeVolume(devicePath, 0)
}

// GetFreeSpace returns the free space of the volume in bytes, total size of the volume in bytes and the used space of the volume in bytes
func GetFreeSpace(path string) (int64, int64, int64, error) {
var totalNumberOfBytes, totalNumberOfFreeBytes uint64
dirName := windows.StringToUTF16Ptr(path)
err := windows.GetDiskFreeSpaceEx(dirName, nil, &totalNumberOfBytes, &totalNumberOfFreeBytes)
return int64(totalNumberOfFreeBytes), int64(totalNumberOfBytes), int64(totalNumberOfBytes - totalNumberOfFreeBytes), err
}

// GetVolumeStats get volume usage
func (mounter *winMounter) GetVolumeStats(ctx context.Context, path string) (*csi.VolumeUsage, error) {
volumeID, err := volume.GetVolumeIDFromTargetPath(path)
freeBytesAvailable, totalBytes, totalBytesUsed, err := GetFreeSpace(path)
if err != nil {
return nil, fmt.Errorf("GetVolumeIDFromMount(%s) failed with error: %v", path, err)
}
klog.V(6).Infof("GetVolumeStats(%s) returned volumeID(%s)", path, volumeID)
volumeSize, volumeUsedSize, err := volume.GetVolumeStats(volumeID)
if err != nil {
return nil, fmt.Errorf("GetVolumeStats(%s) failed with error: %v", volumeID, err)
return nil, status.Errorf(codes.Internal, "failed to get free space on path %s: %v", path, err)
}

volUsage := &csi.VolumeUsage{
Unit: csi.VolumeUsage_BYTES,
Available: volumeSize - volumeUsedSize,
Total: volumeSize,
Used: volumeUsedSize,
Available: freeBytesAvailable,
Total: totalBytes,
Used: totalBytesUsed,
}
return volUsage, nil
}
24 changes: 0 additions & 24 deletions pkg/os/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,30 +177,6 @@ func ResizeVolume(volumeID string, size int64) error {
return nil
}

// GetVolumeStats - retrieves the volume stats for a given volume
func GetVolumeStats(volumeID string) (int64, int64, error) {
// get the size and sizeRemaining for the volume
cmd := "(Get-Volume -UniqueId \"$Env:volumeID\" | Select SizeRemaining,Size) | ConvertTo-Json"
out, err := azureutils.RunPowershellCmd(cmd, fmt.Sprintf("volumeID=%s", volumeID))

if err != nil {
return -1, -1, fmt.Errorf("error getting capacity and used size of volume. cmd: %s, output: %s, error: %v", cmd, string(out), err)
}

var getVolume map[string]int64
outString := string(out)
err = json.Unmarshal([]byte(outString), &getVolume)
if err != nil {
return -1, -1, fmt.Errorf("out %v outstring %v err %v", out, outString, err)
}

volumeSize := getVolume["Size"]
volumeSizeRemaining := getVolume["SizeRemaining"]

volumeUsedSize := volumeSize - volumeSizeRemaining
return volumeSize, volumeUsedSize, nil
}

// GetDiskNumberFromVolumeID - gets the disk number where the volume is.
func GetDiskNumberFromVolumeID(volumeID string) (uint32, error) {
// get the size and sizeRemaining for the volume
Expand Down

0 comments on commit 4a1656c

Please sign in to comment.