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

Commit

Permalink
persist: remove VCStore from hypervisor
Browse files Browse the repository at this point in the history
Remove usage of VCStore from hypervisors.

Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
WeiZhang555 committed Dec 30, 2019
1 parent 687f2db commit 633748a
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 220 deletions.
9 changes: 1 addition & 8 deletions cli/kata-check_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
package main

import (
"context"
"fmt"
"io/ioutil"
"strings"
"syscall"
"unsafe"

vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/store"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -238,12 +236,7 @@ func acrnIsUsable() error {
kataLog.WithField("device", acrnDevice).Info("device available")

acrnInst := vc.Acrn{}
vcStore, err := store.NewVCSandboxStore(context.Background(), "kata-check")
if err != nil {
return err
}

uuidStr, err := acrnInst.GetNextAvailableUUID(vcStore)
uuidStr, err := acrnInst.GetNextAvailableUUID()
if err != nil {
return err
}
Expand Down
139 changes: 98 additions & 41 deletions virtcontainers/acrn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package virtcontainers

import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/kata-containers/runtime/pkg/rootless"
"github.com/kata-containers/runtime/virtcontainers/device/config"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/kata-containers/runtime/virtcontainers/pkg/uuid"
Expand All @@ -28,6 +30,25 @@ import (
"github.com/kata-containers/runtime/virtcontainers/utils"
)

// Since ACRN is using the store in a quite abnormal way, let's first draw it back from store to here

// UUIDPathSuffix is the suffix used for uuid storage
const (
UUIDPathSuffix = "uuid"
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("/run/vc", 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 @@ -73,7 +94,6 @@ type AcrnState struct {
// Acrn is an Hypervisor interface implementation for the Linux acrn hypervisor.
type Acrn struct {
id string
store *store.VCStore
config HypervisorConfig
acrnConfig Config
state AcrnState
Expand Down Expand Up @@ -276,7 +296,7 @@ func (a *Acrn) buildDevices(imagePath string) ([]Device, error) {
}

// setup sets the Acrn structure up.
func (a *Acrn) setup(id string, hypervisorConfig *HypervisorConfig, vcStore *store.VCStore) error {
func (a *Acrn) setup(id string, hypervisorConfig *HypervisorConfig) error {
span, _ := a.trace("setup")
defer span.Finish()

Expand All @@ -286,24 +306,19 @@ func (a *Acrn) setup(id string, hypervisorConfig *HypervisorConfig, vcStore *sto
}

a.id = id
a.store = vcStore
a.config = *hypervisorConfig
a.arch = newAcrnArch(a.config)

var create bool
var uuid string

if a.store != nil { //use old store
if err = a.store.Load(store.Hypervisor, &a.state); err != nil {
create = true
}
} else if a.state.UUID == "" { // new store
if a.state.UUID == "" {
create = true
}

if create {
a.Logger().Debug("Setting UUID")
if uuid, err = a.GetNextAvailableUUID(nil); err != nil {
if uuid, err = a.GetNextAvailableUUID(); err != nil {
return err
}
a.state.UUID = uuid
Expand All @@ -316,10 +331,6 @@ func (a *Acrn) setup(id string, hypervisorConfig *HypervisorConfig, vcStore *sto
return err
}

if err = a.storeState(); err != nil {
return err
}

if err = a.storeInfo(); err != nil {
return err
}
Expand Down Expand Up @@ -348,14 +359,14 @@ func (a *Acrn) createDummyVirtioBlkDev(devices []Device) ([]Device, error) {
}

// createSandbox is the Hypervisor sandbox creation.
func (a *Acrn) createSandbox(ctx context.Context, id string, networkNS NetworkNamespace, hypervisorConfig *HypervisorConfig, store *store.VCStore, stateful bool) error {
func (a *Acrn) createSandbox(ctx context.Context, id string, networkNS NetworkNamespace, hypervisorConfig *HypervisorConfig, stateful bool) error {
// Save the tracing context
a.ctx = ctx

span, _ := a.trace("createSandbox")
defer span.Finish()

if err := a.setup(id, hypervisorConfig, store); err != nil {
if err := a.setup(id, hypervisorConfig); err != nil {
return err
}

Expand Down Expand Up @@ -458,11 +469,6 @@ func (a *Acrn) startSandbox(timeoutSecs int) error {
return err
}

//Store VMM information
if err = a.storeState(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -499,7 +505,7 @@ func (a *Acrn) stopSandbox() (err error) {
uuid := a.state.UUID
Idx := acrnUUIDsToIdx[uuid]

if err = a.store.Load(store.UUID, &a.info); err != nil {
if err = a.loadInfo(); err != nil {
a.Logger().Info("Failed to load UUID availabiity info")
return err
}
Expand Down Expand Up @@ -698,7 +704,7 @@ func (a *Acrn) getPids() []int {
return []int{a.state.PID}
}

func (a *Acrn) fromGrpc(ctx context.Context, hypervisorConfig *HypervisorConfig, store *store.VCStore, j []byte) error {
func (a *Acrn) fromGrpc(ctx context.Context, hypervisorConfig *HypervisorConfig, j []byte) error {
return errors.New("acrn is not supported by VM cache")
}

Expand Down Expand Up @@ -737,20 +743,14 @@ func (a *Acrn) GetACRNUUIDBytes(uid string) (uuid.UUID, error) {

// GetNextAvailableUUID returns next available UUID VM creation
// If no validl UUIDs are available it returns err.
func (a *Acrn) GetNextAvailableUUID(uuidstore *store.VCStore) (string, error) {
func (a *Acrn) GetNextAvailableUUID() (string, error) {
var MaxVMSupported uint8
var Idx uint8
var uuidStr string
var err error

if uuidstore == nil {
uuidstore = a.store
}

if uuidstore != nil { //use old store
if err = uuidstore.Load(store.UUID, &a.info); err != nil {
a.Logger().Infof("Load UUID store failed")
}
if err = a.loadInfo(); err != nil {
a.Logger().Infof("Load UUID store failed")
}

if MaxVMSupported, err = a.GetMaxSupportedACRNVM(); err != nil {
Expand Down Expand Up @@ -795,22 +795,79 @@ func (a *Acrn) GetMaxSupportedACRNVM() (uint8, error) {
return platformInfo.maxKataContainers, nil
}

func (a *Acrn) storeState() error {
if a.store != nil {
if err := a.store.Store(store.Hypervisor, a.state); err != nil {
a.Logger().WithError(err).Error("failed to store acrn state")
func (a *Acrn) storeInfo() error {
dirPath := VMUUIDStoragePath()

_, err := os.Stat(dirPath)
if os.IsNotExist(err) {
// Root directory
a.Logger().WithField("path", dirPath).Debugf("Creating UUID directory")
if err := os.MkdirAll(dirPath, store.DirMode); err != nil {
return err
}
} else if err != nil {
return err
}

dirf, err := os.Open(dirPath)
if err != nil {
return err
}
defer dirf.Close()

if err := syscall.Flock(int(dirf.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
return err
}

// write data
f, err := os.OpenFile(filepath.Join(dirPath, uuidFile), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return fmt.Errorf("failed to store information into uuid.json: %v", err)
}
defer f.Close()

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

func (a *Acrn) storeInfo() error {
if a.store != nil {
if err := a.store.Store(store.UUID, a.info); err != nil {
a.Logger().WithError(err).Error("failed to store acrn info")
return err
}
func (a *Acrn) loadInfo() error {
dirPath := VMUUIDStoragePath()

_, err := os.Stat(dirPath)
if err != nil {
return fmt.Errorf("failed to load ACRN information: %v", err)
}

dirf, err := os.Open(dirPath)
if err != nil {
return err
}

if err := syscall.Flock(int(dirf.Fd()), syscall.LOCK_SH|syscall.LOCK_NB); err != nil {
dirf.Close()
return err
}

defer dirf.Close()

// write data
f, err := os.Open(filepath.Join(dirPath, uuidFile))
if err != nil {
return fmt.Errorf("failed to load information into uuid.json: %v", err)
}

dec := json.NewDecoder(f)
if dec != nil {
return fmt.Errorf("failed to create json decoder")
}

err = dec.Decode(&a.info)
if err != nil {
return fmt.Errorf("could not decode data: %v", err)
}
return nil
}
2 changes: 1 addition & 1 deletion virtcontainers/acrn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestAcrnCreateSandbox(t *testing.T) {
//set PID to 1 to ignore hypercall to get UUID and set a random UUID
a.state.PID = 1
a.state.UUID = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"
err = a.createSandbox(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig, nil, false)
err = a.createSandbox(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig, false)
assert.NoError(err)
assert.Exactly(acrnConfig, a.config)
}
Expand Down
Loading

0 comments on commit 633748a

Please sign in to comment.