16
16
def __init__(self, interval=300, monitor_interval=60 * 60,
17
17
mounts_file="/proc/mounts", create_time=time.time,
18
statvfs=None, mtab_file="/etc/mtab"):
18
statvfs=None, hal_manager=None, mtab_file="/etc/mtab"):
19
19
self.run_interval = interval
20
20
self._monitor_interval = monitor_interval
21
21
self._create_time = create_time
29
29
self._mount_info = []
30
30
self._mount_info_to_persist = None
32
from landscape.hal import HALManager
34
self._hal_manager = hal_manager
36
self._hal_manager = hal_manager or HALManager()
32
38
from gi.repository import GUdev
33
39
except ImportError:
34
40
self._gudev_client = None
116
122
current_mount_points.add(mount_point)
118
124
def _get_removable_devices(self):
119
if self._gudev_client is not None:
125
if self._hal_manager is not None:
126
return self._get_hal_removable_devices()
127
elif self._gudev_client is not None:
120
128
return self._get_udev_removable_devices()
131
139
return is_removable()
141
def _get_hal_removable_devices(self):
142
block_devices = {} # {udi: [device, ...]}
143
children = {} # {parent_udi: [child_udi, ...]}
146
# We walk the list of devices building up a dictionary of all removable
147
# devices, and a mapping of {UDI => [block devices]}
148
# We differentiate between devices that we definitely know are
149
# removable and devices that _may_ be removable, depending on their
150
# parent device, e.g. /dev/sdb1 isn't flagged as removable, but
151
# /dev/sdb may well be removable.
153
# Unfortunately, HAL doesn't guarantee the order of the devices
154
# returned from get_devices(), so we may not know that a parent device
155
# is removable when we find it's first child.
156
devices = self._hal_manager.get_devices()
157
for device in devices:
158
block_device = device.properties.get("block.device")
160
if device.properties.get("storage.removable"):
161
removable.add(device.udi)
164
block_devices[device.udi].append(block_device)
166
block_devices[device.udi] = [block_device]
168
parent_udi = device.properties.get("info.parent")
169
if parent_udi is not None:
171
children[parent_udi].append(device.udi)
173
children[parent_udi] = [device.udi]
175
# Propagate the removable flag from each node all the way to
180
for parent_udi in children:
181
if parent_udi in removable:
182
for child_udi in children[parent_udi]:
183
if child_udi not in removable:
184
removable.add(child_udi)
187
# We've now seen _all_ devices, and have the definitive list of
188
# removable UDIs, so we can now find all the removable devices in the
190
removable_devices = set()
191
for udi in removable:
192
removable_devices.update(block_devices[udi])
194
return removable_devices
133
196
def _get_mount_info(self):
134
197
"""Generator yields local mount points worth recording data for."""
135
198
removable_devices = self._get_removable_devices()