runtime: redefine and add functions to handle VirtualVolume to storage

1) Extract function `handleBlockVolume` to create Storage only.
2) Add functions to handle KataVirtualVolume device and construct
   corresponding storages.

Signed-off-by: ChengyuZhu6 <chengyu.zhu@intel.com>
This commit is contained in:
ChengyuZhu6
2023-09-04 13:29:16 +08:00
parent 29eb2c02d9
commit 5ad3eba8b1

View File

@@ -83,42 +83,44 @@ const (
type customRequestTimeoutKeyType struct{} type customRequestTimeoutKeyType struct{}
var ( var (
checkRequestTimeout = 30 * time.Second checkRequestTimeout = 30 * time.Second
defaultRequestTimeout = 60 * time.Second defaultRequestTimeout = 60 * time.Second
imageRequestTimeout = 60 * time.Second imageRequestTimeout = 60 * time.Second
remoteRequestTimeout = 300 * time.Second remoteRequestTimeout = 300 * time.Second
customRequestTimeoutKey = customRequestTimeoutKeyType(struct{}{}) customRequestTimeoutKey = customRequestTimeoutKeyType(struct{}{})
errorMissingOCISpec = errors.New("Missing OCI specification") errorMissingOCISpec = errors.New("Missing OCI specification")
defaultKataHostSharedDir = "/run/kata-containers/shared/sandboxes/" defaultKataGuestVirtualVolumedir = "/run/kata-containers/virtual-volumes/"
defaultKataGuestSharedDir = "/run/kata-containers/shared/containers/" defaultKataHostSharedDir = "/run/kata-containers/shared/sandboxes/"
defaultKataGuestNydusRootDir = "/run/kata-containers/shared/" defaultKataGuestSharedDir = "/run/kata-containers/shared/containers/"
mountGuestTag = "kataShared" defaultKataGuestNydusRootDir = "/run/kata-containers/shared/"
defaultKataGuestSandboxDir = "/run/kata-containers/sandbox/" mountGuestTag = "kataShared"
type9pFs = "9p" defaultKataGuestSandboxDir = "/run/kata-containers/sandbox/"
typeVirtioFS = "virtiofs" type9pFs = "9p"
typeOverlayFS = "overlay" typeVirtioFS = "virtiofs"
kata9pDevType = "9p" typeOverlayFS = "overlay"
kataMmioBlkDevType = "mmioblk" kata9pDevType = "9p"
kataBlkDevType = "blk" kataMmioBlkDevType = "mmioblk"
kataBlkCCWDevType = "blk-ccw" kataBlkDevType = "blk"
kataSCSIDevType = "scsi" kataBlkCCWDevType = "blk-ccw"
kataNvdimmDevType = "nvdimm" kataSCSIDevType = "scsi"
kataVirtioFSDevType = "virtio-fs" kataNvdimmDevType = "nvdimm"
kataOverlayDevType = "overlayfs" kataVirtioFSDevType = "virtio-fs"
kataWatchableBindDevType = "watchable-bind" kataOverlayDevType = "overlayfs"
kataVfioPciDevType = "vfio-pci" // VFIO device to used as VFIO in the container kataWatchableBindDevType = "watchable-bind"
kataVfioPciGuestKernelDevType = "vfio-pci-gk" // VFIO device for consumption by the guest kernel kataVfioPciDevType = "vfio-pci" // VFIO device to used as VFIO in the container
kataVfioApDevType = "vfio-ap" kataVfioPciGuestKernelDevType = "vfio-pci-gk" // VFIO device for consumption by the guest kernel
sharedDir9pOptions = []string{"trans=virtio,version=9p2000.L,cache=mmap", "nodev"} kataVfioApDevType = "vfio-ap"
sharedDirVirtioFSOptions = []string{} kataDmVerityBlkDevType = "dmverity"
sharedDirVirtioFSDaxOptions = "dax" sharedDir9pOptions = []string{"trans=virtio,version=9p2000.L,cache=mmap", "nodev"}
shmDir = "shm" sharedDirVirtioFSOptions = []string{}
kataEphemeralDevType = "ephemeral" sharedDirVirtioFSDaxOptions = "dax"
defaultEphemeralPath = filepath.Join(defaultKataGuestSandboxDir, kataEphemeralDevType) shmDir = "shm"
grpcMaxDataSize = int64(1024 * 1024) kataEphemeralDevType = "ephemeral"
localDirOptions = []string{"mode=0777"} defaultEphemeralPath = filepath.Join(defaultKataGuestSandboxDir, kataEphemeralDevType)
maxHostnameLen = 64 grpcMaxDataSize = int64(1024 * 1024)
GuestDNSFile = "/etc/resolv.conf" localDirOptions = []string{"mode=0777"}
maxHostnameLen = 64
GuestDNSFile = "/etc/resolv.conf"
) )
const ( const (
@@ -1544,14 +1546,36 @@ func (k *kataAgent) handleLocalStorage(mounts []specs.Mount, sandboxID string, r
return localStorages, nil return localStorages, nil
} }
// handleDeviceBlockVolume handles volume that is block device file // Add the source block type to DriverOptions in the volume with dm-verity
// and DeviceBlock type. func handleDmVerityBlockVolume(driverType, source string, verityInfo *types.DmVerityInfo, vol *grpc.Storage) (*grpc.Storage, error) {
func (k *kataAgent) handleDeviceBlockVolume(c *Container, m Mount, device api.Device) (*grpc.Storage, error) { no, err := json.Marshal(verityInfo)
if err != nil {
return nil, err
}
vol.Driver = kataDmVerityBlkDevType
vol.DriverOptions = append(vol.DriverOptions, "verity_info="+string(no))
switch driverType {
case kataNvdimmDevType:
vol.DriverOptions = append(vol.DriverOptions, "source_type=pmem")
case kataBlkCCWDevType:
vol.DriverOptions = append(vol.DriverOptions, "source_type=virtio_ccw")
case kataBlkDevType:
vol.DriverOptions = append(vol.DriverOptions, "source_type=virtio_pci")
case kataMmioBlkDevType:
vol.DriverOptions = append(vol.DriverOptions, "source_type=virtio_mmio")
case kataSCSIDevType:
vol.DriverOptions = append(vol.DriverOptions, "source_type=scsi")
}
vol.Options = []string{"ro"}
vol.MountPoint = filepath.Join(defaultKataGuestVirtualVolumedir, "verity", verityInfo.Hash)
return vol, nil
}
func handleBlockVolume(c *Container, device api.Device) (*grpc.Storage, error) {
vol := &grpc.Storage{} vol := &grpc.Storage{}
blockDrive, ok := device.GetDeviceInfo().(*config.BlockDrive) blockDrive, ok := device.GetDeviceInfo().(*config.BlockDrive)
if !ok || blockDrive == nil { if !ok || blockDrive == nil {
k.Logger().Error("malformed block drive")
return nil, fmt.Errorf("malformed block drive") return nil, fmt.Errorf("malformed block drive")
} }
switch { switch {
@@ -1576,6 +1600,47 @@ func (k *kataAgent) handleDeviceBlockVolume(c *Container, m Mount, device api.De
default: default:
return nil, fmt.Errorf("Unknown block device driver: %s", c.sandbox.config.HypervisorConfig.BlockDeviceDriver) return nil, fmt.Errorf("Unknown block device driver: %s", c.sandbox.config.HypervisorConfig.BlockDeviceDriver)
} }
return vol, nil
}
// handleVirtualVolumeStorageObject handles KataVirtualVolume that is block device file.
func handleVirtualVolumeStorageObject(c *Container, blockDeviceId string, virtVolume *types.KataVirtualVolume) (*grpc.Storage, error) {
var vol *grpc.Storage
if virtVolume.VolumeType == types.KataVirtualVolumeImageRawBlockType || virtVolume.VolumeType == types.KataVirtualVolumeLayerRawBlockType {
device := c.sandbox.devManager.GetDeviceByID(blockDeviceId)
if device == nil {
return nil, fmt.Errorf("Failed to find device by id (id=%s) in handleVirtualVolumeStorageObject", blockDeviceId)
}
var err error
vol, err = handleBlockVolume(c, device)
if err != nil {
return nil, err
}
filename := b64.URLEncoding.EncodeToString([]byte(vol.Source))
vol.MountPoint = filepath.Join(defaultKataGuestVirtualVolumedir, filename)
//convert block storage to dmverity storage if dm-verity info is available
if virtVolume.DmVerity != nil {
vol, err = handleDmVerityBlockVolume(vol.Driver, virtVolume.Source, virtVolume.DmVerity, vol)
if err != nil {
return nil, err
}
}
} else if virtVolume.VolumeType == types.KataVirtualVolumeImageGuestPullType {
///TODO implement the logic with pulling image in the guest.
return nil, nil
}
return vol, nil
}
// handleDeviceBlockVolume handles volume that is block device file
// and DeviceBlock type.
func (k *kataAgent) handleDeviceBlockVolume(c *Container, m Mount, device api.Device) (*grpc.Storage, error) {
vol, err := handleBlockVolume(c, device)
if err != nil {
return nil, err
}
vol.MountPoint = m.Destination vol.MountPoint = m.Destination