qvm-add-template 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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] <vm-template-name>\n"\
  27. "Adds an already installed template to the Qubes DB"
  28. parser = OptionParser (usage)
  29. parser.add_option ("-p", "--path", dest="dir_path",
  30. help="Specify path to the template directory")
  31. parser.add_option ("-c", "--conf", dest="conf_file",
  32. help="Specify the Xen VM .conf file to use\
  33. (relative to the template dir path)")
  34. parser.add_option ("--rpm", action="store_true", dest="installed_by_rpm",
  35. help="Template files have been installed by RPM", default=False)
  36. (options, args) = parser.parse_args ()
  37. if (len (args) != 1):
  38. parser.error ("You must specify at least the TemplateVM name!")
  39. vmname = args[0]
  40. qvm_collection = QubesVmCollection()
  41. qvm_collection.lock_db_for_writing()
  42. qvm_collection.load()
  43. if qvm_collection.get_vm_by_name(vmname) is not None:
  44. print "ERROR: A VM with the name '{0}' already exists in the system.".format(vmname)
  45. exit(1)
  46. vm = qvm_collection.add_new_templatevm(vmname,
  47. conf_file=options.conf_file,
  48. dir_path=options.dir_path,
  49. installed_by_rpm=options.installed_by_rpm)
  50. try:
  51. vm.verify_files()
  52. except QubesException as err:
  53. print "ERROR: {0}".format(err)
  54. qvm_collection.pop(vm.qid)
  55. exit (1)
  56. try:
  57. vm.add_to_xen_storage()
  58. except (IOError, OSError) as err:
  59. print "ERROR: {0}".format(err)
  60. qvm_collection.pop(vm.qid)
  61. exit (1)
  62. qvm_collection.save()
  63. qvm_collection.unlock_db()
  64. main()