qvm_create.py 5.2 KB

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