Skip to content

Commit d2d611c

Browse files
james-worldpavansaikrishna78
authored andcommitted
Fix minikube load on windows (kubernetes#20529) (kubernetes#20921)
getWindowsVolumeNameCmd uses the deprecated wmic.exe. See https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmic for details. Replace the implementation with a call to the Powershell equivalent. Look in path and system environment vars to locate Powershell. Improve Powershell discovery
1 parent 42ed020 commit d2d611c

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

pkg/minikube/localpath/localpath.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package localpath
1818

1919
import (
20+
"bytes"
21+
"fmt"
2022
"os"
2123
"os/exec"
2224
"path"
@@ -189,27 +191,56 @@ func replaceWinDriveLetterToVolumeName(s string) (string, error) {
189191
return p, nil
190192
}
191193

192-
func getWindowsVolumeNameCmd(d string) (string, error) {
193-
cmd := exec.Command("wmic", "volume", "where", "DriveLetter = '"+d+":'", "get", "DeviceID")
194+
// findPowerShell locates the PowerShell executable
195+
func findPowerShell() (string, error) {
196+
// First try to find powershell.exe in PATH
197+
if ps, err := exec.LookPath("powershell.exe"); err == nil {
198+
return ps, nil
199+
}
194200

195-
stdout, err := cmd.Output()
196-
if err != nil {
197-
return "", err
201+
// Fallback to using SystemRoot environment variable
202+
systemRoot := os.Getenv("SystemRoot")
203+
if systemRoot == "" {
204+
systemRoot = "C:\\Windows"
198205
}
199206

200-
outs := strings.Split(strings.ReplaceAll(string(stdout), "\r", ""), "\n")
207+
// Try common PowerShell locations
208+
locations := []string{
209+
filepath.Join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe"),
210+
filepath.Join(systemRoot, "SysWOW64", "WindowsPowerShell", "v1.0", "powershell.exe"),
211+
}
201212

202-
var vname string
203-
for _, l := range outs {
204-
s := strings.TrimSpace(l)
205-
if strings.HasPrefix(s, `\\?\Volume{`) && strings.HasSuffix(s, `}\`) {
206-
vname = s
207-
break
213+
for _, loc := range locations {
214+
if _, err := os.Stat(loc); err == nil {
215+
return loc, nil
208216
}
209217
}
210218

211-
if vname == "" {
212-
return "", errors.New("failed to get a volume GUID")
219+
return "", fmt.Errorf("PowerShell not found in PATH or common locations")
220+
}
221+
222+
// getWindowsVolumeNameCmd returns the Windows volume GUID for a given drive letter
223+
func getWindowsVolumeNameCmd(d string) (string, error) {
224+
psPath, err := findPowerShell()
225+
if err != nil {
226+
return "", fmt.Errorf("failed to locate PowerShell: %w", err)
227+
}
228+
229+
psCommand := `Get-CimInstance -ClassName Win32_Volume -Filter "DriveLetter = '` + d + `:'" | Select-Object -ExpandProperty DeviceID`
230+
231+
cmd := exec.Command(psPath, "-NoProfile", "-NonInteractive", "-Command", psCommand)
232+
233+
var out bytes.Buffer
234+
cmd.Stdout = &out
235+
236+
err = cmd.Run()
237+
if err != nil {
238+
return "", fmt.Errorf("PowerShell command failed: %w", err)
239+
}
240+
241+
vname := strings.TrimSpace(out.String())
242+
if !strings.HasPrefix(vname, `\\?\Volume{`) || !strings.HasSuffix(vname, `}\`) {
243+
return "", fmt.Errorf("failed to get a volume GUID, got: %s", vname)
213244
}
214245

215246
return vname, nil

0 commit comments

Comments
 (0)