qvm-clone 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. parser.add_option ("-P", "--pool", dest="pool_name",
  39. help="Specify in to which storage pool to clone")
  40. (options, args) = parser.parse_args ()
  41. if (len (args) != 2):
  42. parser.error ("You must specify at least the src and dst TemplateVM names!")
  43. srcname = args[0]
  44. dstname = args[1]
  45. if hasattr(os, "geteuid") and os.geteuid() == 0:
  46. if not options.force_root:
  47. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  48. print >> sys.stderr, "Retry as unprivileged user."
  49. print >> sys.stderr, "... or use --force-root to continue anyway."
  50. exit(1)
  51. qvm_collection = QubesVmCollection()
  52. qvm_collection.lock_db_for_writing()
  53. qvm_collection.load()
  54. src_vm = qvm_collection.get_vm_by_name(srcname)
  55. if src_vm is None:
  56. print >> sys.stderr, "ERROR: A VM with the name '{0}' does not exist in the system.".format(srcname)
  57. exit(1)
  58. if options.pool_name is None:
  59. pool_name = src_vm.pool_name
  60. else:
  61. pool_name = options.pool_name
  62. if qvm_collection.get_vm_by_name(dstname) is not None:
  63. print >> sys.stderr, "ERROR: A VM with the name '{0}' already exists in the system.".format(dstname)
  64. exit(1)
  65. if src_vm.is_disposablevm():
  66. print >> sys.stderr, "ERROR: Clone not supported for this type of VM"
  67. exit(1)
  68. dst_vm = qvm_collection.add_new_vm(src_vm.__class__.__name__,
  69. name=dstname, template=src_vm.template, pool_name=pool_name,
  70. dir_path=options.dir_path, installed_by_rpm=False)
  71. try:
  72. dst_vm.clone_attrs(src_vm)
  73. dst_vm.clone_disk_files (src_vm=src_vm, verbose=options.verbose)
  74. except (IOError, OSError) as err:
  75. print >> sys.stderr, "ERROR: {0}".format(err)
  76. qvm_collection.pop(dst_vm.qid)
  77. dst_vm.remove_from_disk()
  78. exit (1)
  79. qvm_collection.save()
  80. qvm_collection.unlock_db()
  81. main()