[host] linux: change file if in a lxc container.

This commit is contained in:
shirou 2017-10-22 16:39:36 +09:00
parent 48fc561289
commit dc3a09c1d0
2 changed files with 14 additions and 1 deletions

View File

@ -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

View File

@ -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) {