diff --git a/src/runtime/virtcontainers/acrn.go b/src/runtime/virtcontainers/acrn.go index c742bbee9b..7540ad653d 100644 --- a/src/runtime/virtcontainers/acrn.go +++ b/src/runtime/virtcontainers/acrn.go @@ -28,6 +28,7 @@ import ( "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/uuid" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" + vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" ) // Since ACRN is using the store in a quite abnormal way, let's first draw it back from store to here @@ -550,8 +551,8 @@ func (a *Acrn) updateBlockDevice(drive *config.BlockDrive) error { slot := AcrnBlkdDevSlot[drive.Index] - //Explicitly set PCIAddr to NULL, so that VirtPath can be used - drive.PCIAddr = "" + //Explicitly set PCIPath to NULL, so that VirtPath can be used + drive.PCIPath = vcTypes.PciPath{} args := []string{"blkrescan", a.acrnConfig.Name, fmt.Sprintf("%d,%s", slot, drive.File)} diff --git a/src/runtime/virtcontainers/clh.go b/src/runtime/virtcontainers/clh.go index 097c23931d..79e56904f0 100644 --- a/src/runtime/virtcontainers/clh.go +++ b/src/runtime/virtcontainers/clh.go @@ -32,6 +32,7 @@ import ( "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" + vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" ) // @@ -439,8 +440,8 @@ func (clh *cloudHypervisor) hotplugAddBlockDevice(drive *config.BlockDrive) erro driveID := clhDriveIndexToID(drive.Index) - //Explicitly set PCIAddr to NULL, so that VirtPath can be used - drive.PCIAddr = "" + //Explicitly set PCIPath to NULL, so that VirtPath can be used + drive.PCIPath = vcTypes.PciPath{} if drive.Pmem { err = fmt.Errorf("pmem device hotplug not supported") diff --git a/src/runtime/virtcontainers/device/config/config.go b/src/runtime/virtcontainers/device/config/config.go index e59278ee2c..aeacbd98cf 100644 --- a/src/runtime/virtcontainers/device/config/config.go +++ b/src/runtime/virtcontainers/device/config/config.go @@ -16,6 +16,7 @@ import ( "github.com/go-ini/ini" "golang.org/x/sys/unix" + vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" ) // DeviceType indicates device type @@ -156,8 +157,8 @@ type BlockDrive struct { // MmioAddr is used to identify the slot at which the drive is attached (order?). MmioAddr string - // PCIAddr is the PCI address used to identify the slot at which the drive is attached. - PCIAddr string + // PCIPath is the PCI path used to identify the slot at which the drive is attached. + PCIPath vcTypes.PciPath // SCSI Address of the block device, in case the device is attached using SCSI driver // SCSI address is in the format SCSI-Id:LUN diff --git a/src/runtime/virtcontainers/device/drivers/block.go b/src/runtime/virtcontainers/device/drivers/block.go index e37e69c551..7ffa23e769 100644 --- a/src/runtime/virtcontainers/device/drivers/block.go +++ b/src/runtime/virtcontainers/device/drivers/block.go @@ -170,7 +170,7 @@ func (device *BlockDevice) Save() persistapi.DeviceState { ID: drive.ID, Index: drive.Index, MmioAddr: drive.MmioAddr, - PCIAddr: drive.PCIAddr, + PCIPath: drive.PCIPath, SCSIAddr: drive.SCSIAddr, NvdimmID: drive.NvdimmID, VirtPath: drive.VirtPath, @@ -196,7 +196,7 @@ func (device *BlockDevice) Load(ds persistapi.DeviceState) { ID: bd.ID, Index: bd.Index, MmioAddr: bd.MmioAddr, - PCIAddr: bd.PCIAddr, + PCIPath: bd.PCIPath, SCSIAddr: bd.SCSIAddr, NvdimmID: bd.NvdimmID, VirtPath: bd.VirtPath, diff --git a/src/runtime/virtcontainers/kata_agent.go b/src/runtime/virtcontainers/kata_agent.go index 2bbdcef496..a506d5b74b 100644 --- a/src/runtime/virtcontainers/kata_agent.go +++ b/src/runtime/virtcontainers/kata_agent.go @@ -1074,7 +1074,7 @@ func (k *kataAgent) appendBlockDevice(dev ContainerDevice, c *Container) *grpc.D kataDevice.Id = d.DevNo case config.VirtioBlock: kataDevice.Type = kataBlkDevType - kataDevice.Id = d.PCIAddr + kataDevice.Id = d.PCIPath.String() kataDevice.VmPath = d.VirtPath case config.VirtioSCSI: kataDevice.Type = kataSCSIDevType @@ -1178,10 +1178,10 @@ func (k *kataAgent) buildContainerRootfs(sandbox *Sandbox, c *Container, rootPat rootfs.Source = blockDrive.DevNo case sandbox.config.HypervisorConfig.BlockDeviceDriver == config.VirtioBlock: rootfs.Driver = kataBlkDevType - if blockDrive.PCIAddr == "" { + if blockDrive.PCIPath.IsNil() { rootfs.Source = blockDrive.VirtPath } else { - rootfs.Source = blockDrive.PCIAddr + rootfs.Source = blockDrive.PCIPath.String() } case sandbox.config.HypervisorConfig.BlockDeviceDriver == config.VirtioSCSI: @@ -1427,10 +1427,10 @@ func (k *kataAgent) handleDeviceBlockVolume(c *Container, m Mount, device api.De vol.Source = blockDrive.DevNo case c.sandbox.config.HypervisorConfig.BlockDeviceDriver == config.VirtioBlock: vol.Driver = kataBlkDevType - if blockDrive.PCIAddr == "" { + if blockDrive.PCIPath.IsNil() { vol.Source = blockDrive.VirtPath } else { - vol.Source = blockDrive.PCIAddr + vol.Source = blockDrive.PCIPath.String() } case c.sandbox.config.HypervisorConfig.BlockDeviceDriver == config.VirtioMmio: vol.Driver = kataMmioBlkDevType diff --git a/src/runtime/virtcontainers/kata_agent_test.go b/src/runtime/virtcontainers/kata_agent_test.go index 7a7cb73fec..226504d0fe 100644 --- a/src/runtime/virtcontainers/kata_agent_test.go +++ b/src/runtime/virtcontainers/kata_agent_test.go @@ -32,6 +32,7 @@ import ( "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/mock" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/rootless" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" + vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" ) var ( @@ -39,7 +40,7 @@ var ( testBlockDeviceCtrPath = "testBlockDeviceCtrPath" testDevNo = "testDevNo" testNvdimmID = "testNvdimmID" - testPCIAddr = "04/02" + testPCIPath, _ = vcTypes.PciPathFromString("04/02") testSCSIAddr = "testSCSIAddr" testVirtPath = "testVirtPath" ) @@ -271,13 +272,13 @@ func TestHandleDeviceBlockVolume(t *testing.T) { inputMount: Mount{}, inputDev: &drivers.BlockDevice{ BlockDrive: &config.BlockDrive{ - PCIAddr: testPCIAddr, + PCIPath: testPCIPath, VirtPath: testVirtPath, }, }, resultVol: &pb.Storage{ Driver: kataBlkDevType, - Source: testPCIAddr, + Source: testPCIPath.String(), }, }, { @@ -353,16 +354,17 @@ func TestHandleBlockVolume(t *testing.T) { bDestination := "/DeviceBlock/destination" dDestination := "/DeviceDirectBlock/destination" vPCIAddr := "0001:01" - bPCIAddr := "0002:01" - dPCIAddr := "0003:01" + bPCIPath, err := vcTypes.PciPathFromString("03/04") + assert.NoError(t, err) + dPCIPath, err := vcTypes.PciPathFromString("04/05") vDev := drivers.NewVhostUserBlkDevice(&config.DeviceInfo{ID: vDevID}) bDev := drivers.NewBlockDevice(&config.DeviceInfo{ID: bDevID}) dDev := drivers.NewBlockDevice(&config.DeviceInfo{ID: dDevID}) vDev.VhostUserDeviceAttrs = &config.VhostUserDeviceAttrs{PCIAddr: vPCIAddr} - bDev.BlockDrive = &config.BlockDrive{PCIAddr: bPCIAddr} - dDev.BlockDrive = &config.BlockDrive{PCIAddr: dPCIAddr} + bDev.BlockDrive = &config.BlockDrive{PCIPath: bPCIPath} + dDev.BlockDrive = &config.BlockDrive{PCIPath: dPCIPath} var devices []api.Device devices = append(devices, vDev, bDev, dDev) @@ -418,14 +420,14 @@ func TestHandleBlockVolume(t *testing.T) { Fstype: "bind", Options: []string{"bind"}, Driver: kataBlkDevType, - Source: bPCIAddr, + Source: bPCIPath.String(), } dStorage := &pb.Storage{ MountPoint: dDestination, Fstype: "ext4", Options: []string{"ro"}, Driver: kataBlkDevType, - Source: dPCIAddr, + Source: dPCIPath.String(), } assert.Equal(t, vStorage, volumeStorages[0], "Error while handle VhostUserBlk type block volume") @@ -462,7 +464,7 @@ func TestAppendDevices(t *testing.T) { ID: id, }, BlockDrive: &config.BlockDrive{ - PCIAddr: testPCIAddr, + PCIPath: testPCIPath, }, }, } @@ -489,7 +491,7 @@ func TestAppendDevices(t *testing.T) { { Type: kataBlkDevType, ContainerPath: testBlockDeviceCtrPath, - Id: testPCIAddr, + Id: testPCIPath.String(), }, } updatedDevList := k.appendDevices(devList, c) @@ -509,7 +511,7 @@ func TestAppendVhostUserBlkDevices(t *testing.T) { }, VhostUserDeviceAttrs: &config.VhostUserDeviceAttrs{ Type: config.VhostUserBlk, - PCIAddr: testPCIAddr, + PCIAddr: testPCIPath.String(), }, }, } @@ -537,7 +539,7 @@ func TestAppendVhostUserBlkDevices(t *testing.T) { { Type: kataBlkDevType, ContainerPath: testBlockDeviceCtrPath, - Id: testPCIAddr, + Id: testPCIPath.String(), }, } updatedDevList := k.appendDevices(devList, c) diff --git a/src/runtime/virtcontainers/persist/api/device.go b/src/runtime/virtcontainers/persist/api/device.go index 8900c5f6fa..9c8b0814ad 100644 --- a/src/runtime/virtcontainers/persist/api/device.go +++ b/src/runtime/virtcontainers/persist/api/device.go @@ -6,6 +6,8 @@ package persistapi +import vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" + // ============= sandbox level resources ============= // BlockDrive represents a block storage drive which may be used in case the storage @@ -26,8 +28,8 @@ type BlockDrive struct { // MmioAddr is used to identify the slot at which the drive is attached (order?). MmioAddr string - // PCIAddr is the PCI address used to identify the slot at which the drive is attached. - PCIAddr string + // PCIPath is the PCI path used to identify the slot at which the drive is attached. + PCIPath vcTypes.PciPath // SCSI Address of the block device, in case the device is attached using SCSI driver // SCSI address is in the format SCSI-Id:LUN diff --git a/src/runtime/virtcontainers/qemu.go b/src/runtime/virtcontainers/qemu.go index 39135ae2e8..e9cfa4c2e0 100644 --- a/src/runtime/virtcontainers/qemu.go +++ b/src/runtime/virtcontainers/qemu.go @@ -1289,8 +1289,18 @@ func (q *qemu) hotplugAddBlockDevice(drive *config.BlockDrive, op operation, dev } }() - // PCI address is in the format bridge-addr/device-addr eg. "03/02" - drive.PCIAddr = fmt.Sprintf("%02x", bridge.Addr) + "/" + addr + bridgeSlot, err := vcTypes.PciSlotFromInt(bridge.Addr) + if err != nil { + return err + } + devSlot, err := vcTypes.PciSlotFromString(addr) + if err != nil { + return err + } + drive.PCIPath, err = vcTypes.PciPathFromSlots(bridgeSlot, devSlot) + if err != nil { + return err + } if err = q.qmpMonitorCh.qmp.ExecutePCIDeviceAdd(q.qmpMonitorCh.ctx, drive.ID, devID, driver, addr, bridge.ID, romFile, 0, true, defaultDisableModern); err != nil { return err