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

View file

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

View file

@ -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())
}

View file

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

View file

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

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 (
"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")

View file

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

View file

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