qvm_start.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (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 along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. '''qvm-start - Start a domain'''
  22. # TODO notification in tray
  23. import argparse
  24. import os
  25. import sys
  26. import qubes
  27. class DriveAction(argparse.Action):
  28. '''Action for argument parser that stores drive image path.'''
  29. # pylint: disable=redefined-builtin,too-few-public-methods
  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. # pylint: disable=redefined-outer-name
  42. setattr(namespace, self.dest, self.prefix + values)
  43. parser = qubes.tools.QubesArgumentParser(vmname_nargs=1,
  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 attach 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 not start the gui daemon')
  70. parser.add_argument('--no-guid',
  71. action='store_false', dest='start_guid',
  72. help='same as --no-start-guid')
  73. parser.add_argument('--skip-if-running',
  74. action='store_true', default=False,
  75. help='Do not fail if the qube is already runnning')
  76. #parser.add_option ("--tray", action="store_true", dest="tray", default=False,
  77. # help="Use tray notifications instead of stdout" )
  78. parser.set_defaults(drive=None)
  79. def main(args=None):
  80. '''Main routine of :program:`qvm-start`.
  81. :param list args: Optional arguments to override those delivered from \
  82. command line.
  83. '''
  84. args = parser.parse_args(args)
  85. # if options.tray:
  86. # tray_notify_init()
  87. vm = args.domains[0]
  88. if args.skip_if_running and vm.is_running():
  89. return
  90. if args.drive is not None:
  91. if 'drive' not in (prop.__name__ for prop in vm.property_list()):
  92. parser.error(
  93. 'domain {!r} does not support attaching drives'.format(vm.name))
  94. else:
  95. if args.drive == 'cdrom:dom0:/usr/lib/qubes/qubes-windows-tools.iso':
  96. path = args.drive.split(':', 2)[2]
  97. if not os.path.exists(path):
  98. parser.error('qubes-windows-tools package not installed')
  99. if args.conf_file is not None:
  100. vm.conf_file = args.conf_file
  101. if args.debug:
  102. vm.debug = args.debug
  103. if args.debug:
  104. vm.start(
  105. preparing_dvm=args.preparing_dvm,
  106. start_guid=args.start_guid)
  107. else:
  108. try:
  109. vm.start(
  110. preparing_dvm=args.preparing_dvm,
  111. start_guid=args.start_guid)
  112. except qubes.exc.QubesException as e:
  113. parser.error_runtime('Qubes error: {!r}'.format(e))
  114. return 0
  115. if __name__ == '__main__':
  116. sys.exit(main())