shirou / gopsutil

psutil for golang
Other
10.62k stars 1.59k forks source link

host.Virtualization does not detect lxc guest #679

Open danielnelson opened 5 years ago

danielnelson commented 5 years ago

Describe the bug

Calling host.Virtualization() from inside a LXC container does not detect that it is being ran under LXC. This prevents the fix in #441 from working.

To Reproduce

package main

import (
        "fmt"

        "github.com/shirou/gopsutil/host"
)

func main() {
        system, role, err := host.Virtualization()
        fmt.Println("system:", system)
        fmt.Println("role:", role)
        fmt.Println("err:", err)
}

Actual behavior

system: 
role: 
err: <nil>

Expected behavior

system: lxc
role: guest
err: <nil>

Environment:

LXC Host: Debian Buster Linux buster 4.13.0-1-amd64 #1 SMP Debian 4.13.13-1 (2017-11-16) x86_64 GNU/Linux

$ dpkg -l | grep lxc
ii  liblxc1                       1:3.1.0+really3.0.3-8          amd64        Linux Containers userspace tools (library)
ii  lxc                           1:3.1.0+really3.0.3-8          amd64        Linux Containers userspace tools
ii  lxc-templates                 3.0.3-1                        amd64        Linux Containers userspace tools (templates)
ii  lxcfs                         3.0.3-2                        amd64        FUSE based filesystem for LXC

LXC Guest: Debian Buster

Additional context This detection technique appears to work:

cat /proc/1/environ | tr '\0' '\n' | grep ^container

https://lists.linuxcontainers.org/pipermail/lxc-users/2013-December/005998.html

shirou commented 5 years ago

Thank you for reporting!

memo: ohai implementation can help.