qvm_create.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.get_parser_base(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. #parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  62. def main():
  63. args = parser.parse_args()
  64. qubes.tools.set_verbosity(parser, args)
  65. qubes.tools.dont_run_as_root(parser, 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. app = qubes.Qubes(args.xml)
  71. try:
  72. label = app.get_label(args.properties['label'])
  73. except KeyError:
  74. parser.error('no such label: {!r}; available: {}'.format(args.label,
  75. ', '.join(repr(l.name) for l in app.labels)))
  76. try:
  77. cls = qubes.vm.BaseVM.register[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 = app.add_new_vm(cls, **args.properties)
  84. # if not options.standalone and any([options.root_copy_from, options.root_move_from]):
  85. # print >> sys.stderr, "root.img can be specified only for standalone VMs"
  86. # exit (1)
  87. # if options.hvm_template and options.template is not None:
  88. # print >> sys.stderr, "Template VM cannot be based on another template"
  89. # exit (1)
  90. # if options.root_copy_from is not None and not os.path.exists(options.root_copy_from):
  91. # print >> sys.stderr, "File specified as root.img does not exists"
  92. # exit (1)
  93. # if options.root_move_from is not None and not os.path.exists(options.root_move_from):
  94. # print >> sys.stderr, "File specified as root.img does not exists"
  95. # exit (1)
  96. # elif not options.hvm and not options.hvm_template:
  97. # if qvm_collection.get_default_template() is None:
  98. # print >> sys.stderr, "No default TemplateVM defined!"
  99. # exit (1)
  100. # else:
  101. # template = qvm_collection.get_default_template()
  102. # if (options.verbose):
  103. # print('--> Using default TemplateVM: {0}'.format(template.name))
  104. if not args.no_root:
  105. try:
  106. vm.create_on_disk()
  107. if args.root_move_from is not None:
  108. # if (options.verbose):
  109. # print "--> Replacing root.img with provided file"
  110. os.unlink(vm.root_img)
  111. os.rename(options.root_move_from, vm.root_img)
  112. elif args.root_copy_from is not None:
  113. # if (options.verbose):
  114. # print "--> Replacing root.img with provided file"
  115. os.unlink(vm.root_img)
  116. # use 'cp' to preserve sparse file
  117. subprocess.check_call(['cp', options.root_copy_from, vm.root_img])
  118. except (IOError, OSError) as err:
  119. parser.error(str(err))
  120. app.save()
  121. return True
  122. if __name__ == '__main__':
  123. sys.exit(not main())