qvm-start 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 ("--debug", action="store_true", dest="debug", default=False,
  57. help="Enable debug mode for this VM (until its shutdown)")
  58. (options, args) = parser.parse_args ()
  59. if (len (args) != 1):
  60. parser.error ("You must specify VM name!")
  61. vmname = args[0]
  62. if options.tray:
  63. tray_notify_init()
  64. qvm_collection = QubesVmCollection()
  65. qvm_collection.lock_db_for_reading()
  66. qvm_collection.load()
  67. qvm_collection.unlock_db()
  68. vm = qvm_collection.get_vm_by_name(vmname)
  69. if vm is None:
  70. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  71. exit(1)
  72. if bool(options.drive_hd) + bool(options.drive_cdrom) + bool(options.drive) + bool(options.install_windows_tools) > 1:
  73. print >> sys.stderr, "Only one of --drive, --cdrom, --hddisk, --install-windows-tools can be specified"
  74. exit(1)
  75. if options.install_windows_tools:
  76. options.drive = 'cdrom:dom0:/usr/lib/qubes/qubes-windows-tools.iso'
  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. if vm.type.endswith("HVM"):
  104. print >> sys.stderr, " /var/log/xen/console/guest-%s-dm.log" % vmname
  105. print >> sys.stderr, " /var/log/qubes/guid.%s.log" % vmname
  106. print >> sys.stderr, " /var/log/qubes/qrexec.%s.log" % vmname
  107. main()