Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ jobs:
GHW_TESTING_SKIP_GPU: "1"
run: go test -v ./...

windows-2022:
runs-on: windows-2022
windows-latest:
runs-on: windows-latest
strategy:
matrix:
go: ['1.23', '1.24', '1.24']
Expand Down Expand Up @@ -119,8 +119,8 @@ jobs:
# the tests have block skipped because we cannot get meaningful information
# about the block devices in the Github Actions Runner virtual machines. So
# this is really just a test of whether the library builds on MacOS 13.
macos-13:
runs-on: macos-13
macos-latest:
runs-on: macos-latest
strategy:
matrix:
go: ['1.22', '1.23']
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
coverage*.*
*~
bin/
27 changes: 23 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
.PHONY: test
VERSION ?= $(shell git describe --tags --always --dirty)

.PHONY: test clean vet fmt fmtcheck build run

bin/ghwc:
@cd cmd/ghwc && go build -o ../../bin/ghwc main.go && cd ../../

# If the first argument is "run"...
ifeq (run,$(firstword $(MAKECMDGOALS)))
# use the rest as arguments for "run"
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# ...and turn them into do-nothing targets
$(eval $(RUN_ARGS):;@:)
endif

build: clean bin/ghwc

run: build
@bin/ghwc $(RUN_ARGS)

test: vet
go test -v ./...

.PHONY: fmt
fmt:
@echo "Running gofmt on all sources..."
@gofmt -s -l -w .

.PHONY: fmtcheck
fmtcheck:
@bash -c "diff -u <(echo -n) <(gofmt -d .)"

.PHONY: vet
vet:
go vet ./...

