qvm_create.py 5.3 KB

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