Better logging + runVM impl
This commit is contained in:
parent
b905244626
commit
a63030fd00
7 changed files with 192 additions and 74 deletions
|
|
@ -2,7 +2,11 @@ package vm
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/AlexSSD7/vldisk/utils"
|
||||
"github.com/alessio/shellescape"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
|
|
@ -36,12 +40,12 @@ func (fm *FileManager) Init() error {
|
|||
}
|
||||
|
||||
func (fm *FileManager) Lsblk() ([]byte, error) {
|
||||
c, err := fm.vi.DialSSH()
|
||||
sc, err := fm.vi.DialSSH()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "dial vm ssh")
|
||||
}
|
||||
|
||||
sess, err := c.NewSession()
|
||||
sess, err := sc.NewSession()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "create new vm ssh session")
|
||||
}
|
||||
|
|
@ -57,3 +61,42 @@ func (fm *FileManager) Lsblk() ([]byte, error) {
|
|||
|
||||
return ret.Bytes(), nil
|
||||
}
|
||||
|
||||
type MountOptions struct {
|
||||
FSType string
|
||||
}
|
||||
|
||||
func (fm *FileManager) Mount(devName string, mo MountOptions) error {
|
||||
if devName == "" {
|
||||
return fmt.Errorf("device name is empty")
|
||||
}
|
||||
|
||||
// It does allow mapper/ prefix for mapped devices.
|
||||
// This is to enable the support for LVM and LUKS.
|
||||
if !utils.ValidateDevName(devName) {
|
||||
return fmt.Errorf("bad device name")
|
||||
}
|
||||
|
||||
fullDevPath := filepath.Clean("/dev/" + devName)
|
||||
|
||||
if mo.FSType == "" {
|
||||
return fmt.Errorf("fs type is empty")
|
||||
}
|
||||
|
||||
sc, err := fm.vi.DialSSH()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "dial vm ssh")
|
||||
}
|
||||
|
||||
sess, err := sc.NewSession()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create new vm ssh session")
|
||||
}
|
||||
|
||||
err = sess.Run("mount -t " + shellescape.Quote(mo.FSType) + " " + shellescape.Quote(fullDevPath) + " /mnt")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "run mount cmd")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
16
vm/vm.go
16
vm/vm.go
|
|
@ -15,8 +15,9 @@ import (
|
|||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"log/slog"
|
||||
|
||||
"github.com/alessio/shellescape"
|
||||
"github.com/inconshreveable/log15"
|
||||
"github.com/phayes/freeport"
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/multierr"
|
||||
|
|
@ -29,7 +30,7 @@ type USBDevicePassthroughConfig struct {
|
|||
}
|
||||
|
||||
type Instance struct {
|
||||
logger log15.Logger
|
||||
logger *slog.Logger
|
||||
|
||||
ctx context.Context
|
||||
ctxCancel context.CancelFunc
|
||||
|
|
@ -53,7 +54,7 @@ type Instance struct {
|
|||
canceled uint32
|
||||
}
|
||||
|
||||
func NewInstance(logger log15.Logger, alpineImagePath string, usbDevices []USBDevicePassthroughConfig, debug bool) (*Instance, error) {
|
||||
func NewInstance(logger *slog.Logger, alpineImagePath string, usbDevices []USBDevicePassthroughConfig, debug bool) (*Instance, error) {
|
||||
alpineImagePath = filepath.Clean(alpineImagePath)
|
||||
_, err := os.Stat(alpineImagePath)
|
||||
if err != nil {
|
||||
|
|
@ -157,6 +158,8 @@ func (vi *Instance) Run() error {
|
|||
return
|
||||
}
|
||||
|
||||
vi.logger.Info("Setting the VM up")
|
||||
|
||||
sshSigner, err := vi.sshSetup()
|
||||
if err != nil {
|
||||
globalErrFn(errors.Wrap(err, "set up ssh"))
|
||||
|
|
@ -192,7 +195,7 @@ func (vi *Instance) Run() error {
|
|||
// This is to notify everyone waiting for SSH to be up that it's ready to go.
|
||||
close(vi.sshReadyCh)
|
||||
|
||||
vi.logger.Info("SSH up, the VM ready for work")
|
||||
vi.logger.Info("The VM is ready")
|
||||
}()
|
||||
|
||||
_, err = vi.cmd.Process.Wait()
|
||||
|
|
@ -209,8 +212,11 @@ func (vi *Instance) Run() error {
|
|||
combinedErr := multierr.Combine(
|
||||
append(globalErrs, errors.Wrap(cancelErr, "cancel on exit"))...,
|
||||
)
|
||||
if combinedErr != nil {
|
||||
return fmt.Errorf("%w %v", combinedErr, getLogErrMsg(vi.stderrBuf.String()))
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w %v", combinedErr, getLogErrMsg(vi.stderrBuf.String()))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vi *Instance) Cancel() error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue