talos/internal/pkg/pci/sysfs.go
Andrey Smirnov 33a631f026
feat: look up Links PCI vendor/product via PCI ID database
This increases `initramfs` size by 356060 bytes (raw text database is
1.3 MiB).

In QEMU:

```
$ talosctl -n 172.20.0.2 get links eth0 -o yaml
spec:
    ...
    productID: "0x1000"
    vendorID: "0x1af4"
    product: Virtio network device
    vendor: Red Hat, Inc.
```

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
2022-05-23 17:21:49 +04:00

56 lines
1.0 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package pci
import (
"bytes"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"strconv"
)
const sysfsPath = "/sys/bus/pci/devices/%s/%s"
func readID(busPath, name string) (uint16, error) {
contents, err := ioutil.ReadFile(fmt.Sprintf(sysfsPath, busPath, name))
if err != nil {
return 0, err
}
v, err := strconv.ParseUint(string(bytes.TrimSpace(contents)), 0, 16)
return uint16(v), err
}
// SysfsDeviceInfo looks up vendor and product ID from sysfs.
func SysfsDeviceInfo(busPath string) (*Device, error) {
var (
d Device
err error
)
d.ProductID, err = readID(busPath, "device")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
return nil, err
}
d.VendorID, err = readID(busPath, "vendor")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
return nil, err
}
return &d, err
}