Many minor fixes/improvements

This commit is contained in:
AlexSSD7 2023-08-29 10:59:50 +01:00
commit a47b4fc0ec
9 changed files with 53 additions and 39 deletions

View file

@ -88,7 +88,7 @@ func (bc *BuildContext) BuildWithInterruptHandler() error {
defer func() {
err := bc.vi.Cancel()
if err != nil {
bc.logger.Error("Failed to cancel VM context", "error", err)
bc.logger.Error("Failed to cancel VM context", "error", err.Error())
}
}()
@ -129,7 +129,7 @@ func (bc *BuildContext) BuildWithInterruptHandler() error {
err := bc.vi.Cancel()
if err != nil {
lg.Warn("Failed to cancel VM context", "error", err)
lg.Warn("Failed to cancel VM context", "error", err.Error())
}
}
}

View file

@ -19,13 +19,13 @@ var rootCmd = &cobra.Command{
bc, err := builder.NewBuildContext(slog.With("caller", "build-context"), baseISOPath, outImagePath, vmDebugFlag)
if err != nil {
slog.Error("Failed to create a new build context", "error", err)
slog.Error("Failed to create a new build context", "error", err.Error())
os.Exit(1)
}
err = bc.BuildWithInterruptHandler()
if err != nil {
slog.Error("Failed to build an image", "error", err)
slog.Error("Failed to build an image", "error", err.Error())
os.Exit(1)
}

View file

@ -20,11 +20,16 @@ var lsCmd = &cobra.Command{
os.Exit(runVM(args[0], func(ctx context.Context, i *vm.VM, fm *vm.FileManager) int {
lsblkOut, err := fm.Lsblk()
if err != nil {
slog.Error("Failed to list block devices in the VM", "error", err)
slog.Error("Failed to list block devices in the VM", "error", err.Error())
return 1
}
fmt.Print(string(lsblkOut))
if len(lsblkOut) == 0 {
fmt.Printf("<empty lsblk output>\n")
} else {
fmt.Print(string(lsblkOut))
}
return 0
}, nil, false))
},

View file

@ -26,6 +26,9 @@ func Execute() {
var vmDebugFlag bool
var unrestrictedNetworkingFlag bool
var vmMemAllocFlag uint64
// TODO: Version command.
func init() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, nil)))
@ -34,6 +37,7 @@ func init() {
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(shellCmd)
rootCmd.PersistentFlags().BoolVar(&vmDebugFlag, "vmdebug", false, "Enable VM debug mode. This will open an accessible VM monitor. You can log in with root user and no password.")
rootCmd.PersistentFlags().BoolVar(&unrestrictedNetworkingFlag, "unrestricted-networking", false, "Enable unrestricted networking. This will allow the VM to connect to the internet.")
rootCmd.PersistentFlags().BoolVar(&vmDebugFlag, "vmdebug", false, "Enables the VM debug mode. This will open an accessible VM monitor. You can log in with root user and no password.")
rootCmd.PersistentFlags().BoolVar(&unrestrictedNetworkingFlag, "unrestricted-networking", false, "Enables unrestricted networking. This will allow the VM to connect to the internet.")
rootCmd.PersistentFlags().Uint64Var(&vmMemAllocFlag, "vm-mem-alloc", 512, "Specifies the VM memory allocation in KiB")
}

View file

