LUKS support + other things

This commit is contained in:
AlexSSD7 2023-08-25 19:55:11 +01:00
commit 4369fb82dd
8 changed files with 201 additions and 22 deletions

View file

@ -3,8 +3,10 @@ package cmd
import (
"context"
"os"
"os/signal"
"os/user"
"sync"
"syscall"
"log/slog"
@ -23,7 +25,7 @@ func checkIfRoot() (bool, error) {
func doRootCheck() {
ok, err := checkIfRoot()
if err != nil {
slog.Error("Failed to check whether the command is ran by root", "error", err.Error())
slog.Error("Failed to check whether the command is ran by root", "error", err)
os.Exit(1)
}
@ -39,9 +41,9 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.Fil
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)
vi, err := vm.NewInstance(slog.Default().With("caller", "vm"), "alpine-img/alpine.qcow2", []vm.USBDevicePassthroughConfig{passthroughConfig}, true)
if err != nil {
slog.Error("Failed to create vm instance", "error", err.Error())
slog.Error("Failed to create vm instance", "error", err)
os.Exit(1)
}
@ -49,6 +51,10 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.Fil
var wg sync.WaitGroup
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
interrupt := make(chan os.Signal, 2)
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
wg.Add(1)
go func() {
@ -59,17 +65,42 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.Fil
runErrCh <- err
}()
fm := vm.NewFileManager(vi)
go func() {
for i := 0; ; i++ {
select {
case <-ctx.Done():
signal.Reset()
return
case sig := <-interrupt:
lg := slog.With("signal", sig)
if i == 0 {
lg.Warn("Caught interrupt, safely shutting down")
} else if i < 10 {
lg.Warn("Caught subsequent interrupt, please interrupt n more times to panic", "n", 10-i)
} else {
panic("force interrupt")
}
err := vi.Cancel()
if err != nil {
lg.Warn("Failed to cancel VM context", "error", err)
}
}
}
}()
fm := vm.NewFileManager(slog.Default().With("caller", "file-manager"), vi)
for {
select {
case err := <-runErrCh:
slog.Error("Failed to start the VM", "error", err.Error())
slog.Error("Failed to start the VM", "error", err)
os.Exit(1)
case <-vi.SSHUpNotifyChan():
err := fm.Init()
if err != nil {
slog.Error("Failed to initialize File Manager", "error", err.Error())
slog.Error("Failed to initialize File Manager", "error", err)
os.Exit(1)
}
@ -77,7 +108,7 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.Fil
err = vi.Cancel()
if err != nil {
slog.Error("Failed to cancel VM context", "error", err.Error())
slog.Error("Failed to cancel VM context", "error", err)
os.Exit(1)
}
@ -86,7 +117,7 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.Fil
select {
case err := <-runErrCh:
if err != nil {
slog.Error("Failed to run the VM", "error", err.Error())
slog.Error("Failed to run the VM", "error", err)
os.Exit(1)
}
default: