container-edits_unix.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //go:build !windows
  2. // +build !windows
  3. /*
  4. Copyright © 2021 The CDI Authors
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package cdi
  16. import (
  17. "fmt"
  18. runc "github.com/opencontainers/runc/libcontainer/devices"
  19. )
  20. // fillMissingInfo fills in missing mandatory attributes from the host device.
  21. func (d *DeviceNode) fillMissingInfo() error {
  22. if d.HostPath == "" {
  23. d.HostPath = d.Path
  24. }
  25. if d.Type != "" && (d.Major != 0 || d.Type == "p") {
  26. return nil
  27. }
  28. hostDev, err := runc.DeviceFromPath(d.HostPath, "rwm")
  29. if err != nil {
  30. return fmt.Errorf("failed to stat CDI host device %q: %w", d.HostPath, err)
  31. }
  32. if d.Type == "" {
  33. d.Type = string(hostDev.Type)
  34. } else {
  35. if d.Type != string(hostDev.Type) {
  36. return fmt.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)",
  37. d.Path, d.HostPath, d.Type, string(hostDev.Type))
  38. }
  39. }
  40. if d.Major == 0 && d.Type != "p" {
  41. d.Major = hostDev.Major
  42. d.Minor = hostDev.Minor
  43. }
  44. return nil
  45. }