qvm_create.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 sys
  30. import qubesadmin
  31. import qubesadmin.tools
  32. parser = qubesadmin.tools.QubesArgumentParser()
  33. parser.add_argument('--class', '-C', dest='cls',
  34. default='AppVM',
  35. help='specify the class of the new domain (default: %(default)s)')
  36. parser.add_argument('--property', '--prop',
  37. action=qubesadmin.tools.PropertyAction,
  38. help='set domain\'s property, like "internal", "memory" or "vcpus"')
  39. parser.add_argument('--pool', '-p',
  40. action='append',
  41. metavar='VOLUME_NAME=POOL_NAME',
  42. help='specify the pool to use for a volume')
  43. parser.add_argument('-P',
  44. metavar='POOL_NAME',
  45. dest='one_pool',
  46. default='',
  47. help='change all volume pools to specified pool')
  48. parser.add_argument('--template', '-t',
  49. action=qubesadmin.tools.SinglePropertyAction,
  50. help='specify the TemplateVM to use')
  51. parser.add_argument('--label', '-l',
  52. action=qubesadmin.tools.SinglePropertyAction,
  53. help='specify the label to use for the new domain'
  54. ' (e.g. red, yellow, green, ...)')
  55. parser_root = parser.add_mutually_exclusive_group()
  56. parser_root.add_argument('--root-copy-from', '-r', metavar='FILENAME',
  57. help='use provided root.img instead of default/empty one'
  58. ' (file will be COPIED)')
  59. parser_root.add_argument('--root-move-from', '-R', metavar='FILENAME',
  60. help='use provided root.img instead of default/empty one'
  61. ' (file will be MOVED)')
  62. # silently ignored
  63. parser_root.add_argument('--no-root',
  64. action='store_true', default=False,
  65. help=argparse.SUPPRESS)
  66. parser.add_argument('name', metavar='VMNAME',
  67. action=qubesadmin.tools.SinglePropertyAction,
  68. nargs='?',
  69. help='name of the domain to create')
  70. def main(args=None, app=None):
  71. '''Main function of qvm-create tool'''
  72. args = parser.parse_args(args, app=app)
  73. pools = {}
  74. pool = None
  75. if hasattr(args, 'pool') and args.pool:
  76. for pool_vol in args.pool:
  77. try:
  78. volume_name, pool_name = pool_vol.split('=')
  79. pools[volume_name] = pool_name
  80. except ValueError:
  81. parser.error(
  82. 'Pool argument must be of form: -P volume_name=pool_name')
  83. if args.one_pool:
  84. pool = args.one_pool
  85. if 'label' not in args.properties:
  86. parser.error('--label option is mandatory')
  87. if 'name' not in args.properties:
  88. parser.error('VMNAME is mandatory')
  89. if args.root_copy_from or args.root_move_from:
  90. parser.error(
  91. '--root-copy-from and --root-move-from not implemented yet')
  92. try:
  93. args.app.get_label(args.properties['label'])
  94. except KeyError:
  95. parser.error('no such label: {!r}; available: {}'.format(
  96. args.properties['label'],
  97. ', '.join(repr(l.name) for l in args.app.labels)))
  98. try:
  99. args.app.get_vm_class(args.cls)
  100. except KeyError:
  101. parser.error('no such domain class: {!r}'.format(args.cls))
  102. try:
  103. vm = args.app.add_new_vm(args.cls,
  104. name=args.properties.pop('name'),
  105. label=args.properties.pop('label'),
  106. template=args.properties.pop('template', None),
  107. pool=pool,
  108. pools=pools)
  109. except qubesadmin.exc.QubesException as e:
  110. args.app.log.error('Error creating VM: {!s}'.format(e))
  111. return 1
  112. retcode = 0
  113. for prop, value in args.properties.items():
  114. try:
  115. setattr(vm, prop, value)
  116. except qubesadmin.exc.QubesException as e:
  117. args.app.log.error(
  118. 'Error setting property {} (but VM created): {!s}'.
  119. format(prop, e))
  120. retcode = 2
  121. return retcode
  122. if __name__ == '__main__':
  123. sys.exit(main())