Skip to content

Commit

Permalink
device: rescan pci bus before waiting for new devices
Browse files Browse the repository at this point in the history
So that we make sure the device pci kobject shows up.

Signed-off-by: Peng Tao <bergwolf@gmail.com>
  • Loading branch information
bergwolf committed Sep 20, 2018
1 parent 1310f3d commit a628496
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
17 changes: 16 additions & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 1 addition & 7 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]*"
Expand Down Expand Up @@ -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")
}

Expand Down

0 comments on commit a628496

Please sign in to comment.