qvm_start.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.QubesArgumentParser(
  43. want_vm=True,
  44. description='start a domain')
  45. parser_drive = parser.add_mutually_exclusive_group()
  46. parser_drive.add_argument('--drive', metavar='DRIVE',
  47. help='temporarily attach specified drive as CD/DVD or hard disk (can be'
  48. ' specified with prefix "hd:" or "cdrom:", default is cdrom)')
  49. parser_drive.add_argument('--hddisk',
  50. action=DriveAction, prefix='hd:',
  51. help='temporarily attach specified drive as hard disk')
  52. parser_drive.add_argument('--cdrom', metavar='IMAGE',
  53. action=DriveAction, prefix='cdrom:',
  54. help='temporarily attach specified drive as CD/DVD')
  55. parser_drive.add_argument('--install-windows-tools',
  56. action='store_const', dest='drive', default=False,
  57. const='cdrom:dom0:/usr/lib/qubes/qubes-windows-tools.iso',
  58. help='temporarily ttach Windows tools CDROM to the domain')
  59. parser.add_argument('--conf-file', metavar='FILE',
  60. help='use custom libvirt config instead of Qubes-generated one')
  61. parser.add_argument('--debug',
  62. action='store_true', default=False,
  63. help='enable debug mode for this domain (until its shutdown)')
  64. parser.add_argument('--preparing-dvm',
  65. action='store_true', default=False,
  66. help='do actions necessary when preparing DVM image')
  67. parser.add_argument('--no-start-guid',
  68. action='store_false', dest='start_guid', default=True,
  69. help='do actions necessary when preparing DVM image')
  70. #parser.add_option ("--no-guid", action="store_true", dest="noguid", default=False,
  71. # help="Do not start the GUId (ignored)")
  72. #parser.add_option ("--tray", action="store_true", dest="tray", default=False,
  73. # help="Use tray notifications instead of stdout" )
  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. # if options.tray:
  82. # tray_notify_init()
  83. vm = args.vm
  84. if args.drive is not None:
  85. if 'drive' not in (prop.__name__ for prop in vm.property_list()):
  86. parser.error(
  87. 'domain {!r} does not support attaching drives'.format(vm.name))
  88. if args.conf_file is not None:
  89. vm.conf_file = args.conf_file
  90. if args.debug:
  91. vm.debug = args.debug
  92. try:
  93. vm.verify_files()
  94. except (IOError, OSError) as e:
  95. parser.error(
  96. 'error verifying files for domain {!r}: {!r}'.format(vm.name, e))
  97. try:
  98. xid = vm.start(
  99. preparing_dvm=args.preparing_dvm,
  100. start_guid=args.start_guid)
  101. # notify_function=
  102. except MemoryError:
  103. # TODO tray
  104. parser.error_runtime(
  105. 'not enough memory to start domain {!r}'.format(vm.name))
  106. except qubes.QubesException as e:
  107. parser.error_runtime('Qubes error: {!r}'.format(e))
  108. return True
  109. if __name__ == '__main__':
  110. sys.exit(not main())