...
1
2
3
4 package pci
5
6 import (
7 "context"
8 "fmt"
9 "path/filepath"
10 "strings"
11
12 "github.com/Microsoft/hcsshim/internal/guest/storage"
13 "github.com/Microsoft/hcsshim/internal/guest/storage/vmbus"
14 )
15
16 var storageWaitForFileMatchingPattern = storage.WaitForFileMatchingPattern
17 var vmbusWaitForDevicePath = vmbus.WaitForDevicePath
18
19
20 func WaitForPCIDeviceFromVMBusGUID(ctx context.Context, vmBusGUID string) error {
21 _, err := FindDeviceBusLocationFromVMBusGUID(ctx, vmBusGUID)
22 return err
23 }
24
25
26
27 func FindDeviceBusLocationFromVMBusGUID(ctx context.Context, vmBusGUID string) (string, error) {
28 fullPath, err := FindDeviceFullPath(ctx, vmBusGUID)
29 if err != nil {
30 return "", err
31 }
32
33 _, busFile := filepath.Split(fullPath)
34 return busFile, nil
35 }
36
37
38
39 func FindDeviceFullPath(ctx context.Context, vmBusGUID string) (string, error) {
40 pciDir, err := findVMBusPCIDir(ctx, vmBusGUID)
41 if err != nil {
42 return "", err
43 }
44
45 return findVMBusPCIDevice(ctx, pciDir)
46 }
47
48
49
50
51 func findVMBusPCIDir(ctx context.Context, vmBusGUID string) (string, error) {
52 vmBusPCIPathPattern := filepath.Join(vmBusGUID, "pci*")
53 return vmbusWaitForDevicePath(ctx, vmBusPCIPathPattern)
54 }
55
56
57
58 func findVMBusPCIDevice(ctx context.Context, pciDirFullPath string) (string, error) {
59
60 _, pciDirName := filepath.Split(pciDirFullPath)
61 busPrefix := strings.TrimPrefix(pciDirName, "pci")
62
63 busPathPattern := filepath.Join(pciDirFullPath, fmt.Sprintf("%s*", busPrefix))
64 busFileFullPath, err := storageWaitForFileMatchingPattern(ctx, busPathPattern)
65 if err != nil {
66 return "", err
67 }
68
69 return busFileFullPath, nil
70 }
71
View as plain text