clean:
@rm -f bin/ghwc
110 changes: 63 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1332,59 +1332,75 @@ cpu, err := ghw.CPU(ghw.WithPathOverrides(ghw.PathOverrides{

### Reading hardware information from a `ghw` snapshot (Linux only)

The `ghw-snapshot` tool can create a snapshot of a host's hardware information.

Please read [`SNAPSHOT.md`](SNAPSHOT.md) to learn about creating snapshots with
the `ghw-snapshot` tool.

You can make `ghw` read hardware information from a snapshot created with
`ghw-snapshot` using environment variables or programmatically.

Use the `GHW_SNAPSHOT_PATH` environment variable to specify the filepath to a
snapshot that `ghw` will read to determine hardware information. All the needed
chroot changes will be automatically performed. By default, the snapshot is
unpacked into a temporary directory managed by `ghw`. This temporary directory
is automatically deleted when `ghw` is finished reading the snapshot.

Three other environment variables are relevant if and only if `GHW_SNAPSHOT_PATH`
is not empty:

* `GHW_SNAPSHOT_ROOT` let users specify the directory on which the snapshot
should be unpacked. This moves the ownership of that directory from `ghw` to
users. For this reason, `ghw` will *not* automatically clean up the content
unpacked into `GHW_SNAPSHOT_ROOT`.
* `GHW_SNAPSHOT_EXCLUSIVE` tells `ghw` that the directory is meant only to
contain the given snapshot, thus `ghw` will *not* attempt to unpack it unless
the directory is empty. You can use both `GHW_SNAPSHOT_ROOT` and
`GHW_SNAPSHOT_EXCLUSIVE` to make sure `ghw` unpacks the snapshot only once
regardless of how many `ghw` packages (e.g. cpu, memory) access it. Set the
value of this environment variable to any non-empty string.
* `GHW_SNAPSHOT_PRESERVE` tells `ghw` not to clean up the unpacked snapshot.
Set the value of this environment variable to any non-empty string.
The `ghwc snapshot` command creates a snapshot of a host's hardware information.

```go
cpu, err := ghw.CPU(ghw.WithSnapshot(ghw.SnapshotOptions{
Path: "/path/to/linux-amd64-d4771ed3300339bc75f856be09fc6430.tar.gz",
}))


myRoot := "/my/safe/directory"
cpu, err := ghw.CPU(ghw.WithSnapshot(ghw.SnapshotOptions{
Path: "/path/to/linux-amd64-d4771ed3300339bc75f856be09fc6430.tar.gz",
Root: &myRoot,
}))
Use the `ghwc -s` flag to supply a path to a snapshot to read with the `ghwc`
command-line program.

myOtherRoot := "/my/other/safe/directory"
cpu, err := ghw.CPU(ghw.WithSnapshot(ghw.SnapshotOptions{
Path: "/path/to/linux-amd64-d4771ed3300339bc75f856be09fc6430.tar.gz",
Root: &myOtherRoot,
Exclusive: true,
}))
```
$ ghwc -s testdata/snapshots/linux-amd64-amd-ryzen-1600.tar.gz
block storage (8 disks, 3TB physical storage)
dm-0 SSD (90GB) Unknown [@unknown (node #0)] vendor=unknown
dm-1 Unknown (16GB) Unknown [@unknown (node #0)] vendor=unknown
dm-2 SSD (60GB) Unknown [@unknown (node #0)] vendor=unknown
dm-3 SSD (13GB) Unknown [@unknown (node #0)] vendor=unknown
dm-4 SSD (436GB) Unknown [@unknown (node #0)] vendor=unknown
sda SSD (239GB) SCSI [@unknown (node #0)] vendor=unknown
sda1 (128MB) [unknown]
sda2 (384MB) [unknown]
sda3 (238GB) [unknown]
sdb HDD (932GB) SCSI [@unknown (node #0)] vendor=unknown
sdb1 (2GB) [unknown]
sdb2 (930GB) [unknown]
sdc SSD (466GB) SCSI [@unknown (node #0)] vendor=unknown
sdc1 (32GB) [unknown]
sdc2 (434GB) [unknown]
cpu (1 physical package, 6 cores, 12 hardware threads)
physical package #0 (6 cores, 12 hardware threads)
processor core #0 (2 threads), logical processors [0 6]
processor core #1 (2 threads), logical processors [1 7]
processor core #5 (2 threads), logical processors [4 10]
processor core #6 (2 threads), logical processors [5 11]
processor core #2 (2 threads), logical processors [2 8]
processor core #4 (2 threads), logical processors [3 9]
capabilities: [msr pae mce cx8 apic sep
mtrr pge mca cmov pat pse36
clflush mmx fxsr sse sse2 ht
syscall nx mmxext fxsr_opt pdpe1gb rdtscp
lm constant_tsc art rep_good nopl nonstop_tsc
extd_apicid aperfmperf eagerfpu pni pclmulqdq monitor
ssse3 fma cx16 sse4_1 sse4_2 movbe
popcnt aes xsave avx f16c rdrand
lahf_lm cmp_legacy svm extapic cr8_legacy abm
sse4a misalignsse 3dnowprefetch osvw skinit wdt
tce topoext perfctr_core perfctr_nb bpext perfctr_l2
hw_pstate sme retpoline_amd ssbd ibpb vmmcall
fsgsbase bmi1 avx2 smep bmi2 rdseed
adx smap clflushopt sha_ni xsaveopt xsavec
xgetbv1 clzero irperf xsaveerptr arat npt
lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid
decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif
overflow_recov succor smca]
gpu (1 graphics card)
card #0 @0000:0a:00.0 -> driver: '' class: 'Display controller' vendor: 'Advanced Micro Devices, Inc. [AMD/ATI]' product: 'Turks XT [Radeon HD 6670/7670]'
processing accelerators (0 devices)
memory (32GB physical, 32GB usable)
net (4 NICs)
enp3s0
enp6s0f0
enp6s0f1
enp8s0
topology NUMA (0 nodes)
chassis type=unknown vendor=unknown version=unknown
bios vendor=unknown version=unknown
baseboard vendor=unknown version=unknown product=unknown
product family=unknown name=unknown vendor=unknown sku=unknown version=unknown
PCI (43 devices)
```

### Creating snapshots

You can create `ghw` snapshots using the `ghw-snapshot` tool or
You can create `ghw` snapshots using the `ghwc snapshot` command or
programmatically using the `pkg/snapshot` package.

Below is an example of creating a `ghw` snapshot using the `pkg/snapshot`
Expand Down
10 changes: 3 additions & 7 deletions SNAPSHOT.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ the paths ghw cares about. The snapshot concept was introduced [to make ghw easi

## Create and consume snapshot

The recommended way to create snapshots for ghw is to use the `ghw-snapshot` tool.
The recommended way to create snapshots for ghw is to use the `ghwc snapshot` command.

This tool is maintained by the ghw authors, and snapshots created with this tool are guaranteed to work.

To consume the ghw snapshots, please check the `README.md` document.
To display hardware information from a ghw snapshot, use the `ghwc -s` flag, passing the filepath to the snapshot to use.

## Snapshot design and definitions

Expand All @@ -21,10 +22,6 @@ expect, we recommend to check also the [project issues](https://github.com/jaypi
### Scope

ghw supports snapshots only on linux platforms. This restriction may be lifted in future releases.
Snapshots must be consumable in the following supported ways:

1. (way 1) from docker (or podman), mounting them as volumes. See `hack/run-against-snapshot.sh`
2. (way 2) using the environment variables `GHW_SNAPSHOT_*`. See `README.md` for the full documentation.

Other combinations are possible, but are unsupported and may stop working any time.
You should depend only on the supported ways to consume snapshots.
Expand All @@ -42,4 +39,3 @@ Stemming from the use cases, the snapshot content must have the following proper
It must be noted that trivially cloning subtrees from `/proc` and `/sys` and creating a tarball out of them doesn't work
because both pseudo filesystems make use of symlinks, and [docker doesn't really play nice with symlinks](https://github.com/jaypipes/ghw/commit/f8ffd4d24e62eb9017511f072ccf51f13d4a3399).
This conflcits with (way 1) above.

5 changes: 2 additions & 3 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import (
"github.com/jaypipes/ghw/pkg/usb"
)

// DEPRECATED: Please use Option
type WithOption = option.Option
type Option = option.Option

var (
WithChroot = option.WithChroot
WithSnapshot = option.WithSnapshot
WithAlerter = option.WithAlerter
WithNullAlerter = option.WithNullAlerter
// match the existing environ variable to minimize surprises
Expand All @@ -37,8 +38,6 @@ var (
WithPathOverrides = option.WithPathOverrides
)

type SnapshotOptions = option.SnapshotOptions

type PathOverrides = option.PathOverrides

type CPUInfo = cpu.Info
Expand Down
47 changes: 0 additions & 47 deletions cmd/ghw-snapshot/command/read.go

This file was deleted.

59 changes: 0 additions & 59 deletions cmd/ghw-snapshot/command/root.go

This file was deleted.

16 changes: 0 additions & 16 deletions cmd/ghw-snapshot/main.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/ghwc/commands/accelerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var acceleratorCmd = &cobra.Command{

// showAccelerator show processing accelerators information for the host system.
func showAccelerator(cmd *cobra.Command, args []string) error {
accel, err := ghw.Accelerator()
opts := cmd.Context().Value(optsKey).([]ghw.Option)
accel, err := ghw.Accelerator(opts...)
if err != nil {
return errors.Wrap(err, "error getting Accelerator info")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/ghwc/commands/baseboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var baseboardCmd = &cobra.Command{

// showBaseboard shows baseboard information for the host system.
func showBaseboard(cmd *cobra.Command, args []string) error {
baseboard, err := ghw.Baseboard()
opts := cmd.Context().Value(optsKey).([]ghw.Option)
baseboard, err := ghw.Baseboard(opts...)
if err != nil {
return errors.Wrap(err, "error getting baseboard info")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/ghwc/commands/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var biosCmd = &cobra.Command{

// showBIOS shows BIOS host system.
func showBIOS(cmd *cobra.Command, args []string) error {
bios, err := ghw.BIOS()
opts := cmd.Context().Value(optsKey).([]ghw.Option)
bios, err := ghw.BIOS(opts...)
if err != nil {
return errors.Wrap(err, "error getting BIOS info")
}
Expand Down
Loading