Some restructuring

This commit is contained in:
AlexSSD7 2023-08-27 13:44:57 +01:00
commit ee447087f6
8 changed files with 131 additions and 110 deletions

View file

@ -18,7 +18,7 @@ var lsCmd = &cobra.Command{
// Short: "",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
os.Exit(runVM(args[0], func(ctx context.Context, i *vm.Instance, fm *vm.FileManager) int {
os.Exit(runVM(args[0], func(ctx context.Context, i *vm.VM, fm *vm.FileManager) int {
lsblkOut, err := fm.Lsblk()
if err != nil {
slog.Error("Failed to list block devices in the VM", "error", err)

View file

@ -29,7 +29,7 @@ var runCmd = &cobra.Command{
// TODO: `slog` library prints entire stack traces for errors which makes reading errors challenging.
os.Exit(runVM(args[0], func(ctx context.Context, i *vm.Instance, fm *vm.FileManager) int {
os.Exit(runVM(args[0], func(ctx context.Context, i *vm.VM, fm *vm.FileManager) int {
err := fm.Mount(vmMountDevName, vm.MountOptions{
FSType: fsType,
LUKS: luksFlag,
@ -59,7 +59,7 @@ var runCmd = &cobra.Command{
<-ctx.Done()
return 0
}, []vm.PortForwardingConfig{{
}, []vm.PortForwardingRule{{
HostIP: net.ParseIP("127.0.0.1"), // TODO: Make this changeable.
HostPort: networkSharePort,
VMPort: 445,

View file

@ -23,23 +23,23 @@ var shellCmd = &cobra.Command{
passthroughArg = args[0]
}
var forwardPortsConfig []vm.PortForwardingConfig
var forwardPortRules []vm.PortForwardingRule
for i, fp := range strings.Split(forwardPortsFlagStr, ",") {
if fp == "" {
continue
}
fpc, err := vm.ParsePortForwardString(fp)
fpr, err := vm.ParsePortForwardString(fp)
if err != nil {
slog.Error("Failed to parse port forward string", "index", i, "value", fp, "error", err)
os.Exit(1)
}
forwardPortsConfig = append(forwardPortsConfig, fpc)
forwardPortRules = append(forwardPortRules, fpr)
}
os.Exit(runVM(passthroughArg, func(ctx context.Context, i *vm.Instance, fm *vm.FileManager) int {
os.Exit(runVM(passthroughArg, func(ctx context.Context, i *vm.VM, fm *vm.FileManager) int {
sc, err := i.DialSSH()
if err != nil {
slog.Error("Failed to dial VM SSH", "error", err)
@ -120,7 +120,7 @@ var shellCmd = &cobra.Command{
}
return 0
}, forwardPortsConfig, unrestrictedNetworkingFlag))
}, forwardPortRules, unrestrictedNetworkingFlag))
return nil
},

View file

@ -37,7 +37,7 @@ func doRootCheck() {
}
}
func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.FileManager) int, forwardPorts []vm.PortForwardingConfig, unrestrictedNetworking bool) int {
func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManager) int, forwardPortsRules []vm.PortForwardingRule, unrestrictedNetworking bool) int {
doRootCheck()
var passthroughConfig []vm.USBDevicePassthroughConfig
@ -46,8 +46,18 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.Fil
passthroughConfig = []vm.USBDevicePassthroughConfig{getDevicePassthroughConfig(passthroughArg)}
}
vmCfg := vm.VMConfig{
CdromImagePath: "alpine-img/alpine.qcow2",
USBDevices: passthroughConfig,
ExtraPortForwardingRules: forwardPortsRules,
DebugUnrestrictedNetworking: unrestrictedNetworking,
DebugShowDisplay: vmDebugFlag,
}
// TODO: Alpine image should be downloaded from somewhere.
vi, err := vm.NewInstance(slog.Default().With("caller", "vm"), "alpine-img/alpine.qcow2", passthroughConfig, vmDebugFlag, forwardPorts, unrestrictedNetworking)
vi, err := vm.NewVM(slog.Default().With("caller", "vm"), vmCfg)
if err != nil {
slog.Error("Failed to create vm instance", "error", err)
os.Exit(1)