qvm-start 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Joanna Rutkowska <joanna@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. from qubes.qubes import QubesException
  24. from optparse import OptionParser
  25. import subprocess
  26. import os
  27. import sys
  28. import dbus
  29. notify_object = None
  30. def tray_notify(str, label, timeout = 3000):
  31. if notify_object:
  32. notify_object.Notify("Qubes", 0, label.icon, "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  33. def tray_notify_error(str, timeout = 3000):
  34. if notify_object:
  35. notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  36. def tray_notify_generic(level, str):
  37. if level == "info":
  38. tray_notify(str, label=vm.label)
  39. elif level == "error":
  40. tray_notify_error(str)
  41. def main():
  42. usage = "usage: %prog [options] <vm-name>"
  43. parser = OptionParser (usage)
  44. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  45. parser.add_option ("--tray", action="store_true", dest="tray", default=False,
  46. help="Use tray notifications instead of stdout" )
  47. parser.add_option ("--no-guid", action="store_true", dest="noguid", default=False,
  48. help="Do not start the GUId (ignored)")
  49. parser.add_option ("--drive", dest="drive", default=None,
  50. help="Temporarily attach specified drive as CD/DVD or hard disk (can be specified with prefix 'hd:' or 'cdrom:', default is cdrom)")
  51. parser.add_option ("--hddisk", dest="drive_hd", default=None,
  52. help="Temporarily attach specified drive as hard disk")
  53. parser.add_option ("--cdrom", dest="drive_cdrom", default=None,
  54. help="Temporarily attach specified drive as CD/DVD")
  55. parser.add_option ("--install-windows-tools", action="store_true", dest="install_windows_tools", default=False,
  56. help="Attach Windows tools CDROM to the VM")
  57. parser.add_option ("--dvm", action="store_true", dest="preparing_dvm", default=False,
  58. help="Do actions necessary when preparing DVM image")
  59. parser.add_option ("--custom-config", action="store", dest="custom_config", default=None,
  60. help="Use custom Xen config instead of Qubes-generated one")
  61. parser.add_option ("--debug", action="store_true", dest="debug", default=False,
  62. help="Enable debug mode for this VM (until its shutdown)")
  63. (options, args) = parser.parse_args ()
  64. if (len (args) != 1):
  65. parser.error ("You must specify VM name!")
  66. vmname = args[0]
  67. if options.tray:
  68. global notify_object
  69. try:
  70. notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
  71. except dbus.DBusException as ex:
  72. print >>sys.stderr, "WARNING: failed connect to tray notification service: %s" % str(ex)
  73. qvm_collection = QubesVmCollection()
  74. qvm_collection.lock_db_for_reading()
  75. qvm_collection.load()
  76. qvm_collection.unlock_db()
  77. vm = qvm_collection.get_vm_by_name(vmname)
  78. if vm is None:
  79. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  80. exit(1)
  81. if bool(options.drive_hd) + bool(options.drive_cdrom) + bool(options.drive) + bool(options.install_windows_tools) > 1:
  82. print >> sys.stderr, "Only one of --drive, --cdrom, --hddisk, --install-windows-tools can be specified"
  83. exit(1)
  84. if options.install_windows_tools:
  85. options.drive = 'cdrom:dom0:/usr/lib/qubes/qubes-windows-tools.iso'
  86. if options.drive_hd:
  87. options.drive = 'hd:' + options.drive_hd
  88. if options.drive_cdrom:
  89. options.drive = 'cdrom:' + options.drive_cdrom
  90. if options.drive:
  91. if hasattr(vm, 'drive'):
  92. vm.drive = options.drive
  93. else:
  94. print >> sys.stderr, "This VM does not support attaching drives"
  95. exit (1)
  96. if options.custom_config:
  97. vm.conf_file = options.custom_config
  98. if options.debug:
  99. vm.debug = True
  100. try:
  101. vm.verify_files()
  102. xid = vm.start(verbose=options.verbose, preparing_dvm=options.preparing_dvm, start_guid=not options.noguid, notify_function=tray_notify_generic if options.tray else None)
  103. except (IOError, OSError, QubesException, MemoryError) as err:
  104. if options.tray:
  105. tray_notify_error(str(err))
  106. else:
  107. print >> sys.stderr, "ERROR: {0}".format(err)
  108. exit (1)
  109. if options.debug:
  110. print >> sys.stderr, "--> Debug mode enabled. Useful logs: "
  111. print >> sys.stderr, " /var/log/xen/console/guest-%s.log" % vmname
  112. if vm.type.endswith("HVM"):
  113. print >> sys.stderr, " /var/log/xen/console/guest-%s-dm.log" % vmname
  114. print >> sys.stderr, " /var/log/qubes/guid.%s.log" % vmname
  115. print >> sys.stderr, " /var/log/qubes/qrexec.%s.log" % vmname
  116. main()