Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: idiomatic/safe method to detect nanos & klibs at runtime #1885

Closed
rinor opened this issue Jun 30, 2023 · 3 comments
Closed

question: idiomatic/safe method to detect nanos & klibs at runtime #1885

rinor opened this issue Jun 30, 2023 · 3 comments

Comments

@rinor
Copy link
Contributor

rinor commented Jun 30, 2023

  1. what would be an alternate idiomatic/safe method for an app (process) to detect at runtime if it's running on nanos, without making use of the environment variable(s) injected by ops/manually? - not a feature request and not important

  2. what would be an alternate idiomatic/safe method for an app (process) to detect at runtime the enabled klibs with the respective features, without making use of the environment variable(s) injected by ops/manually? - not a feature request and not important

@francescolavra
Copy link
Member

  1. A process can detect whether it's running on Nanos via the uname() syscall; more specifically, the sysname field of struct utsname will contain the string "Nanos". Example:
$ ops run /bin/uname -a "-s"
running local instance
booting /home/francesco/.ops/images/uname ...
en1: assigned 10.0.2.15
Nanos
  1. Currenly the kernel does not expose information about enabled klibs to the process

@rinor
Copy link
Contributor Author

rinor commented Jun 30, 2023

thank you

@rinor rinor closed this as completed Jun 30, 2023
@rinor
Copy link
Contributor Author

rinor commented Jul 1, 2023

running local instance
...
en1: assigned 10.0.2.15
Nanos version: "Nanos 5.0-0.1.45 888809a7a8f0003b66a8ec2af2f5e8dd749866d0 x86_64"
  • sample code - go
package main

import (
	"fmt"
	"syscall"
)

func main() {
	fmt.Printf("Nanos version: %q\n", Version())
}

func Version() string {
	var uts syscall.Utsname
	if err := syscall.Uname(&uts); err != nil {
		return ""
	}

	sysname := int8ToString(uts.Sysname[:])
	release := int8ToString(uts.Release[:])
	version := int8ToString(uts.Version[:])
	machine := int8ToString(uts.Machine[:])

	return sysname + " " + release + " " + version + " " + machine
}

func int8ToString(s []int8) string {
	b := make([]byte, 0, len(s))
	for _, v := range s {
		if v == 0x00 {
			break
		}
		b = append(b, byte(v))
	}
	return string(b)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants