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

Commit

Permalink
newstore: remove file "devices.json"
Browse files Browse the repository at this point in the history
When using experimental feature "newstore", we save and load devices
information from `persist.json` instead of `devices.json`, in such case,
file `devices.json` isn't needed anymore, so remove it.

Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
WeiZhang555 committed May 6, 2019
1 parent 341a988 commit 4c19213
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 59 deletions.
4 changes: 4 additions & 0 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ func statusContainer(sandbox *Sandbox, containerID string) (ContainerStatus, err
}

if !running {
virtLog.WithFields(logrus.Fields{
"state": container.state.State,
"process pid": container.process.Pid}).
Info("container isn't running")
if err := container.stop(); err != nil {
return ContainerStatus{}, err
}
Expand Down
47 changes: 31 additions & 16 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,11 @@ func (c *Container) mountSharedDirMounts(hostSharedDir, guestSharedDir string) (
return nil, nil, err
}

if err := c.sandbox.storeSandboxDevices(); err != nil {
//TODO: roll back?
return nil, nil, err
if !c.sandbox.supportNewStore() {
if err := c.sandbox.storeSandboxDevices(); err != nil {
//TODO: roll back?
return nil, nil, err
}
}
continue
}
Expand Down Expand Up @@ -712,11 +714,6 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
}
}

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

var process Process
if err := c.store.Load(store.Process, &process); err == nil {
c.process = process
Expand Down Expand Up @@ -1012,6 +1009,16 @@ func (c *Container) stop() error {
return err
}

defer func() {
// Save device and drive data.
// TODO: can we merge this saving with setContainerState()?
if c.sandbox.supportNewStore() {
if err := c.sandbox.Save(); err != nil {
c.Logger().WithError(err).Info("save container state failed")
}
}
}()

if err := c.sandbox.agent.stopContainer(c.sandbox, *c); err != nil {
return err
}
Expand Down Expand Up @@ -1273,8 +1280,10 @@ func (c *Container) plugDevice(devicePath string) error {
return err
}

if err := c.sandbox.storeSandboxDevices(); err != nil {
return err
if !c.sandbox.supportNewStore() {
if err := c.sandbox.storeSandboxDevices(); err != nil {
return err
}
}
}
return nil
Expand Down Expand Up @@ -1307,8 +1316,10 @@ func (c *Container) removeDrive() (err error) {
}
}

if err := c.sandbox.storeSandboxDevices(); err != nil {
return err
if !c.sandbox.supportNewStore() {
if err := c.sandbox.storeSandboxDevices(); err != nil {
return err
}
}
}

Expand All @@ -1325,8 +1336,10 @@ func (c *Container) attachDevices() error {
}
}

if err := c.sandbox.storeSandboxDevices(); err != nil {
return err
if !c.sandbox.supportNewStore() {
if err := c.sandbox.storeSandboxDevices(); err != nil {
return err
}
}
return nil
}
Expand All @@ -1351,8 +1364,10 @@ func (c *Container) detachDevices() error {
}
}

