qvm_create.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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', '-p',
  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('--template', '-t',
  45. action=qubes.tools.SinglePropertyAction,
  46. help='specify the TemplateVM to use')
  47. parser.add_argument('--label', '-l',
  48. action=qubes.tools.SinglePropertyAction,
  49. help='specify the label to use for the new domain'
  50. ' (e.g. red, yellow, green, ...)')
  51. parser_root = parser.add_mutually_exclusive_group()
  52. parser_root.add_argument('--root-copy-from', '-r', metavar='FILENAME',
  53. help='use provided root.img instead of default/empty one'
  54. ' (file will be COPIED)')
  55. parser_root.add_argument('--root-move-from', '-R', metavar='FILENAME',
  56. help='use provided root.img instead of default/empty one'
  57. ' (file will be MOVED)')
  58. parser_root.add_argument('--no-root',
  59. action='store_true', default=False,
  60. help=argparse.SUPPRESS)
  61. parser.add_argument('name', metavar='VMNAME',
  62. action=qubes.tools.SinglePropertyAction,
  63. nargs='?',
  64. help='name of the domain to create')
  65. def main(args=None):
  66. args = parser.parse_args(args)
  67. if args.pool:
  68. args.properties['volume_config'] = {}
  69. for pool_vol in args.pool:
  70. try:
  71. pool_name, volume_name = pool_vol.split(':')
  72. config = {'pool': pool_name, 'name': volume_name}
  73. args.properties['volume_config'][volume_name] = config
  74. except ValueError:
  75. parser.error(
  76. 'Pool argument must be of form: -P pool_name:volume_name')
  77. if 'label' not in args.properties:
  78. parser.error('--label option is mandatory')
  79. if 'name' not in args.properties:
  80. parser.error('VMNAME is mandatory')
  81. try:
  82. args.app.get_label(args.properties['label'])
  83. except KeyError:
  84. parser.error('no such label: {!r}; available: {}'.format(
  85. args.properties['label'],
  86. ', '.join(repr(l.name) for l in args.app.labels)))
  87. try:
  88. cls = args.app.get_vm_class(args.cls)
  89. except KeyError:
  90. parser.error('no such domain class: {!r}'.format(args.cls))
  91. if 'template' in args.properties and \
  92. 'template' not in (prop.__name__ for prop in cls.property_list()):
  93. parser.error('this domain class does not support template')
  94. vm = args.app.add_new_vm(cls, **args.properties)
  95. # pylint: disable=line-too-long
  96. # if not options.standalone and any([options.root_copy_from, options.root_move_from]):
  97. # print >> sys.stderr, "root.img can be specified only for standalone VMs"
  98. # exit (1)
  99. # if options.hvm_template and options.template is not None:
  100. # print >> sys.stderr, "Template VM cannot be based on another template"
  101. # exit (1)
  102. # if options.root_copy_from is not None and not os.path.exists(options.root_copy_from):
  103. # print >> sys.stderr, "File specified as root.img does not exists"
  104. # exit (1)
  105. # if options.root_move_from is not None and not os.path.exists(options.root_move_from):
  106. # print >> sys.stderr, "File specified as root.img does not exists"
  107. # exit (1)
  108. # elif not options.hvm and not options.hvm_template:
  109. # if qvm_collection.get_default_template() is None:
  110. # print >> sys.stderr, "No default TemplateVM defined!"
  111. # exit (1)
  112. # else:
  113. # template = qvm_collection.get_default_template()
  114. # if (options.verbose):
  115. # print('--> Using default TemplateVM: {0}'.format(template.name))
  116. if not args.no_root:
  117. try:
  118. vm.create_on_disk()
  119. if args.root_move_from is not None:
  120. # if (options.verbose):
  121. # print "--> Replacing root.img with provided file"
  122. os.unlink(vm.root_img)
  123. os.rename(args.root_move_from, vm.root_img)
  124. elif args.root_copy_from is not None:
  125. # if (options.verbose):
  126. # print "--> Replacing root.img with provided file"
  127. os.unlink(vm.root_img)
  128. # use 'cp' to preserve sparse file
  129. subprocess.check_call(['cp', args.root_copy_from, vm.root_img])
  130. except (IOError, OSError) as err:
  131. parser.error(str(err))
  132. args.app.save()
  133. return 0
  134. if __name__ == '__main__':
  135. sys.exit(main())