linsk/vm/os_specifics_windows.go

49 lines
1.3 KiB
Go
Raw Normal View History

2023-08-28 11:35:57 +02:00
// go:build windows
package vm
import (
"fmt"
"os/exec"
2023-08-31 21:01:45 +01:00
"regexp"
"strings"
2023-08-28 11:35:57 +02:00
"syscall"
2023-08-31 21:01:45 +01:00
"github.com/pkg/errors"
2023-08-28 11:35:57 +02:00
)
func prepareVMCmd(cmd *exec.Cmd) {
2023-08-29 14:24:18 +01:00
// This is to prevent Ctrl+C propagating to the child process.
2023-08-28 11:35:57 +02:00
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
}
func terminateProcess(pid int) error {
return exec.Command("TASKKILL", "/T", "/F", "/PID", fmt.Sprint(pid)).Run()
}
2023-08-30 14:59:27 +02:00
2023-08-31 21:01:45 +01:00
var physicalDriveRegexp = regexp.MustCompile(`PhysicalDrive(\d+)`)
// This is never used except for a band-aid that would check
// that there are no double-mounts.
2023-08-30 14:59:27 +02:00
func checkDeviceSeemsMounted(path string) (bool, error) {
2023-08-31 21:01:45 +01:00
// Quite a bit hacky implementation, but it's to be used as a failsafe band-aid anyway.
matches := physicalDriveRegexp.FindAllStringSubmatch(path, 1)
if len(matches) == 0 {
return false, fmt.Errorf("bad device path '%v'", path)
}
match := matches[0]
if want, have := 2, len(match); want != have {
return false, fmt.Errorf("bad match items length: want %v, have %v (%v)", want, have, match)
}
out, err := exec.Command("wmic", "path", "Win32_LogicalDiskToPartition", "get", "Antecedent").Output()
if err != nil {
return false, errors.Wrap(err, "exec wmic cmd")
}
return strings.Contains(string(out), fmt.Sprintf("Disk #%v", match[1])), nil
2023-08-30 14:59:27 +02:00
}