Skip to content

Commit

Permalink
agent: judge whether /sys/devices/system/memory/block_size_bytes exist.
Browse files Browse the repository at this point in the history
/sys/devices/system/memory/block_size_bytes will not exist when guestos
doesn't support memory hotplug. Add judgement when file
/sys/devices/system/memory/block_size_bytes doesn't exist.

Fixes kata-containers#394

Signed-off-by: Clare Chen <clare.chenhui@huawei.com>
  • Loading branch information
cedriccchen committed Oct 6, 2018
1 parent 861b18d commit d12910e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
27 changes: 18 additions & 9 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ const (
)

var (
sysfsCPUOnlinePath = "/sys/devices/system/cpu"
sysfsMemOnlinePath = "/sys/devices/system/memory"
sysfsConnectedCPUsPath = filepath.Join(sysfsCPUOnlinePath, "online")
sysfsCPUOnlinePath = "/sys/devices/system/cpu"
sysfsMemOnlinePath = "/sys/devices/system/memory"
sysfsMemoryBlockSizePath = "/sys/devices/system/memory/block_size_bytes"
sysfsConnectedCPUsPath = filepath.Join(sysfsCPUOnlinePath, "online")
)

type onlineResource struct {
Expand Down Expand Up @@ -1320,13 +1321,21 @@ func (a *agentGRPC) ReseedRandomDev(ctx context.Context, req *pb.ReseedRandomDev
func (a *agentGRPC) GetGuestDetails(ctx context.Context, req *pb.GuestDetailsRequest) (*pb.GuestDetailsResponse, error) {
var details pb.GuestDetailsResponse
if req.MemBlockSize {
data, err := ioutil.ReadFile("/sys/devices/system/memory/block_size_bytes")
data, err := ioutil.ReadFile(sysfsMemoryBlockSizePath)
if err != nil {
return nil, err
}
details.MemBlockSizeBytes, err = strconv.ParseUint(string(data[:len(data)-1]), 16, 64)
if err != nil {
return nil, err
if os.IsNotExist(err) {
agentLog.WithField("sysfsMemoryBlockSizePath", sysfsMemoryBlockSizePath).Info("Guest kernel config doesn't support memory hotplug")
} else {
return nil, err
}
} else {
if len(data) == 0 {
return nil, fmt.Errorf("%v is empty", sysfsMemoryBlockSizePath)
}
details.MemBlockSizeBytes, err = strconv.ParseUint(string(data[:len(data)-1]), 16, 64)
if err != nil {
return nil, err
}
}
}

Expand Down
29 changes: 24 additions & 5 deletions grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"path/filepath"
"reflect"
"sort"
"strconv"
"testing"
"time"

"strconv"
"sync"

pb "github.com/kata-containers/agent/protocols/grpc"
Expand Down Expand Up @@ -666,19 +666,38 @@ func TestGetGuestDetails(t *testing.T) {
MemBlockSize: true,
}

resp, err := a.GetGuestDetails(context.TODO(), req)
// sysfsMemoryBlockSizePath exist with error format
file, err := ioutil.TempFile("", "test")
assert.NoError(err)

data, err := ioutil.ReadFile("/sys/devices/system/memory/block_size_bytes")
sysfsMemoryBlockSizePath = file.Name()
// empty
_, err = a.GetGuestDetails(context.TODO(), req)
assert.Error(err)
// random string
err = ioutil.WriteFile(sysfsMemoryBlockSizePath, []byte(sysfsMemoryBlockSizePath), 0666)
assert.NoError(err)
_, err = a.GetGuestDetails(context.TODO(), req)
assert.Error(err)

// sysfsMemoryBlockSizePath exist with correct format
err = ioutil.WriteFile(sysfsMemoryBlockSizePath, []byte("123"), 0666)
assert.NoError(err)
resp, err := a.GetGuestDetails(context.TODO(), req)
assert.NoError(err)
data, err := ioutil.ReadFile(sysfsMemoryBlockSizePath)
assert.NoError(err)
size, err := strconv.ParseUint(string(data[:len(data)-1]), 16, 64)
assert.NoError(err)

assert.Equal(resp.MemBlockSizeBytes, size)

seccompSupport := a.haveSeccomp()
testAgentDetails(assert, resp.AgentDetails, seccompSupport)

// sysfsMemoryBlockSizePath not exist
os.Remove(sysfsMemoryBlockSizePath)
resp, err = a.GetGuestDetails(context.TODO(), req)
assert.NoError(err)
assert.Equal(resp.MemBlockSizeBytes, uint64(0))
}

func TestGetAgentDetails(t *testing.T) {
Expand Down

0 comments on commit d12910e

Please sign in to comment.