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

Commit

Permalink
Merge pull request #282 from WeiZhang555/device-manager
Browse files Browse the repository at this point in the history
virtcontainers: refactor device.go to device manager
  • Loading branch information
Eric Ernst authored May 8, 2018
2 parents 81503d7 + f4a453b commit fa848ba
Show file tree
Hide file tree
Showing 40 changed files with 1,935 additions and 1,443 deletions.
2 changes: 2 additions & 0 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"syscall"

deviceApi "github.com/kata-containers/runtime/virtcontainers/device/api"
"github.com/sirupsen/logrus"
)

Expand All @@ -27,6 +28,7 @@ func SetLogger(logger logrus.FieldLogger) {
}

virtLog = logger.WithFields(fields)
deviceApi.SetLogger(virtLog)
}

// CreateSandbox is the virtcontainers sandbox creation entry point.
Expand Down
40 changes: 23 additions & 17 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (

"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"

"github.com/kata-containers/runtime/virtcontainers/device/api"
"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/device/drivers"
"github.com/kata-containers/runtime/virtcontainers/utils"
)

// Process gathers data related to a container process.
Expand Down Expand Up @@ -83,7 +88,7 @@ type ContainerConfig struct {
Mounts []Mount

// Device configuration for devices that must be available within the container.
DeviceInfos []DeviceInfo
DeviceInfos []config.DeviceInfo

// Resources container resources
Resources ContainerResources
Expand Down Expand Up @@ -134,7 +139,7 @@ type Container struct {

mounts []Mount

devices []Device
devices []api.Device

systemMountsInfo SystemMountsInfo
}
Expand Down Expand Up @@ -245,7 +250,7 @@ func (c *Container) storeDevices() error {
return c.sandbox.storage.storeContainerDevices(c.sandboxID, c.id, c.devices)
}

func (c *Container) fetchDevices() ([]Device, error) {
func (c *Container) fetchDevices() ([]api.Device, error) {
return c.sandbox.storage.fetchContainerDevices(c.sandboxID, c.id)
}

Expand Down Expand Up @@ -321,17 +326,18 @@ func (c *Container) mountSharedDirMounts(hostSharedDir, guestSharedDir string) (
// Check if mount is a block device file. If it is, the block device will be attached to the host
// instead of passing this as a shared mount.
if c.checkBlockDeviceSupport() && stat.Mode&unix.S_IFBLK == unix.S_IFBLK {
b := &BlockDevice{
DeviceType: DeviceBlock,
DeviceInfo: DeviceInfo{
// TODO: remove dependency of package drivers
b := &drivers.BlockDevice{
DevType: config.DeviceBlock,
DeviceInfo: config.DeviceInfo{
HostPath: m.Source,
ContainerPath: m.Destination,
DevType: "b",
},
}

// Attach this block device, all other devices passed in the config have been attached at this point
if err := b.attach(c.sandbox.hypervisor, c); err != nil {
if err := b.Attach(c.sandbox); err != nil {
return nil, err
}

Expand All @@ -346,7 +352,7 @@ func (c *Container) mountSharedDirMounts(hostSharedDir, guestSharedDir string) (
continue
}

randBytes, err := generateRandomBytes(8)
randBytes, err := utils.GenerateRandomBytes(8)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -449,7 +455,7 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
// If devices were not found in storage, create Device implementations
// from the configuration. This should happen at create.

devices, err := newDevices(contConfig.DeviceInfos)
devices, err := sandbox.devManager.NewDevices(contConfig.DeviceInfos)
if err != nil {
return &Container{}, err
}
Expand Down Expand Up @@ -824,8 +830,8 @@ func (c *Container) hotplugDrive() error {
}

// Add drive with id as container id
devID := makeNameID("drive", c.id)
drive := Drive{
devID := utils.MakeNameID("drive", c.id, maxDevIDSize)
drive := drivers.Drive{
File: devicePath,
Format: "raw",
ID: devID,
Expand Down Expand Up @@ -861,8 +867,8 @@ func (c *Container) removeDrive() (err error) {
if c.isDriveUsed() && c.state.HotpluggedDrive {
c.Logger().Info("unplugging block device")

devID := makeNameID("drive", c.id)
drive := &Drive{
devID := utils.MakeNameID("drive", c.id, maxDevIDSize)
drive := &drivers.Drive{
ID: devID,
}

Expand All @@ -880,7 +886,7 @@ func (c *Container) removeDrive() (err error) {

func (c *Container) attachDevices() error {
for _, device := range c.devices {
if err := device.attach(c.sandbox.hypervisor, c); err != nil {
if err := device.Attach(c.sandbox); err != nil {
return err
}
}
Expand All @@ -890,7 +896,7 @@ func (c *Container) attachDevices() error {

func (c *Container) detachDevices() error {
for _, device := range c.devices {
if err := device.detach(c.sandbox.hypervisor); err != nil {
if err := device.Detach(c.sandbox); err != nil {
return err
}
}
Expand All @@ -904,7 +910,7 @@ func (c *Container) addResources() error {
return nil
}

vCPUs := ConstraintsToVCPUs(c.config.Resources.CPUQuota, c.config.Resources.CPUPeriod)
vCPUs := utils.ConstraintsToVCPUs(c.config.Resources.CPUQuota, c.config.Resources.CPUPeriod)
if vCPUs != 0 {
virtLog.Debugf("hot adding %d vCPUs", vCPUs)
if err := c.sandbox.hypervisor.hotplugAddDevice(uint32(vCPUs), cpuDev); err != nil {
Expand All @@ -923,7 +929,7 @@ func (c *Container) removeResources() error {
return nil
}

vCPUs := ConstraintsToVCPUs(c.config.Resources.CPUQuota, c.config.Resources.CPUPeriod)
vCPUs := utils.ConstraintsToVCPUs(c.config.Resources.CPUQuota, c.config.Resources.CPUPeriod)
if vCPUs != 0 {
virtLog.Debugf("hot removing %d vCPUs", vCPUs)
if err := c.sandbox.hypervisor.hotplugRemoveDevice(uint32(vCPUs), cpuDev); err != nil {
Expand Down
Loading

0 comments on commit fa848ba

Please sign in to comment.