qvm_start.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. '''qvm-start - Start a domain'''
  24. # TODO notification in tray
  25. import argparse
  26. import qubes
  27. class DriveAction(argparse.Action):
  28. '''Action for argument parser that stores drive image path.'''
  29. # pylint: disable=redefined-builtin
  30. def __init__(self,
  31. option_strings,
  32. dest='drive',
  33. prefix='cdrom:',
  34. metavar='IMAGE',
  35. required=False,
  36. help='Attach drive'):
  37. super(DriveAction, self).__init__(option_strings, dest,
  38. metavar=metavar, help=help)
  39. self.prefix = prefix
  40. def __call__(self, parser, namespace, values, option_string=None):
  41. setattr(namespace, self.dest, prefix + value)
  42. parser = qubes.tools.get_parser_base(description='start a domain')
  43. parser_drive = parser.add_mutually_exclusive_group()
  44. parser_drive.add_argument('--drive', metavar='DRIVE',
  45. help='temporarily attach specified drive as CD/DVD or hard disk (can be'
  46. ' specified with prefix "hd:" or "cdrom:", default is cdrom)')
  47. parser_drive.add_argument('--hddisk',
  48. action=DriveAction, prefix='hd:',
  49. help='temporarily attach specified drive as hard disk')
  50. parser_drive.add_argument('--cdrom', metavar='IMAGE',
  51. action=DriveAction, prefix='cdrom:',
  52. help='temporarily attach specified drive as CD/DVD')
  53. parser_drive.add_argument('--install-windows-tools',
  54. action='store_const', dest='drive', default=False,
  55. const='cdrom:dom0:/usr/lib/qubes/qubes-windows-tools.iso',
  56. help='temporarily ttach Windows tools CDROM to the domain')
  57. parser.add_argument('--conf-file', metavar='FILE',
  58. help='use custom libvirt config instead of Qubes-generated one')
  59. parser.add_argument('--debug',
  60. action='store_true', default=False,
  61. help='enable debug mode for this domain (until its shutdown)')
  62. parser.add_argument('--preparing-dvm',
  63. action='store_true', default=False,
  64. help='do actions necessary when preparing DVM image')
  65. parser.add_argument('--no-start-guid',
  66. action='store_false', dest='start_guid', default=True,
  67. help='do actions necessary when preparing DVM image')
  68. #parser.add_option ("--no-guid", action="store_true", dest="noguid", default=False,
  69. # help="Do not start the GUId (ignored)")
  70. #parser.add_option ("--tray", action="store_true", dest="tray", default=False,
  71. # help="Use tray notifications instead of stdout" )
  72. parser.add_argument('name', metavar='VMNAME',
  73. help='domain to start')
  74. parser.set_defaults(drive=None)
  75. def main(args=None):
  76. '''Main routine of :program:`qvm-start`.
  77. :param list args: Optional arguments to override those delivered from \
  78. command line.
  79. '''
  80. args = parser.parse_args(args)
  81. qubes.tools.set_verbosity(parser, args)
  82. app = qubes.Qubes(args.xml)
  83. # if options.tray:
  84. # tray_notify_init()
  85. try:
  86. vm = app.domains[args.name]
  87. except KeyError:
  88. parser.error('no such domain: {!r}'.format(args.name))
  89. if args.drive is not None:
  90. if 'drive' not in (prop.__name__ for prop in vm.property_list()):
  91. parser.error(
  92. 'domain {!r} does not support attaching drives'.format(vm.name))
  93. if args.conf_file is not None:
  94. vm.conf_file = args.conf_file
  95. if args.debug:
  96. vm.debug = args.debug
  97. try:
  98. vm.verify_files()
  99. except (IOError, OSError) as e:
  100. parser.error(
  101. 'error verifying files for domain {!r}: {!r}'.format(vm.name, e))
  102. try:
  103. xid = vm.start(
  104. preparing_dvm=args.preparing_dvm,
  105. start_guid=args.start_guid)
  106. # notify_function=
  107. except MemoryError:
  108. # TODO tray
  109. parser.error('not enough memory to start domain {!r}'.format(vm.name))
  110. except qubes.QubesException as e:
  111. parser.error('Qubes error: {!r}'.format(e))
  112. return True
  113. if __name__ == '__main__':
  114. sys.exit(not main())