qubes-features.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. :py:class:`qubes.vm.Features` - Qubes VM features, services
  2. ============================================================
  3. Features are generic mechanism for storing key-value pairs attached to a
  4. VM. The primary use case for them is data storage for extensions (you can think
  5. of them as more flexible properties, defined by extensions), but some are also
  6. used in the qubes core itself. There is no definite list of supported features,
  7. each extension can set their own and there is no requirement of registration,
  8. but :program:`qvm-features` man page contains well known ones.
  9. In addition, there is a mechanism for VM request setting a feature. This is
  10. useful for extensions to discover if its VM part is present.
  11. Features can have three distinct values: no value (not present in mapping,
  12. which is closest thing to :py:obj:`None`), empty string (which is
  13. interpreted as :py:obj:`False`) and non-empty string, which is
  14. :py:obj:`True`. Anything assigned to the mapping is coerced to strings,
  15. however if you assign instances of :py:class:`bool`, they are converted as
  16. described above. Be aware that assigning the number `0` (which is considered
  17. false in Python) will result in string `'0'`, which is considered true.
  18. :py:class:`qubes.vm.Features` inherits from :py:class:`dict`, so provide all the
  19. standard functions to get, list and set values. Additionally provide helper
  20. functions to check if given feature is set on the VM and default to the value
  21. on the VM's template or netvm. This is useful for features which nature is
  22. inherited from other VMs, like "is package X is installed" or "is VM behind a
  23. VPN".
  24. Example usage of features in extension:
  25. .. code-block:: python
  26. import qubes.exc
  27. import qubes.ext
  28. class ExampleExtension(qubes.ext.Extension):
  29. @qubes.ext.handler('domain-pre-start')
  30. def on_domain_start(self, vm, event, **kwargs):
  31. if vm.features.get('do-not-start', False):
  32. raise qubes.exc.QubesVMError(vm,
  33. 'Start prohibited because of do-not-start feature')
  34. if vm.features.check_with_template('something-installed', False):
  35. # do something
  36. The above extension does two things:
  37. - prevent starting a qube with ``do-not-start`` feature set
  38. - do something when ``something-installed`` feature is set on the qube, or its template
  39. qvm-features-request, qubes.PostInstall service
  40. ------------------------------------------------
  41. When some package in the VM want to request feature to be set (aka advertise
  42. support for it), it should place a shell script in ``/etc/qubes/post-install.d``.
  43. This script should call :program:`qvm-features-request` with ``FEATURE=VALUE`` pair(s) as
  44. arguments to request those features. It is recommended to use very simple
  45. values here (for example ``1``). The script should be named in form
  46. ``XX-package-name.sh`` where ``XX`` is two-digits number below 90 and
  47. ``package-name`` is unique name specific to this package (preferably actual
  48. package name). The script needs executable bit set.
  49. ``qubes.PostInstall`` service will call all those scripts after any package
  50. installation and also after initial template installation.
  51. This way package have a chance to report to dom0 if any feature is
  52. added/removed.
  53. The features flow to dom0 according to the diagram below. Important part is
  54. that qubes core :py:class:`qubes.ext.Extension` is responsible for handling such request in
  55. ``features-request`` event handler. If no extension handles given feature request,
  56. it will be ignored. The extension should carefuly validate requested
  57. features (ignoring those not recognized - may be for another extension) and
  58. only then set appropriate value on VM object
  59. (:py:attr:`qubes.vm.BaseVM.features`). It is recommended to make the
  60. verification code as bulletproof as possible (for example allow only specific
  61. simple values, instead of complex structures), because feature requests
  62. come from untrusted sources. The features actually set on the VM in some cases
  63. may not be necessary those requested. Similar for values.
  64. .. graphviz::
  65. digraph {
  66. "qubes.PostInstall";
  67. "/etc/qubes/post-install.d/ scripts";
  68. "qvm-features-request";
  69. "qubes.FeaturesRequest";
  70. "qubes core extensions";
  71. "VM features";
  72. "qubes.PostInstall" -> "/etc/qubes/post-install.d/ scripts";
  73. "/etc/qubes/post-install.d/ scripts" -> "qvm-features-request"
  74. [xlabel="each script calls"];
  75. "qvm-features-request" -> "qubes.FeaturesRequest"
  76. [xlabel="last script call the service to dom0"];
  77. "qubes.FeaturesRequest" -> "qubes core extensions"
  78. [xlabel="features-request event"];
  79. "qubes core extensions" -> "VM features"
  80. [xlabel="verification"];
  81. }
  82. Example ``/etc/qubes/post-install.d/20-example.sh`` file:
  83. .. code-block:: shell
  84. #!/bin/sh
  85. qvm-features-request example-feature=1
  86. Example extension handling the above:
  87. .. code-block:: python
  88. import qubes.ext
  89. class ExampleExtension(qubes.ext.Extension):
  90. # the last argument must be named untrusted_features
  91. @qubes.ext.handler('features-request')
  92. def on_features_request(self, vm, event, untrusted_features):
  93. # don't allow TemplateBasedVMs to request the feature - should be
  94. # requested by the template instead
  95. if hasattr(vm, 'template'):
  96. return
  97. untrusted_value = untrusted_features.get('example-feature', None)
  98. # check if feature is advertised and verify its value
  99. if untrusted_value != '1':
  100. return
  101. value = untrusted_value
  102. # and finally set the value
  103. vm.features['example-feature'] = value
  104. Services
  105. ---------
  106. `Qubes services <https://www.qubes-os.org/doc/qubes-service/>`_ are implemented
  107. as features with ``service.`` prefix. The
  108. :py:class:`qubes.ext.services.ServicesExtension` enumerate all the features
  109. in form of ``service.<service-name>`` prefix and write them to QubesDB as
  110. ``/qubes-service/<service-name>`` and value either ``0`` or ``1``.
  111. VM startup scripts list those entries for for each with value of ``1``, create
  112. ``/var/run/qubes-service/<service-name>`` file. Then, it can be conveniently
  113. used by other scripts to check whether dom0 wishes service to be enabled or
  114. disabled.
  115. VM package can advertise what services are supported. For that, it needs to
  116. request ``supported-service.<service-name>`` feature with value ``1`` according
  117. to description above. The :py:class:`qubes.ext.services.ServicesExtension` will
  118. handle such request and set this feature on VM object. ``supported-service.``
  119. features that stop being advertised with ``qvm-features-request`` call are
  120. removed. This way, it's enough to remove the file from
  121. ``/etc/qubes/post-install.d`` (for example by uninstalling package providing
  122. the service) to tell dom0 the service is no longer supported. Services
  123. advertised by TemplateBasedVMs are currently ignored (related
  124. ``supported-service.`` features are not set), but retrieving them may be added
  125. in the future. Applications checking for specific service support should use
  126. ``vm.features.check_with_template('supported-service.<service-name>', False)``
  127. call on desired VM object. When enumerating all supported services, application
  128. should consider both the vm and its template (if any).
  129. Various tools will use this information to discover if given service is
  130. supported. The API does not enforce service being first advertised before being
  131. enabled (means: there can be service which is enabled, but without matching
  132. ``supported-service.`` feature). The list of well known services is in
  133. :program:`qvm-service` man page.
  134. Example ``/etc/qubes/post-install.d/20-my-service.sh``:
  135. .. code-block:: shell
  136. #!/bin/sh
  137. qvm-features-request supported-service.my-service=1
  138. Services and features can be then inspected from dom0 using
  139. :program:`qvm-features` tool, for example:
  140. .. code-block:: shell
  141. $ qvm-features my-qube
  142. supported-service.my-service 1
  143. Module contents
  144. ---------------
  145. .. autoclass:: qubes.vm.Features
  146. :members:
  147. :show-inheritance:
  148. .. vim: ts=3 sw=3 et