| 1 | |
| 2 | |
| 3 | |
| 4 | package windows |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "fmt" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | var vmIDRegex = regexp.MustCompile(`--vm-id\s\{(?P<vmID>.{36})\}`) |
| 15 | |
| 16 | |
| 17 | func GetInstanceVMID(ctx context.Context, instanceName string) (string, error) { |
| 18 | distroID, err := GetDistroID(instanceName) |
| 19 | if err != nil { |
| 20 | return "", err |
| 21 | } |
| 22 | |
| 23 | cmdLines, err := GetProcessCommandLine(ctx, "wslhost.exe") |
| 24 | if err != nil { |
| 25 | return "", err |
| 26 | } |
| 27 | |
| 28 | vmID := "" |
| 29 | for _, cmdLine := range cmdLines { |
| 30 | if strings.Contains(cmdLine, distroID) { |
| 31 | if matches := vmIDRegex.FindStringSubmatch(cmdLine); matches != nil { |
| 32 | vmID = matches[vmIDRegex.SubexpIndex("vmID")] |
| 33 | break |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if vmID == "" { |
| 39 | return "", fmt.Errorf("failed to find VM ID for instance %#q", instanceName) |
| 40 | } |
| 41 | |
| 42 | return vmID, nil |
| 43 | } |