mirror of
				https://github.com/kata-containers/kata-containers.git
				synced 2025-10-31 09:26:52 +00:00 
			
		
		
		
	factory: set guest time after resuming
We might have paused a guest for a long time so we need to sync its time. Fixes:#951 Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
		| @@ -8,6 +8,7 @@ package virtcontainers | |||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"syscall" | 	"syscall" | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/kata-containers/agent/protocols/grpc" | 	"github.com/kata-containers/agent/protocols/grpc" | ||||||
| 	"github.com/kata-containers/runtime/virtcontainers/pkg/types" | 	"github.com/kata-containers/runtime/virtcontainers/pkg/types" | ||||||
| @@ -246,4 +247,7 @@ type agent interface { | |||||||
|  |  | ||||||
| 	// getGuestDetails will tell the agent to get some information of guest | 	// getGuestDetails will tell the agent to get some information of guest | ||||||
| 	getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsResponse, error) | 	getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsResponse, error) | ||||||
|  |  | ||||||
|  | 	// setGuestDateTime asks the agent to set guest time to the provided one | ||||||
|  | 	setGuestDateTime(time.Time) error | ||||||
| } | } | ||||||
|   | |||||||
| @@ -248,6 +248,12 @@ func (f *factory) GetVM(ctx context.Context, config vc.VMConfig) (*vc.VM, error) | |||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// sync guest time since we might have paused it for a long time. | ||||||
|  | 	err = vm.SyncTime() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	online := false | 	online := false | ||||||
| 	baseConfig := f.base.Config().HypervisorConfig | 	baseConfig := f.base.Config().HypervisorConfig | ||||||
| 	if baseConfig.NumVCPUs < hypervisorConfig.NumVCPUs { | 	if baseConfig.NumVCPUs < hypervisorConfig.NumVCPUs { | ||||||
|   | |||||||
| @@ -1000,3 +1000,8 @@ func (h *hyper) getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsRe | |||||||
| 	// hyperstart-agent does not support getGuestDetails | 	// hyperstart-agent does not support getGuestDetails | ||||||
| 	return nil, nil | 	return nil, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (h *hyper) setGuestDateTime(time.Time) error { | ||||||
|  | 	// hyperstart-agent does not support setGuestDateTime | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|   | |||||||
| @@ -1490,6 +1490,9 @@ func (k *kataAgent) installReqFunc(c *kataclient.AgentClient) { | |||||||
| 	k.reqHandlers["grpc.GuestDetailsRequest"] = func(ctx context.Context, req interface{}, opts ...golangGrpc.CallOption) (interface{}, error) { | 	k.reqHandlers["grpc.GuestDetailsRequest"] = func(ctx context.Context, req interface{}, opts ...golangGrpc.CallOption) (interface{}, error) { | ||||||
| 		return k.client.GetGuestDetails(ctx, req.(*grpc.GuestDetailsRequest), opts...) | 		return k.client.GetGuestDetails(ctx, req.(*grpc.GuestDetailsRequest), opts...) | ||||||
| 	} | 	} | ||||||
|  | 	k.reqHandlers["grpc.SetGuestDateTimeRequest"] = func(ctx context.Context, req interface{}, opts ...golangGrpc.CallOption) (interface{}, error) { | ||||||
|  | 		return k.client.SetGuestDateTime(ctx, req.(*grpc.SetGuestDateTimeRequest), opts...) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (k *kataAgent) sendReq(request interface{}) (interface{}, error) { | func (k *kataAgent) sendReq(request interface{}) (interface{}, error) { | ||||||
| @@ -1563,6 +1566,15 @@ func (k *kataAgent) getGuestDetails(req *grpc.GuestDetailsRequest) (*grpc.GuestD | |||||||
| 	return resp.(*grpc.GuestDetailsResponse), nil | 	return resp.(*grpc.GuestDetailsResponse), nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (k *kataAgent) setGuestDateTime(tv time.Time) error { | ||||||
|  | 	_, err := k.sendReq(&grpc.SetGuestDateTimeRequest{ | ||||||
|  | 		Sec:  tv.Unix(), | ||||||
|  | 		Usec: int64(tv.Nanosecond() / 1e3), | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  |  | ||||||
| func (k *kataAgent) convertToKataAgentIPFamily(ipFamily int) aTypes.IPFamily { | func (k *kataAgent) convertToKataAgentIPFamily(ipFamily int) aTypes.IPFamily { | ||||||
| 	switch ipFamily { | 	switch ipFamily { | ||||||
| 	case netlink.FAMILY_V4: | 	case netlink.FAMILY_V4: | ||||||
|   | |||||||
| @@ -243,6 +243,10 @@ func (p *gRPCProxy) GetGuestDetails(ctx context.Context, req *pb.GuestDetailsReq | |||||||
| 	return &pb.GuestDetailsResponse{}, nil | 	return &pb.GuestDetailsResponse{}, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (p *gRPCProxy) SetGuestDateTime(ctx context.Context, req *pb.SetGuestDateTimeRequest) (*gpb.Empty, error) { | ||||||
|  | 	return &gpb.Empty{}, nil | ||||||
|  | } | ||||||
|  |  | ||||||
| func gRPCRegister(s *grpc.Server, srv interface{}) { | func gRPCRegister(s *grpc.Server, srv interface{}) { | ||||||
| 	switch g := srv.(type) { | 	switch g := srv.(type) { | ||||||
| 	case *gRPCProxy: | 	case *gRPCProxy: | ||||||
| @@ -262,6 +266,7 @@ var reqList = []interface{}{ | |||||||
| 	&pb.CheckRequest{}, | 	&pb.CheckRequest{}, | ||||||
| 	&pb.WaitProcessRequest{}, | 	&pb.WaitProcessRequest{}, | ||||||
| 	&pb.StatsContainerRequest{}, | 	&pb.StatsContainerRequest{}, | ||||||
|  | 	&pb.SetGuestDateTimeRequest{}, | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestKataAgentSendReq(t *testing.T) { | func TestKataAgentSendReq(t *testing.T) { | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ package virtcontainers | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"syscall" | 	"syscall" | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/kata-containers/agent/protocols/grpc" | 	"github.com/kata-containers/agent/protocols/grpc" | ||||||
| 	"github.com/kata-containers/runtime/virtcontainers/pkg/types" | 	"github.com/kata-containers/runtime/virtcontainers/pkg/types" | ||||||
| @@ -203,3 +204,8 @@ func (n *noopAgent) setProxy(sandbox *Sandbox, proxy proxy, pid int, url string) | |||||||
| func (n *noopAgent) getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsResponse, error) { | func (n *noopAgent) getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsResponse, error) { | ||||||
| 	return nil, nil | 	return nil, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // setGuestDateTime is the Noop agent guest time setter. It does nothing. | ||||||
|  | func (n *noopAgent) setGuestDateTime(time.Time) error { | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ import ( | |||||||
| 	"context" | 	"context" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/kata-containers/runtime/virtcontainers/pkg/uuid" | 	"github.com/kata-containers/runtime/virtcontainers/pkg/uuid" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| @@ -296,6 +297,13 @@ func (v *VM) ReseedRNG() error { | |||||||
| 	return v.agent.reseedRNG(data) | 	return v.agent.reseedRNG(data) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // SyncTime syncs guest time with host time. | ||||||
|  | func (v *VM) SyncTime() error { | ||||||
|  | 	now := time.Now() | ||||||
|  | 	v.logger().WithField("time", now).Infof("sync guest time") | ||||||
|  | 	return v.agent.setGuestDateTime(now) | ||||||
|  | } | ||||||
|  |  | ||||||
| func (v *VM) assignSandbox(s *Sandbox) error { | func (v *VM) assignSandbox(s *Sandbox) error { | ||||||
| 	// add vm symlinks | 	// add vm symlinks | ||||||
| 	// - link vm socket from sandbox dir (/run/vc/vm/sbid/<kata.sock>) to vm dir (/run/vc/vm/vmid/<kata.sock>) | 	// - link vm socket from sandbox dir (/run/vc/vm/sbid/<kata.sock>) to vm dir (/run/vc/vm/vmid/<kata.sock>) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user