From 060d46a2824c42e20b749230f93f6fe172a03b7f Mon Sep 17 00:00:00 2001
From: Bob Lantz <rlantz@cs.stanford.edu>
Date: Wed, 5 Nov 2014 16:18:13 -0800
Subject: [PATCH] Set VM date based on host date.

This should fix #398 for real; note that if we try to shut down ntpd
right at boot, it doesn't work! ;-(

However, setting the Unix time in the traditional manner using seconds
since 1970 should do the trick!
---
 util/vm/build.py | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/util/vm/build.py b/util/vm/build.py
index 20725901..5d5edf57 100755
--- a/util/vm/build.py
+++ b/util/vm/build.py
@@ -487,17 +487,16 @@ def login( vm, user='mininet', password='mininet' ):
     log( '* Waiting for login...' )
 
 
-def disableNtpd( vm, prompt=Prompt ):
+def setClock( vm, prompt=Prompt ):
     "Turn off ntpd and set clock immediately"
-    log( '* Turning off ntpd' )
+    log( '* Attempting to shut down ntpd' )
     vm.sendline( 'sudo -n service ntp stop' )
     vm.expect( prompt )
-    log( '* Setting clock' )
-    # -gq: set and quit immediately
-    vm.sendline( 'sudo -n ntpd -gq' )
-    vm.expect( prompt )
-    log( '* Waiting one second and running date command' )
-    vm.sendline( 'sleep 1 && date ' )
+    log( '* Getting seconds since epoch from this server' )
+    # Note r'date +%s' specifies a format for 'date', not python!
+    seconds = int( run( r'date +%s' ) )
+    log( '* Setting VM clock' )
+    vm.sendline( 'sudo date -s @%d' % seconds )
 
 
 def sanityTest( vm ):
@@ -832,9 +831,9 @@ def build( flavor='raring32server', tests=None, pre='', post='', memory=1024 ):
 
 def runTests( vm, tests=None, pre='', post='', prompt=Prompt ):
     "Run tests (list) in vm (pexpect object)"
-    # We disable ntpd and set the time so that it won't be
+    # We disable ntpd and set the time so that ntpd won't be
     # messing with the time during tests
-    disableNtpd( vm )
+    setClock( vm )
     vm.expect( prompt )
     if Branch:
         checkOutBranch( vm, branch=Branch )
-- 
GitLab