github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/qemu/qemu_provider.go (about)

     1  package qemu
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/emc-advanced-dev/pkg/errors"
     8  	"github.com/solo-io/unik/pkg/config"
     9  	"github.com/solo-io/unik/pkg/state"
    10  )
    11  
    12  var debuggerTargetImageName string
    13  
    14  type QemuProvider struct {
    15  	config config.Qemu
    16  	state  state.State
    17  }
    18  
    19  func QemuStateFile() string {
    20  	return filepath.Join(config.Internal.UnikHome, "qemu/state.json")
    21  
    22  }
    23  func qemuImagesDirectory() string {
    24  	return filepath.Join(config.Internal.UnikHome, "qemu/images/")
    25  }
    26  
    27  func qemuInstancesDirectory() string {
    28  	return filepath.Join(config.Internal.UnikHome, "qemu/instances/")
    29  }
    30  
    31  func qemuVolumesDirectory() string {
    32  	return filepath.Join(config.Internal.UnikHome, "qemu/volumes/")
    33  }
    34  
    35  func NewQemuProvider(config config.Qemu) (*QemuProvider, error) {
    36  
    37  	os.MkdirAll(qemuImagesDirectory(), 0777)
    38  	os.MkdirAll(qemuInstancesDirectory(), 0777)
    39  	os.MkdirAll(qemuVolumesDirectory(), 0777)
    40  
    41  	if config.DebuggerPort == 0 {
    42  		config.DebuggerPort = 3001
    43  	}
    44  
    45  	if err := startDebuggerListener(config.DebuggerPort); err != nil {
    46  		return nil, errors.New("establishing debugger tcp listener", err)
    47  	}
    48  
    49  	p := &QemuProvider{
    50  		config: config,
    51  		state:  state.NewBasicState(QemuStateFile()),
    52  	}
    53  
    54  	return p, nil
    55  }
    56  
    57  func (p *QemuProvider) WithState(state state.State) *QemuProvider {
    58  	p.state = state
    59  	return p
    60  }
    61  
    62  func getImagePath(imageName string) string {
    63  	return filepath.Join(qemuImagesDirectory(), imageName, "boot.img")
    64  }
    65  
    66  func getKernelPath(imageName string) string {
    67  	return filepath.Join(qemuImagesDirectory(), imageName, "program.bin")
    68  }
    69  
    70  func getCmdlinePath(imageName string) string {
    71  	return filepath.Join(qemuImagesDirectory(), imageName, "cmdline")
    72  }
    73  
    74  func getVolumePath(volumeName string) string {
    75  	return filepath.Join(qemuVolumesDirectory(), volumeName, "data.img")
    76  }