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

Commit

Permalink
persist: save and restore state from persist.json
Browse files Browse the repository at this point in the history
Save and restore state from persist.json instead of state.json

Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
WeiZhang555 committed Jan 8, 2019
1 parent f47580b commit a373ba2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 40 deletions.
1 change: 1 addition & 0 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func createSandboxFromConfig(ctx context.Context, sandboxConfig SandboxConfig, f
}
}()

s.Logger().Error("=====")
if err := s.getAndStoreGuestDetails(); err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ func (c *Container) setContainerState(state stateString) error {
}

c.sandbox.PersistState()
if err := c.sandbox.store.Dump(); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -625,9 +628,14 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
ctx: sandbox.ctx,
}

state, err := c.sandbox.storage.fetchContainerState(c.sandboxID, c.id)
/*state, err := c.sandbox.storage.fetchContainerState(c.sandboxID, c.id)
if err == nil {
c.state = state
}*/

if err := c.Restore(); err != nil &&
!os.IsNotExist(err) && err != ContainerPersistNotExist {
return nil, err
}

process, err := c.sandbox.storage.fetchContainerProcess(c.sandboxID, c.id)
Expand Down
76 changes: 48 additions & 28 deletions virtcontainers/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
package virtcontainers

import (
//"fmt"

//"github.com/sirupsen/logrus"
"errors"

"github.com/kata-containers/runtime/virtcontainers/device/api"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
)

var (
SandboxPersistNotExist = errors.New("sandbox doesn't exist in persist data")
ContainerPersistNotExist = errors.New("container doesn't exist in persist data")
)

// PersistVersion set persist data version to current version in runtime
func (s *Sandbox) PersistVersion() {
s.store.RegisterHook("version", func(ss *persistapi.SandboxState, cs map[string]persistapi.ContainerState) error {
Expand Down Expand Up @@ -88,44 +91,61 @@ func (s *Sandbox) PersistDevices() {
})
}

// Restore will restore data from persist disk on disk
func (s *Sandbox) Restore() error {
if err := s.store.Restore(s.id); err != nil {
return err
}

func (s *Sandbox) getSbxAndCntStates() (*persistapi.SandboxState, map[string]persistapi.ContainerState, error) {
ss, cs, err := s.store.GetStates()
if err != nil {
return err
return nil, nil, err
}

/*
// TODO: need more modifications, restoring containers
// will make sandbox.addContainer failing
if s.containers == nil {
s.containers = make(map[string]*Container)
if len(cs) == 0 {
if err := s.store.Restore(s.id); err != nil {
return nil, nil, err
}

for id, cont := range cs {
s.containers[id] = &Container{
state: State{
State: stateString(cont.State),
BlockDeviceID: cont.Rootfs.BlockDeviceID,
Fstype: cont.Rootfs.FsType,
Pid: cont.ShimPid,
},
}
ss, cs, err = s.store.GetStates()
if err != nil {
return nil, nil, err
}

sbxCont, ok := s.containers[ss.SandboxContainer]
if !ok {
return fmt.Errorf("failed to get sandbox container state")
if len(cs) == 0 {
return nil, nil, SandboxPersistNotExist
}
*/
}
return ss, cs, nil
}

// Restore will restore sandbox ata from persist file on disk
func (s *Sandbox) Restore() error {
ss, cs, err := s.getSbxAndCntStates()
if err != nil {
return err
}

s.state.GuestMemoryBlockSizeMB = ss.GuestMemoryBlockSizeMB
s.state.BlockIndex = ss.HypervisorState.BlockIndex
s.state.State = stateString(cs[ss.SandboxContainer].State)
s.state.Pid = cs[ss.SandboxContainer].ShimPid

return nil
}

// RestoreContainer will restore container data from persist file on disk
func (c *Container) Restore() error {
_, cs, err := c.sandbox.getSbxAndCntStates()
if err != nil {
return err
}

if _, ok := cs[c.id]; !ok {
return ContainerPersistNotExist
}

c.state = State{
State: stateString(cs[c.id].State),
BlockDeviceID: cs[c.id].Rootfs.BlockDeviceID,
Fstype: cs[c.id].Rootfs.FsType,
Pid: cs[c.id].ShimPid,
}

return nil
}
14 changes: 3 additions & 11 deletions virtcontainers/persist/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package fs
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
Expand Down Expand Up @@ -139,15 +140,12 @@ func (fs *FS) Restore(sid string) error {
}

fs.sandboxState.SandboxContainer = sid

sandboxDir, err := fs.sandboxDir()
if err != nil {
return err
}

if err := os.MkdirAll(sandboxDir, dirMode); err != nil {
return err
}

if err := fs.lock(); err != nil {
return err
}
Expand All @@ -166,13 +164,7 @@ func (fs *FS) Restore(sid string) error {
}

// walk sandbox dir and find container
d, err := os.OpenFile(sandboxDir, os.O_RDONLY, fileMode)
if err != nil {
return err
}
defer d.Close()

files, err := d.Readdir(-1)
files, err := ioutil.ReadDir(sandboxDir)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1680,11 +1680,15 @@ func (s *Sandbox) Stop() error {
}

for _, c := range s.containers {
c.Logger().Errorf("============container %q stop", c.id)
if err := c.stop(); err != nil {
c.Logger().Errorf("============container %q stop error: %v", c.id, err)
return err
}
c.Logger().Errorf("============container %q stop success", c.id)
}

s.Logger().Errorf("============sandbox stop VM")
if err := s.stopVM(); err != nil {
return err
}
Expand Down

0 comments on commit a373ba2

Please sign in to comment.