qvm_clone.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser 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 qubesadmin.tools import QubesArgumentParser
  25. parser = QubesArgumentParser(description=__doc__, vmname_nargs=1)
  26. parser.add_argument('new_name',
  27. metavar='NEWVM',
  28. action='store',
  29. help='name of the domain to create')
  30. parser.add_argument('--class', '-C', dest='cls',
  31. default=None,
  32. help='specify the class of the new domain (default: same as source)')
  33. group = parser.add_mutually_exclusive_group()
  34. group.add_argument('-P',
  35. metavar='POOL',
  36. dest='one_pool',
  37. default='',
  38. help='pool to use for the new domain')
  39. group.add_argument('-p',
  40. '--pool',
  41. action='append',
  42. metavar='VOLUME=POOL',
  43. help='specify the pool to use for the specific volume')
  44. def main(args=None, app=None):
  45. ''' Clones an existing VM by copying all its disk files '''
  46. args = parser.parse_args(args, app=app)
  47. app = args.app
  48. src_vm = args.domains[0]
  49. new_name = args.new_name
  50. pool = None
  51. pools = {}
  52. if args.one_pool:
  53. pool = args.one_pool
  54. elif hasattr(args, 'pool') and args.pool:
  55. for pool_vol in args.pool:
  56. try:
  57. volume_name, pool_name = pool_vol.split('=')
  58. pools[volume_name] = pool_name
  59. except ValueError:
  60. parser.error(
  61. 'Pool argument must be of form: -P volume_name=pool_name')
  62. app.clone_vm(src_vm, new_name, new_cls=args.cls, pool=pool, pools=pools)
  63. if __name__ == '__main__':
  64. sys.exit(main())