qvm_start.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(
  45. want_vm=True,
  46. description='start a domain')
  47. parser_drive = parser.add_mutually_exclusive_group()
  48. parser_drive.add_argument('--drive', metavar='DRIVE',
  49. help='temporarily attach specified drive as CD/DVD or hard disk (can be'
  50. ' specified with prefix "hd:" or "cdrom:", default is cdrom)')
  51. parser_drive.add_argument('--hddisk',
  52. action=DriveAction, prefix='hd:',
  53. help='temporarily attach specified drive as hard disk')
  54. parser_drive.add_argument('--cdrom', metavar='IMAGE',
  55. action=DriveAction, prefix='cdrom:',
  56. help='temporarily attach specified drive as CD/DVD')
  57. parser_drive.add_argument('--install-windows-tools',
  58. action='store_const', dest='drive', default=False,
  59. const='cdrom:dom0:/usr/lib/qubes/qubes-windows-tools.iso',
  60. help='temporarily ttach Windows tools CDROM to the domain')
  61. parser.add_argument('--conf-file', metavar='FILE',
  62. help='use custom libvirt config instead of Qubes-generated one')
  63. parser.add_argument('--debug',
  64. action='store_true', default=False,
  65. help='enable debug mode for this domain (until its shutdown)')
  66. parser.add_argument('--preparing-dvm',
  67. action='store_true', default=False,
  68. help='do actions necessary when preparing DVM image')
  69. parser.add_argument('--no-start-guid',
  70. action='store_false', dest='start_guid', default=True,
  71. help='do not start the gui daemon (ignored)')
  72. parser.add_argument('--skip-if-running',
  73. action='store_true', default=False,
  74. help='Do not fail if the qube is already runnning')
  75. #parser.add_option ("--tray", action="store_true", dest="tray", default=False,
  76. # help="Use tray notifications instead of stdout" )
  77. parser.set_defaults(drive=None)
  78. def main(args=None):
  79. '''Main routine of :program:`qvm-start`.
  80. :param list args: Optional arguments to override those delivered from \
  81. command line.
  82. '''
  83. args = parser.parse_args(args)
  84. # if options.tray:
  85. # tray_notify_init()
  86. vm = args.vm
  87. if args.skip_if_running and vm.is_running():
  88. return
  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 =
  104. vm.start(
  105. preparing_dvm=args.preparing_dvm,
  106. start_guid=args.start_guid)
  107. # notify_function=
  108. except qubes.exc.QubesException as e:
  109. parser.error_runtime('Qubes error: {!r}'.format(e))
  110. return 0
  111. if __name__ == '__main__':
  112. sys.exit(main())