qvm_pool.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. # :pylint: disable=too-few-public-methods
  4. #
  5. # The Qubes OS Project, http://www.qubes-os.org
  6. #
  7. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. '''Manages Qubes pools and their options'''
  24. from __future__ import print_function
  25. import argparse
  26. import sys
  27. import qubes
  28. import qubes.ext
  29. import qubes.storage
  30. import qubes.tools
  31. drivers = qubes.storage.pool_drivers()
  32. class _HelpDrivers(argparse.Action):
  33. ''' Action for argument parser that displays all drivers and their options
  34. and exits.
  35. '''
  36. def __init__(self,
  37. option_strings,
  38. dest=argparse.SUPPRESS,
  39. default=argparse.SUPPRESS):
  40. super(_HelpDrivers, self).__init__(
  41. option_strings=option_strings,
  42. dest=dest,
  43. default=default,
  44. nargs=0,
  45. help='list all drivers with their options and exit')
  46. def __call__(self, parser, namespace, values, option_string=None):
  47. result = []
  48. for driver in drivers:
  49. params = driver_parameters(driver)
  50. driver_options = ', '.join(params)
  51. result += [(driver, 'driver options', driver_options)]
  52. qubes.tools.print_table(result)
  53. parser.exit(0)
  54. class _Info(qubes.tools.PoolsAction):
  55. ''' Action for argument parser that displays pool info and exits. '''
  56. def __init__(self, option_strings, help='print pool info and exit',
  57. **kwargs):
  58. # pylint: disable=redefined-builtin
  59. super(_Info, self).__init__(option_strings, help=help, **kwargs)
  60. def __call__(self, parser, namespace, values, option_string=None):
  61. setattr(namespace, 'command', 'info')
  62. super(_Info, self).__call__(parser, namespace, values, option_string)
  63. def pool_info(pool):
  64. ''' Prints out pool name and config '''
  65. data = [("name", pool.name)]
  66. data += [i for i in pool.config.items() if i[0] != 'name']
  67. qubes.tools.print_table(data)
  68. def list_pools(app):
  69. ''' Prints out all known pools and their drivers '''
  70. result = [('NAME', 'DRIVER')]
  71. for pool in app.pools.values():
  72. if len(pool.volumes) == 0 and issubclass(
  73. pool.__class__, qubes.storage.domain.DomainPool):
  74. # skip empty DomainPools
  75. continue
  76. result += [(pool.name, pool.driver)]
  77. qubes.tools.print_table(result)
  78. class _Remove(argparse.Action):
  79. ''' Action for argument parser that removes a pool '''
  80. def __init__(self, option_strings, dest=None, default=None, metavar=None):
  81. super(_Remove, self).__init__(option_strings=option_strings,
  82. dest=dest,
  83. metavar=metavar,
  84. default=default,
  85. help='remove pool')
  86. def __call__(self, parser, namespace, name, option_string=None):
  87. setattr(namespace, 'command', 'remove')
  88. setattr(namespace, 'name', name)
  89. class _Add(argparse.Action):
  90. ''' Action for argument parser that adds a pool. '''
  91. def __init__(self, option_strings, dest=None, default=None, metavar=None):
  92. super(_Add, self).__init__(option_strings=option_strings,
  93. dest=dest,
  94. metavar=metavar,
  95. default=default,
  96. nargs=2,
  97. help='add pool')
  98. def __call__(self, parser, namespace, values, option_string=None):
  99. name, driver = values
  100. if driver not in drivers:
  101. parser.error('driver %s is unknown \n' % driver)
  102. else:
  103. setattr(namespace, 'command', 'add')
  104. setattr(namespace, 'name', name)
  105. setattr(namespace, 'driver', driver)
  106. class _Options(argparse.Action):
  107. ''' Action for argument parser that parsers options. '''
  108. def __init__(self, option_strings, dest, default, metavar='options'):
  109. super(_Options, self).__init__(
  110. option_strings=option_strings,
  111. dest=dest,
  112. metavar=metavar,
  113. default=default,
  114. help='comma-separated list of driver options')
  115. def __call__(self, parser, namespace, options, option_string=None):
  116. setattr(namespace, 'options',
  117. dict([option.split('=', 1) for option in options.split(',')]))
  118. def get_parser():
  119. ''' Parses the provided args '''
  120. epilog = 'available pool drivers: ' \
  121. + ', '.join(drivers)
  122. parser = qubes.tools.QubesArgumentParser(description=__doc__,
  123. epilog=epilog)
  124. parser.add_argument('--help-drivers', action=_HelpDrivers)
  125. parser.add_argument('-o', action=_Options, dest='options', default={})
  126. group = parser.add_mutually_exclusive_group()
  127. group.add_argument('-l',
  128. '--list',
  129. dest='command',
  130. const='list',
  131. action='store_const',
  132. help='list all pools and exit (default action)')
  133. group.add_argument('-i', '--info', metavar='POOLNAME', dest='pools',
  134. action=_Info, default=[])
  135. group.add_argument('-a',
  136. '--add',
  137. action=_Add,
  138. dest='command',
  139. metavar=('NAME', 'DRIVER'))
  140. group.add_argument('-r', '--remove', metavar='NAME', action=_Remove)
  141. return parser
  142. def driver_parameters(name):
  143. ''' Get __init__ parameters from a driver with out `self` & `name`. '''
  144. init_function = qubes.utils.get_entry_point_one(
  145. qubes.storage.STORAGE_ENTRY_POINT, name).__init__
  146. params = init_function.func_code.co_varnames
  147. ignored_params = ['self', 'name']
  148. return [p for p in params if p not in ignored_params]
  149. def main(args=None):
  150. '''Main routine of :program:`qvm-pools`.
  151. :param list args: Optional arguments to override those delivered from \
  152. command line.
  153. '''
  154. parser = get_parser()
  155. try:
  156. args = parser.parse_args(args)
  157. except qubes.exc.QubesException as e:
  158. parser.print_error(e.message)
  159. return 1
  160. if args.command is None or args.command == 'list':
  161. list_pools(args.app)
  162. elif args.command == 'add':
  163. if args.name in args.app.pools.keys():
  164. parser.error('pool named %s already exists \n' % args.name)
  165. args.app.add_pool(name=args.name, driver=args.driver, **args.options)
  166. args.app.save()
  167. elif args.command == 'remove':
  168. if args.name in args.app.pools.keys():
  169. args.app.remove_pool(args.name)
  170. args.app.save()
  171. else:
  172. parser.print_error('no such pool %s\n' % args.name)
  173. elif args.command == 'info':
  174. pool_info(args.pools)
  175. return 0
  176. if __name__ == '__main__':
  177. sys.exit(main())