qvm-clone 3.2 KB

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