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 Feb 8, 2019
1 parent 212fea9 commit 122fe74
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 43 deletions.
16 changes: 13 additions & 3 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ func (c *Container) setContainerState(state types.StateString) error {
return err
}

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

Expand Down Expand Up @@ -623,11 +626,18 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
ctx: sandbox.ctx,
}

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
}

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

process, err := c.sandbox.storage.fetchContainerProcess(c.sandboxID, c.id)
if err == nil {
c.process = process
Expand Down
84 changes: 55 additions & 29 deletions virtcontainers/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
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"
"github.com/kata-containers/runtime/virtcontainers/types"
)

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

func (s *Sandbox) dumpState(ss *persistapi.SandboxState, cs map[string]persistapi.ContainerState) error {
ss.SandboxContainer = s.id
ss.GuestMemoryBlockSizeMB = s.state.GuestMemoryBlockSizeMB
Expand All @@ -30,7 +33,13 @@ func (s *Sandbox) dumpState(ss *persistapi.SandboxState, cs map[string]persistap
BlockDeviceID: cont.state.BlockDeviceID,
FsType: cont.state.Fstype,
}
state.ShimPid = cont.state.Pid

// FIXME: container should store its own PID
if id == s.id {
state.ShimPid = s.state.Pid
} else {
state.ShimPid = cont.state.Pid
}
cs[id] = state
}
return nil
Expand Down Expand Up @@ -96,44 +105,61 @@ func (s *Sandbox) persistDevices() {
s.store.RegisterHook("devices", s.dumpDevices)
}

// 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 = types.StateString(ss.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 = types.State{
State: types.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

0 comments on commit 122fe74

Please sign in to comment.