Restore working state

This commit is contained in:
AlexSSD7 2023-08-27 15:53:44 +01:00
commit 3cbe45c420
7 changed files with 55 additions and 34 deletions

View file

@ -1,6 +1,7 @@
package builder package builder
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"os" "os"
@ -13,6 +14,7 @@ import (
"log/slog" "log/slog"
"github.com/AlexSSD7/linsk/utils"
"github.com/AlexSSD7/linsk/vm" "github.com/AlexSSD7/linsk/vm"
"github.com/alessio/shellescape" "github.com/alessio/shellescape"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -176,6 +178,9 @@ func runAlpineSetupCmd(sc *ssh.Client, pkgs []string) error {
// TODO: Timeout for this command. // TODO: Timeout for this command.
stderr := bytes.NewBuffer(nil)
sess.Stderr = stderr
defer func() { defer func() {
_ = sess.Close() _ = sess.Close()
}() }()
@ -191,9 +196,11 @@ func runAlpineSetupCmd(sc *ssh.Client, pkgs []string) error {
cmd += " && mount /dev/vda3 /mnt && chroot /mnt apk add " + strings.Join(pkgsQuoted, " ") cmd += " && mount /dev/vda3 /mnt && chroot /mnt apk add " + strings.Join(pkgsQuoted, " ")
} }
cmd += `&& chroot /mnt ash -c 'echo "PasswordAuthentication no" >> /etc/ssh/sshd_config && addgroup -g 1000 linsk && adduser -G linsk linsk -S -u 1000'`
err = sess.Run(cmd) err = sess.Run(cmd)
if err != nil { if err != nil {
return errors.Wrap(err, "run setup cmd") return utils.WrapErrWithLog(err, "run setup cmd", stderr.String())
} }
return nil return nil

View file

@ -47,7 +47,10 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
} }
vmCfg := vm.VMConfig{ vmCfg := vm.VMConfig{
CdromImagePath: "alpine-img/alpine.qcow2", Drives: []vm.DriveConfig{{
Path: "alpine.qcow2",
SnapshotMode: true,
}},
USBDevices: passthroughConfig, USBDevices: passthroughConfig,
ExtraPortForwardingRules: forwardPortsRules, ExtraPortForwardingRules: forwardPortsRules,
@ -111,6 +114,10 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
for { for {
select { select {
case err := <-runErrCh: case err := <-runErrCh:
if err == nil {
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)
os.Exit(1) os.Exit(1)
case <-vi.SSHUpNotifyChan(): case <-vi.SSHUpNotifyChan():

26
utils/errors.go Normal file
View file

@ -0,0 +1,26 @@
package utils
import (
"fmt"
"strings"
"github.com/pkg/errors"
)
func WrapErrWithLog(err error, msg, log string) error {
return errors.Wrapf(err, "%v %v", msg, GetLogErrMsg(log))
}
func GetLogErrMsg(s string) string {
logToInclude := strings.ReplaceAll(s, "\n", "\\n")
logToInclude = strings.TrimSuffix(logToInclude, "\\n")
logToInclude = ClearUnprintableChars(logToInclude, false)
origLogLen := len(logToInclude)
const maxLogLen = 256
if origLogLen > maxLogLen {
logToInclude = fmt.Sprintf("[%v chars trimmed]", origLogLen) + logToInclude[len(logToInclude)-maxLogLen:]
}
return fmt.Sprintf("(log: '%v')", logToInclude)
}

View file

@ -1,31 +1,9 @@
package vm package vm
import ( import (
"fmt"
"strings"
"github.com/AlexSSD7/linsk/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
var ( var (
ErrSSHUnavailable = errors.New("ssh unavailable") ErrSSHUnavailable = errors.New("ssh unavailable")
) )
func wrapErrWithLog(err error, msg, log string) error {
return errors.Wrapf(err, "%v %v", msg, getLogErrMsg(log))
}
func getLogErrMsg(s string) string {
logToInclude := strings.ReplaceAll(s, "\n", "\\n")
logToInclude = strings.TrimSuffix(logToInclude, "\\n")
logToInclude = utils.ClearUnprintableChars(logToInclude, false)
origLogLen := len(logToInclude)
const maxLogLen = 256
if origLogLen > maxLogLen {
logToInclude = fmt.Sprintf("[%v chars trimmed]", origLogLen) + logToInclude[len(logToInclude)-maxLogLen:]
}
return fmt.Sprintf("(log: '%v')", logToInclude)
}

View file

@ -141,7 +141,7 @@ func (fm *FileManager) luksOpen(sc *ssh.Client, fullDevPath string) error {
err = sess.Wait() err = sess.Wait()
if err != nil { if err != nil {
return wrapErrWithLog(err, "wait for cryptsetup luksopen cmd to finish", stderrBuf.String()) return utils.WrapErrWithLog(err, "wait for cryptsetup luksopen cmd to finish", stderrBuf.String())
} }
lg.Info("LUKS device opened successfully") lg.Info("LUKS device opened successfully")
@ -279,7 +279,7 @@ create mask = 0664`
err = sess.Wait() err = sess.Wait()
if err != nil { if err != nil {
return wrapErrWithLog(err, "wait for change samba password cmd", stderr.String()) return utils.WrapErrWithLog(err, "wait for change samba password cmd", stderr.String())
} }
return nil return nil

View file

@ -121,7 +121,7 @@ func (vm *VM) sshSetup() (ssh.Signer, error) {
case <-vm.ctx.Done(): case <-vm.ctx.Done():
return nil, vm.ctx.Err() return nil, vm.ctx.Err()
case <-time.After(time.Until(deadline)): case <-time.After(time.Until(deadline)):
return nil, fmt.Errorf("setup command timed out %v", getLogErrMsg(stdOutErrBuf.String())) return nil, fmt.Errorf("setup command timed out %v", utils.GetLogErrMsg(stdOutErrBuf.String()))
case data := <-vm.serialStdoutCh: case data := <-vm.serialStdoutCh:
prefix := []byte("SERIAL STATUS: ") prefix := []byte("SERIAL STATUS: ")
stdOutErrBuf.WriteString(utils.ClearUnprintableChars(string(data), true)) stdOutErrBuf.WriteString(utils.ClearUnprintableChars(string(data), true))
@ -132,7 +132,7 @@ func (vm *VM) sshSetup() (ssh.Signer, error) {
if data[len(prefix)] != '0' { if data[len(prefix)] != '0' {
fmt.Fprintf(os.Stderr, "SSH SETUP FAILURE:\n%v", stdOutErrBuf.String()) fmt.Fprintf(os.Stderr, "SSH SETUP FAILURE:\n%v", stdOutErrBuf.String())
return nil, fmt.Errorf("non-zero setup command status code: '%v' %v", string(data[len(prefix)]), getLogErrMsg(stdOutErrBuf.String())) return nil, fmt.Errorf("non-zero setup command status code: '%v' %v", string(data[len(prefix)]), utils.GetLogErrMsg(stdOutErrBuf.String()))
} }
return sshSigner, nil return sshSigner, nil
@ -173,7 +173,7 @@ func runSSHCmd(c *ssh.Client, cmd string) ([]byte, error) {
err = sess.Run(cmd) err = sess.Run(cmd)
if err != nil { if err != nil {
return nil, wrapErrWithLog(err, "run cmd", stderr.String()) return nil, utils.WrapErrWithLog(err, "run cmd", stderr.String())
} }
return stdout.Bytes(), nil return stdout.Bytes(), nil

View file

@ -18,6 +18,7 @@ import (
"log/slog" "log/slog"
"github.com/AlexSSD7/linsk/utils"
"github.com/alessio/shellescape" "github.com/alessio/shellescape"
"github.com/bramvdbogaerde/go-scp" "github.com/bramvdbogaerde/go-scp"
"github.com/phayes/freeport" "github.com/phayes/freeport"
@ -59,10 +60,10 @@ type DriveConfig struct {
type VMConfig struct { type VMConfig struct {
CdromImagePath string CdromImagePath string
Drives []DriveConfig
USBDevices []USBDevicePassthroughConfig USBDevices []USBDevicePassthroughConfig
ExtraPortForwardingRules []PortForwardingRule ExtraPortForwardingRules []PortForwardingRule
Drives []DriveConfig
// Mostly debug-related options. // Mostly debug-related options.
UnrestrictedNetworking bool UnrestrictedNetworking bool
@ -126,13 +127,15 @@ func NewVM(logger *slog.Logger, cfg VMConfig) (*VM, error) {
driveArgs := "file=" + shellescape.Quote(extraDrive.Path) + ",format=qcow2,if=virtio" driveArgs := "file=" + shellescape.Quote(extraDrive.Path) + ",format=qcow2,if=virtio"
if extraDrive.SnapshotMode { if extraDrive.SnapshotMode {
driveArgs += ",snapshot" driveArgs += ",snapshot=on"
} }
cmdArgs = append(cmdArgs, "-drive", driveArgs) cmdArgs = append(cmdArgs, "-drive", driveArgs)
} }
if cdromImagePath != "" { // We're not using clean `cdromImagePath` here because it is set to "."
// when the original string is empty.
if cfg.CdromImagePath != "" {
cmdArgs = append(cmdArgs, "-boot", "d", "-cdrom", cdromImagePath) cmdArgs = append(cmdArgs, "-boot", "d", "-cdrom", cdromImagePath)
} }
@ -264,14 +267,14 @@ func (vm *VM) Run() error {
errors.Wrap(cancelErr, "cancel"), errors.Wrap(cancelErr, "cancel"),
) )
return fmt.Errorf("%w %v", combinedErr, getLogErrMsg(vm.stderrBuf.String())) return fmt.Errorf("%w %v", combinedErr, utils.GetLogErrMsg(vm.stderrBuf.String()))
} }
combinedErr := multierr.Combine( combinedErr := multierr.Combine(
append(globalErrs, errors.Wrap(cancelErr, "cancel on exit"))..., append(globalErrs, errors.Wrap(cancelErr, "cancel on exit"))...,
) )
if combinedErr != nil { if combinedErr != nil {
return fmt.Errorf("%w %v", combinedErr, getLogErrMsg(vm.stderrBuf.String())) return fmt.Errorf("%w %v", combinedErr, utils.GetLogErrMsg(vm.stderrBuf.String()))
} }
return nil return nil