From 1634dc8b4b28ea8733403033f8209b70742663b6 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Wed, 11 Sep 2024 06:58:06 +0000 Subject: [PATCH] fix: resize failure when cloning a volume with bigger size on Windows --- .../safe_mounter_host_process_windows.go | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/mounter/safe_mounter_host_process_windows.go b/pkg/mounter/safe_mounter_host_process_windows.go index 34fe7b4c08..0d5fd6c34f 100644 --- a/pkg/mounter/safe_mounter_host_process_windows.go +++ b/pkg/mounter/safe_mounter_host_process_windows.go @@ -233,8 +233,30 @@ func (mounter *winMounter) GetVolumeSizeInBytes(volumePath string) (int64, error } // ResizeVolume resizes the volume to the maximum available size. -func (mounter *winMounter) ResizeVolume(devicePath string) error { - return volume.ResizeVolume(devicePath, 0) +// source could be the disk number or the volume id. +func (mounter *winMounter) ResizeVolume(source string) error { + var volumeID string + + diskNum, err := strconv.Atoi(source) + if err == nil { + // List the volumes on the given disk. + volumeIds, err := volume.ListVolumesOnDisk(uint32(diskNum), 0) + if err != nil { + return err + } + if len(volumeIds) == 0 { + return fmt.Errorf("no volumes found on disk %d", diskNum) + } + + // TODO: consider partitions and choose the right partition. + // For now just choose the first volume. + volumeID = volumeIds[0] + } else { + klog.V(2).Infof("parse %s failed with error: %v", source, err) + volumeID = source + } + + return volume.ResizeVolume(volumeID, 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