Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
factory: set guest time after resuming
Browse files Browse the repository at this point in the history
We might have paused a guest for a long time so we need to sync
its time.

Fixes:#951
Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Dec 12, 2018
1 parent f813708 commit 8444a7a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions virtcontainers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package virtcontainers
import (
"fmt"
"syscall"
"time"

"github.com/kata-containers/agent/protocols/grpc"
"github.com/kata-containers/runtime/virtcontainers/pkg/types"
Expand Down Expand Up @@ -246,4 +247,7 @@ type agent interface {

// getGuestDetails will tell the agent to get some information of guest
getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsResponse, error)

// setGuestDateTime asks the agent to set guest time to the provided one
setGuestDateTime(time.Time) error
}
6 changes: 6 additions & 0 deletions virtcontainers/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ func (f *factory) GetVM(ctx context.Context, config vc.VMConfig) (*vc.VM, error)
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
baseConfig := f.base.Config().HypervisorConfig
if baseConfig.NumVCPUs < hypervisorConfig.NumVCPUs {
Expand Down
5 changes: 5 additions & 0 deletions virtcontainers/hyperstart_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,3 +1000,8 @@ func (h *hyper) getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsRe
// hyperstart-agent does not support getGuestDetails
return nil, nil
}

func (h *hyper) setGuestDateTime(time.Time) error {
// hyperstart-agent does not support setGuestDateTime
return nil
}
12 changes: 12 additions & 0 deletions virtcontainers/kata_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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) {
Expand Down Expand Up @@ -1563,6 +1566,15 @@ func (k *kataAgent) getGuestDetails(req *grpc.GuestDetailsRequest) (*grpc.GuestD
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 {
switch ipFamily {
case netlink.FAMILY_V4:
Expand Down
5 changes: 5 additions & 0 deletions virtcontainers/kata_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ func (p *gRPCProxy) GetGuestDetails(ctx context.Context, req *pb.GuestDetailsReq
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{}) {
switch g := srv.(type) {
case *gRPCProxy:
Expand All @@ -262,6 +266,7 @@ var reqList = []interface{}{
&pb.CheckRequest{},
&pb.WaitProcessRequest{},
&pb.StatsContainerRequest{},
&pb.SetGuestDateTimeRequest{},
}

func TestKataAgentSendReq(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions virtcontainers/noop_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package virtcontainers

import (
"syscall"
"time"

"github.com/kata-containers/agent/protocols/grpc"
"github.com/kata-containers/runtime/virtcontainers/pkg/types"
Expand Down Expand Up @@ -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) {
return nil, nil
}

// setGuestDateTime is the Noop agent guest time setter. It does nothing.
func (n *noopAgent) setGuestDateTime(time.Time) error {
return nil
}
8 changes: 8 additions & 0 deletions virtcontainers/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"os"
"path/filepath"
"time"

"github.com/kata-containers/runtime/virtcontainers/pkg/uuid"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -296,6 +297,13 @@ func (v *VM) ReseedRNG() error {
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 {
// add vm symlinks
// - link vm socket from sandbox dir (/run/vc/vm/sbid/<kata.sock>) to vm dir (/run/vc/vm/vmid/<kata.sock>)
Expand Down

0 comments on commit 8444a7a

Please sign in to comment.