@@ -15,25 +15,32 @@ class UDisks2Device:
1515 def __init__ (self , devpath ):
1616 self ._logger = logging .getLogger ("Device: " )
1717 self .devpath = devpath
18- client = UDisks .Client .new_sync (None )
18+ self .fs = None
19+
20+ def _setup (self ):
21+ """Try to find the devpath
1922
23+ Raises:
24+ ValueError: no udisks2 device or no filesystem found on devpath
25+ """
26+ client = UDisks .Client .new_sync (None )
2027 manager = client .get_object_manager ()
2128 for obj in manager .get_objects ():
2229 block = obj .get_block ()
2330 if not block :
2431 continue
2532
2633 device_path = block .get_cached_property ("Device" ).get_bytestring ().decode ('utf-8' )
27- if device_path == devpath :
34+ if device_path == self . devpath :
2835 self .fs = obj .get_filesystem ()
2936 if self .fs is None :
30- raise ValueError (f"no filesystem found on { devpath } " )
37+ raise ValueError (f"no filesystem found on { self . devpath } " )
3138
3239 return
3340
34- raise ValueError (f"No udisks2 device found for { devpath } " )
41+ raise ValueError (f"No udisks2 device found for { self . devpath } " )
3542
36- def mount (self , readonly = False ):
43+ def mount (self , readonly = False , retries = 0 ):
3744 opts = GLib .Variant ('a{sv}' , {'options' : GLib .Variant ('s' , 'ro' if readonly else 'rw' )})
3845
3946 try :
@@ -83,17 +90,39 @@ def unmount(self, lazy=False):
8390
8491_devs = {}
8592
86- def _get_udisks2_dev (devpath ):
93+ def _get_udisks2_dev (devpath , retries ):
94+ """Try to get the udisks2 device
95+
96+ Args:
97+ devpath (str): Device name
98+ retries (int): Number of retries to allow
99+
100+ Raises:
101+ ValueError: Failed to obtain the device (e.g. does not exist)
102+ """
87103 if devpath not in _devs :
88- _devs [devpath ] = UDisks2Device (devpath = devpath )
104+ dev = UDisks2Device (devpath = devpath )
105+ while True :
106+ try :
107+ dev ._setup ()
108+ break
109+ except ValueError as exc :
110+ if 'No udisks2 device' not in str (exc ) or not retries :
111+ raise
112+ retries -= 1
113+ dev ._logger .warning ('udisks2: Retrying %s...' , devpath )
114+ time .sleep (1 )
115+
116+ # Success, so record the new device
117+ _devs [devpath ] = dev
89118 return _devs [devpath ]
90119
91- def handle_mount (devpath ):
92- dev = _get_udisks2_dev (devpath )
120+ def handle_mount (devpath , retries = 0 ):
121+ dev = _get_udisks2_dev (devpath , retries )
93122 return dev .mount ()
94123
95124def handle_unmount (devpath , lazy = False ):
96- dev = _get_udisks2_dev (devpath )
125+ dev = _get_udisks2_dev (devpath , 0 )
97126 return dev .unmount (lazy = lazy )
98127
99128methods = {
0 commit comments