qvm_clone.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. import qubesadmin.exc
  25. from qubesadmin.tools import QubesArgumentParser
  26. parser = QubesArgumentParser(description=__doc__, vmname_nargs=1)
  27. parser.add_argument('new_name',
  28. metavar='NEWVM',
  29. action='store',
  30. help='name of the domain to create')
  31. parser.add_argument('--class', '-C', dest='cls',
  32. default=None,
  33. help='specify the class of the new domain (default: same as source)')
  34. parser.add_argument('--ignore-errors', action='store_true',
  35. default=False,
  36. help='log errors encountered during setting metadata'
  37. 'but continue clone operation')
  38. group = parser.add_mutually_exclusive_group()
  39. group.add_argument('-P',
  40. metavar='POOL',
  41. dest='one_pool',
  42. default='',
  43. help='pool to use for the new domain')
  44. group.add_argument('-p',
  45. '--pool',
  46. action='append',
  47. metavar='VOLUME=POOL',
  48. help='specify the pool to use for the specific volume')
  49. def main(args=None, app=None):
  50. ''' Clones an existing VM by copying all its disk files '''
  51. args = parser.parse_args(args, app=app)
  52. app = args.app
  53. src_vm = args.domains[0]
  54. new_name = args.new_name
  55. pool = None
  56. pools = {}
  57. if args.one_pool:
  58. pool = args.one_pool
  59. elif hasattr(args, 'pool') and args.pool:
  60. for pool_vol in args.pool:
  61. try:
  62. volume_name, pool_name = pool_vol.split('=')
  63. pools[volume_name] = pool_name
  64. except ValueError:
  65. parser.error(
  66. 'Pool argument must be of form: -P volume_name=pool_name')
  67. try:
  68. app.clone_vm(src_vm, new_name, new_cls=args.cls, pool=pool, pools=pools,
  69. ignore_errors=args.ignore_errors)
  70. except qubesadmin.exc.QubesException as e:
  71. parser.error_runtime(e)
  72. if __name__ == '__main__':
  73. sys.exit(main())