qvm-sync-clock 4.3 KB

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