qvm-create 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. from qubes.qubes import vmm
  32. def main():
  33. usage = "usage: %prog [options] <vm-name>"
  34. parser = OptionParser (usage)
  35. parser.add_option ("-t", "--template", dest="template",
  36. help="Specify the TemplateVM to use")
  37. parser.add_option ("-l", "--label", dest="label",
  38. help="Specify the label to use for the new VM (e.g. red, yellow, green, ...)")
  39. parser.add_option ("-p", "--proxy", action="store_true", dest="proxyvm", default=False,
  40. help="Create ProxyVM")
  41. parser.add_option ("-P", "--pool", dest="pool_name",
  42. help="Specify which storage pool to use")
  43. parser.add_option ("-H", "--hvm", action="store_true", dest="hvm", default=False,
  44. help="Create HVM (standalone unless --template option used)")
  45. parser.add_option ("--hvm-template", action="store_true", dest="hvm_template", default=False,
  46. help="Create HVM template")
  47. parser.add_option ("-n", "--net", action="store_true", dest="netvm", default=False,
  48. help="Create NetVM")
  49. parser.add_option ("-s", "--standalone", action="store_true", dest="standalone", default=False,
  50. help="Create standalone VM - independent of template ")
  51. parser.add_option ("-R", "--root-move-from", dest="root_move", default=None,
  52. help="Use provided root.img instead of default/empty one (file will be MOVED)")
  53. parser.add_option ("-r", "--root-copy-from", dest="root_copy", default=None,
  54. help="Use provided root.img instead of default/empty one (file will be COPIED)")
  55. parser.add_option ("-m", "--mem", dest="mem", default=None,
  56. help="Initial memory size (in MB)")
  57. parser.add_option ("-c", "--vcpus", dest="vcpus", default=None,
  58. help="VCPUs count")
  59. parser.add_option ("--offline-mode", dest="offline_mode",
  60. action="store_true", default=False,
  61. help="Offline mode")
  62. parser.add_option ("-i", "--internal", action="store_true", dest="internal", default=False,
  63. help="Create VM for internal use only (hidden in qubes-manager, no appmenus)")
  64. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  65. help="Force to run, even with root privileges")
  66. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  67. (options, args) = parser.parse_args ()
  68. if (len (args) != 1):
  69. parser.error ("You must specify VM name!")
  70. vmname = args[0]
  71. if options.pool_name is None:
  72. pool_name = "default"
  73. else :
  74. pool_name = options.pool_name
  75. if (options.netvm + options.proxyvm + options.hvm + options.hvm_template) > 1:
  76. parser.error ("You must specify at most one VM type switch")
  77. if not options.force_root and hasattr(os, "geteuid") and os.geteuid() == 0:
  78. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  79. print >> sys.stderr, "Retry as unprivileged user."
  80. print >> sys.stderr, "... or use --force-root to continue anyway."
  81. exit(1)
  82. if options.label is None:
  83. print >> sys.stderr, "You must choose a label for the new VM by passing the --label option."
  84. print >> sys.stderr, "Possible values are:"
  85. for l in QubesVmLabels.values():
  86. print >> sys.stderr, "* {0}".format(l.name)
  87. exit (1)
  88. if options.label not in QubesVmLabels:
  89. print >> sys.stderr, "Wrong label name, supported values are the following:"
  90. for l in QubesVmLabels.values():
  91. print >> sys.stderr, "* {0}".format(l.name)
  92. exit (1)
  93. label = QubesVmLabels[options.label]
  94. if options.hvm and not options.template:
  95. options.standalone = True
  96. if options.hvm_template:
  97. options.standalone = True
  98. if not options.standalone and any([options.root_copy, options.root_move]):
  99. print >> sys.stderr, "root.img can be specified only for standalone VMs"
  100. exit (1)
  101. if options.hvm_template and options.template is not None:
  102. print >> sys.stderr, "Template VM cannot be based on another template"
  103. exit (1)
  104. if options.root_copy and options.root_move:
  105. print >> sys.stderr, "Only one of --root-move-from and --root-copy from can be specified"
  106. exit(1)
  107. if options.root_copy is not None and not os.path.exists(options.root_copy):
  108. print >> sys.stderr, "File specified as root.img does not exists"
  109. exit (1)
  110. if options.root_move is not None and not os.path.exists(options.root_move):
  111. print >> sys.stderr, "File specified as root.img does not exists"
  112. exit (1)
  113. if options.offline_mode:
  114. vmm.offline_mode = True
  115. qvm_collection = QubesVmCollection()
  116. qvm_collection.lock_db_for_writing()
  117. qvm_collection.load()
  118. if qvm_collection.get_vm_by_name(vmname) is not None:
  119. print >> sys.stderr, "A VM with the name '{0}' already exists in the system.".format(vmname)
  120. exit(1)
  121. template = None
  122. if options.template is not None:
  123. template = qvm_collection.get_vm_by_name(options.template)
  124. if template is None:
  125. print >> sys.stderr, "There is no (Template)VM with the name '{0}'".format(options.template)
  126. exit (1)
  127. if not template.is_template():
  128. print >> sys.stderr, "VM '{0}' is not a TemplateVM".format(options.template)
  129. exit (1)
  130. if (options.verbose):
  131. print "--> Using TemplateVM: {0}".format(template.name)
  132. elif not options.hvm and not options.hvm_template:
  133. if qvm_collection.get_default_template() is None:
  134. print >> sys.stderr, "No default TemplateVM defined!"
  135. exit (1)
  136. else:
  137. template = qvm_collection.get_default_template()
  138. if (options.verbose):
  139. print "--> Using default TemplateVM: {0}".format(template.name)
  140. if options.standalone:
  141. new_vm_template = None
  142. else:
  143. new_vm_template = template
  144. vm = None
  145. if options.netvm:
  146. vmtype = "QubesNetVm"
  147. elif options.proxyvm:
  148. vmtype = "QubesProxyVm"
  149. elif options.hvm:
  150. vmtype = "QubesHVm"
  151. elif options.hvm_template:
  152. vmtype = "QubesTemplateHVm"
  153. else:
  154. vmtype = "QubesAppVm"
  155. try:
  156. vm=qvm_collection.add_new_vm(vmtype, name=vmname,
  157. template=new_vm_template, label=label,
  158. pool_name=pool_name)
  159. except QubesException as err:
  160. print >> sys.stderr, "ERROR: {0}".format(err)
  161. exit (1)
  162. if options.internal:
  163. vm.internal = True
  164. if options.mem is not None:
  165. vm.memory = options.mem
  166. if options.vcpus is not None:
  167. vm.vcpus = options.vcpus
  168. try:
  169. vm.create_on_disk(verbose=options.verbose, source_template=template)
  170. if options.root_move:
  171. if (options.verbose):
  172. print "--> Replacing root.img with provided file"
  173. os.unlink(vm.root_img)
  174. os.rename(options.root_move, vm.root_img)
  175. elif options.root_copy:
  176. if (options.verbose):
  177. print "--> Replacing root.img with provided file"
  178. os.unlink(vm.root_img)
  179. # use "cp" to preserve sparse file
  180. subprocess.check_call(["cp", options.root_copy, vm.root_img])
  181. except (IOError, OSError) as err:
  182. print >> sys.stderr, "ERROR: {0}".format(err)
  183. exit (1)
  184. qvm_collection.save()
  185. qvm_collection.unlock_db()
  186. main()