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(
  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_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 =
  99. vm.start(
  100. preparing_dvm=args.preparing_dvm,
  101. start_guid=args.start_guid)
  102. # notify_function=
  103. except qubes.exc.QubesException as e:
  104. parser.error_runtime('Qubes error: {!r}'.format(e))
  105. return True
  106. if __name__ == '__main__':
  107. sys.exit(not main())