qvm-create 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python2.6
  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 optparse import OptionParser;
  25. import subprocess
  26. def main():
  27. usage = "usage: %prog [options] <vm-name>"
  28. parser = OptionParser (usage)
  29. parser.add_option ("-t", "--template", dest="template",
  30. help="Specify the TemplateVM to use")
  31. parser.add_option ("-l", "--label", dest="label",
  32. help="Specify the label to use for the new VM (e.g. red, yellow, green, ...)")
  33. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  34. (options, args) = parser.parse_args ()
  35. if (len (args) != 1):
  36. parser.error ("You must specify VM name!")
  37. vmname = args[0]
  38. if options.label is None:
  39. print "You must choose a label for the new VM by passing the --label option."
  40. print "Possible values are:"
  41. for l in QubesVmLabels.values():
  42. print "* {0}".format(l.name)
  43. exit (1)
  44. if options.label not in QubesVmLabels:
  45. print "Wrong label name, supported values are the following:"
  46. for l in QubesVmLabels.values():
  47. print "* {0}".format(l.name)
  48. exit (1)
  49. label = QubesVmLabels[options.label]
  50. qvm_collection = QubesVmCollection()
  51. qvm_collection.lock_db_for_writing()
  52. qvm_collection.load()
  53. if qvm_collection.get_vm_by_name(vmname) is not None:
  54. print "A VM with the name '{0}' already exists in the system.".format(vmname)
  55. exit(1)
  56. if options.template is not None:
  57. template_vm = qvm_collection.get_vm_by_name(options.template)
  58. if template_vm is None:
  59. print "There is no (Templete)VM with the name '{0}'".format(options.template)
  60. exit (1)
  61. if not template_vm.is_templete():
  62. print "VM '{0}' is not a TemplateVM".format(options.template)
  63. exit (1)
  64. if (options.verbose):
  65. print "--> Using TemplateVM: {0}".format(template_vm.name)
  66. else:
  67. if qvm_collection.get_default_template_vm() is None:
  68. print "No default TempleteVM defined!"
  69. exit (1)
  70. else:
  71. template_vm = qvm_collection.get_default_template_vm()
  72. if (options.verbose):
  73. print "--> Using default TemplateVM: {0}".format(template_vm.name)
  74. vm = qvm_collection.add_new_appvm(vmname, template_vm, label = label)
  75. try:
  76. vm.create_on_disk(verbose=options.verbose)
  77. vm.add_to_xen_storage()
  78. except (IOError, OSError) as err:
  79. print "ERROR: {0}".format(err)
  80. vm.remove_from_disk()
  81. exit (1)
  82. qvm_collection.save()
  83. qvm_collection.unlock_db()
  84. main()