Better logging + runVM impl
This commit is contained in:
parent
b905244626
commit
a63030fd00
7 changed files with 192 additions and 74 deletions
80
cmd/ls.go
80
cmd/ls.go
|
|
@ -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{}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package cmd
|
|||
import (
|
||||
"os"
|
||||
|
||||
"log/slog"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
|
@ -21,5 +23,8 @@ func Execute() {
|
|||
}
|
||||
|
||||
func init() {
|
||||
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, nil)))
|
||||
|
||||
rootCmd.AddCommand(lsCmd)
|
||||
rootCmd.AddCommand(runCmd)
|
||||
}
|
||||
|
|
|
|||
34
cmd/run.go
Normal file
34
cmd/run.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/AlexSSD7/vldisk/vm"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var runCmd = &cobra.Command{
|
||||
Use: "run",
|
||||
// TODO: Fill this
|
||||
// Short: "",
|
||||
Args: cobra.ExactArgs(3),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
vmMountDevName := args[1]
|
||||
fsType := args[2]
|
||||
|
||||
runVM(args[0], func(ctx context.Context, i *vm.Instance, fm *vm.FileManager) {
|
||||
err := fm.Mount(vmMountDevName, vm.MountOptions{FSType: fsType})
|
||||
if err != nil {
|
||||
slog.Error("Failed to mount the disk inside the VM", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Mounted! Now sleeping")
|
||||
<-ctx.Done()
|
||||
})
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
74
cmd/utils.go
74
cmd/utils.go
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue