Ver código fonte

ext/block: make use of QubesDB watch

Actually use just introduced API.

Also document new `device-list-change:class` event.

QubesOS/qubes-issues#2940
Marek Marczykowski-Górecki 7 anos atrás
pai
commit
67c382c8b0
2 arquivos alterados com 18 adições e 0 exclusões
  1. 6 0
      qubes/devices.py
  2. 12 0
      qubes/ext/block.py

+ 6 - 0
qubes/devices.py

@@ -44,11 +44,17 @@ Such extension should provide:
  domain of given identifier
  - handle `device-list-attached:class` event - list currently attached
  devices to this domain
+ - fire `device-list-change:class` event when device list change is detected
+ (new/removed device)
 
 Note that device-listing event handlers can not be asynchronous. This for
 example means you can not call qrexec service there. This is intentional to
 keep device listing operation cheap. You need to design the extension to take
 this into account (for example by using QubesDB).
+
+Extension may use QubesDB watch API (QubesVM.watch_qdb_path(path), then handle
+`domain-qdb-change:path`) to detect changes and fire
+`device-list-change:class` event.
 '''
 import asyncio
 

+ 12 - 0
qubes/ext/block.py

@@ -106,6 +106,18 @@ class BlockDevice(qubes.devices.DeviceInfo):
 
 
 class BlockDeviceExtension(qubes.ext.Extension):
+    @qubes.ext.handler('domain-init', 'domain-load')
+    def on_domain_init_load(self, vm, event):
+        '''Initialize watching for changes'''
+        # pylint: disable=unused-argument,no-self-use
+        vm.watch_qdb_path('/qubes-block-devices')
+
+    @qubes.ext.handler('domain-qdb-change:/qubes-block-devices')
+    def on_qdb_change(self, vm, event, path):
+        '''A change in QubesDB means a change in device list'''
+        # pylint: disable=unused-argument,no-self-use
+        vm.fire_event('device-list-change:block')
+
     def device_get(self, vm, ident):
         # pylint: disable=no-self-use
         '''Read information about device from QubesDB