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,10 +1,14 @@
package cmd
import (
"fmt"
"context"
"os"
"os/user"
"sync"
"log/slog"
"github.com/AlexSSD7/vldisk/vm"
"github.com/pkg/errors"
)
@ -19,12 +23,76 @@ func checkIfRoot() (bool, error) {
func doRootCheck() {
ok, err := checkIfRoot()
if err != nil {
fmt.Printf("Failed to check whether the command is ran by root: %v.\n", err)
slog.Error("Failed to check whether the command is ran by root", "error", err.Error())
os.Exit(1)
}
if !ok {
fmt.Printf("Root permissions are required.\n")
slog.Error("You must run this program as root")
os.Exit(1)
}
}
func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.FileManager)) *vm.Instance {
doRootCheck()
passthroughConfig := getDevicePassthroughConfig(passthroughArg)
// TODO: Alpine image should be downloaded from somewhere.
vi, err := vm.NewInstance(slog.Default(), "alpine-img/alpine.qcow2", []vm.USBDevicePassthroughConfig{passthroughConfig}, true)
if err != nil {
slog.Error("Failed to create vm instance", "error", err.Error())
os.Exit(1)
}
runErrCh := make(chan error, 1)
var wg sync.WaitGroup
ctx, ctxCancel := context.WithCancel(context.Background())
wg.Add(1)
go func() {
defer wg.Done()
err := vi.Run()
ctxCancel()
runErrCh <- err
}()
fm := vm.NewFileManager(vi)
for {
select {
case err := <-runErrCh:
slog.Error("Failed to start the VM", "error", err.Error())
os.Exit(1)
case <-vi.SSHUpNotifyChan():
err := fm.Init()
if err != nil {
slog.Error("Failed to initialize File Manager", "error", err.Error())
os.Exit(1)
}
fn(ctx, vi, fm)
err = vi.Cancel()
if err != nil {
slog.Error("Failed to cancel VM context", "error", err.Error())
os.Exit(1)
}
wg.Wait()
select {
case err := <-runErrCh:
if err != nil {
slog.Error("Failed to run the VM", "error", err.Error())
os.Exit(1)
}
default:
}
return nil
}
}
}