Better logging + runVM impl

This commit is contained in:
AlexSSD7 2023-08-25 16:54:58 +01:00
commit a63030fd00
7 changed files with 192 additions and 74 deletions

View file

@ -1,14 +1,14 @@
package cmd
import (
"context"
"fmt"
"log/slog"
"os"
"strconv"
"strings"
"sync"
"github.com/AlexSSD7/vldisk/vm"
"github.com/inconshreveable/log15"
"github.com/spf13/cobra"
)
@ -18,72 +18,24 @@ var lsCmd = &cobra.Command{
// Short: "",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
doRootCheck()
passthroughConfig := getDevicePassthroughConfig(args[0])
// TODO: We should download alpine image ourselves.
// TODO: ALSO, we need make it usable offline. We can't always download packages from the web.
// TODO: CLI-friendly logging.
vi, err := vm.NewInstance(log15.New(), "alpine-img/alpine.qcow2", []vm.USBDevicePassthroughConfig{passthroughConfig}, true)
if err != nil {
fmt.Printf("Failed to create VM instance: %v.\n", err)
os.Exit(1)
}
runErrCh := make(chan error, 1)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := vi.Run()
runErrCh <- err
}()
fm := vm.NewFileManager(vi)
for {
select {
case err := <-runErrCh:
fmt.Printf("Failed to run the VM: %v.\n", err)
runVM(args[0], func(ctx context.Context, i *vm.Instance, fm *vm.FileManager) {
lsblkOut, err := fm.Lsblk()
if err != nil {
slog.Error("Failed to list block devices in the VM", "error", err.Error())
os.Exit(1)
case <-vi.SSHUpNotifyChan():
err := fm.Init()
if err != nil {
fmt.Printf("Failed to initialize file manager: %v.\n", err)
os.Exit(1)
}
lsblkOut, err := fm.Lsblk()
if err != nil {
fmt.Printf("Failed to run list block devices in the VM: %v.\n", err)
os.Exit(1)
}
fmt.Print(string(lsblkOut))
err = vi.Cancel()
if err != nil {
fmt.Printf("Failed to cancel VM context: %v.\n", err)
os.Exit(1)
}
wg.Wait()
return nil
}
}
fmt.Print(string(lsblkOut))
})
return nil
},
}
func getDevicePassthroughConfig(val string) vm.USBDevicePassthroughConfig {
valSplit := strings.Split(val, ":")
if want, have := 2, len(valSplit); want != have {
fmt.Printf("Bad device passthrough syntax (wrong items split by ':' count: want %v, have %v).\n", want, have)
slog.Error("Bad device passthrough syntax", "error", fmt.Errorf("wrong items split by ':' count: want %v, have %v", want, have).Error())
os.Exit(1)
}
@ -91,19 +43,19 @@ func getDevicePassthroughConfig(val string) vm.USBDevicePassthroughConfig {
case "usb":
usbValsSplit := strings.Split(valSplit[1], ",")
if want, have := 2, len(usbValsSplit); want != have {
fmt.Printf("Bad USB device passthrough syntax (wrong args split by ',' count: want %v, have %v).\n", want, have)
slog.Error("Bad USB device passthrough syntax", "error", fmt.Errorf("wrong args split by ',' count: want %v, have %v", want, have).Error())
os.Exit(1)
}
usbBus, err := strconv.ParseUint(usbValsSplit[0], 10, 8)
if err != nil {
fmt.Printf("Bad USB device bus number '%v' (%v).\n", usbValsSplit[0], err)
slog.Error("Bad USB device bus number", "value", usbValsSplit[0])
os.Exit(1)
}
usbPort, err := strconv.ParseUint(usbValsSplit[1], 10, 8)
if err != nil {
fmt.Printf("Bad USB device port number '%v' (%v).\n", usbValsSplit[1], err)
slog.Error("Bad USB device port number", "value", usbValsSplit[1])
os.Exit(1)
}
@ -112,7 +64,7 @@ func getDevicePassthroughConfig(val string) vm.USBDevicePassthroughConfig {
HostPort: uint8(usbPort),
}
default:
fmt.Printf("Unknown device passthrough type '%v'.\n", valSplit[0])
slog.Error("Unknown device passthrough type", "value", valSplit[0])
os.Exit(1)
// This unreachable code is required to compile.
return vm.USBDevicePassthroughConfig{}