Restore working state
This commit is contained in:
parent
f9cdbe5ac9
commit
3cbe45c420
7 changed files with 55 additions and 34 deletions
22
vm/errors.go
22
vm/errors.go
|
|
@ -1,31 +1,9 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/AlexSSD7/linsk/utils"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -141,7 +141,7 @@ func (fm *FileManager) luksOpen(sc *ssh.Client, fullDevPath string) error {
|
|||
|
||||
err = sess.Wait()
|
||||
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")
|
||||
|
|
@ -279,7 +279,7 @@ create mask = 0664`
|
|||
|
||||
err = sess.Wait()
|
||||
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
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ func (vm *VM) sshSetup() (ssh.Signer, error) {
|
|||
case <-vm.ctx.Done():
|
||||
return nil, vm.ctx.Err()
|
||||
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:
|
||||
prefix := []byte("SERIAL STATUS: ")
|
||||
stdOutErrBuf.WriteString(utils.ClearUnprintableChars(string(data), true))
|
||||
|
|
@ -132,7 +132,7 @@ func (vm *VM) sshSetup() (ssh.Signer, error) {
|
|||
|
||||
if data[len(prefix)] != '0' {
|
||||
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
|
||||
|
|
@ -173,7 +173,7 @@ func runSSHCmd(c *ssh.Client, cmd string) ([]byte, error) {
|
|||
|
||||
err = sess.Run(cmd)
|
||||
if err != nil {
|
||||
return nil, wrapErrWithLog(err, "run cmd", stderr.String())
|
||||
return nil, utils.WrapErrWithLog(err, "run cmd", stderr.String())
|
||||
}
|
||||
|
||||
return stdout.Bytes(), nil
|
||||
|
|
|
|||
13
vm/vm.go
13
vm/vm.go
|
|
@ -18,6 +18,7 @@ import (
|
|||
|
||||
"log/slog"
|
||||
|
||||
"github.com/AlexSSD7/linsk/utils"
|
||||
"github.com/alessio/shellescape"
|
||||
"github.com/bramvdbogaerde/go-scp"
|
||||
"github.com/phayes/freeport"
|
||||
|
|
@ -59,10 +60,10 @@ type DriveConfig struct {
|
|||
|
||||
type VMConfig struct {
|
||||
CdromImagePath string
|
||||
Drives []DriveConfig
|
||||
|
||||
USBDevices []USBDevicePassthroughConfig
|
||||
ExtraPortForwardingRules []PortForwardingRule
|
||||
Drives []DriveConfig
|
||||
|
||||
// Mostly debug-related options.
|
||||
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"
|
||||
if extraDrive.SnapshotMode {
|
||||
driveArgs += ",snapshot"
|
||||
driveArgs += ",snapshot=on"
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
@ -264,14 +267,14 @@ func (vm *VM) Run() error {
|
|||
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(
|
||||
append(globalErrs, errors.Wrap(cancelErr, "cancel on exit"))...,
|
||||
)
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue