From 58039acc3cf5945f0b23de03c1231e966457c7e6 Mon Sep 17 00:00:00 2001 From: AlexSSD7 Date: Sat, 2 Sep 2023 11:28:23 +0100 Subject: [PATCH] Better OS checks --- cmd/root.go | 4 ++-- cmd/run.go | 8 ++++---- cmd/shell.go | 4 ++-- cmd/utils.go | 14 +++++++------- imgbuilder/imgbuilder.go | 4 ++-- osspecifics/oschecks.go | 17 +++++++++++++++++ share/defaults.go | 5 +++-- share/smb.go | 11 ++++++----- vm/cfg.go | 10 +++++----- 9 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 osspecifics/oschecks.go diff --git a/cmd/root.go b/cmd/root.go index b3cb0e1..0a1ec03 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,10 +4,10 @@ import ( "fmt" "os" "path/filepath" - "runtime" "log/slog" + "github.com/AlexSSD7/linsk/osspecifics" "github.com/spf13/cobra" ) @@ -64,7 +64,7 @@ func init() { slog.Error("Failed to get user home directory, will use a local directory as a fallback", "error", err.Error(), "dir", defaultDataDir) } else { homeDirName := ".linsk" - if runtime.GOOS == "windows" { + if osspecifics.IsWindows() { homeDirName = "Linsk" } diff --git a/cmd/run.go b/cmd/run.go index 82b467e..cfa254d 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -5,9 +5,9 @@ import ( "fmt" "log/slog" "os" - "runtime" "strings" + "github.com/AlexSSD7/linsk/osspecifics" "github.com/AlexSSD7/linsk/share" "github.com/AlexSSD7/linsk/vm" "github.com/sethvargo/go-password/password" @@ -126,10 +126,10 @@ func init() { runCmd.Flags().BoolVar(&debugShellFlag, "debug-shell", false, "Start a VM shell when the network file share is active.") var defaultShareType string - switch runtime.GOOS { - case "windows": + switch { + case osspecifics.IsWindows(): defaultShareType = "smb" - case "darwin": + case osspecifics.IsMacOS(): defaultShareType = "afp" default: defaultShareType = "ftp" diff --git a/cmd/shell.go b/cmd/shell.go index a510418..708a263 100644 --- a/cmd/shell.go +++ b/cmd/shell.go @@ -4,9 +4,9 @@ import ( "context" "log/slog" "os" - "runtime" "strings" + "github.com/AlexSSD7/linsk/osspecifics" "github.com/AlexSSD7/linsk/share" "github.com/AlexSSD7/linsk/vm" "github.com/pkg/errors" @@ -94,7 +94,7 @@ func runVMShell(ctx context.Context, vi *vm.VM) error { }() termFDGetSize := termFD - if runtime.GOOS == "windows" { + if osspecifics.IsWindows() { // Workaround for Windows. termFDGetSize = int(os.Stdout.Fd()) } diff --git a/cmd/utils.go b/cmd/utils.go index cc0d0ea..a7e8c44 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -6,7 +6,6 @@ import ( "os" "os/signal" "path/filepath" - "runtime" "strconv" "strings" "sync" @@ -74,11 +73,11 @@ func runVM(passthroughArg string, fn runVMFunc, forwardPortsRules []vm.PortForwa // libusbK driver, which nullifies the UX. This is a problem with how QEMU works, and unfortunately there isn't // much we can do about it from our side. - switch runtime.GOOS { - case "windows": + switch { + case osspecifics.IsWindows(): // TODO: To document: installation of libusbK driver with Zadig utility. slog.Warn("USB passthrough is unstable on Windows and requires installation of libusbK driver. Please consider using raw block device passthrough instead.") - case "darwin": + case osspecifics.IsMacOS(): slog.Warn("USB passthrough is unstable on macOS. Please consider using raw block device passthrough instead.") } } @@ -235,11 +234,12 @@ func innerRunVM(vmCfg vm.VMConfig, tapRuntimeCtx *share.NetTapRuntimeContext, fn case sig := <-interrupt: lg := slog.With("signal", sig) - if i == 0 { + switch { + case i == 0: lg.Warn("Caught interrupt, safely shutting down") - } else if i < 10 { + case i < 10: lg.Warn("Caught subsequent interrupt, please interrupt n more times to panic", "n", 10-i) - } else { + default: panic("force interrupt") } diff --git a/imgbuilder/imgbuilder.go b/imgbuilder/imgbuilder.go index e608388..a4d8725 100644 --- a/imgbuilder/imgbuilder.go +++ b/imgbuilder/imgbuilder.go @@ -8,13 +8,13 @@ import ( "os/exec" "os/signal" "path/filepath" - "runtime" "strings" "sync" "syscall" "log/slog" + "github.com/AlexSSD7/linsk/osspecifics" "github.com/AlexSSD7/linsk/utils" "github.com/AlexSSD7/linsk/vm" "github.com/alessio/shellescape" @@ -76,7 +76,7 @@ func createQEMUImg(outPath string) error { outPath = filepath.Clean(outPath) baseCmd := "qemu-img" - if runtime.GOOS == "windows" { + if osspecifics.IsWindows() { baseCmd += ".exe" } diff --git a/osspecifics/oschecks.go b/osspecifics/oschecks.go new file mode 100644 index 0000000..5d9cb8c --- /dev/null +++ b/osspecifics/oschecks.go @@ -0,0 +1,17 @@ +package osspecifics + +import "runtime" + +// For some reason, `runtime` package does not provide this while +// "goconst" linter complains about us not using constants in +// expressions like `runtime.GOOS == "windows"`. And it is +// not wrong, accidentally mispelling these OS IDs is a +// matter of time. + +func IsWindows() bool { + return runtime.GOOS == "windows" +} + +func IsMacOS() bool { + return runtime.GOOS == "darwin" +} diff --git a/share/defaults.go b/share/defaults.go index 0feb327..00f194e 100644 --- a/share/defaults.go +++ b/share/defaults.go @@ -2,11 +2,12 @@ package share import ( "net" - "runtime" + + "github.com/AlexSSD7/linsk/osspecifics" ) func IsSMBExtModeDefault() bool { - return runtime.GOOS == "windows" + return osspecifics.IsWindows() } var defaultListenIP = net.ParseIP("127.0.0.1") diff --git a/share/smb.go b/share/smb.go index 65819d2..c3f1533 100644 --- a/share/smb.go +++ b/share/smb.go @@ -4,9 +4,9 @@ import ( "context" "fmt" "net" - "runtime" "strings" + "github.com/AlexSSD7/linsk/osspecifics" "github.com/AlexSSD7/linsk/vm" "github.com/pkg/errors" ) @@ -60,15 +60,16 @@ func (b *SMBBackend) Apply(ctx context.Context, sharePWD string, vc *VMShareCont } var shareURL string - if b.sharePort != nil { + switch { + case b.sharePort != nil: shareURL = "smb://" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(*b.sharePort)) + "/linsk" - } else if vc.NetTapCtx != nil { - if runtime.GOOS == "windows" { + case vc.NetTapCtx != nil: + if osspecifics.IsWindows() { shareURL = `\\` + strings.ReplaceAll(vc.NetTapCtx.Net.GuestIP.String(), ":", "-") + ".ipv6-literal.net" + `\linsk` } else { shareURL = "smb://" + net.JoinHostPort(vc.NetTapCtx.Net.GuestIP.String(), fmt.Sprint(smbPort)) + "/linsk" } - } else { + default: return "", fmt.Errorf("no port forwarding and net tap configured") } diff --git a/vm/cfg.go b/vm/cfg.go index d239a40..1692634 100644 --- a/vm/cfg.go +++ b/vm/cfg.go @@ -28,7 +28,7 @@ func getUniqueQEMUDriveID() string { func cleanQEMUPath(s string) string { path := filepath.Clean(s) - if runtime.GOOS == "windows" { + if osspecifics.IsWindows() { // QEMU doesn't work well with Windows backslashes, so we're replacing them to forward slashes // that work perfectly fine. path = strings.ReplaceAll(s, "\\", "/") @@ -40,7 +40,7 @@ func cleanQEMUPath(s string) string { func configureBaseVMCmd(logger *slog.Logger, cfg VMConfig) (string, []qemucli.Arg, error) { baseCmd := "qemu-system" - if runtime.GOOS == "windows" { + if osspecifics.IsWindows() { baseCmd += ".exe" } @@ -51,14 +51,14 @@ func configureBaseVMCmd(logger *slog.Logger, cfg VMConfig) (string, []qemucli.Ar } var accel string - switch runtime.GOOS { - case "windows": + 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" - case "darwin": + case osspecifics.IsMacOS(): accel = "hvf" default: accel = "kvm"