qvm_start.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 sys
  27. import qubes
  28. class DriveAction(argparse.Action):
  29. '''Action for argument parser that stores drive image path.'''
  30. # pylint: disable=redefined-builtin,too-few-public-methods
  31. def __init__(self,
  32. option_strings,
  33. dest='drive',
  34. prefix='cdrom:',
  35. metavar='IMAGE',
  36. required=False,
  37. help='Attach drive'):
  38. super(DriveAction, self).__init__(option_strings, dest,
  39. metavar=metavar, help=help)
  40. self.prefix = prefix
  41. def __call__(self, parser, namespace, values, option_string=None):
  42. # pylint: disable=redefined-outer-name
  43. setattr(namespace, self.dest, self.prefix + values)
  44. parser = qubes.tools.QubesArgumentParser(vmname_nargs=1,
  45. description='start a domain')
  46. parser_drive = parser.add_mutually_exclusive_group()
  47. parser_drive.add_argument('--drive', metavar='DRIVE',
  48. help='temporarily attach specified drive as CD/DVD or hard disk (can be'
  49. ' specified with prefix "hd:" or "cdrom:", default is cdrom)')
  50. parser_drive.add_argument('--hddisk',
  51. action=DriveAction, prefix='hd:',
  52. help='temporarily attach specified drive as hard disk')
  53. parser_drive.add_argument('--cdrom', metavar='IMAGE',
  54. action=DriveAction, prefix='cdrom:',
  55. help='temporarily attach specified drive as CD/DVD')
  56. parser_drive.add_argument('--install-windows-tools',
  57. action='store_const', dest='drive', default=False,
  58. const='cdrom:dom0:/usr/lib/qubes/qubes-windows-tools.iso',
  59. help='temporarily ttach Windows tools CDROM to the domain')
  60. parser.add_argument('--conf-file', metavar='FILE',
  61. help='use custom libvirt config instead of Qubes-generated one')
  62. parser.add_argument('--debug',
  63. action='store_true', default=False,
  64. help='enable debug mode for this domain (until its shutdown)')
  65. parser.add_argument('--preparing-dvm',
  66. action='store_true', default=False,
  67. help='do actions necessary when preparing DVM image')
  68. parser.add_argument('--no-start-guid',
  69. action='store_false', dest='start_guid', default=True,
  70. help='do not start the gui daemon (ignored)')
  71. parser.add_argument('--skip-if-running',
  72. action='store_true', default=False,
  73. help='Do not fail if the qube is already runnning')
  74. #parser.add_option ("--tray", action="store_true", dest="tray", default=False,
  75. # help="Use tray notifications instead of stdout" )
  76. parser.set_defaults(drive=None)
  77. def main(args=None):
  78. '''Main routine of :program:`qvm-start`.
  79. :param list args: Optional arguments to override those delivered from \
  80. command line.
  81. '''
  82. args = parser.parse_args(args)
  83. # if options.tray:
  84. # tray_notify_init()
  85. vm = args.domains[0]
  86. if args.skip_if_running and vm.is_running():
  87. return
  88. if args.drive is not None:
  89. if 'drive' not in (prop.__name__ for prop in vm.property_list()):
  90. parser.error(
  91. 'domain {!r} does not support attaching drives'.format(vm.name))
  92. if args.conf_file is not None:
  93. vm.conf_file = args.conf_file
  94. if args.debug:
  95. vm.debug = args.debug
  96. try:
  97. vm.start(
  98. preparing_dvm=args.preparing_dvm,
  99. start_guid=args.start_guid)
  100. except qubes.exc.QubesException as e:
  101. parser.error_runtime('Qubes error: {!r}'.format(e))
  102. return 0
  103. if __name__ == '__main__':
  104. sys.exit(main())