qvm-start 5.3 KB

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