Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers/hypervisors: support new persist API
Browse files Browse the repository at this point in the history
Fix hypervisor implementations and unit tests to support the new persist API

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Feb 12, 2020
1 parent 00307a7 commit 9585bc9
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 106 deletions.
35 changes: 7 additions & 28 deletions virtcontainers/acrn.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/persist"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
"github.com/kata-containers/runtime/virtcontainers/pkg/rootless"
"github.com/kata-containers/runtime/virtcontainers/pkg/uuid"
"github.com/kata-containers/runtime/virtcontainers/types"
"github.com/kata-containers/runtime/virtcontainers/utils"
Expand All @@ -39,17 +36,6 @@ const (
uuidFile = "uuid.json"
)

// VMUUIDStoragePath is the uuid directory.
// It will contain all uuid info used by guest vm.
var VMUUIDStoragePath = func() string {
path := filepath.Join(fs.StorageRootPath(), UUIDPathSuffix)
if rootless.IsRootless() {
return filepath.Join(rootless.GetRootlessDir(), path)
}
return path

}

// ACRN currently supports only known UUIDs for security
// reasons (FuSa). When launching VM, only these pre-defined
// UUID should be used else VM launch will fail. The main
Expand Down Expand Up @@ -101,6 +87,7 @@ type Acrn struct {
info AcrnInfo
arch acrnArch
ctx context.Context
store persistapi.PersistDriver
}

