github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/device/drivers/vhost_user_fs.go (about)

     1  // Copyright (C) 2019 Red Hat, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package drivers
     7  
     8  import (
     9  	"encoding/hex"
    10  
    11  	"github.com/kata-containers/runtime/virtcontainers/device/api"
    12  	"github.com/kata-containers/runtime/virtcontainers/device/config"
    13  	"github.com/kata-containers/runtime/virtcontainers/utils"
    14  )
    15  
    16  // VhostUserFSDevice is a virtio-fs vhost-user device
    17  type VhostUserFSDevice struct {
    18  	*GenericDevice
    19  	config.VhostUserDeviceAttrs
    20  }
    21  
    22  // Device interface
    23  
    24  func (device *VhostUserFSDevice) Attach(devReceiver api.DeviceReceiver) (err error) {
    25  	skip, err := device.bumpAttachCount(true)
    26  	if err != nil {
    27  		return err
    28  	}
    29  	if skip {
    30  		return nil
    31  	}
    32  
    33  	defer func() {
    34  		if err != nil {
    35  			device.bumpAttachCount(false)
    36  		}
    37  	}()
    38  
    39  	// generate a unique ID to be used for hypervisor commandline fields
    40  	randBytes, err := utils.GenerateRandomBytes(8)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	id := hex.EncodeToString(randBytes)
    45  
    46  	device.DevID = id
    47  	device.Type = device.DeviceType()
    48  
    49  	return devReceiver.AppendDevice(device)
    50  }
    51  
    52  func (device *VhostUserFSDevice) Detach(devReceiver api.DeviceReceiver) error {
    53  	_, err := device.bumpAttachCount(false)
    54  	return err
    55  }
    56  
    57  func (device *VhostUserFSDevice) DeviceType() config.DeviceType {
    58  	return config.VhostUserFS
    59  }
    60  
    61  // GetDeviceInfo returns device information that the device is created based on
    62  func (device *VhostUserFSDevice) GetDeviceInfo() interface{} {
    63  	device.Type = device.DeviceType()
    64  	return &device.VhostUserDeviceAttrs
    65  }