diff --git a/go.mod b/go.mod index 0a459d7..8eb0d27 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.22.3 require ( github.com/charmbracelet/log v0.4.0 - github.com/klauspost/cpuid/v2 v2.2.8 + github.com/klauspost/cpuid/v2 v2.2.9-0.20240805145549-92d5326f011e github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 ) @@ -39,5 +39,3 @@ require ( gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) - -replace github.com/klauspost/cpuid/v2 => github.com/numtide/cpuid/v2 v2.0.0-20240729135509-23632be66920 diff --git a/go.sum b/go.sum index 8467fac..0553af0 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/klauspost/cpuid/v2 v2.2.9-0.20240805145549-92d5326f011e h1:XLeT7xVis8xyC0F4CqQ2fAcuBar61PMI7GhrUEBBKas= +github.com/klauspost/cpuid/v2 v2.2.9-0.20240805145549-92d5326f011e/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -40,8 +42,6 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= -github.com/numtide/cpuid/v2 v2.0.0-20240729135509-23632be66920 h1:Vn5x7huCjyilTNXaV4JmbDPg+GeyxZ60qj8LvSIw4LM= -github.com/numtide/cpuid/v2 v2.0.0-20240729135509-23632be66920/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/nix/packages/nixos-facter/default.nix b/nix/packages/nixos-facter/default.nix index 8d7e89d..ba0ec93 100644 --- a/nix/packages/nixos-facter/default.nix +++ b/nix/packages/nixos-facter/default.nix @@ -1,6 +1,7 @@ args @ { flake, inputs, + perSystem, system, pkgs, pname, @@ -9,7 +10,7 @@ args @ { inherit (pkgs) go lib; fs = lib.fileset; in - inputs.gomod2nix.legacyPackages.${system}.buildGoApplication rec { + perSystem.gomod2nix.buildGoApplication rec { inherit pname; # there's no good way of tying in the version to a git tag or branch # so for simplicity's sake we set the version as the commit revision hash diff --git a/nix/packages/nixos-facter/gomod2nix.toml b/nix/packages/nixos-facter/gomod2nix.toml index 3653667..1ecfca3 100644 --- a/nix/packages/nixos-facter/gomod2nix.toml +++ b/nix/packages/nixos-facter/gomod2nix.toml @@ -23,9 +23,8 @@ schema = 3 version = "v1.1.0" hash = "sha256-XWlYH0c8IcxAwQTnIi6WYqq44nOKUylSWxWO/vi+8pE=" [mod."github.com/klauspost/cpuid/v2"] - version = "v2.0.0-20240729135509-23632be66920" - hash = "sha256-meD7SbiElORwI1Z0MCfT/FB6hOVAA8DG0nZYnU8tszI=" - replaced = "github.com/numtide/cpuid/v2" + version = "v2.2.9-0.20240805145549-92d5326f011e" + hash = "sha256-yCZS40L97G7WZHhy/A6I8ArEkyHi86DGAW43SziYPek=" [mod."github.com/lucasb-eyer/go-colorful"] version = "v1.2.0" hash = "sha256-Gg9dDJFCTaHrKHRR1SrJgZ8fWieJkybljybkI9x0gyE=" diff --git a/pkg/virt/detect_vm_cpu_id.go b/pkg/virt/detect_vm_cpu_id.go index ac5df97..d7b0b62 100644 --- a/pkg/virt/detect_vm_cpu_id.go +++ b/pkg/virt/detect_vm_cpu_id.go @@ -8,44 +8,29 @@ import ( // See https://github.com/systemd/systemd/blob/main/src/basic/virt.c -var cpuMapping = map[string]Type{ - "XenVMMXenVMM": TypeXen, - "KVMKVMKVM": TypeKvm, // qemu with KVM - "Linux KVM Hv": TypeKvm, - "TCGTCGTCGTCG": TypeQemu, // qemu without KVM - /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */ - "VMwareVMware": TypeVmware, - /* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs */ - "Microsoft Hv": TypeMicrosoft, - /* https://wiki.freebsd.org/bhyve */ - "bhyve bhyve ": TypeBhyve, - "QNXQVMBSQG": TypeQnx, - /* https://wiki.freebsd.org/bhyve */ - "ACRNACRNACRN": TypeAcrn, - /* https://www.lockheedmartin.com/en-us/products/Hardened-Security-for-Intel-Processors.html */ - "SRESRESRESRE": TypeSre, - "Apple VZ": TypeApple, +var vendorMapping = map[cpuid.Vendor]Type{ + cpuid.XenHVM: TypeXen, + cpuid.KVM: TypeKvm, + cpuid.QEMU: TypeQemu, + cpuid.VMware: TypeVmware, + cpuid.MSVM: TypeMicrosoft, + cpuid.Bhyve: TypeBhyve, + cpuid.QNX: TypeQnx, + cpuid.ACRN: TypeAcrn, + cpuid.SRE: TypeSre, + cpuid.Apple: TypeApple, } func init() { // https://lwn.net/Articles/301888/ - detectVmCpuId = _detectVmCpuId -} - -func _detectVmCpuId() (Type, error) { - if !cpuid.CPU.VM() { - return TypeNone, nil - } - - _, b, c, d := cpuid.Cpuid(0x40000000) - v := string(cpuid.ValAsString(b, c, d)) - - if vmType, ok := cpuMapping[cpuid.CPU.VendorString]; ok { - return vmType, nil - } - vm, ok := cpuMapping[v] - if !ok { - return TypeVmOther, nil + detectVmCpuId = func() (Type, error) { + if !cpuid.CPU.VM() { + return TypeNone, nil + } + vm, ok := vendorMapping[cpuid.CPU.HypervisorVendorID] + if !ok { + return TypeVmOther, nil + } + return vm, nil } - return vm, nil }