Skip to content

Commit

Permalink
Merge pull request #1217 from vojtechtrefny/3.9-devel_btrfs-list-subv…
Browse files Browse the repository at this point in the history
…olumes-fix

Misc fixes related to btrfs subvolume listing
  • Loading branch information
vojtechtrefny authored Mar 28, 2024
2 parents 69faaf3 + edb2319 commit 3993194
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
21 changes: 15 additions & 6 deletions blivet/devices/btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from ..formats import get_format, DeviceFormat
from ..size import Size
from ..mounts import mounts_cache
from .. import missing_plugs

import logging
log = logging.getLogger("blivet")
Expand Down Expand Up @@ -374,12 +375,17 @@ def _list_subvolumes(self, mountpoint, snapshots_only=False):
except (blockdev.BtrfsError, blockdev.BlockDevNotImplementedError) as e:
log.debug("failed to list subvolumes: %s", e)
else:
self._get_default_subvolume_id()
self._get_default_subvolume_id(mountpoint)

return subvols

def list_subvolumes(self, snapshots_only=False):
subvols = []

if "btrfs" in missing_plugs:
log.debug("not listing btrfs subvolumes, libblockdev btrfs plugin is missing")
return subvols

if flags.auto_dev_updates:
self.setup(orig=True)

Expand All @@ -404,13 +410,16 @@ def list_subvolumes(self, snapshots_only=False):
def remove_subvolume(self, name):
raise NotImplementedError()

def _get_default_subvolume_id(self):
def _get_default_subvolume_id(self, mountpoint=None):
subvolid = None
with self._do_temp_mount() as mountpoint:
try:

try:
if mountpoint:
subvolid = blockdev.btrfs.get_default_subvolume_id(mountpoint)
except (blockdev.BtrfsError, blockdev.BlockDevNotImplementedError) as e:
log.debug("failed to get default subvolume id: %s", e)
elif flags.auto_dev_updates:
subvolid = blockdev.btrfs.get_default_subvolume_id(mountpoint)
except (blockdev.BtrfsError, blockdev.BlockDevNotImplementedError) as e:
log.debug("failed to get default subvolume id: %s", e)

self._default_subvolume_id = subvolid

Expand Down
30 changes: 28 additions & 2 deletions tests/unit_tests/devices_test/btrfs_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import unittest

try:
from unittest.mock import patch
from unittest.mock import patch, PropertyMock
except ImportError:
from mock import patch
from mock import patch, PropertyMock

import blivet

from blivet.devices import StorageDevice
from blivet.devices import BTRFSVolumeDevice
from blivet.devices import BTRFSSubVolumeDevice
from blivet.size import Size
from blivet.formats.fs import BTRFS


DEVICE_CLASSES = [
Expand Down Expand Up @@ -65,3 +66,28 @@ def test_device_id(self):

sub = BTRFSSubVolumeDevice("testsub", parents=[vol])
self.assertEqual(sub.device_id, "BTRFS-" + vol.uuid + "-testsub")

def test_btrfs_list_subvolumes(self):
bd = StorageDevice("bd1", fmt=blivet.formats.get_format("btrfs"),
size=Size("2 GiB"), exists=True)

vol = BTRFSVolumeDevice("testvolume", parents=[bd])

with patch("blivet.devices.btrfs.blockdev.btrfs") as blockdev:
# not mounted and flags.auto_dev_updates is not set
vol.list_subvolumes()
blockdev.list_subvolumes.assert_not_called()
blockdev.get_default_subvolume_id.assert_not_called()

# mounted
with patch.object(BTRFS, "system_mountpoint", new=PropertyMock(return_value='/fake/mountpoint')):
vol.list_subvolumes()
blockdev.list_subvolumes.assert_called_with("/fake/mountpoint", snapshots_only=False)
blockdev.get_default_subvolume_id.assert_called_with("/fake/mountpoint")

# mounted but libblockdev btrfs plugin not available
blockdev.reset_mock()
with patch("blivet.devices.btrfs.missing_plugs", new={"btrfs"}):
vol.list_subvolumes()
blockdev.list_subvolumes.assert_not_called()
blockdev.get_default_subvolume_id.assert_not_called()

0 comments on commit 3993194

Please sign in to comment.