29a940
From dae3375e720fe67870fe92e0aecd9638726c4d43 Mon Sep 17 00:00:00 2001
29a940
From: Vojtech Trefny <vtrefny@redhat.com>
29a940
Date: Wed, 9 Sep 2020 15:26:39 +0200
29a940
Subject: [PATCH 1/2] Try to not use udev.resolve_devspec when querying
29a940
 MountsCache
29a940
29a940
udev.resolve_devspec is slow and uses udev.settle, we should avoid
29a940
using it if possible when getting system mountpoints.
29a940
---
29a940
 blivet/mounts.py | 8 ++++++++
29a940
 1 file changed, 8 insertions(+)
29a940
29a940
diff --git a/blivet/mounts.py b/blivet/mounts.py
29a940
index 7ce41d77..ef2def89 100644
29a940
--- a/blivet/mounts.py
29a940
+++ b/blivet/mounts.py
29a940
@@ -27,6 +27,8 @@
29a940
 import logging
29a940
 log = logging.getLogger("blivet")
29a940
 
29a940
+import os
29a940
+
29a940
 
29a940
 class _MountinfoCache(object):
29a940
 
29a940
@@ -113,6 +115,12 @@ def get_mountpoints(self, devspec, subvolspec=None):
29a940
 
29a940
         # devspec == None means "get 'nodev' mount points"
29a940
         if devspec not in (None, "tmpfs"):
29a940
+            if devspec.startswith("/dev"):
29a940
+                # try to avoid using resolve_devspec if possible
29a940
+                name = os.path.realpath(devspec).split("/")[-1]
29a940
+                if (name, subvolspec) in self.mountpoints.keys():
29a940
+                    return self.mountpoints[(name, subvolspec)]
29a940
+
29a940
             # use the canonical device path (if available)
29a940
             canon_devspec = resolve_devspec(devspec, sysname=True)
29a940
             if canon_devspec is not None:
29a940
29a940
From ae32d008e7425610d437c72bb284664ace7ce5b7 Mon Sep 17 00:00:00 2001
29a940
From: Vojtech Trefny <vtrefny@redhat.com>
29a940
Date: Wed, 9 Sep 2020 15:27:57 +0200
29a940
Subject: [PATCH 2/2] Do not run udev.settle in StorageDevice._pre_teardown
29a940
29a940
We currently run udev.settle for every _pre_teardown call even if
29a940
there is no change or format teardown. This commit moves the
29a940
udev.settle call to format classes so it is called only when
29a940
format.teardown calls in _pre_teardown change the format.
29a940
---
29a940
 blivet/devices/storage.py                 | 1 -
29a940
 blivet/formats/fs.py                      | 2 ++
29a940
 blivet/formats/luks.py                    | 5 +++++
29a940
 blivet/formats/swap.py                    | 3 +++
29a940
 tests/devices_test/device_methods_test.py | 2 --
29a940
 5 files changed, 10 insertions(+), 3 deletions(-)
29a940
29a940
diff --git a/blivet/devices/storage.py b/blivet/devices/storage.py
29a940
index d47affca..bde0b7d6 100644
29a940
--- a/blivet/devices/storage.py
29a940
+++ b/blivet/devices/storage.py
29a940
@@ -425,7 +425,6 @@ def _pre_teardown(self, recursive=None):
29a940
             self.original_format.teardown()
29a940
         if self.format.exists:
29a940
             self.format.teardown()
29a940
-        udev.settle()
29a940
         return True
29a940
 
29a940
     def _teardown(self, recursive=None):
29a940
diff --git a/blivet/formats/fs.py b/blivet/formats/fs.py
29a940
index 9c14649e..d351dee1 100644
29a940
--- a/blivet/formats/fs.py
29a940
+++ b/blivet/formats/fs.py
29a940
@@ -614,6 +614,8 @@ def _teardown(self, **kwargs):
29a940
         if mountpoint == self._chrooted_mountpoint:
