Better OS checks

This commit is contained in:
AlexSSD7 2023-09-02 11:28:23 +01:00
commit 58039acc3c
9 changed files with 48 additions and 29 deletions

View file

@ -4,10 +4,10 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"log/slog" "log/slog"
"github.com/AlexSSD7/linsk/osspecifics"
"github.com/spf13/cobra" "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) slog.Error("Failed to get user home directory, will use a local directory as a fallback", "error", err.Error(), "dir", defaultDataDir)
} else { } else {
homeDirName := ".linsk" homeDirName := ".linsk"
if runtime.GOOS == "windows" { if osspecifics.IsWindows() {
homeDirName = "Linsk" homeDirName = "Linsk"
} }

View file

@ -5,9 +5,9 @@ import (
"fmt" "fmt"
"log/slog" "log/slog"
"os" "os"
"runtime"
"strings" "strings"
"github.com/AlexSSD7/linsk/osspecifics"
"github.com/AlexSSD7/linsk/share" "github.com/AlexSSD7/linsk/share"
"github.com/AlexSSD7/linsk/vm" "github.com/AlexSSD7/linsk/vm"
"github.com/sethvargo/go-password/password" "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.") runCmd.Flags().BoolVar(&debugShellFlag, "debug-shell", false, "Start a VM shell when the network file share is active.")
var defaultShareType string var defaultShareType string
switch runtime.GOOS { switch {
case "windows": case osspecifics.IsWindows():
defaultShareType = "smb" defaultShareType = "smb"
case "darwin": case osspecifics.IsMacOS():
defaultShareType = "afp" defaultShareType = "afp"
default: default:
defaultShareType = "ftp" defaultShareType = "ftp"

View file

@ -4,9 +4,9 @@ import (
"context" "context"
"log/slog" "log/slog"
"os" "os"
"runtime"
"strings" "strings"
"github.com/AlexSSD7/linsk/osspecifics"
"github.com/AlexSSD7/linsk/share" "github.com/AlexSSD7/linsk/share"
"github.com/AlexSSD7/linsk/vm" "github.com/AlexSSD7/linsk/vm"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -94,7 +94,7 @@ func runVMShell(ctx context.Context, vi *vm.VM) error {
}() }()
termFDGetSize := termFD termFDGetSize := termFD
if runtime.GOOS == "windows" { if osspecifics.IsWindows() {
// Workaround for Windows. // Workaround for Windows.
termFDGetSize = int(os.Stdout.Fd()) termFDGetSize = int(os.Stdout.Fd())
} }

View file

@ -6,7 +6,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"strings" "strings"
"sync" "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 // 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. // much we can do about it from our side.
switch runtime.GOOS { switch {
case "windows": case osspecifics.IsWindows():
// TODO: To document: installation of libusbK driver with Zadig utility. // 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.") 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.") 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: case sig := <-interrupt:
lg := slog.With("signal", sig) lg := slog.With("signal", sig)
if i == 0 { switch {
case i == 0:
lg.Warn("Caught interrupt, safely shutting down") 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) lg.Warn("Caught subsequent interrupt, please interrupt n more times to panic", "n", 10-i)
} else { default:
panic("force interrupt") panic("force interrupt")
} }

View file

@ -8,13 +8,13 @@ import (
"os/exec" "os/exec"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
"log/slog" "log/slog"
"github.com/AlexSSD7/linsk/osspecifics"
"github.com/AlexSSD7/linsk/utils" "github.com/AlexSSD7/linsk/utils"
"github.com/AlexSSD7/linsk/vm" "github.com/AlexSSD7/linsk/vm"
"github.com/alessio/shellescape" "github.com/alessio/shellescape"
@ -76,7 +76,7 @@ func createQEMUImg(outPath string) error {
outPath = filepath.Clean(outPath) outPath = filepath.Clean(outPath)
baseCmd := "qemu-img" baseCmd := "qemu-img"
if runtime.GOOS == "windows" { if osspecifics.IsWindows() {
baseCmd += ".exe" baseCmd += ".exe"
} }

17
osspecifics/oschecks.go Normal file
View file

@ -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"
}

View file

@ -2,11 +2,12 @@ package share
import ( import (
"net" "net"
"runtime"
"github.com/AlexSSD7/linsk/osspecifics"
) )
func IsSMBExtModeDefault() bool { func IsSMBExtModeDefault() bool {
return runtime.GOOS == "windows" return osspecifics.IsWindows()
} }
var defaultListenIP = net.ParseIP("127.0.0.1") var defaultListenIP = net.ParseIP("127.0.0.1")

View file

@ -4,9 +4,9 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"runtime"
"strings" "strings"
"github.com/AlexSSD7/linsk/osspecifics"
"github.com/AlexSSD7/linsk/vm" "github.com/AlexSSD7/linsk/vm"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -60,15 +60,16 @@ func (b *SMBBackend) Apply(ctx context.Context, sharePWD string, vc *VMShareCont
} }
var shareURL string var shareURL string
if b.sharePort != nil { switch {
case b.sharePort != nil:
shareURL = "smb://" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(*b.sharePort)) + "/linsk" shareURL = "smb://" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(*b.sharePort)) + "/linsk"
} else if vc.NetTapCtx != nil { case vc.NetTapCtx != nil:
if runtime.GOOS == "windows" { if osspecifics.IsWindows() {
shareURL = `\\` + strings.ReplaceAll(vc.NetTapCtx.Net.GuestIP.String(), ":", "-") + ".ipv6-literal.net" + `\linsk` shareURL = `\\` + strings.ReplaceAll(vc.NetTapCtx.Net.GuestIP.String(), ":", "-") + ".ipv6-literal.net" + `\linsk`
} else { } else {
shareURL = "smb://" + net.JoinHostPort(vc.NetTapCtx.Net.GuestIP.String(), fmt.Sprint(smbPort)) + "/linsk" 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") return "", fmt.Errorf("no port forwarding and net tap configured")
} }

View file

@ -28,7 +28,7 @@ func getUniqueQEMUDriveID() string {
func cleanQEMUPath(s string) string { func cleanQEMUPath(s string) string {
path := filepath.Clean(s) 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 // QEMU doesn't work well with Windows backslashes, so we're replacing them to forward slashes
// that work perfectly fine. // that work perfectly fine.
path = strings.ReplaceAll(s, "\\", "/") path = strings.ReplaceAll(s, "\\", "/")
@ -40,7 +40,7 @@ func cleanQEMUPath(s string) string {
func configureBaseVMCmd(logger *slog.Logger, cfg VMConfig) (string, []qemucli.Arg, error) { func configureBaseVMCmd(logger *slog.Logger, cfg VMConfig) (string, []qemucli.Arg, error) {
baseCmd := "qemu-system" baseCmd := "qemu-system"
if runtime.GOOS == "windows" { if osspecifics.IsWindows() {
baseCmd += ".exe" baseCmd += ".exe"
} }
@ -51,14 +51,14 @@ func configureBaseVMCmd(logger *slog.Logger, cfg VMConfig) (string, []qemucli.Ar
} }
var accel string var accel string
switch runtime.GOOS { switch {
case "windows": case osspecifics.IsWindows():
// TODO: To document: For Windows, we need to install QEMU using an installer and add it to PATH. // 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". // 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. // 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. // This can be easily done with a program called Zadiag by Akeo.
accel = "whpx,kernel-irqchip=off" accel = "whpx,kernel-irqchip=off"
case "darwin": case osspecifics.IsMacOS():
accel = "hvf" accel = "hvf"
default: default:
accel = "kvm" accel = "kvm"