qvm-create 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. from qubes.qubes import QubesVmCollection
  23. from qubes.qubes import QubesVmLabels
  24. from qubes.qubes import QubesException
  25. from optparse import OptionParser;
  26. import subprocess
  27. import re
  28. import os
  29. import sys
  30. def main():
  31. usage = "usage: %prog [options] <vm-name>"
  32. parser = OptionParser (usage)
  33. parser.add_option ("-t", "--template", dest="template",
  34. help="Specify the TemplateVM to use")
  35. parser.add_option ("-l", "--label", dest="label",
  36. help="Specify the label to use for the new VM (e.g. red, yellow, green, ...)")
  37. parser.add_option ("-p", "--proxy", action="store_true", dest="proxyvm", default=False,
  38. help="Create ProxyVM")
  39. parser.add_option ("-H", "--hvm", action="store_true", dest="hvm", default=False,
  40. help="Create HVM (standalone unless --template option used)")
  41. parser.add_option ("--hvm-template", action="store_true", dest="hvm_template", default=False,
  42. help="Create HVM template")
  43. parser.add_option ("-n", "--net", action="store_true", dest="netvm", default=False,
  44. help="Create NetVM")
  45. parser.add_option ("-s", "--standalone", action="store_true", dest="standalone", default=False,
  46. help="Create standalone VM - independent of template ")
  47. parser.add_option ("-R", "--root-move-from", dest="root_move", default=None,
  48. help="Use provided root.img instead of default/empty one (file will be MOVED)")
  49. parser.add_option ("-r", "--root-copy-from", dest="root_copy", default=None,
  50. help="Use provided root.img instead of default/empty one (file will be COPIED)")
  51. parser.add_option ("-m", "--mem", dest="mem", default=None,
  52. help="Initial memory size (in MB)")
  53. parser.add_option ("-c", "--vcpus", dest="vcpus", default=None,
  54. help="VCPUs count")
  55. parser.add_option ("-i", "--internal", action="store_true", dest="internal", default=False,
  56. help="Create VM for internal use only (hidden in qubes-manager, no appmenus)")
  57. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  58. help="Force to run, even with root privileges")
  59. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  60. (options, args) = parser.parse_args ()
  61. if (len (args) != 1):
  62. parser.error ("You must specify VM name!")
  63. vmname = args[0]
  64. if (options.netvm + options.proxyvm + options.hvm + options.hvm_template) > 1:
  65. parser.error ("You must specify at most one VM type switch")
  66. if os.geteuid() == 0:
  67. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  68. if options.force_root:
  69. print >> sys.stderr, "Continuing as commanded. You have been warned."
  70. else:
  71. print >> sys.stderr, "Retry as unprivileged user."
  72. print >> sys.stderr, "... or use --force-root to continue anyway."
  73. exit(1)
  74. if options.label is None:
  75. print >> sys.stderr, "You must choose a label for the new VM by passing the --label option."
  76. print >> sys.stderr, "Possible values are:"
  77. for l in QubesVmLabels.values():
  78. print >> sys.stderr, "* {0}".format(l.name)
  79. exit (1)
  80. if options.label not in QubesVmLabels:
  81. print >> sys.stderr, "Wrong label name, supported values are the following:"
  82. for l in QubesVmLabels.values():
  83. print >> sys.stderr, "* {0}".format(l.name)
  84. exit (1)
  85. label = QubesVmLabels[options.label]
  86. if options.hvm and not options.template:
  87. options.standalone = True
  88. if options.hvm_template:
  89. options.standalone = True
  90. if not options.standalone and any([options.root_copy, options.root_move]):
  91. print >> sys.stderr, "root.img can be specified only for standalone VMs"
  92. exit (1)
  93. if options.hvm_template and options.template is not None:
  94. print >> sys.stderr, "Template VM cannot be based on another template"
  95. exit (1)
  96. if options.root_copy and options.root_move:
  97. print >> sys.stderr, "Only one of --root-move-from and --root-copy from can be specified"
  98. exit(1)
  99. if options.root_copy is not None and not os.path.exists(options.root_copy):
  100. print >> sys.stderr, "File specified as root.img does not exists"
  101. exit (1)
  102. if options.root_move is not None and not os.path.exists(options.root_move):
  103. print >> sys.stderr, "File specified as root.img does not exists"
  104. exit (1)
  105. qvm_collection = QubesVmCollection()
  106. qvm_collection.lock_db_for_writing()
  107. qvm_collection.load()
  108. if qvm_collection.get_vm_by_name(vmname) is not None:
  109. print >> sys.stderr, "A VM with the name '{0}' already exists in the system.".format(vmname)
  110. exit(1)
  111. template = None
  112. if options.template is not None:
  113. template = qvm_collection.get_vm_by_name(options.template)
  114. if template is None:
  115. print >> sys.stderr, "There is no (Template)VM with the name '{0}'".format(options.template)
  116. exit (1)
  117. if not template.is_template():
  118. print >> sys.stderr, "VM '{0}' is not a TemplateVM".format(options.template)
  119. exit (1)
  120. if (options.verbose):
  121. print "--> Using TemplateVM: {0}".format(template.name)
  122. elif not options.hvm and not options.hvm_template:
  123. if qvm_collection.get_default_template() is None:
  124. print >> sys.stderr, "No default TemplateVM defined!"
  125. exit (1)
  126. else:
  127. template = qvm_collection.get_default_template()
  128. if (options.verbose):
  129. print "--> Using default TemplateVM: {0}".format(template.name)
  130. if options.standalone:
  131. new_vm_template = None
  132. else:
  133. new_vm_template = template
  134. vm = None
  135. if options.netvm:
  136. vmtype = "QubesNetVm"
  137. elif options.proxyvm:
  138. vmtype = "QubesProxyVm"
  139. elif options.hvm:
  140. vmtype = "QubesHVm"
  141. elif options.hvm_template:
  142. vmtype = "QubesTemplateHVm"
  143. else:
  144. vmtype = "QubesAppVm"
  145. try:
  146. vm = qvm_collection.add_new_vm(vmtype, name=vmname, template=new_vm_template, label = label)
  147. except QubesException as err:
  148. print >> sys.stderr, "ERROR: {0}".format(err)
  149. exit (1)
  150. if options.internal:
  151. vm.internal = True
  152. if options.mem is not None:
  153. vm.memory = options.mem
  154. if options.vcpus is not None:
  155. vm.vcpus = options.vcpus
  156. try:
  157. vm.create_on_disk(verbose=options.verbose, source_template=template)
  158. if options.root_move:
  159. os.unlink(vm.root_img)
  160. os.rename(options.root_move, vm.root_img)
  161. elif options.root_copy:
  162. os.unlink(vm.root_img)
  163. # use "cp" to preserve sparse file
  164. subprocess.check_call(["cp", options.root_copy, vm.root_img])
  165. except (IOError, OSError) as err:
  166. print >> sys.stderr, "ERROR: {0}".format(err)
  167. exit (1)
  168. qvm_collection.save()
  169. qvm_collection.unlock_db()
  170. main()