if err := c.sandbox.storeSandboxDevices(); err != nil {
return err
if !c.sandbox.supportNewStore() {
if err := c.sandbox.storeSandboxDevices(); err != nil {
return err
}
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions virtcontainers/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func TestContainerRemoveDrive(t *testing.T) {
ctx: context.Background(),
id: "sandbox",
devManager: manager.NewDeviceManager(manager.VirtioSCSI, nil),
config: &SandboxConfig{},
}

vcStore, err := store.NewVCSandboxStore(sandbox.ctx, sandbox.id)
Expand Down
14 changes: 12 additions & 2 deletions virtcontainers/device/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,37 @@ type DeviceReceiver interface {
type Device interface {
Attach(DeviceReceiver) error
Detach(DeviceReceiver) error

// ID returns device identifier
DeviceID() string

// DeviceType indicates which kind of device it is
// e.g. block, vfio or vhost user
DeviceType() config.DeviceType

// GetMajorMinor returns major and minor numbers
GetMajorMinor() (int64, int64)

// GetDeviceInfo returns device specific data used for hotplugging by hypervisor
// Caller could cast the return value to device specific struct
// e.g. Block device returns *config.BlockDrive and
// vfio device returns []*config.VFIODev
GetDeviceInfo() interface{}

// GetAttachCount returns how many times the device has been attached
GetAttachCount() uint

// Reference adds one reference to device then returns final ref count
Reference() uint

// Dereference removes one reference to device then returns final ref count
Dereference() uint

// Persist convert and return data in persist format
Dump() persistapi.DeviceState
// Save converts Device to DeviceState
Save() persistapi.DeviceState

// Load loads DeviceState and converts it to specific device
Load(persistapi.DeviceState)
}

// DeviceManager can be used to create a new device, this can be used as single
Expand All @@ -79,4 +88,5 @@ type DeviceManager interface {
IsDeviceAttached(string) bool
GetDeviceByID(string) Device
GetAllDevices() []Device
LoadDevices([]persistapi.DeviceState)
}
30 changes: 26 additions & 4 deletions virtcontainers/device/drivers/block.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2017-2018 Intel Corporation
// Copyright (c) 2018 Huawei Corporation
// Copyright (c) 2018-2019 Huawei Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -147,9 +147,9 @@ func (device *BlockDevice) GetDeviceInfo() interface{} {
return device.BlockDrive
}

// Dump convert and return data in persist format
func (device *BlockDevice) Dump() persistapi.DeviceState {
ds := device.GenericDevice.Dump()
// Save converts Device to DeviceState
func (device *BlockDevice) Save() persistapi.DeviceState {
ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType())

drive := device.BlockDrive
Expand All @@ -169,5 +169,27 @@ func (device *BlockDevice) Dump() persistapi.DeviceState {
return ds
}

// Load loads DeviceState and converts it to specific device
func (device *BlockDevice) Load(ds persistapi.DeviceState) {
device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds)

bd := ds.BlockDrive
if bd == nil {
return
}
device.BlockDrive = &config.BlockDrive{
File: bd.File,
Format: bd.Format,
ID: bd.ID,
Index: bd.Index,
MmioAddr: bd.MmioAddr,
PCIAddr: bd.PCIAddr,
SCSIAddr: bd.SCSIAddr,
NvdimmID: bd.NvdimmID,
VirtPath: bd.VirtPath,
}
}

// It should implement GetAttachCount() and DeviceID() as api.Device implementation
// here it shares function from *GenericDevice so we don't need duplicate codes
20 changes: 17 additions & 3 deletions virtcontainers/device/drivers/generic.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2017-2018 Intel Corporation
// Copyright (c) 2018 Huawei Corporation
// Copyright (c) 2018-2019 Huawei Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -117,8 +117,8 @@ func (device *GenericDevice) bumpAttachCount(attach bool) (skip bool, err error)
}
}

