qvm-sync-clock 3.4 KB

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