Skip to content

Commit

Permalink
feat: move hypervisor vendor id code into klauspost/cpuid
Browse files Browse the repository at this point in the history
Reading hypervisor vendor id has been merged upstream in klauspost/cpuid so we can do away with our custom fork.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
  • Loading branch information
brianmcgee committed Aug 9, 2024
1 parent 8a39965 commit 8b14ea8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 44 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
3 changes: 2 additions & 1 deletion nix/packages/nixos-facter/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
args @ {
flake,
inputs,
perSystem,
system,
pkgs,
pname,
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions nix/packages/nixos-facter/gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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="
Expand Down
55 changes: 20 additions & 35 deletions pkg/virt/detect_vm_cpu_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 8b14ea8

Please sign in to comment.