qvm-clone-template 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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] <src-template-name> <new-template-name>\n"\
  27. "Clones an existing template by copying all its disk files"
  28. parser = OptionParser (usage)
  29. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  30. parser.add_option ("-p", "--path", dest="dir_path",
  31. help="Specify path to the template directory")
  32. (options, args) = parser.parse_args ()
  33. if (len (args) != 2):
  34. parser.error ("You must specify at least the src and dst TemplateVM names!")
  35. srcname = args[0]
  36. dstname = args[1]
  37. qvm_collection = QubesVmCollection()
  38. qvm_collection.lock_db_for_writing()
  39. qvm_collection.load()
  40. src_tvm = qvm_collection.get_vm_by_name(srcname)
  41. if src_tvm is None:
  42. print "ERROR: A VM with the name '{0}' does not exist in the system.".format(srcname)
  43. exit(1)
  44. if qvm_collection.get_vm_by_name(dstname) is not None:
  45. print "ERROR: A VM with the name '{0}' already exists in the system.".format(dstname)
  46. exit(1)
  47. dst_tvm = qvm_collection.clone_templatevm(src_template_vm=src_tvm,
  48. name=dstname,
  49. dir_path=options.dir_path)
  50. try:
  51. dst_tvm.clone_disk_files (src_template_vm=src_tvm, verbose=options.verbose)
  52. if options.verbose:
  53. print "--> Adding to Xen Storage..."
  54. dst_tvm.add_to_xen_storage()
  55. except (IOError, OSError) as err:
  56. print "ERROR: {0}".format(err)
  57. qvm_collection.pop(dst_tvm.qid)
  58. exit (1)
  59. qvm_collection.save()
  60. qvm_collection.unlock_db()
  61. main()