diff --git a/device.go b/device.go index abe1d465dc..99c162dba2 100644 --- a/device.go +++ b/device.go @@ -30,7 +30,11 @@ const ( driverEphemeralType = "ephemeral" ) -const rootBusPath = "/devices/pci0000:00" +const ( + rootBusPath = "/devices/pci0000:00" + pciBusRescanFile = "/sys/bus/pci/rescan" + pciBusMode = 0220 +) var ( sysBusPrefix = "/sys/bus/pci/devices" @@ -58,6 +62,10 @@ var deviceHandlerList = map[string]deviceHandler{ driverSCSIType: virtioSCSIDeviceHandler, } +func rescanPciBus() error { + return ioutil.WriteFile(pciBusRescanFile, []byte{'1'}, pciBusMode) +} + // getDevicePCIAddress fetches the complete PCI address in sysfs, based on the PCI // identifier provided. This should be in the format: "bridgeAddr/deviceAddr". // Here, bridgeAddr is the address at which the brige is attached on the root bus, @@ -122,6 +130,13 @@ func getPCIDeviceName(s *sandbox, pciID string) (string, error) { } } + // Rescan pci bus if we need to wait for a new pci device + if err = rescanPciBus(); err != nil { + fieldLogger.WithError(err).Error("Failed to scan pci bus") + s.Unlock() + return "", err + } + // If device is not found in the device map, hotplug event has not // been received yet, create and add channel to the watchers map. // The key of the watchers map is the device we are interested in. diff --git a/device_test.go b/device_test.go index 39416af2c2..30093074a2 100644 --- a/device_test.go +++ b/device_test.go @@ -443,3 +443,12 @@ func TestUpdateSpecDeviceList(t *testing.T) { err = updateSpecDeviceList(device, spec) assert.NoError(err) } + +func TestRescanPciBus(t *testing.T) { + skipUnlessRoot(t) + + assert := assert.New(t) + + err := rescanPciBus() + assert.Nil(err) +} diff --git a/grpc.go b/grpc.go index bee1031b8c..a673a41f9e 100644 --- a/grpc.go +++ b/grpc.go @@ -39,12 +39,6 @@ type agentGRPC struct { version string } -// PCI scanning -const ( - pciBusRescanFile = "/sys/bus/pci/rescan" - pciBusMode = 0220 -) - // CPU and Memory hotplug const ( cpuRegexpPattern = "cpu[0-9]*" @@ -530,7 +524,7 @@ func (a *agentGRPC) CreateContainer(ctx context.Context, req *pb.CreateContainer // re-scan PCI bus // looking for hidden devices - if err = ioutil.WriteFile(pciBusRescanFile, []byte("1"), pciBusMode); err != nil { + if err = rescanPciBus(); err != nil { agentLog.WithError(err).Warn("Could not rescan PCI bus") }