qvm-start 5.7 KB

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