Dmidecode inside go program running in a kubernetes pod

0

I have a go routine running in docker container. I need output of the command dmidecode. But its coming blank.

Go:

func main() {

    cmd := exec.Command("dmidecode","-t 1")
    x,_ := cmd.Output()
    fmt.Println("output =======", string(x))

}

Docker run:

docker run --device /dev/mem:/dev/mem --cap-add SYS_RAWIO -p 8086:8086 -it my_img:1.0.1

What am I missing here?

Updated:

The above worked in docker after I added below in Dockerfile:

FROM alpine:latest RUN apk --no-cache --update --verbose add grep bash dmidecode &&
rm -rf /var/cache/apk/* /tmp/* /sbin/halt /sbin/poweroff /sbin/reboot

And below in docker compose file:

privileged: true

But When tried to use the above in kubernetes it not able to fetch demidecode output.

A help will be really appreciated.

devops docker go kubernetes
2021-11-23 17:03:05
1

2

What am I missing here?

For starters ,error handling.

    x,_ := cmd.Output()

Never, ever ignore an error in Go. Unlike languages like, say, Pyhton, there is no exception raising - handling error return values is your only chance to figure out if something went wrong.

Secondly, you're also ignoring your command's Standard Output stream. This is likely to contain a useful error message whenever a command execution doesn't work, so os/exec's Output() provides it as part of the error value if not already captured in the Cmd configuration. Part of your error handling should be doing a type assertion on that error value, if not nil, and if it's a valid *exec.ExitError, and if that type assertion succeeds, check its Stderr field for an error message.

Third, looking at your command, I can see you made an easy mistake:

    cmd := exec.Command("dmidecode","-t 1")

At the shell, whitespace separates arguments. but there is no shell here; you're passing -t 1 all as one argument to dmidecode. You should be passing them as separate arguments, almost certainly:

    cmd := exec.Command("dmidecode","-t", "1")

Finally, you've already found Can't run dmidecode on docker container , but make sure to read and understand the accepted answer. Then, get your docker container configured to be able to run dmidecode without Go. Once it works at the command line, the same docker configuration should allow it to work under Go invocation as well.

2021-11-23 17:22:34

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................