qvm-create 8.2 KB

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