diff --git a/pkg/snapshot/clonetree_linux.go b/pkg/snapshot/clonetree_linux.go index b174bef3..88bff08f 100644 --- a/pkg/snapshot/clonetree_linux.go +++ b/pkg/snapshot/clonetree_linux.go @@ -55,6 +55,12 @@ func ExpectedCloneStaticContent() []string { "/sys/class/tpm/tpm*/caps", "/sys/class/tpm/tpm*/tpm_version_major", "/sys/class/tpm/tpm*/device/vendor", + // The DeviceTree properties ghw reads, but not serial-number: like + // the MAC addresses, it identifies the machine. + "/sys/firmware/devicetree/base/compatible", + "/sys/firmware/devicetree/base/model", + "/sys/firmware/devicetree/base/chassis-type", + "/sys/firmware/devicetree/base/chosen/u-boot,version", } } @@ -74,7 +80,7 @@ func cloneContentByClass(devClass string, subEntries []string, filterName filter // warning: don't use the context package here, this means not even the linuxpath package. // TODO(fromani) remove the path duplication - sysClass := filepath.Join("sys", "class", devClass) + sysClass := filepath.Join("/sys", "class", devClass) entries, err := os.ReadDir(sysClass) if err != nil { // we should not import context, hence we can't Warn() diff --git a/pkg/snapshot/clonetree_linux_test.go b/pkg/snapshot/clonetree_linux_test.go index 5dc14bc6..3cc6e38d 100644 --- a/pkg/snapshot/clonetree_linux_test.go +++ b/pkg/snapshot/clonetree_linux_test.go @@ -15,7 +15,10 @@ import ( "strings" "testing" + "github.com/zededa/ghw/internal/config" + "github.com/zededa/ghw/pkg/linuxdt" "github.com/zededa/ghw/pkg/snapshot" + "github.com/zededa/ghw/pkg/util" ) // NOTE: we intentionally use `os.RemoveAll` - not `snapshot.Cleanup` because we @@ -136,3 +139,71 @@ func includedInto(s string, items []string) bool { } return false } + +// The network interfaces and GPUs a snapshot clones are found under /sys/class +// whatever the working directory. +func TestExpectedCloneContentByClassFromAnyDir(t *testing.T) { + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chdir(cwd) }) + specs := func(dir string) []string { + t.Helper() + if err := os.Chdir(dir); err != nil { + t.Fatal(err) + } + return append(snapshot.ExpectedCloneNetContent(), snapshot.ExpectedCloneGPUContent()...) + } + fromRoot := specs("/") + if len(fromRoot) == 0 { + t.Skip("no physical network interface or GPU on this host") + } + if got := specs(t.TempDir()); !reflect.DeepEqual(got, fromRoot) { + t.Errorf("from another directory = %v, want %v", got, fromRoot) + } +} + +// A snapshot of a DeviceTree board carries the properties ghw reads from the +// DeviceTree, all but the board's serial number. +func TestCloneDeviceTree(t *testing.T) { + host := t.TempDir() + base := filepath.Join(host, "sys", "firmware", "devicetree", "base") + for name, value := range map[string]string{ + "compatible": "nvidia,p3737-0000+p3701-0000\x00nvidia,tegra234\x00", + "model": "Jetson AGX Orin Developer Kit\x00", + "chassis-type": "embedded\x00", + "serial-number": "0123456789\x00", + "chosen/u-boot,version": "2023.04\x00", + } { + path := filepath.Join(base, name) + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(value), 0o644); err != nil { + t.Fatal(err) + } + } + var specs []string + for _, spec := range snapshot.ExpectedCloneStaticContent() { + specs = append(specs, filepath.Join(host, spec)) + } + clone := t.TempDir() + if err := snapshot.CopyFilesInto(context.Background(), specs, clone, nil); err != nil { + t.Fatal(err) + } + read := func(root string) []string { + ctx := config.ContextFromArgs(config.WithChroot(root)) + return []string{ + linuxdt.Model(ctx), linuxdt.Vendor(ctx), linuxdt.SoC(ctx), + linuxdt.ChassisType(ctx), linuxdt.UBootVersion(ctx), + } + } + if got, want := read(filepath.Join(clone, host)), read(host); !reflect.DeepEqual(got, want) { + t.Errorf("from the snapshot = %q, want %q", got, want) + } + ctx := config.ContextFromArgs(config.WithChroot(filepath.Join(clone, host))) + if serial := linuxdt.SerialNumber(ctx); serial != util.UNKNOWN { + t.Errorf("snapshot carries the serial number %q", serial) + } +}