type acrnPlatformInfo struct {
Expand Down Expand Up @@ -328,7 +315,7 @@ func (a *Acrn) setup(id string, hypervisorConfig *HypervisorConfig) error {

// The path might already exist, but in case of VM templating,
// we have to create it since the sandbox has not created it yet.
if err = os.MkdirAll(filepath.Join(fs.RunStoragePath(), id), DirMode); err != nil {
if err = os.MkdirAll(filepath.Join(a.store.RunStoragePath(), id), DirMode); err != nil {
return err
}

Expand Down Expand Up @@ -444,7 +431,7 @@ func (a *Acrn) startSandbox(timeoutSecs int) error {
a.Logger().WithField("default-kernel-parameters", formatted).Debug()
}

vmPath := filepath.Join(fs.RunVMStoragePath(), a.id)
vmPath := filepath.Join(a.store.RunVMStoragePath(), a.id)
err := os.MkdirAll(vmPath, DirMode)
if err != nil {
return err
Expand Down Expand Up @@ -658,7 +645,7 @@ func (a *Acrn) getSandboxConsole(id string) (string, error) {
span, _ := a.trace("getSandboxConsole")
defer span.Finish()

return utils.BuildSocketPath(fs.RunVMStoragePath(), id, acrnConsoleSocket)
return utils.BuildSocketPath(a.store.RunVMStoragePath(), id, acrnConsoleSocket)
}

func (a *Acrn) saveSandbox() error {
Expand Down Expand Up @@ -734,7 +721,7 @@ func (a *Acrn) check() error {
}

func (a *Acrn) generateSocket(id string, useVsock bool) (interface{}, error) {
return generateVMSocket(id, useVsock)
return generateVMSocket(id, useVsock, a.store.RunVMStoragePath())
}

// GetACRNUUIDBytes returns UUID bytes that is used for VM creation
Expand Down Expand Up @@ -797,32 +784,24 @@ func (a *Acrn) GetMaxSupportedACRNVM() (uint8, error) {
}

func (a *Acrn) storeInfo() error {
store, err := persist.GetDriver("fs")
if err != nil {
return err
}
relPath := filepath.Join(UUIDPathSuffix, uuidFile)

jsonOut, err := json.Marshal(a.info)
if err != nil {
return fmt.Errorf("Could not marshal data: %s", err)
}

if err := store.GlobalWrite(relPath, jsonOut); err != nil {
if err := a.store.GlobalWrite(relPath, jsonOut); err != nil {
return fmt.Errorf("failed to write uuid to file: %v", err)
}

return nil
}

func (a *Acrn) loadInfo() error {
store, err := persist.GetDriver("fs")
if err != nil {
return err
}
relPath := filepath.Join(UUIDPathSuffix, uuidFile)

data, err := store.GlobalRead(relPath)
data, err := a.store.GlobalRead(relPath)
if err != nil {
return fmt.Errorf("failed to read uuid from file: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/acrn_arch_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestAcrnArchBaseAppendConsoles(t *testing.T) {
assert := assert.New(t)
acrnArchBase := newAcrnArchBase()

path := filepath.Join(filepath.Join(fs.RunStoragePath(), sandboxID), consoleSocket)
path := filepath.Join(filepath.Join(fs.MockRunStoragePath(), sandboxID), consoleSocket)

expectedOut := []Device{
ConsoleDevice{
Expand Down
20 changes: 15 additions & 5 deletions virtcontainers/acrn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"

"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
"github.com/kata-containers/runtime/virtcontainers/persist"
"github.com/kata-containers/runtime/virtcontainers/types"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -194,11 +194,16 @@ func TestAcrnUpdateBlockDeviceInvalidIdx(t *testing.T) {

func TestAcrnGetSandboxConsole(t *testing.T) {
assert := assert.New(t)

store, err := persist.GetDriver()
assert.NoError(err)

a := &Acrn{
ctx: context.Background(),
ctx: context.Background(),
store: store,
}
sandboxID := "testSandboxID"
expected := filepath.Join(fs.RunVMStoragePath(), sandboxID, consoleSocket)
expected := filepath.Join(a.store.RunVMStoragePath(), sandboxID, consoleSocket)

result, err := a.getSandboxConsole(sandboxID)
assert.NoError(err)
Expand All @@ -208,7 +213,12 @@ func TestAcrnGetSandboxConsole(t *testing.T) {
func TestAcrnCreateSandbox(t *testing.T) {
assert := assert.New(t)
acrnConfig := newAcrnConfig()
a := &Acrn{}
store, err := persist.GetDriver()
assert.NoError(err)

a := &Acrn{
store: store,
}

sandbox := &Sandbox{
ctx: context.Background(),
Expand All @@ -218,7 +228,7 @@ func TestAcrnCreateSandbox(t *testing.T) {
},
}

err := globalSandboxList.addSandbox(sandbox)
err = globalSandboxList.addSandbox(sandbox)
assert.NoError(err)

defer globalSandboxList.removeSandbox(sandbox.id)
Expand Down
18 changes: 9 additions & 9 deletions virtcontainers/clh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"time"

persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
chclient "github.com/kata-containers/runtime/virtcontainers/pkg/cloud-hypervisor/client"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
Expand Down Expand Up @@ -111,6 +110,7 @@ type cloudHypervisor struct {
vmconfig chclient.VmConfig
cmdOutput bytes.Buffer
virtiofsd Virtiofsd
store persistapi.PersistDriver
}

var clhKernelParams = []Param{
Expand Down Expand Up @@ -303,7 +303,7 @@ func (clh *cloudHypervisor) startSandbox(timeout int) error {

clh.Logger().WithField("function", "startSandbox").Info("starting Sandbox")

vmPath := filepath.Join(fs.RunVMStoragePath(), clh.id)
vmPath := filepath.Join(clh.store.RunVMStoragePath(), clh.id)
err := os.MkdirAll(vmPath, DirMode)
if err != nil {
return err
Expand Down Expand Up @@ -604,23 +604,23 @@ func (clh *cloudHypervisor) generateSocket(id string, useVsock bool) (interface{
}

func (clh *cloudHypervisor) virtioFsSocketPath(id string) (string, error) {
return utils.BuildSocketPath(fs.RunVMStoragePath(), id, virtioFsSocket)
return utils.BuildSocketPath(clh.store.RunVMStoragePath(), id, virtioFsSocket)
}

func (clh *cloudHypervisor) vsockSocketPath(id string) (string, error) {
return utils.BuildSocketPath(fs.RunVMStoragePath(), id, clhSocket)
return utils.BuildSocketPath(clh.store.RunVMStoragePath(), id, clhSocket)
}

func (clh *cloudHypervisor) serialPath(id string) (string, error) {
return utils.BuildSocketPath(fs.RunVMStoragePath(), id, clhSerial)
return utils.BuildSocketPath(clh.store.RunVMStoragePath(), id, clhSerial)
}

func (clh *cloudHypervisor) apiSocketPath(id string) (string, error) {
return utils.BuildSocketPath(fs.RunVMStoragePath(), id, clhAPISocket)
return utils.BuildSocketPath(clh.store.RunVMStoragePath(), id, clhAPISocket)
}

func (clh *cloudHypervisor) logFilePath(id string) (string, error) {
return utils.BuildSocketPath(fs.RunVMStoragePath(), id, clhLogFile)
return utils.BuildSocketPath(clh.store.RunVMStoragePath(), id, clhLogFile)
}

func (clh *cloudHypervisor) waitVMM(timeout uint) error {
Expand Down Expand Up @@ -999,7 +999,7 @@ func (clh *cloudHypervisor) cleanupVM(force bool) error {
}

// cleanup vm path
dir := filepath.Join(fs.RunVMStoragePath(), clh.id)
dir := filepath.Join(clh.store.RunVMStoragePath(), clh.id)

// If it's a symlink, remove both dir and the target.
link, err := filepath.EvalSymlinks(dir)
Expand Down Expand Up @@ -1028,7 +1028,7 @@ func (clh *cloudHypervisor) cleanupVM(force bool) error {
}

if clh.config.VMid != "" {
dir = filepath.Join(fs.RunStoragePath(), clh.config.VMid)
dir = filepath.Join(clh.store.RunStoragePath(), clh.config.VMid)
if err := os.RemoveAll(dir); err != nil {
if !force {
return err
Expand Down
43 changes: 25 additions & 18 deletions virtcontainers/clh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"

"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
"github.com/kata-containers/runtime/virtcontainers/persist"
chclient "github.com/kata-containers/runtime/virtcontainers/pkg/cloud-hypervisor/client"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -172,33 +172,32 @@ func TestCloudHypervisorBootVM(t *testing.T) {
}

func TestCloudHypervisorCleanupVM(t *testing.T) {
clh := &cloudHypervisor{}
assert := assert.New(t)
store, err := persist.GetDriver()
assert.NoError(err, "persist.GetDriver() unexpected error")

if err := clh.cleanupVM(true); err == nil {
t.Errorf("cloudHypervisor.cleanupVM() expected error != %v", err)
clh := &cloudHypervisor{
store: store,
}

err = clh.cleanupVM(true)
assert.Error(err, "persist.GetDriver() expected error")

clh.id = "cleanVMID"

if err := clh.cleanupVM(true); err != nil {
t.Errorf("cloudHypervisor.cleanupVM() expected error != %v", err)
}
err = clh.cleanupVM(true)
assert.NoError(err, "persist.GetDriver() unexpected error")

dir := filepath.Join(fs.RunVMStoragePath(), clh.id)
dir := filepath.Join(clh.store.RunVMStoragePath(), clh.id)
os.MkdirAll(dir, os.ModePerm)

if err := clh.cleanupVM(false); err != nil {
t.Errorf("cloudHypervisor.cleanupVM() expected error != %v", err)
}
_, err := os.Stat(dir)
err = clh.cleanupVM(false)
assert.NoError(err, "persist.GetDriver() unexpected error")

if err == nil {
t.Errorf("dir should not exist %s", dir)
}
_, err = os.Stat(dir)
assert.Error(err, "dir should not exist %s", dir)

if !os.IsNotExist(err) {
t.Errorf("Unexpected error = %v", err)
}
assert.True(os.IsNotExist(err), "persist.GetDriver() unexpected error")
}

func TestClhCreateSandbox(t *testing.T) {
Expand All @@ -207,8 +206,12 @@ func TestClhCreateSandbox(t *testing.T) {
clhConfig, err := newClhConfig()
assert.NoError(err)

store, err := persist.GetDriver()
assert.NoError(err)

clh := &cloudHypervisor{
config: clhConfig,
store: store,
}

sandbox := &Sandbox{
Expand All @@ -229,10 +232,14 @@ func TestClooudHypervisorStartSandbox(t *testing.T) {
clhConfig, err := newClhConfig()
assert.NoError(err)

store, err := persist.GetDriver()
assert.NoError(err)

clh := &cloudHypervisor{
config: clhConfig,
APIClient: &clhClientMock{},
virtiofsd: &virtiofsdMock{},
store: store,
}

err = clh.startSandbox(10)
Expand Down
23 changes: 17 additions & 6 deletions virtcontainers/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"strings"

"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/persist"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
"github.com/kata-containers/runtime/virtcontainers/types"
"github.com/kata-containers/runtime/virtcontainers/utils"
)
Expand Down Expand Up @@ -193,15 +193,26 @@ func (hType *HypervisorType) String() string {

// newHypervisor returns an hypervisor from and hypervisor type.
func newHypervisor(hType HypervisorType) (hypervisor, error) {
store, err := persist.GetDriver()
if err != nil {
return nil, err
}

switch hType {
case QemuHypervisor:
return &qemu{}, nil
return &qemu{
store: store,
}, nil
case FirecrackerHypervisor:
return &firecracker{}, nil
case AcrnHypervisor:
return &Acrn{}, nil
return &Acrn{
store: store,
}, nil
case ClhHypervisor:
return &cloudHypervisor{}, nil
return &cloudHypervisor{
store: store,
}, nil
case MockHypervisor:
return &mockHypervisor{}, nil
default:
Expand Down Expand Up @@ -713,7 +724,7 @@ func getHypervisorPid(h hypervisor) int {
return pids[0]
}

func generateVMSocket(id string, useVsock bool) (interface{}, error) {
func generateVMSocket(id string, useVsock bool, vmStogarePath string) (interface{}, error) {
if useVsock {
vhostFd, contextID, err := utils.FindContextID()
if err != nil {
Expand All @@ -727,7 +738,7 @@ func generateVMSocket(id string, useVsock bool) (interface{}, error) {
}, nil
}

path, err := utils.BuildSocketPath(filepath.Join(fs.RunVMStoragePath(), id), defaultSocketName)
path, err := utils.BuildSocketPath(filepath.Join(vmStogarePath, id), defaultSocketName)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 9585bc9

Please sign in to comment.