qvm-sync-clock 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Marek Marczykowski <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. from qubes.qubes import QubesVmCollection
  23. import os.path
  24. import os
  25. import sys
  26. import re
  27. import subprocess
  28. qvm_collection = None
  29. def get_netvm_of_vm(vm):
  30. netvm = vm
  31. while netvm.netvm is not None:
  32. netvm = netvm.netvm
  33. if netvm is None or netvm.name == 'dom0':
  34. print >> sys.stderr, 'There seems to be no network connected to ClockVM, aborting.'
  35. sys.exit(1)
  36. return netvm
  37. def main():
  38. verbose = False
  39. if len(sys.argv) > 1 and sys.argv[1] in [ '--verbose', '-v' ]:
  40. verbose = True
  41. qvm_collection = QubesVmCollection()
  42. qvm_collection.lock_db_for_reading()
  43. qvm_collection.load()
  44. qvm_collection.unlock_db()
  45. clock_vm = qvm_collection.get_clockvm_vm()
  46. if clock_vm is None:
  47. print >> sys.stderr, 'There is no selected ClockVM, aborting.'
  48. sys.exit(1)
  49. if not clock_vm.is_running():
  50. print >> sys.stderr, 'ClockVM not started, exiting!'
  51. sys.exit(1)
  52. net_vm = get_netvm_of_vm(clock_vm)
  53. if verbose:
  54. print >> sys.stderr, '--> Waiting for network for ClockVM.'
  55. # Ignore retcode, try even if nm-online failed - user can setup network manually
  56. # on-online has timeout 30sec by default
  57. net_vm.run('nm-online -x', verbose=verbose, wait=True, ignore_stderr=True)
  58. # Sync clock
  59. if clock_vm.run('QUBESRPC qubes.SyncNtpClock dom0', user="root", verbose=verbose, wait=True, ignore_stderr=True) != 0:
  60. print >> sys.stderr, 'Time sync failed, aborting!'
  61. sys.exit(1)
  62. # Use the date format based on RFC2822 to avoid localisation issues
  63. p = clock_vm.run('date -u -R', verbose=verbose, passio_popen=True, ignore_stderr=True)
  64. date_out = p.stdout.read(100)
  65. date_out = date_out.strip()
  66. if not re.match(r'^[A-Za-z]+[,] [0-9][0-9] [A-Za-z]+ [0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [+]0000$', date_out):
  67. print >> sys.stderr, date_out, 'Invalid date output, aborting!'
  68. sys.exit(1)
  69. # Sync dom0 time
  70. if verbose:
  71. print >> sys.stderr, '--> Syncing dom0 clock.'
  72. subprocess.check_call(['sudo', 'date', '-u', '-R', '-s', date_out])
  73. subprocess.check_call(['sudo', 'hwclock', '--systohc'])
  74. # Sync other VMs clock
  75. for vm in qvm_collection.values():
  76. if vm.is_running() and vm.qid != 0 and vm.qid != clock_vm.qid:
  77. if verbose:
  78. print >> sys.stderr, '--> Syncing \'%s\' clock.' % vm.name
  79. try:
  80. vm.run('date -u -R -s "%s"' % date_out, user="root", verbose=verbose)
  81. except Exception as e:
  82. print >> sys.stderr, "ERROR syncing time in VM '%s': %s" % (vm.name, str(e))
  83. pass
  84. main()