diff --git a/grpc.go b/grpc.go index 2924bada2b..5eb64c4d1d 100644 --- a/grpc.go +++ b/grpc.go @@ -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 { @@ -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 + } } } diff --git a/grpc_test.go b/grpc_test.go index f3bb5edba8..8a21468770 100644 --- a/grpc_test.go +++ b/grpc_test.go @@ -14,10 +14,10 @@ import ( "path/filepath" "reflect" "sort" + "strconv" "testing" "time" - "strconv" "sync" pb "github.com/kata-containers/agent/protocols/grpc" @@ -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) {