@@ -13,6 +13,7 @@ import (
13
13
"github.com/lima-vm/lima/pkg/guestagent/api"
14
14
"github.com/lima-vm/lima/pkg/guestagent/iptables"
15
15
"github.com/lima-vm/lima/pkg/guestagent/procnettcp"
16
+ "github.com/lima-vm/lima/pkg/guestagent/timesync"
16
17
"github.com/sirupsen/logrus"
17
18
"github.com/yalue/native_endian"
18
19
)
@@ -37,6 +38,7 @@ func New(newTicker func() (<-chan time.Time, func()), iptablesIdle time.Duration
37
38
}
38
39
39
40
go a .setWorthCheckingIPTablesRoutine (auditClient , iptablesIdle )
41
+ go a .fixSystemTimeSkew ()
40
42
return a , nil
41
43
}
42
44
@@ -244,3 +246,31 @@ func (a *agent) Info(ctx context.Context) (*api.Info, error) {
244
246
}
245
247
return & info , nil
246
248
}
249
+
250
+ const deltaLimit = 2 * time .Second
251
+
252
+ func (a * agent ) fixSystemTimeSkew () {
253
+ for {
254
+ ticker := time .NewTicker (10 * time .Second )
255
+ for now := range ticker .C {
256
+ rtc , err := timesync .GetRTCTime ()
257
+ if err != nil {
258
+ logrus .Warnf ("fixSystemTimeSkew: lookup error: %s" , err .Error ())
259
+ continue
260
+ }
261
+ d := rtc .Sub (now )
262
+ logrus .Debugf ("fixSystemTimeSkew: rtc=%s systime=%s delta=%s" ,
263
+ rtc .Format (time .RFC3339 ), now .Format (time .RFC3339 ), d )
264
+ if d > deltaLimit || d < - deltaLimit {
265
+ err = timesync .SetSystemTime (rtc )
266
+ if err != nil {
267
+ logrus .Warnf ("fixSystemTimeSkew: set system clock error: %s" , err .Error ())
268
+ continue
269
+ }
270
+ logrus .Infof ("fixSystemTimeSkew: system time synchronized with rtc" )
271
+ break
272
+ }
273
+ }
274
+ ticker .Stop ()
275
+ }
276
+ }
0 commit comments