qvm-clone 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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,
  21. # USA.
  22. import os
  23. import sys
  24. from optparse import OptionParser
  25. from qubes.qubes import QubesVmCollection
  26. def main():
  27. usage = "usage: %prog [options] <src-name> <new-name>\n"\
  28. "Clones an existing VM by copying all its disk files"
  29. parser = OptionParser(usage)
  30. parser.add_option("-q", "--quiet", action="store_false", dest="verbose",
  31. default=True)
  32. parser.add_option("-p", "--path", dest="dir_path",
  33. help="Specify path to the template directory")
  34. parser.add_option("--force-root", action="store_true", dest="force_root",
  35. default=False,
  36. help="Force to run, even with root privileges")
  37. parser.add_option("-P", "--pool", dest="pool_name",
  38. help="Specify in to which storage pool to clone")
  39. (options, args) = parser.parse_args()
  40. if (len(args) != 2):
  41. parser.error(
  42. "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" + \
  48. " strongly discouraged, this will lead you in permissions" + \
  49. "problems."
  50. print >> sys.stderr, "Retry as unprivileged user."
  51. print >> sys.stderr, "... or use --force-root to continue anyway."
  52. exit(1)
  53. qvm_collection = QubesVmCollection()
  54. qvm_collection.lock_db_for_writing()
  55. qvm_collection.load()
  56. src_vm = qvm_collection.get_vm_by_name(srcname)
  57. if src_vm is None:
  58. print >> sys.stderr, \
  59. "ERROR: A VM with the name '{0}' does not exist in the system." \
  60. .format(srcname)
  61. exit(1)
  62. if options.pool_name is None:
  63. pool_name = src_vm.pool_name
  64. else:
  65. pool_name = options.pool_name
  66. if qvm_collection.get_vm_by_name(dstname) is not None:
  67. print >> sys.stderr, \
  68. "ERROR: A VM with the name '{0}' already exists in the system." \
  69. .format(dstname)
  70. exit(1)
  71. if src_vm.is_disposablevm():
  72. print >> sys.stderr, "ERROR: Clone not supported for this type of VM"
  73. exit(1)
  74. dst_vm = qvm_collection.add_new_vm(src_vm.__class__.__name__,
  75. name=dstname, template=src_vm.template,
  76. pool_name=pool_name,
  77. dir_path=options.dir_path,
  78. installed_by_rpm=False)
  79. try:
  80. dst_vm.clone_attrs(src_vm)
  81. dst_vm.clone_disk_files(src_vm=src_vm, verbose=options.verbose)
  82. except (IOError, OSError) as err:
  83. print >> sys.stderr, "ERROR: {0}".format(err)
  84. qvm_collection.pop(dst_vm.qid)
  85. dst_vm.remove_from_disk()
  86. exit(1)
  87. qvm_collection.save()
  88. qvm_collection.unlock_db()
  89. main()