From dc3a09c1d02458a20248826cdfc955e7beedbb0f Mon Sep 17 00:00:00 2001 From: shirou Date: Sun, 22 Oct 2017 16:39:36 +0900 Subject: [PATCH] [host] linux: change file if in a lxc container. --- host/host_linux.go | 13 ++++++++++++- host/host_test.go | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/host/host_linux.go b/host/host_linux.go index 14893a4..06d469b 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -95,7 +95,18 @@ func BootTime() (uint64, error) { if t != 0 { return t, nil } - filename := common.HostProc("stat") + + system, role, err := Virtualization() + if err != nil { + return 0, err + } + statFile := "stat" + if system == "lxc" && role == "guest" { + // if lxc, /proc/uptime is used. + statFile = "uptime" + } + + filename := common.HostProc(statFile) lines, err := common.ReadLines(filename) if err != nil { return 0, err diff --git a/host/host_test.go b/host/host_test.go index 65f7a75..d9e6611 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -40,11 +40,13 @@ func TestBoot_time(t *testing.T) { if v < 946652400 { t.Errorf("Invalid Boottime, older than 2000-01-01") } + t.Logf("first boot time: %d", v) v2, err := BootTime() if v != v2 { t.Errorf("cached boot time is different") } + t.Logf("second boot time: %d", v2) } func TestUsers(t *testing.T) {