qvm_create.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #
  2. # The Qubes OS Project, http://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. # Copyright (C) 2017 Marek Marczykowski-Górecki
  7. # <marmarek@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU Lesser General Public License as published by
  11. # the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser 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-create tool'''
  24. # TODO list available classes
  25. # TODO list labels (maybe in qvm-prefs)
  26. # TODO features, devices, tags
  27. from __future__ import print_function
  28. import argparse
  29. import os
  30. import sys
  31. import qubesadmin
  32. import qubesadmin.tools
  33. parser = qubesadmin.tools.QubesArgumentParser()
  34. parser.add_argument('--class', '-C', dest='cls',
  35. default='AppVM',
  36. help='specify the class of the new domain (default: %(default)s)')
  37. parser.add_argument('--property', '--prop',
  38. action=qubesadmin.tools.PropertyAction,
  39. help='set domain\'s property, like "internal", "memory" or "vcpus"')
  40. parser.add_argument('--pool', '-p',
  41. action='append',
  42. metavar='VOLUME_NAME=POOL_NAME',
  43. help='specify the pool to use for a volume')
  44. parser.add_argument('-P',
  45. metavar='POOL_NAME',
  46. dest='one_pool',
  47. default='',
  48. help='change all volume pools to specified pool')
  49. parser.add_argument('--template', '-t',
  50. action=qubesadmin.tools.SinglePropertyAction,
  51. help='specify the TemplateVM to use')
  52. parser.add_argument('--label', '-l',
  53. action=qubesadmin.tools.SinglePropertyAction,
  54. help='specify the label to use for the new domain'
  55. ' (e.g. red, yellow, green, ...)')
  56. parser.add_argument('--help-classes',
  57. action='store_true',
  58. help='List available classes and exit')
  59. parser_root = parser.add_mutually_exclusive_group()
  60. parser_root.add_argument('--root-copy-from', '-r', metavar='FILENAME',
  61. help='use provided root.img instead of default/empty one'
  62. ' (file will be COPIED)')
  63. parser_root.add_argument('--root-move-from', '-R', metavar='FILENAME',
  64. help='use provided root.img instead of default/empty one'
  65. ' (file will be MOVED)')
  66. # silently ignored
  67. parser_root.add_argument('--no-root',
  68. action='store_true', default=False,
  69. help=argparse.SUPPRESS)
  70. parser.add_argument('name', metavar='VMNAME',
  71. action=qubesadmin.tools.SinglePropertyAction,
  72. nargs='?',
  73. help='name of the domain to create')
  74. def main(args=None, app=None):
  75. '''Main function of qvm-create tool'''
  76. args = parser.parse_args(args, app=app)
  77. if args.help_classes:
  78. vm_classes = args.app.qubesd_call('dom0', 'admin.vmclass.List').decode()
  79. vm_classes = vm_classes.splitlines()
  80. print('\n'.join(sorted(vm_classes)))
  81. return 0
  82. pools = {}
  83. pool = None
  84. if hasattr(args, 'pool') and args.pool:
  85. for pool_vol in args.pool:
  86. try:
  87. volume_name, pool_name = pool_vol.split('=')
  88. pools[volume_name] = pool_name
  89. except ValueError:
  90. parser.error(
  91. 'Pool argument must be of form: -P volume_name=pool_name')
  92. if args.one_pool:
  93. pool = args.one_pool
  94. if 'label' not in args.properties:
  95. parser.error('--label option is mandatory')
  96. if 'name' not in args.properties:
  97. parser.error('VMNAME is mandatory')
  98. root_source_path = args.root_copy_from or args.root_move_from
  99. if root_source_path and not os.path.exists(root_source_path):
  100. parser.error(
  101. 'File pointed by --root-copy-from/--root-move-from does not exist')
  102. try:
  103. args.app.get_label(args.properties['label'])
  104. except KeyError:
  105. parser.error('no such label: {!r}; available: {}'.format(
  106. args.properties['label'],
  107. ', '.join(repr(l.name) for l in args.app.labels)))
  108. try:
  109. args.app.get_vm_class(args.cls)
  110. except KeyError:
  111. parser.error('no such domain class: {!r}'.format(args.cls))
  112. try:
  113. vm = args.app.add_new_vm(args.cls,
  114. name=args.properties.pop('name'),
  115. label=args.properties.pop('label'),
  116. template=args.properties.pop('template', None),
  117. pool=pool,
  118. pools=pools)
  119. except qubesadmin.exc.QubesException as e:
  120. args.app.log.error('Error creating VM: {!s}'.format(e))
  121. return 1
  122. retcode = 0
  123. for prop, value in args.properties.items():
  124. try:
  125. setattr(vm, prop, value)
  126. except qubesadmin.exc.QubesException as e:
  127. args.app.log.error(
  128. 'Error setting property {} (but VM created): {!s}'.
  129. format(prop, e))
  130. retcode = 2
  131. if root_source_path:
  132. try:
  133. with open(root_source_path, 'rb') as root_file:
  134. vm.volumes['root'].import_data(root_file)
  135. if args.root_move_from:
  136. os.unlink(root_source_path)
  137. except qubesadmin.exc.QubesException as e:
  138. args.app.log.error(
  139. 'Error importing root volume (but VM created): {}'.
  140. format(e))
  141. retcode = 3
  142. return retcode
  143. if __name__ == '__main__':
  144. sys.exit(main())