diff --git a/blivet/mounts.py b/blivet/mounts.py index 617ca21f0..af45c7e80 100644 --- a/blivet/mounts.py +++ b/blivet/mounts.py @@ -21,6 +21,7 @@ # from collections import defaultdict from . import util +from .devicelibs import btrfs import logging log = logging.getLogger("blivet") @@ -40,7 +41,7 @@ def getMountpoints(self, devspec, subvolspec=None): :param devscpec: device specification, eg. "/dev/vda1" :type devspec: str :param subvolspec: btrfs subvolume specification, eg. ID or name - :type subvolspec: str + :type subvolspec: object (may be NoneType) :returns: list of mountpoints (path) :rtype: list of str or empty list @@ -50,6 +51,9 @@ def getMountpoints(self, devspec, subvolspec=None): """ self._cacheCheck() + if subvolspec is not None: + subvolspec = str(subvolspec) + return self.mountpoints[(devspec, subvolspec)] def isMountpoint(self, path): @@ -61,6 +65,23 @@ def isMountpoint(self, path): return any(path in p for p in self.mountpoints.values()) + def _getSubvolSpec(self, devspec, mountpoint): + """ Get the subvolume specification for this btrfs volume. + + :param str devspec: the device specification + :param str mountpoint: the mountpoint + + :returns: the subvolume specification, 5 for a top-level volume + :rtype: str or NoneType + """ + for line in open("/proc/self/mountinfo").readlines(): + fields = line.split() + if fields[4] == mountpoint and fields[9] == devspec: + # empty _subvol[1:] means it is a top-level volume + subvolspec = fields[3] + return subvolspec[1:] or str(btrfs.MAIN_VOLUME_ID) + return None + def _getActiveMounts(self): """ Get information about mounted devices from /proc/mounts and /proc/self/mountinfo @@ -76,18 +97,11 @@ def _getActiveMounts(self): continue if fstype == "btrfs": - # get the subvol name from /proc/self/mountinfo - for line in open("/proc/self/mountinfo").readlines(): - fields = line.split() - _subvol = fields[3] - _mountpoint = fields[4] - _devspec = fields[9] - if _mountpoint == mountpoint and _devspec == devspec: - # empty _subvol[1:] means it is a top-level volume - subvolspec = _subvol[1:] or 5 - - self.mountpoints[(devspec, subvolspec)].append(mountpoint) - + subvolspec = self._getSubvolSpec(devspec, mountpoint) + if subvolspec is not None: + self.mountpoints[(devspec, subvolspec)].append(mountpoint) + else: + log.error("failed to obtain subvolspec for btrfs device %s", devspec) else: self.mountpoints[(devspec, None)].append(mountpoint)