qvm_create.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. # TODO list available classes
  22. # TODO list labels (maybe in qvm-prefs)
  23. # TODO features, devices, tags
  24. from __future__ import print_function
  25. import argparse
  26. import os
  27. import subprocess
  28. import sys
  29. import qubes
  30. import qubes.tools
  31. parser = qubes.tools.QubesArgumentParser(want_force_root=True)
  32. parser.add_argument('--class', '-C', dest='cls',
  33. default='AppVM',
  34. help='specify the class of the new domain (default: %(default)s)')
  35. parser.add_argument('--property', '--prop',
  36. action=qubes.tools.PropertyAction,
  37. help='set domain\'s property, like "internal", "memory" or "vcpus"')
  38. parser.add_argument('--pool', '-p',
  39. action='append',
  40. metavar='POOL_NAME:VOLUME_NAME',
  41. help='specify the pool to use for a volume')
  42. parser.add_argument('-P',
  43. metavar='POOL_NAME',
  44. dest='one_pool',
  45. default='',
  46. help='change all volume pools to specified pool')
  47. parser.add_argument('--template', '-t',
  48. action=qubes.tools.SinglePropertyAction,
  49. help='specify the TemplateVM to use')
  50. parser.add_argument('--label', '-l',
  51. action=qubes.tools.SinglePropertyAction,
  52. help='specify the label to use for the new domain'
  53. ' (e.g. red, yellow, green, ...)')
  54. parser_root = parser.add_mutually_exclusive_group()
  55. parser_root.add_argument('--root-copy-from', '-r', metavar='FILENAME',
  56. help='use provided root.img instead of default/empty one'
  57. ' (file will be COPIED)')
  58. parser_root.add_argument('--root-move-from', '-R', metavar='FILENAME',
  59. help='use provided root.img instead of default/empty one'
  60. ' (file will be MOVED)')
  61. parser_root.add_argument('--no-root',
  62. action='store_true', default=False,
  63. help=argparse.SUPPRESS)
  64. parser.add_argument('name', metavar='VMNAME',
  65. action=qubes.tools.SinglePropertyAction,
  66. nargs='?',
  67. help='name of the domain to create')
  68. def main(args=None):
  69. args = parser.parse_args(args)
  70. pools = {}
  71. pool = None
  72. if hasattr(args, 'pools') and args.pools:
  73. for pool_vol in args.pool:
  74. try:
  75. pool_name, volume_name = pool_vol.split(':')
  76. pools[volume_name] = pool_name
  77. except ValueError:
  78. parser.error(
  79. 'Pool argument must be of form: -P pool_name:volume_name')
  80. if args.one_pool:
  81. pool = args.one_pool
  82. if 'label' not in args.properties:
  83. parser.error('--label option is mandatory')
  84. if 'name' not in args.properties:
  85. parser.error('VMNAME is mandatory')
  86. try:
  87. args.app.get_label(args.properties['label'])
  88. except KeyError:
  89. parser.error('no such label: {!r}; available: {}'.format(
  90. args.properties['label'],
  91. ', '.join(repr(l.name) for l in args.app.labels)))
  92. try:
  93. cls = args.app.get_vm_class(args.cls)
  94. except KeyError:
  95. parser.error('no such domain class: {!r}'.format(args.cls))
  96. if 'template' in args.properties and \
  97. 'template' not in (prop.__name__ for prop in cls.property_list()):
  98. parser.error('this domain class does not support template')
  99. vm = args.app.add_new_vm(cls, **args.properties)
  100. # pylint: disable=line-too-long
  101. # if not options.standalone and any([options.root_copy_from, options.root_move_from]):
  102. # print >> sys.stderr, "root.img can be specified only for standalone VMs"
  103. # exit (1)
  104. # if options.hvm_template and options.template is not None:
  105. # print >> sys.stderr, "Template VM cannot be based on another template"
  106. # exit (1)
  107. # if options.root_copy_from is not None and not os.path.exists(options.root_copy_from):
  108. # print >> sys.stderr, "File specified as root.img does not exists"
  109. # exit (1)
  110. # if options.root_move_from is not None and not os.path.exists(options.root_move_from):
  111. # print >> sys.stderr, "File specified as root.img does not exists"
  112. # exit (1)
  113. # elif not options.hvm and not options.hvm_template:
  114. # if qvm_collection.get_default_template() is None:
  115. # print >> sys.stderr, "No default TemplateVM defined!"
  116. # exit (1)
  117. # else:
  118. # template = qvm_collection.get_default_template()
  119. # if (options.verbose):
  120. # print('--> Using default TemplateVM: {0}'.format(template.name))
  121. if not args.no_root:
  122. try:
  123. vm.create_on_disk(pool, pools)
  124. # TODO this is file pool specific. Change it to a more general
  125. # solution
  126. root_img_path = vm.volumes['root'].vid
  127. if args.root_move_from is not None:
  128. # if (options.verbose):
  129. # print "--> Replacing root.img with provided file"
  130. os.unlink(root_img_path)
  131. os.rename(args.root_move_from, root_img_path)
  132. elif args.root_copy_from is not None:
  133. # if (options.verbose):
  134. # print "--> Replacing root.img with provided file"
  135. os.unlink(root_img_path)
  136. # use 'cp' to preserve sparse file
  137. subprocess.check_call(['cp', args.root_copy_from, root_img_path])
  138. except (IOError, OSError) as err:
  139. parser.error(str(err))
  140. args.app.save()
  141. return 0
  142. if __name__ == '__main__':
  143. sys.exit(main())