mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-26 18:44:47 +01:00
Fixes #635 Remove `Hotplugged bool` field from device and add two new fields instead: * `RefCount`: how many references to this device. One device can be referenced(`NewDevice()`) many times by same/different container(s), two devices are regarded identical if they have same hostPath * `AttachCount`: how many times this device has been attached. A device can only be hotplugged once to the qemu, every new Attach command will add the AttachCount, and real `Detach` will be done only when `AttachCount == 0` Signed-off-by: Wei Zhang <zhangwei555@huawei.com>
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
// Copyright (c) 2017-2018 Intel Corporation
|
|
// Copyright (c) 2018 Huawei Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package drivers
|
|
|
|
import (
|
|
"encoding/hex"
|
|
|
|
"github.com/kata-containers/runtime/virtcontainers/device/api"
|
|
"github.com/kata-containers/runtime/virtcontainers/device/config"
|
|
"github.com/kata-containers/runtime/virtcontainers/utils"
|
|
)
|
|
|
|
// VhostUserSCSIDevice is a SCSI vhost-user based device
|
|
type VhostUserSCSIDevice struct {
|
|
*GenericDevice
|
|
config.VhostUserDeviceAttrs
|
|
}
|
|
|
|
//
|
|
// VhostUserSCSIDevice's implementation of the device interface:
|
|
//
|
|
|
|
// Attach is standard interface of api.Device, it's used to add device to some
|
|
// DeviceReceiver
|
|
func (device *VhostUserSCSIDevice) Attach(devReceiver api.DeviceReceiver) (err error) {
|
|
skip, err := device.bumpAttachCount(true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if skip {
|
|
return nil
|
|
}
|
|
|
|
// generate a unique ID to be used for hypervisor commandline fields
|
|
randBytes, err := utils.GenerateRandomBytes(8)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
id := hex.EncodeToString(randBytes)
|
|
|
|
device.DevID = id
|
|
device.Type = device.DeviceType()
|
|
|
|
defer func() {
|
|
if err == nil {
|
|
device.AttachCount = 1
|
|
}
|
|
}()
|
|
return devReceiver.AppendDevice(device)
|
|
}
|
|
|
|
// Detach is standard interface of api.Device, it's used to remove device from some
|
|
// DeviceReceiver
|
|
func (device *VhostUserSCSIDevice) Detach(devReceiver api.DeviceReceiver) error {
|
|
skip, err := device.bumpAttachCount(false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if skip {
|
|
return nil
|
|
}
|
|
device.AttachCount = 0
|
|
return nil
|
|
}
|
|
|
|
// DeviceType is standard interface of api.Device, it returns device type
|
|
func (device *VhostUserSCSIDevice) DeviceType() config.DeviceType {
|
|
return config.VhostUserSCSI
|
|
}
|
|
|
|
// GetDeviceInfo returns device information used for creating
|
|
func (device *VhostUserSCSIDevice) GetDeviceInfo() interface{} {
|
|
device.Type = device.DeviceType()
|
|
return &device.VhostUserDeviceAttrs
|
|
}
|
|
|
|
// It should implement GetAttachCount() and DeviceID() as api.Device implementation
|
|
// here it shares function from *GenericDevice so we don't need duplicate codes
|