// Dump convert and return data in persist format
func (device *GenericDevice) Dump() persistapi.DeviceState {
// Save converts Device to DeviceState
func (device *GenericDevice) Save() persistapi.DeviceState {
dss := persistapi.DeviceState{
ID: device.ID,
Type: string(device.DeviceType()),
Expand All @@ -135,3 +135,17 @@ func (device *GenericDevice) Dump() persistapi.DeviceState {
}
return dss
}

// Load loads DeviceState and converts it to specific device
func (device *GenericDevice) Load(ds persistapi.DeviceState) {
device.ID = ds.ID
device.RefCount = ds.RefCount
device.AttachCount = ds.AttachCount

device.DeviceInfo = &config.DeviceInfo{
DevType: ds.DevType,
Major: ds.Major,
Minor: ds.Minor,
DriverOptions: ds.DriverOptions,
}
}
25 changes: 20 additions & 5 deletions virtcontainers/device/drivers/vfio.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2017-2018 Intel Corporation
// Copyright (c) 2018 Huawei Corporation
// Copyright (c) 2018-2019 Huawei Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -140,17 +140,17 @@ func (device *VFIODevice) GetDeviceInfo() interface{} {
return device.VfioDevs
}

// Dump convert and return data in persist format
func (device *VFIODevice) Dump() persistapi.DeviceState {
ds := device.GenericDevice.Dump()
// Save converts Device to DeviceState
func (device *VFIODevice) Save() persistapi.DeviceState {
ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType())

devs := device.VfioDevs
for _, dev := range devs {
if dev != nil {
ds.VFIODevs = append(ds.VFIODevs, &persistapi.VFIODev{
ID: dev.ID,
Type: string(dev.Type),
Type: uint32(dev.Type),
BDF: dev.BDF,
SysfsDev: dev.SysfsDev,
})
Expand All @@ -159,6 +159,21 @@ func (device *VFIODevice) Dump() persistapi.DeviceState {
return ds
}

// Load loads DeviceState and converts it to specific device
func (device *VFIODevice) Load(ds persistapi.DeviceState) {
device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds)

for _, dev := range ds.VFIODevs {
device.VfioDevs = append(device.VfioDevs, &config.VFIODev{
ID: dev.ID,
Type: config.VFIODeviceType(dev.Type),
BDF: dev.BDF,
SysfsDev: dev.SysfsDev,
})
}
}

// It should implement GetAttachCount() and DeviceID() as api.Device implementation
// here it shares function from *GenericDevice so we don't need duplicate codes

Expand Down
26 changes: 22 additions & 4 deletions virtcontainers/device/drivers/vhost_user_blk.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2017-2018 Intel Corporation
// Copyright (c) 2018 Huawei Corporation
// Copyright (c) 2018-2019 Huawei Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -72,9 +72,9 @@ func (device *VhostUserBlkDevice) GetDeviceInfo() interface{} {
return &device.VhostUserDeviceAttrs
}

// Dump convert and return data in persist format
func (device *VhostUserBlkDevice) Dump() persistapi.DeviceState {
ds := device.GenericDevice.Dump()
// Save converts Device to DeviceState
func (device *VhostUserBlkDevice) Save() persistapi.DeviceState {
ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType())
ds.VhostUserDev = &persistapi.VhostUserDeviceAttrs{
DevID: device.DevID,
Expand All @@ -85,5 +85,23 @@ func (device *VhostUserBlkDevice) Dump() persistapi.DeviceState {
return ds
}

// Load loads DeviceState and converts it to specific device
func (device *VhostUserBlkDevice) Load(ds persistapi.DeviceState) {
device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds)

dev := ds.VhostUserDev
if dev == nil {
return
}

device.VhostUserDeviceAttrs = config.VhostUserDeviceAttrs{
DevID: dev.DevID,
SocketPath: dev.SocketPath,
Type: config.DeviceType(dev.Type),
MacAddress: dev.MacAddress,
}
}

// It should implement GetAttachCount() and DeviceID() as api.Device implementation
// here it shares function from *GenericDevice so we don't need duplicate codes
26 changes: 22 additions & 4 deletions virtcontainers/device/drivers/vhost_user_net.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2017-2018 Intel Corporation
// Copyright (c) 2018 Huawei Corporation
// Copyright (c) 2018-2019 Huawei Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -73,9 +73,9 @@ func (device *VhostUserNetDevice) GetDeviceInfo() interface{} {
return &device.VhostUserDeviceAttrs
}

// Dump convert and return data in persist format
func (device *VhostUserNetDevice) Dump() persistapi.DeviceState {
ds := device.GenericDevice.Dump()
// Save converts Device to DeviceState
func (device *VhostUserNetDevice) Save() persistapi.DeviceState {
ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType())
ds.VhostUserDev = &persistapi.VhostUserDeviceAttrs{
DevID: device.DevID,
Expand All @@ -86,5 +86,23 @@ func (device *VhostUserNetDevice) Dump() persistapi.DeviceState {
return ds
}

// Load loads DeviceState and converts it to specific device
func (device *VhostUserNetDevice) Load(ds persistapi.DeviceState) {
device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds)

dev := ds.VhostUserDev
if dev == nil {
return
}

device.VhostUserDeviceAttrs = config.VhostUserDeviceAttrs{
DevID: dev.DevID,
SocketPath: dev.SocketPath,
Type: config.DeviceType(dev.Type),
MacAddress: dev.MacAddress,
}
}

// It should implement GetAttachCount() and DeviceID() as api.Device implementation
// here it shares function from *GenericDevice so we don't need duplicate codes
Loading

0 comments on commit 4c19213

Please sign in to comment.