From 1b69e6294d26961a2f8ee9f456f6f30c791d4ea3 Mon Sep 17 00:00:00 2001 From: AlexSSD7 Date: Sun, 3 Sep 2023 14:08:24 +0100 Subject: [PATCH] Windows fixes --- nettap/impl_windows.go | 6 ++++-- osspecifics/osspecifics_windows.go | 4 ++-- qemucli/args.go | 2 +- qemucli/kv_arg.go | 5 ----- qemucli/validation.go | 4 ++++ vm/cfg.go | 27 ++++++++++++++++++--------- vm/filemanager.go | 2 +- 7 files changed, 30 insertions(+), 20 deletions(-) diff --git a/nettap/impl_windows.go b/nettap/impl_windows.go index b50a6c4..c9630fb 100644 --- a/nettap/impl_windows.go +++ b/nettap/impl_windows.go @@ -28,11 +28,12 @@ import ( "strings" "time" + "log/slog" + "github.com/AlexSSD7/linsk/utils" "github.com/alessio/shellescape" "github.com/google/uuid" "github.com/pkg/errors" - "golang.org/x/exp/slog" ) func Available() bool { @@ -66,7 +67,8 @@ func NewTapManager(logger *slog.Logger) (*TapManager, error) { var tapNameRegexp = regexp.MustCompile(`^LinskTap-\d+$`) func NewUniqueTapName() (string, error) { - return fmt.Sprintf("LinskTap-%v", time.Now().UnixNano()) + time.Sleep(time.Millisecond) + return fmt.Sprintf("LinskTap-%v", time.Now().UnixNano()), nil } func (tm *TapManager) CreateNewTap(tapName string) error { diff --git a/osspecifics/osspecifics_windows.go b/osspecifics/osspecifics_windows.go index 5858e2e..d6c6d55 100644 --- a/osspecifics/osspecifics_windows.go +++ b/osspecifics/osspecifics_windows.go @@ -40,8 +40,8 @@ func TerminateProcess(pid int) error { return exec.Command("TASKKILL", "/T", "/F", "/PID", fmt.Sprint(pid)).Run() } -var physicalDriveCheckRegexp = regexp.MustCompile(`^\\\\.\\PhysicalDrive(\d+)$`) -var physicalDriveFindRegexp = regexp.MustCompile(`PhysicalDrive(\d+)`) +var physicalDriveCheckRegexp = regexp.MustCompile(`(?i)^\\\\.\\PhysicalDrive(\d+)$`) +var physicalDriveFindRegexp = regexp.MustCompile(`(?i)PhysicalDrive(\d+)`) // This is never used except for a band-aid that would check // that there are no double-mounts. diff --git a/qemucli/args.go b/qemucli/args.go index 4c9e2a4..edb2af4 100644 --- a/qemucli/args.go +++ b/qemucli/args.go @@ -34,7 +34,7 @@ const ( ) var safeArgs = map[string]ArgAcceptedValue{ - "accel": ArgAcceptedValueString, + "accel": ArgAcceptedValueKeyValue, "boot": ArgAcceptedValueString, "m": ArgAcceptedValueUint, "smp": ArgAcceptedValueUint, diff --git a/qemucli/kv_arg.go b/qemucli/kv_arg.go index 0aaa9a8..5ba427c 100644 --- a/qemucli/kv_arg.go +++ b/qemucli/kv_arg.go @@ -69,11 +69,6 @@ func NewKeyValueArg(key string, items []KeyValueArgItem) (*KeyValueArg, error) { return nil, fmt.Errorf("empty key not allowed") } - if len(item.Value) == 0 { - // Values *can* be empty, though. We do not allow them for consistency. - return nil, fmt.Errorf("empty value for key '%v' is not allowed", item.Key) - } - err := validateArgStrValue(item.Key) if err != nil { return nil, errors.Wrapf(err, "validate key '%v'", item.Key) diff --git a/qemucli/validation.go b/qemucli/validation.go index 976bbf5..9689dc0 100644 --- a/qemucli/validation.go +++ b/qemucli/validation.go @@ -45,5 +45,9 @@ func validateArgStrValue(s string) error { return fmt.Errorf("backslashes are not allowed") } + if strings.Contains(s, "=") { + return fmt.Errorf("equals sign is not allowed") + } + return nil } diff --git a/vm/cfg.go b/vm/cfg.go index e6eb51c..74f5872 100644 --- a/vm/cfg.go +++ b/vm/cfg.go @@ -35,10 +35,12 @@ import ( ) func getUniqueQEMUNetID() string { + time.Sleep(time.Millisecond) return "net" + utils.IntToStr(time.Now().UnixNano()) } func getUniqueQEMUDriveID() string { + time.Sleep(time.Millisecond) return "drive" + utils.IntToStr(time.Now().UnixNano()) } @@ -56,28 +58,31 @@ func cleanQEMUPath(s string) string { func configureBaseVMCmd(logger *slog.Logger, cfg Config) (string, []qemucli.Arg, error) { baseCmd := "qemu-system" - if osspecifics.IsWindows() { - baseCmd += ".exe" - } - args := []qemucli.Arg{ qemucli.MustNewStringArg("serial", "stdio"), qemucli.MustNewUintArg("m", cfg.MemoryAlloc), qemucli.MustNewUintArg("smp", runtime.NumCPU()), } - var accel string + var accel []qemucli.KeyValueArgItem switch { case osspecifics.IsWindows(): // TODO: To document: For Windows, we need to install QEMU using an installer and add it to PATH. // Then, we should enable Windows Hypervisor Platform in "Turn Windows features on or off". // IMPORTANT: We should also install libusbK drivers for USB devices we want to pass through. // This can be easily done with a program called Zadiag by Akeo. - accel = "whpx,kernel-irqchip=off" + accel = []qemucli.KeyValueArgItem{ + {Key: "whpx"}, + {Key: "kernel-irqchip", Value: "off"}, + } case osspecifics.IsMacOS(): - accel = "hvf" + accel = []qemucli.KeyValueArgItem{{ + Key: "hvf", + }} default: - accel = "kvm" + accel = []qemucli.KeyValueArgItem{{ + Key: "kvm", + }} } switch runtime.GOARCH { @@ -102,7 +107,7 @@ func configureBaseVMCmd(logger *slog.Logger, cfg Config) (string, []qemucli.Arg, return "", nil, fmt.Errorf("arch '%v' is not supported", runtime.GOARCH) } - args = append(args, qemucli.MustNewStringArg("accel", accel)) + args = append(args, qemucli.MustNewKeyValueArg("accel", accel)) if cfg.BIOSPath != "" { biosPath := cleanQEMUPath(cfg.BIOSPath) @@ -130,6 +135,10 @@ func configureBaseVMCmd(logger *slog.Logger, cfg Config) (string, []qemucli.Arg, args = append(args, cdromArg, qemucli.MustNewStringArg("boot", "d")) } + if osspecifics.IsWindows() { + baseCmd += ".exe" + } + return baseCmd, args, nil } diff --git a/vm/filemanager.go b/vm/filemanager.go index 5e70a38..e69998b 100644 --- a/vm/filemanager.go +++ b/vm/filemanager.go @@ -113,7 +113,7 @@ func (fm *FileManager) luksOpen(sc *ssh.Client, fullDevPath string) error { return errors.Wrap(err, "write prompt to stderr") } - pwd, err := term.ReadPassword(syscall.Stdin) + pwd, err := term.ReadPassword(int(syscall.Stdin)) if err != nil { return errors.Wrap(err, "read luks password") }