@ -24,7 +24,7 @@ var runCmd = &cobra.Command{
networkSharePort, err := getClosestAvailPortWithSubsequent(9000, 10)
if err != nil {
slog.Error("Failed to get closest available host port for network file share", "error", err)
slog.Error("Failed to get closest available host port for network file share", "error", err.Error())
os.Exit(1)
}
@ -46,18 +46,20 @@ var runCmd = &cobra.Command{
// TODO: `slog` library prints entire stack traces for errors which makes reading errors challenging.
os.Exit(runVM(args[0], func(ctx context.Context, i *vm.VM, fm *vm.FileManager) int {
slog.Info("Mounting the device", "dev", vmMountDevName, "fs", fsType, "luks", luksFlag)
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)
slog.Error("Failed to mount the disk inside the VM", "error", err.Error())
return 1
}
sharePWD, err := password.Generate(16, 10, 0, false, false)
if err != nil {
slog.Error("Failed to generate ephemeral password for network file share", "error", err)
slog.Error("Failed to generate ephemeral password for network file share", "error", err.Error())
return 1
}
@ -67,7 +69,7 @@ var runCmd = &cobra.Command{
err = fm.StartFTP([]byte(sharePWD), networkSharePort+1, ftpPassivePortCount)
if err != nil {
slog.Error("Failed to start FTP server", "error", err)
slog.Error("Failed to start FTP server", "error", err.Error())
return 1
}

View file

@ -32,7 +32,7 @@ var shellCmd = &cobra.Command{
fpr, err := vm.ParsePortForwardString(fp)
if err != nil {
slog.Error("Failed to parse port forward string", "index", i, "value", fp, "error", err)
slog.Error("Failed to parse port forward string", "index", i, "value", fp, "error", err.Error())
os.Exit(1)
}
@ -42,7 +42,7 @@ var shellCmd = &cobra.Command{
os.Exit(runVM(passthroughArg, func(ctx context.Context, i *vm.VM, fm *vm.FileManager) int {
sc, err := i.DialSSH()
if err != nil {
slog.Error("Failed to dial VM SSH", "error", err)
slog.Error("Failed to dial VM SSH", "error", err.Error())
return 1
}
@ -50,7 +50,7 @@ var shellCmd = &cobra.Command{
sess, err := sc.NewSession()
if err != nil {
slog.Error("Failed to create new VM SSH session", "error", err)
slog.Error("Failed to create new VM SSH session", "error", err.Error())
return 1
}
@ -59,14 +59,14 @@ var shellCmd = &cobra.Command{
termFD := int(os.Stdin.Fd())
termState, err := term.MakeRaw(termFD)
if err != nil {
slog.Error("Failed to make raw terminal", "error", err)
slog.Error("Failed to make raw terminal", "error", err.Error())
return 1
}
defer func() {
err := term.Restore(termFD, termState)
if err != nil {
slog.Error("Failed to restore terminal", "error", err)
slog.Error("Failed to restore terminal", "error", err.Error())
}
}()
@ -78,7 +78,7 @@ var shellCmd = &cobra.Command{
termWidth, termHeight, err := term.GetSize(termFDGetSize)
if err != nil {
slog.Error("Failed to get terminal size", "error", err)
slog.Error("Failed to get terminal size", "error", err.Error())
return 1
}
@ -95,7 +95,7 @@ var shellCmd = &cobra.Command{
err = sess.RequestPty(term, termHeight, termWidth, termModes)
if err != nil {
slog.Error("Failed to request VM SSH pty", "error", err)
slog.Error("Failed to request VM SSH pty", "error", err.Error())
return 1
}
@ -105,7 +105,7 @@ var shellCmd = &cobra.Command{
err = sess.Shell()
if err != nil {
slog.Error("Start VM SSH shell", "error", err)
slog.Error("Start VM SSH shell", "error", err.Error())
return 1
}
@ -114,7 +114,7 @@ var shellCmd = &cobra.Command{
go func() {
err = sess.Wait()
if err != nil {
slog.Error("Failed to wait for VM SSH session to finish", "error", err)
slog.Error("Failed to wait for VM SSH session to finish", "error", err.Error())
}
doneCh <- struct{}{}

View file

@ -40,7 +40,7 @@ func doUSBRootCheck() {
ok, err := checkIfRoot()
if err != nil {
slog.Error("Failed to check whether the command is ran by root", "error", err)
slog.Error("Failed to check whether the command is ran by root", "error", err.Error())
os.Exit(1)
}
@ -64,6 +64,8 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
SnapshotMode: true,
}},
MemoryAlloc: vmMemAllocFlag,
USBDevices: passthroughConfig,
ExtraPortForwardingRules: forwardPortsRules,
@ -74,7 +76,7 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
// TODO: Alpine image should be downloaded from somewhere.
vi, err := vm.NewVM(slog.Default().With("caller", "vm"), vmCfg)
if err != nil {
slog.Error("Failed to create vm instance", "error", err)
slog.Error("Failed to create vm instance", "error", err.Error())
os.Exit(1)
}
@ -115,7 +117,7 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
err := vi.Cancel()
if err != nil {
lg.Warn("Failed to cancel VM context", "error", err)
lg.Warn("Failed to cancel VM context", "error", err.Error())
}
}
}
@ -130,12 +132,12 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
err = fmt.Errorf("operation canceled by user")
}
slog.Error("Failed to start the VM", "error", err)
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)
slog.Error("Failed to initialize File Manager", "error", err.Error())
os.Exit(1)
}
@ -143,7 +145,7 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
err = vi.Cancel()
if err != nil {
slog.Error("Failed to cancel VM context", "error", err)
slog.Error("Failed to cancel VM context", "error", err.Error())
os.Exit(1)
}
@ -152,7 +154,7 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
select {
case err := <-runErrCh:
if err != nil {
slog.Error("Failed to run the VM", "error", err)
slog.Error("Failed to run the VM", "error", err.Error())
os.Exit(1)
}
default: