qvm_clone.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (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 along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. ''' Clone a domain '''
  23. import sys
  24. from qubes.tools import QubesArgumentParser, SinglePropertyAction
  25. parser = QubesArgumentParser(description=__doc__, vmname_nargs=1)
  26. parser.add_argument('new_name',
  27. metavar='NEWVM',
  28. action=SinglePropertyAction,
  29. help='name of the domain to create')
  30. group = parser.add_mutually_exclusive_group()
  31. group.add_argument('-P',
  32. metavar='POOL',
  33. dest='one_pool',
  34. default='',
  35. help='pool to use for the new domain')
  36. group.add_argument('-p',
  37. '--pool',
  38. action='append',
  39. metavar='POOL:VOLUME',
  40. help='specify the pool to use for the specific volume')
  41. def main(args=None):
  42. ''' Clones an existing VM by copying all its disk files '''
  43. args = parser.parse_args(args)
  44. app = args.app
  45. src_vm = args.domains[0]
  46. new_name = args.properties['new_name']
  47. dst_vm = app.add_new_vm(src_vm.__class__, name=new_name)
  48. dst_vm.clone_properties(src_vm)
  49. if args.one_pool:
  50. dst_vm.clone_disk_files(src_vm, pool=args.one_pool)
  51. elif hasattr(args, 'pools') and args.pools:
  52. dst_vm.clone_disk_files(src_vm, pools=args.pools)
  53. else:
  54. dst_vm.clone_disk_files(src_vm)
  55. # try:
  56. app.save() # HACK remove_from_disk on exception hangs for some reason
  57. # except Exception as e: # pylint: disable=broad-except
  58. # dst_vm.remove_from_disk()
  59. # parser.print_error(e)
  60. # return 0
  61. if __name__ == '__main__':
  62. sys.exit(main())