qvm-clone 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 QubesAppVm, QubesTemplateVm, QubesHVm
  25. from qubes.qubes import QubesException
  26. from optparse import OptionParser;
  27. import sys
  28. import os
  29. def main():
  30. usage = "usage: %prog [options] <src-name> <new-name>\n"\
  31. "Clones an existing VM by copying all its disk files"
  32. parser = OptionParser (usage)
  33. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  34. parser.add_option ("-p", "--path", dest="dir_path",
  35. help="Specify path to the template directory")
  36. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  37. help="Force to run, even with root privileges")
  38. (options, args) = parser.parse_args ()
  39. if (len (args) != 2):
  40. parser.error ("You must specify at least the src and dst TemplateVM names!")
  41. srcname = args[0]
  42. dstname = args[1]
  43. if hasattr(os, "geteuid") and os.geteuid() == 0:
  44. if not options.force_root:
  45. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  46. print >> sys.stderr, "Retry as unprivileged user."
  47. print >> sys.stderr, "... or use --force-root to continue anyway."
  48. exit(1)
  49. qvm_collection = QubesVmCollection()
  50. qvm_collection.lock_db_for_writing()
  51. qvm_collection.load()
  52. src_vm = qvm_collection.get_vm_by_name(srcname)
  53. if src_vm is None:
  54. print >> sys.stderr, "ERROR: A VM with the name '{0}' does not exist in the system.".format(srcname)
  55. exit(1)
  56. if qvm_collection.get_vm_by_name(dstname) is not None:
  57. print >> sys.stderr, "ERROR: A VM with the name '{0}' already exists in the system.".format(dstname)
  58. exit(1)
  59. if src_vm.is_disposablevm():
  60. print >> sys.stderr, "ERROR: Clone not supported for this type of VM"
  61. exit(1)
  62. dst_vm = qvm_collection.add_new_vm(src_vm.__class__.__name__,
  63. name=dstname, template=src_vm.template,
  64. dir_path=options.dir_path, installed_by_rpm=False)
  65. try:
  66. dst_vm.clone_attrs(src_vm)
  67. dst_vm.clone_disk_files (src_vm=src_vm, verbose=options.verbose)
  68. except (IOError, OSError) as err:
  69. print >> sys.stderr, "ERROR: {0}".format(err)
  70. qvm_collection.pop(dst_vm.qid)
  71. dst_vm.remove_from_disk()
  72. exit (1)
  73. qvm_collection.save()
  74. qvm_collection.unlock_db()
  75. main()