qvm_clone.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. ''' Clone a domain '''
  21. import sys
  22. from qubes.tools import QubesArgumentParser, SinglePropertyAction
  23. parser = QubesArgumentParser(description=__doc__, vmname_nargs=1)
  24. parser.add_argument('new_name',
  25. metavar='NEWVM',
  26. action=SinglePropertyAction,
  27. help='name of the domain to create')
  28. group = parser.add_mutually_exclusive_group()
  29. group.add_argument('-P',
  30. metavar='POOL',
  31. dest='one_pool',
  32. default='',
  33. help='pool to use for the new domain')
  34. group.add_argument('-p',
  35. '--pool',
  36. action='append',
  37. metavar='POOL:VOLUME',
  38. help='specify the pool to use for the specific volume')
  39. def main(args=None):
  40. ''' Clones an existing VM by copying all its disk files '''
  41. args = parser.parse_args(args)
  42. app = args.app
  43. src_vm = args.domains[0]
  44. new_name = args.properties['new_name']
  45. dst_vm = app.add_new_vm(src_vm.__class__, name=new_name)
  46. dst_vm.clone_properties(src_vm)
  47. if args.one_pool:
  48. dst_vm.clone_disk_files(src_vm, pool=args.one_pool)
  49. elif hasattr(args, 'pools') and args.pools:
  50. dst_vm.clone_disk_files(src_vm, pools=args.pools)
  51. else:
  52. dst_vm.clone_disk_files(src_vm)
  53. # try:
  54. app.save() # HACK remove_from_disk on exception hangs for some reason
  55. # except Exception as e: # pylint: disable=broad-except
  56. # dst_vm.remove_from_disk()
  57. # parser.print_error(e)
  58. # return 0
  59. if __name__ == '__main__':
  60. sys.exit(main())