qvm-sync-clock 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. import fcntl
  24. from optparse import OptionParser
  25. from qubes.qubes import QubesVmCollection
  26. import os.path
  27. import os
  28. import sys
  29. import re
  30. import subprocess
  31. qvm_collection = None
  32. def get_netvm_of_vm(vm):
  33. netvm = vm
  34. while netvm.netvm is not None:
  35. netvm = netvm.netvm
  36. if netvm is None or netvm.name == 'dom0':
  37. print >> sys.stderr, 'There seems to be no network connected to ClockVM, aborting.'
  38. sys.exit(1)
  39. return netvm
  40. def main():
  41. parser = OptionParser()
  42. parser.add_option ("-v", "--verbose", action="store_true", dest="verbose", default=False)
  43. parser.add_option ("-f", "--force", action="store_true", dest="force", default=False)
  44. (options, args) = parser.parse_args ()
  45. lockfile_name = "/var/run/qubes/qvm-sync-clock.lock"
  46. if os.path.exists(lockfile_name):
  47. lockfile = open(lockfile_name, "r")
  48. else:
  49. lockfile = open(lockfile_name, "w")
  50. fcntl.fcntl(lockfile.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  51. try:
  52. fcntl.flock(lockfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
  53. except IOError:
  54. print >>sys.stderr, "qvm-sync-clock already running, aborting"
  55. sys.exit(1)
  56. qvm_collection = QubesVmCollection()
  57. qvm_collection.lock_db_for_reading()
  58. qvm_collection.load()
  59. qvm_collection.unlock_db()
  60. clock_vm = qvm_collection.get_clockvm_vm()
  61. if clock_vm is None:
  62. print >> sys.stderr, 'There is no selected ClockVM, aborting.'
  63. sys.exit(1)
  64. if not clock_vm.is_running():
  65. print >> sys.stderr, 'ClockVM not started, exiting!'
  66. sys.exit(1)
  67. net_vm = get_netvm_of_vm(clock_vm)
  68. if options.verbose:
  69. print >> sys.stderr, '--> Waiting for network for ClockVM.'
  70. # Ignore retcode, try even if nm-online failed - user can setup network manually
  71. # on-online has timeout 30sec by default
  72. net_vm.run('nm-online -x', verbose=options.verbose, gui=False, wait=True,
  73. ignore_stderr=True)
  74. # Sync clock
  75. if clock_vm.run('QUBESRPC qubes.SyncNtpClock dom0', user="root",
  76. verbose=options.verbose, gui=False, wait=True, ignore_stderr=True) \
  77. != 0:
  78. if options.force:
  79. print >> sys.stderr, 'Time sync failed! - Syncing with dom0 ' \
  80. 'anyway as requested'
  81. else:
  82. print >> sys.stderr, 'Time sync failed! - Exiting'
  83. sys.exit(1)
  84. else:
  85. # Use the date format based on RFC2822 to avoid localisation issues
  86. p = clock_vm.run('date -u -Iseconds', verbose=options.verbose,
  87. gui=False, passio_popen=True, ignore_stderr=True)
  88. date_out = p.stdout.read(100)
  89. date_out = date_out.strip()
  90. if not re.match(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+00:?00$', date_out):
  91. print >> sys.stderr, 'Invalid date output, aborting!'
  92. sys.exit(1)
  93. # Sync dom0 time
  94. if options.verbose:
  95. print >> sys.stderr, '--> Syncing dom0 clock.'
  96. subprocess.check_call(['sudo', 'date', '-u', '-Iseconds', '-s', date_out],
  97. stdout=None if options.verbose else open(os.devnull, 'w'))
  98. subprocess.check_call(['sudo', 'hwclock', '--systohc'],
  99. stdout=None if options.verbose else open(os.devnull, 'w'))
  100. # Sync other VMs clock
  101. for vm in qvm_collection.values():
  102. if vm.is_running() and vm.qid != 0 and vm.qid != clock_vm.qid:
  103. if options.verbose:
  104. print >> sys.stderr, '--> Syncing \'%s\' clock.' % vm.name
  105. try:
  106. vm.run_service("qubes.SetDateTime", user="root",
  107. localcmd="date -u -Iseconds")
  108. except Exception as e:
  109. print >> sys.stderr, "ERROR syncing time in VM '%s': %s" % (vm.name, str(e))
  110. pass
  111. # order is important!
  112. os.unlink(lockfile_name)
  113. lockfile.close()
  114. main()