qvm-create 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 (implies --standalone)")
  41. parser.add_option ("-n", "--net", action="store_true", dest="netvm", default=False,
  42. help="Create NetVM")
  43. parser.add_option ("-s", "--standalone", action="store_true", dest="standalone", default=False,
  44. help="Create standalone VM - independent of template ")
  45. parser.add_option ("-r", "--root", dest="root", default=None,
  46. help="Use provided root.img instead of default/empty one (file will be MOVED)")
  47. parser.add_option ("-m", "--mem", dest="mem", default=None,
  48. help="Initial memory size (in MB)")
  49. parser.add_option ("-c", "--vcpus", dest="vcpus", default=None,
  50. help="VCPUs count")
  51. parser.add_option ("-i", "--internal", action="store_true", dest="internal", default=False,
  52. help="Create VM for internal use only (hidden in qubes-manager, no appmenus)")
  53. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  54. help="Force to run, even with root privileges")
  55. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  56. (options, args) = parser.parse_args ()
  57. if (len (args) != 1):
  58. parser.error ("You must specify VM name!")
  59. vmname = args[0]
  60. if options.netvm and options.proxyvm:
  61. parser.error ("You must specify at most one of --proxy and --net")
  62. if os.geteuid() == 0:
  63. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  64. if options.force_root:
  65. print >> sys.stderr, "Continuing as commanded. You have been warned."
  66. else:
  67. print >> sys.stderr, "Retry as unprivileged user."
  68. print >> sys.stderr, "... or use --force-root to continue anyway."
  69. exit(1)
  70. if options.label is None:
  71. print >> sys.stderr, "You must choose a label for the new VM by passing the --label option."
  72. print >> sys.stderr, "Possible values are:"
  73. for l in QubesVmLabels.values():
  74. print >> sys.stderr, "* {0}".format(l.name)
  75. exit (1)
  76. if options.label not in QubesVmLabels:
  77. print >> sys.stderr, "Wrong label name, supported values are the following:"
  78. for l in QubesVmLabels.values():
  79. print >> sys.stderr, "* {0}".format(l.name)
  80. exit (1)
  81. label = QubesVmLabels[options.label]
  82. if options.hvm:
  83. # Only standalone HVMs are supported for now
  84. options.standalone = True
  85. if not options.standalone and options.root is not None:
  86. print >> sys.stderr, "root.img can be specified only for standalone VMs"
  87. exit (1)
  88. if options.root is not None and not os.path.exists(options.root):
  89. print >> sys.stderr, "File specified as root.img does not exists"
  90. exit (1)
  91. qvm_collection = QubesVmCollection()
  92. qvm_collection.lock_db_for_writing()
  93. qvm_collection.load()
  94. if qvm_collection.get_vm_by_name(vmname) is not None:
  95. print >> sys.stderr, "A VM with the name '{0}' already exists in the system.".format(vmname)
  96. exit(1)
  97. template = None
  98. if options.template is not None:
  99. template = qvm_collection.get_vm_by_name(options.template)
  100. if template is None:
  101. print >> sys.stderr, "There is no (Template)VM with the name '{0}'".format(options.template)
  102. exit (1)
  103. if not template.is_template():
  104. print >> sys.stderr, "VM '{0}' is not a TemplateVM".format(options.template)
  105. exit (1)
  106. if (options.verbose):
  107. print "--> Using TemplateVM: {0}".format(template.name)
  108. elif not options.hvm:
  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 options.standalone:
  117. new_vm_template = None
  118. else:
  119. new_vm_template = template
  120. vm = None
  121. if options.netvm:
  122. vmtype = "QubesNetVm"
  123. elif options.proxyvm:
  124. vmtype = "QubesProxyVm"
  125. elif options.hvm:
  126. vmtype = "QubesHVm"
  127. else:
  128. vmtype = "QubesAppVm"
  129. try:
  130. vm = qvm_collection.add_new_vm(vmtype, name=vmname, template=new_vm_template, label = label)
  131. except QubesException as err:
  132. print >> sys.stderr, "ERROR: {0}".format(err)
  133. exit (1)
  134. if options.internal:
  135. vm.internal = True
  136. if options.mem is not None:
  137. vm.memory = options.mem
  138. if options.vcpus is not None:
  139. vm.vcpus = options.vcpus
  140. try:
  141. vm.create_on_disk(verbose=options.verbose, source_template=template)
  142. if options.root:
  143. os.unlink(vm.root_img)
  144. os.rename(options.root, vm.root_img)
  145. except (IOError, OSError) as err:
  146. print >> sys.stderr, "ERROR: {0}".format(err)
  147. exit (1)
  148. qvm_collection.save()
  149. qvm_collection.unlock_db()
  150. main()