qvm-add-appvm 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 QubesException
  24. from optparse import OptionParser;
  25. def main():
  26. usage = "usage: %prog [options] <appvm-name> <vm-template-name>\n\n"\
  27. "Adds an already installed appvm to the Qubes DB\n"\
  28. "WARNING: Noramlly you would not need this command,\n"\
  29. "and you would use qvm-create instead!"
  30. parser = OptionParser (usage)
  31. parser.add_option ("-p", "--path", dest="dir_path",
  32. help="Specify path to the template directory")
  33. parser.add_option ("-c", "--conf", dest="conf_file",
  34. help="Specify the Xen VM .conf file to use\
  35. (relative to the template dir path)")
  36. (options, args) = parser.parse_args ()
  37. if (len (args) != 2):
  38. parser.error ("You must specify at least the AppVM and TemplateVM names!")
  39. vmname = args[0]
  40. templatename = args[1]
  41. qvm_collection = QubesVmCollection()
  42. qvm_collection.lock_db_for_writing()
  43. qvm_collection.load()
  44. if qvm_collection.get_vm_by_name(vmname) is not None:
  45. print "ERROR: A VM with the name '{0}' already exists in the system.".format(vmname)
  46. exit(1)
  47. template_vm = qvm_collection.get_vm_by_name(templatename)
  48. if template_vm is None:
  49. print "ERROR: A Template VM with the name '{0}' does not exist in the system.".format(templatename)
  50. exit(1)
  51. vm = qvm_collection.add_new_appvm(vmname, template_vm,
  52. conf_file=options.conf_file,
  53. dir_path=options.dir_path)
  54. try:
  55. vm.verify_files()
  56. except QubesException as err:
  57. print "ERROR: {0}".format(err)
  58. qvm_collection.pop(vm.qid)
  59. exit (1)
  60. try:
  61. vm.add_to_xen_storage()
  62. except (IOError, OSError) as err:
  63. print "ERROR: {0}".format(err)
  64. qvm_collection.pop(vm.qid)
  65. exit (1)
  66. qvm_collection.save()
  67. qvm_collection.unlock_db()
  68. main()