Skip to content

Commit

Permalink
helpers/osinfo: host architecture detection
Browse files Browse the repository at this point in the history
osinfo needs to detect the host architecture in order to support the
embedded BTF files feature being added to tracee.
  • Loading branch information
rafaeldtinoco authored and Rafael David Tinoco committed Nov 7, 2021
1 parent b060e6b commit 2b469ce
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions helpers/btfhub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package helpers

22 changes: 22 additions & 0 deletions helpers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ func UnameRelease() (string, error) {
return ver, nil
}

// UnameMachine gets the version string of host's architecture
func UnameMachine() (string, error) {
var uname syscall.Utsname
if err := syscall.Uname(&uname); err != nil {
return "", fmt.Errorf("could not get utsname")
}

var buf [65]byte
for i, b := range uname.Machine {
buf[i] = byte(b)
}

arch := string(buf[:])
arch = strings.Trim(arch, "\x00")

if strings.Contains(arch, "aarch64") {
arch = "arm64"
}

return arch, nil
}

// CompareKernelRelease will compare two given kernel version/release
// strings and return -1, 0 or 1 if given version is less, equal or bigger,
// respectively, than the given one
Expand Down
11 changes: 10 additions & 1 deletion helpers/osinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ const (
OS_BUILD_ID
OS_IMAGE_ID
OS_IMAGE_VERSION
OS_KERNEL_RELEASE // not part of default os-release, but we can use it here to facilitate things
// not part of default os-release:
OS_KERNEL_RELEASE
OS_ARCH
)

type OSReleaseField uint32
Expand All @@ -82,6 +84,7 @@ var stringToOSReleaseField = map[string]OSReleaseField{
"IMAGE_ID": OS_IMAGE_ID,
"IMAGE_VERSION": OS_IMAGE_VERSION,
"KERNEL_RELEASE": OS_KERNEL_RELEASE,
"ARCH": OS_ARCH,
}

// osReleaseFieldToString is a map of os-release file fields
Expand All @@ -99,6 +102,7 @@ var osReleaseFieldToString = map[OSReleaseField]string{
OS_IMAGE_ID: "IMAGE_ID",
OS_IMAGE_VERSION: "IMAGE_VERSION",
OS_KERNEL_RELEASE: "KERNEL_RELEASE",
OS_ARCH: "ARCH",
}

// OSBTFEnabled checks if kernel has embedded BTF vmlinux file
Expand All @@ -122,6 +126,11 @@ func GetOSInfo() (*OSInfo, error) {
return &info, fmt.Errorf("could not determine uname release: %w", err)
}

info.osReleaseFieldValues[OS_ARCH], err = UnameMachine()
if err != nil {
return &info, fmt.Errorf("could not determine uname machine: %w", err)
}

info.osReleaseFilePath, err = checkEnvPath("LIBBPFGO_OSRELEASE_FILE") // useful if users wants to mount host os-release in a container
if err != nil {
return &info, err
Expand Down

0 comments on commit 2b469ce

Please sign in to comment.