qvm-start 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. notify_object.Notify("Qubes", 0, label.icon, "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  32. def tray_notify_error(str, timeout = 3000):
  33. notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
  34. def tray_notify_generic(level, str):
  35. if level == "info":
  36. tray_notify(str, label=vm.label)
  37. elif level == "error":
  38. tray_notify_error(str)
  39. def main():
  40. usage = "usage: %prog [options] <vm-name>"
  41. parser = OptionParser (usage)
  42. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  43. parser.add_option ("--tray", action="store_true", dest="tray", default=False,
  44. help="Use tray notifications instead of stdout" )
  45. parser.add_option ("--no-guid", action="store_true", dest="noguid", default=False,
  46. help="Do not start the GUId (ignored)")
  47. parser.add_option ("--drive", dest="drive", default=None,
  48. help="Temporarily attach specified drive as CD/DVD or hard disk (can be specified with prefix 'hd:' or 'cdrom:', default is cdrom)")
  49. parser.add_option ("--hddisk", dest="drive_hd", default=None,
  50. help="Temporarily attach specified drive as hard disk")
  51. parser.add_option ("--cdrom", dest="drive_cdrom", default=None,
  52. help="Temporarily attach specified drive as CD/DVD")
  53. parser.add_option ("--dvm", action="store_true", dest="preparing_dvm", default=False,
  54. help="Do actions necessary when preparing DVM image")
  55. parser.add_option ("--custom-config", action="store", dest="custom_config", default=None,
  56. help="Use custom Xen config instead of Qubes-generated one")
  57. parser.add_option ("--debug", action="store_true", dest="debug", default=False,
  58. help="Enable debug mode for this VM (until its shutdown)")
  59. (options, args) = parser.parse_args ()
  60. if (len (args) != 1):
  61. parser.error ("You must specify VM name!")
  62. vmname = args[0]
  63. if options.tray:
  64. global notify_object
  65. notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
  66. qvm_collection = QubesVmCollection()
  67. qvm_collection.lock_db_for_reading()
  68. qvm_collection.load()
  69. qvm_collection.unlock_db()
  70. vm = qvm_collection.get_vm_by_name(vmname)
  71. if vm is None:
  72. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  73. exit(1)
  74. if bool(options.drive_hd) + bool(options.drive_cdrom) + bool(options.drive) > 1:
  75. print >> sys.stderr, "Only one of --drive, --cdrom, --hddisk can be specified"
  76. exit(1)
  77. if options.drive_hd:
  78. options.drive = 'hd:' + options.drive_hd
  79. if options.drive_cdrom:
  80. options.drive = 'cdrom:' + options.drive_cdrom
  81. if options.drive:
  82. if hasattr(vm, 'drive'):
  83. vm.drive = options.drive
  84. else:
  85. print >> sys.stderr, "This VM does not support attaching drives"
  86. exit (1)
  87. if options.custom_config:
  88. vm.conf_file = options.custom_config
  89. if options.debug:
  90. vm.debug = True
  91. try:
  92. vm.verify_files()
  93. 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)
  94. except (IOError, OSError, QubesException, MemoryError) as err:
  95. if options.tray:
  96. tray_notify_error(str(err))
  97. else:
  98. print >> sys.stderr, "ERROR: {0}".format(err)
  99. exit (1)
  100. if options.debug:
  101. print >> sys.stderr, "--> Debug mode enabled. Useful logs: "
  102. print >> sys.stderr, " /var/log/xen/console/guest-%s.log" % vmname
  103. print >> sys.stderr, " /var/log/qubes/guid.%d.log" % xid
  104. print >> sys.stderr, " /var/log/qubes/qrexec.%d.log" % xid
  105. main()