From df565e1b992359d5ef46d99bbda994d95bf59d1c Mon Sep 17 00:00:00 2001 From: Mikhail Malyshev Date: Tue, 27 Oct 2026 08:42:23 +0000 Subject: [PATCH 1/2] snapshot: read /sys/class from the root, not the working directory cloneContentByClass looked for sys/class/ relative to the working directory, so a snapshot taken anywhere but / silently left out every network interface and GPU: ghwc snapshot run from a scratch directory on a host with physical NICs produced a snapshot without /sys/class/net. Every other path the snapshot clones is absolute; this one is now too. Signed-off-by: Mikhail Malyshev (cherry picked from commit 965ad27728acb66b12aeefa2b1e01150c9303fce, jaypipes/ghw#480) --- pkg/snapshot/clonetree_linux.go | 2 +- pkg/snapshot/clonetree_linux_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/snapshot/clonetree_linux.go b/pkg/snapshot/clonetree_linux.go index b174bef3..6187d41e 100644 --- a/pkg/snapshot/clonetree_linux.go +++ b/pkg/snapshot/clonetree_linux.go @@ -74,7 +74,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..79e9b308 100644 --- a/pkg/snapshot/clonetree_linux_test.go +++ b/pkg/snapshot/clonetree_linux_test.go @@ -136,3 +136,27 @@ 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) + } +} From 846888c03d1113112bf94a0cf560a664e3a76d00 Mon Sep 17 00:00:00 2001 From: Mikhail Malyshev Date: Tue, 27 Oct 2026 10:31:28 +0000 Subject: [PATCH 2/2] snapshot: capture the DeviceTree properties ghw reads On a board without DMI, ghw takes the CPU model, the baseboard and chassis information and the U-Boot version from the DeviceTree under /sys/firmware/devicetree/base, but a snapshot captured none of it: a snapshot of a Jetson AGX Orin reported an empty CPU model where the board itself reports nvidia,tegra234. Capture compatible, model, chassis-type and chosen/u-boot,version. Like the MAC addresses, serial-number is left out: it identifies the board. Signed-off-by: Mikhail Malyshev (cherry picked from commit 31d9c94756e6269c0dd0d93ee07f61443f1b8c4e, jaypipes/ghw#481) --- pkg/snapshot/clonetree_linux.go | 6 ++++ pkg/snapshot/clonetree_linux_test.go | 47 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pkg/snapshot/clonetree_linux.go b/pkg/snapshot/clonetree_linux.go index 6187d41e..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", } } diff --git a/pkg/snapshot/clonetree_linux_test.go b/pkg/snapshot/clonetree_linux_test.go index 79e9b308..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 @@ -160,3 +163,47 @@ func TestExpectedCloneContentByClassFromAnyDir(t *testing.T) { 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) + } +}