29a940
             self._chrooted_mountpoint = None
29a940
 
29a940
+        udev.settle()
29a940
+
29a940
     def read_label(self):
29a940
         """Read this filesystem's label.
29a940
 
29a940
diff --git a/blivet/formats/luks.py b/blivet/formats/luks.py
29a940
index de9f1d32..0d036588 100644
29a940
--- a/blivet/formats/luks.py
29a940
+++ b/blivet/formats/luks.py
29a940
@@ -36,6 +36,7 @@
29a940
 from ..tasks import availability, lukstasks
29a940
 from ..size import Size, KiB
29a940
 from ..static_data import luks_data
29a940
+from .. import udev
29a940
 
29a940
 import logging
29a940
 log = logging.getLogger("blivet")
29a940
@@ -275,6 +276,8 @@ def _teardown(self, **kwargs):
29a940
         log.debug("unmapping %s", self.map_name)
29a940
         blockdev.crypto.luks_close(self.map_name)
29a940
 
29a940
+        udev.settle()
29a940
+
29a940
     def _pre_resize(self):
29a940
         if self.luks_version == "luks2" and not self.has_key:
29a940
             raise LUKSError("Passphrase or key needs to be set before resizing LUKS2 format.")
29a940
@@ -442,5 +445,7 @@ def _teardown(self, **kwargs):
29a940
         # for all devices supported by cryptsetup
29a940
         blockdev.crypto.luks_close(self.map_name)
29a940
 
29a940
+        udev.settle()
29a940
+
29a940
 
29a940
 register_device_format(Integrity)
29a940
diff --git a/blivet/formats/swap.py b/blivet/formats/swap.py
29a940
index 3cc59138..2e4b07df 100644
29a940
--- a/blivet/formats/swap.py
29a940
+++ b/blivet/formats/swap.py
29a940
@@ -29,6 +29,7 @@
29a940
 from ..tasks import fsuuid
29a940
 from . import DeviceFormat, register_device_format
29a940
 from ..size import Size
29a940
+from .. import udev
29a940
 
29a940
 import gi
29a940
 gi.require_version("BlockDev", "2.0")
29a940
@@ -206,6 +207,8 @@ def _teardown(self, **kwargs):
29a940
                         type=self.type, status=self.status)
29a940
         blockdev.swap.swapoff(self.device)
29a940
 
29a940
+        udev.settle()
29a940
+
29a940
     def _create(self, **kwargs):
29a940
         log_method_call(self, device=self.device,
29a940
                         type=self.type, status=self.status)
29a940
diff --git a/tests/devices_test/device_methods_test.py b/tests/devices_test/device_methods_test.py
29a940
index e6718121..f00509be 100644
29a940
--- a/tests/devices_test/device_methods_test.py
29a940
+++ b/tests/devices_test/device_methods_test.py
29a940
@@ -161,7 +161,6 @@ def _destroy():
29a940
 
29a940
         self.assertFalse(self.device.exists)
29a940
         self.assertEqual(self.device.update_sysfs_path.called, self.destroy_updates_sysfs_path)
29a940
-        self.assertEqual(self.patches["udev"].settle.called, self.destroy_calls_udev_settle)
29a940
         self.patches["udev"].reset_mock()
29a940
         self.device.update_sysfs_path.reset_mock()
29a940
 
29a940
@@ -228,7 +227,6 @@ def test_teardown(self):
29a940
             self.device.teardown()
29a940
             self.assertTrue(self.teardown_method_mock.called)
29a940
 
29a940
-        self.assertEqual(self.patches["udev"].settle.called, self.teardown_calls_udev_settle)
29a940
         self.assertEqual(self.device.update_sysfs_path.called, self.teardown_updates_sysfs_path)
29a940
         self.patches["udev"].reset_mock()
29a940
         self.device.update_sysfs_path.reset_mock()