LUKS support + other things
This commit is contained in:
parent
cbc12f1690
commit
4369fb82dd
8 changed files with 201 additions and 22 deletions
|
|
@ -21,7 +21,7 @@ var lsCmd = &cobra.Command{
|
|||
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())
|
||||
slog.Error("Failed to list block devices in the VM", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ var lsCmd = &cobra.Command{
|
|||
func getDevicePassthroughConfig(val string) vm.USBDevicePassthroughConfig {
|
||||
valSplit := strings.Split(val, ":")
|
||||
if want, have := 2, len(valSplit); want != have {
|
||||
slog.Error("Bad device passthrough syntax", "error", fmt.Errorf("wrong items split by ':' count: want %v, have %v", want, have).Error())
|
||||
slog.Error("Bad device passthrough syntax", "error", fmt.Errorf("wrong items split by ':' count: want %v, have %v", want, have))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
|
@ -43,7 +43,7 @@ func getDevicePassthroughConfig(val string) vm.USBDevicePassthroughConfig {
|
|||
case "usb":
|
||||
usbValsSplit := strings.Split(valSplit[1], ",")
|
||||
if want, have := 2, len(usbValsSplit); want != have {
|
||||
slog.Error("Bad USB device passthrough syntax", "error", fmt.Errorf("wrong args split by ',' count: want %v, have %v", want, have).Error())
|
||||
slog.Error("Bad USB device passthrough syntax", "error", fmt.Errorf("wrong args split by ',' count: want %v, have %v", want, have))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
|
|
|||
13
cmd/run.go
13
cmd/run.go
|
|
@ -18,8 +18,13 @@ var runCmd = &cobra.Command{
|
|||
vmMountDevName := args[1]
|
||||
fsType := args[2]
|
||||
|
||||
// TODO: `slog` library prints entire stack traces for errors which makes reading errors challenging.
|
||||
|
||||
runVM(args[0], func(ctx context.Context, i *vm.Instance, fm *vm.FileManager) {
|
||||
err := fm.Mount(vmMountDevName, vm.MountOptions{FSType: fsType})
|
||||
err := fm.Mount(vmMountDevName, vm.MountOptions{
|
||||
FSType: fsType,
|
||||
LUKS: luksFlag,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("Failed to mount the disk inside the VM", "error", err)
|
||||
return
|
||||
|
|
@ -32,3 +37,9 @@ var runCmd = &cobra.Command{
|
|||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var luksFlag bool
|
||||
|
||||
func init() {
|
||||
runCmd.Flags().BoolVarP(&luksFlag, "luks", "l", false, "Use cryptsetup to open a LUKS volume (password will be prompted)")
|
||||
}
|
||||
|
|
|
|||
47
cmd/utils.go
47
cmd/utils.go
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue