qvm_create.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://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. # TODO list available classes
  24. # TODO list labels (maybe in qvm-prefs)
  25. # TODO features, devices, tags
  26. from __future__ import print_function
  27. import argparse
  28. import os
  29. import subprocess
  30. import sys
  31. import qubes
  32. import qubes.tools
  33. parser = qubes.tools.QubesArgumentParser(want_force_root=True)
  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=qubes.tools.PropertyAction,
  39. help='set domain\'s property, like "internal", "memory" or "vcpus"')
  40. parser.add_argument('--pool', '-p',
  41. action='append',
  42. metavar='POOL_NAME:VOLUME_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=qubes.tools.SinglePropertyAction,
  51. help='specify the TemplateVM to use')
  52. parser.add_argument('--label', '-l',
  53. action=qubes.tools.SinglePropertyAction,
  54. help='specify the label to use for the new domain'
  55. ' (e.g. red, yellow, green, ...)')
  56. parser_root = parser.add_mutually_exclusive_group()
  57. parser_root.add_argument('--root-copy-from', '-r', metavar='FILENAME',
  58. help='use provided root.img instead of default/empty one'
  59. ' (file will be COPIED)')
  60. parser_root.add_argument('--root-move-from', '-R', metavar='FILENAME',
  61. help='use provided root.img instead of default/empty one'
  62. ' (file will be MOVED)')
  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=qubes.tools.SinglePropertyAction,
  68. nargs='?',
  69. help='name of the domain to create')
  70. def main(args=None):
  71. args = parser.parse_args(args)
  72. pools = {}
  73. pool = None
  74. if hasattr(args, 'pools') and args.pools:
  75. for pool_vol in args.pool:
  76. try:
  77. pool_name, volume_name = pool_vol.split(':')
  78. pools[volume_name] = pool_name
  79. except ValueError:
  80. parser.error(
  81. 'Pool argument must be of form: -P pool_name:volume_name')
  82. if args.one_pool:
  83. pool = args.one_pool
  84. if 'label' not in args.properties:
  85. parser.error('--label option is mandatory')
  86. if 'name' not in args.properties:
  87. parser.error('VMNAME is mandatory')
  88. try:
  89. args.app.get_label(args.properties['label'])
  90. except KeyError:
  91. parser.error('no such label: {!r}; available: {}'.format(
  92. args.properties['label'],
  93. ', '.join(repr(l.name) for l in args.app.labels)))
  94. try:
  95. cls = args.app.get_vm_class(args.cls)
  96. except KeyError:
  97. parser.error('no such domain class: {!r}'.format(args.cls))
  98. if 'template' in args.properties and \
  99. 'template' not in (prop.__name__ for prop in cls.property_list()):
  100. parser.error('this domain class does not support template')
  101. vm = args.app.add_new_vm(cls, **args.properties)
  102. # pylint: disable=line-too-long
  103. # if not options.standalone and any([options.root_copy_from, options.root_move_from]):
  104. # print >> sys.stderr, "root.img can be specified only for standalone VMs"
  105. # exit (1)
  106. # if options.hvm_template and options.template is not None:
  107. # print >> sys.stderr, "Template VM cannot be based on another template"
  108. # exit (1)
  109. # if options.root_copy_from is not None and not os.path.exists(options.root_copy_from):
  110. # print >> sys.stderr, "File specified as root.img does not exists"
  111. # exit (1)
  112. # if options.root_move_from is not None and not os.path.exists(options.root_move_from):
  113. # print >> sys.stderr, "File specified as root.img does not exists"
  114. # exit (1)
  115. # elif not options.hvm and not options.hvm_template:
  116. # if qvm_collection.get_default_template() is None:
  117. # print >> sys.stderr, "No default TemplateVM defined!"
  118. # exit (1)
  119. # else:
  120. # template = qvm_collection.get_default_template()
  121. # if (options.verbose):
  122. # print('--> Using default TemplateVM: {0}'.format(template.name))
  123. if not args.no_root:
  124. try:
  125. vm.create_on_disk(pool, pools)
  126. # TODO this is file pool specific. Change it to a more general
  127. # solution
  128. root_img_path = vm.volumes['root'].vid
  129. if args.root_move_from is not None:
  130. # if (options.verbose):
  131. # print "--> Replacing root.img with provided file"
  132. os.unlink(root_img_path)
  133. os.rename(args.root_move_from, root_img_path)
  134. elif args.root_copy_from is not None:
  135. # if (options.verbose):
  136. # print "--> Replacing root.img with provided file"
  137. os.unlink(root_img_path)
  138. # use 'cp' to preserve sparse file
  139. subprocess.check_call(['cp', args.root_copy_from, root_img_path])
  140. except (IOError, OSError) as err:
  141. parser.error(str(err))
  142. args.app.save()
  143. return 0
  144. if __name__ == '__main__':
  145. sys.exit(main())