Skip to content

Commit 34113da

Browse files
committed
Fix minikube load on windows (#20529)
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 804c01a commit 34113da

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

pkg/minikube/localpath/localpath.go

Lines changed: 44 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,55 @@ 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+
func getWindowsVolumeNameCmd(d string) (string, error) {
223+
psPath, err := findPowerShell()
224+
if err != nil {
225+
return "", fmt.Errorf("failed to locate PowerShell: %w", err)
226+
}
227+
228+
psCommand := `Get-CimInstance -ClassName Win32_Volume -Filter "DriveLetter = '` + d + `:'" | Select-Object -ExpandProperty DeviceID`
229+
230+
cmd := exec.Command(psPath, "-NoProfile", "-NonInteractive", "-Command", psCommand)
231+
232+
var out bytes.Buffer
233+
cmd.Stdout = &out
234+
235+
err = cmd.Run()
236+
if err != nil {
237+
return "", fmt.Errorf("PowerShell command failed: %w", err)
238+
}
239+
240+
vname := strings.TrimSpace(out.String())
241+
if !strings.HasPrefix(vname, `\\?\Volume{`) || !strings.HasSuffix(vname, `}\`) {
242+
return "", fmt.Errorf("failed to get a volume GUID, got: %s", vname)
213243
}
214244

215245
return vname, nil

0 commit comments

Comments
 (0)