]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume: Rename env var; add warning
authorZack Cerza <zack@redhat.com>
Tue, 21 Jun 2022 17:28:30 +0000 (11:28 -0600)
committerGuillaume Abrioux <gabrioux@redhat.com>
Fri, 5 Aug 2022 06:58:17 +0000 (08:58 +0200)
So that we can have a nice big warning that fires once per invocation, I
think using a callable class with a class attribute seems like a decent
approach. A closure could work too.

Signed-off-by: Zack Cerza <zack@redhat.com>
(cherry picked from commit 69f58f51a2d7967d597a8d61c0ab20b52e8dc374)

src/ceph-volume/ceph_volume/tests/util/test_device.py
src/ceph-volume/ceph_volume/tests/util/test_disk.py
src/ceph-volume/ceph_volume/util/device.py
src/ceph-volume/ceph_volume/util/disk.py

index ab7b8c54e56768cfbad5da11703dd2220501f015..5c87294ddf516874a2cd64af84a93c0cde98b875 100644 (file)
@@ -91,11 +91,11 @@ class TestDevice(object):
     def test_loop_device_is_device(self, fake_call, device_info):
         data = {"/dev/loop0": {"foo": "bar"}}
         lsblk = {"TYPE": "loop"}
-        os.environ["CEPH_VOLUME_USE_LOOP_DEVICES"] = "1"
+        os.environ["CEPH_VOLUME_ALLOW_LOOP_DEVICES"] = "1"
         device_info(devices=data, lsblk=lsblk)
         disk = device.Device("/dev/loop0")
         assert disk.is_device is True
-        del os.environ["CEPH_VOLUME_USE_LOOP_DEVICES"]
+        del os.environ["CEPH_VOLUME_ALLOW_LOOP_DEVICES"]
 
     def test_device_is_rotational(self, fake_call, device_info):
         data = {"/dev/sda": {"rotational": "1"}}
index 6b178eabdbb7392e682652ecd2dabac4915dcf8c..217bb81fad06df97843e1a890917dcd3eb62003e 100644 (file)
@@ -558,3 +558,14 @@ class TestSizeSpecificFormatting(object):
         result = "%s" % size.tb
         assert "%s" % size.tb == "%s" % size.terabytes
         assert result == "1027.00 TB"
+
+
+class TestAllowLoopDevsWarning(object):
+    def test_loop_dev_warning(self, fake_call, caplog):
+        assert disk.allow_loop_devices() is False
+        assert not caplog.records
+        os.environ['CEPH_VOLUME_ALLOW_LOOP_DEVICES'] = "y"
+        assert disk.allow_loop_devices() is True
+        log = caplog.records[0]
+        assert log.levelname == "WARNING"
+        assert "will never be supported in production" in log.message
index 866e5dc2741a480ff090510dfe189492f781743b..02e549c55b706b35b1de41051552b966c4607699 100644 (file)
@@ -8,6 +8,7 @@ from ceph_volume.api import lvm
 from ceph_volume.util import disk, system
 from ceph_volume.util.lsmdisk import LSMDisk
 from ceph_volume.util.constants import ceph_disk_guids
+from ceph_volume.util.disk import allow_loop_devices
 
 
 logger = logging.getLogger(__name__)
@@ -212,7 +213,7 @@ class Device(object):
             device_type = dev.get('TYPE', '')
             # always check is this is an lvm member
             valid_types = ['part', 'disk']
-            if os.environ.get("CEPH_VOLUME_USE_LOOP_DEVICES", False):
+            if allow_loop_devices():
                 valid_types.append('loop')
             if device_type in valid_types:
                 self._set_lvm_membership()
@@ -461,7 +462,7 @@ class Device(object):
             api = self.blkid_api
         if api:
             valid_types = ['disk', 'device', 'mpath']
-            if os.environ.get("CEPH_VOLUME_USE_LOOP_DEVICES", False):
+            if allow_loop_devices():
                 valid_types.append('loop')
             return self.device_type in valid_types
         return False
index 645104010c560cde487bad8a80eac0b2b98538d7..b22ff76bccb73232c75f3947acdfce1edc9e07c2 100644 (file)
@@ -343,12 +343,8 @@ def is_device(dev):
     if not dev.startswith('/dev/'):
         return False
     if dev[len('/dev/'):].startswith('loop'):
-        if os.environ.get("CEPH_VOLUME_USE_LOOP_DEVICES", False) is False:
+        if not allow_loop_devices():
             return False
-        logger.info(
-            "Allowing the use of loop devices since "
-            "CEPH_VOLUME_USE_LOOP_DEVICES is set."
-        )
 
     # fallback to stat
     return _stat_is_device(os.lstat(dev).st_mode)
@@ -766,6 +762,31 @@ def get_block_devs_lsblk(device=''):
     return [re.split(r'\s+', line) for line in stdout]
 
 
+class AllowLoopDevices(object):
+    allow = False
+    warned = False
+
+    @classmethod
+    def __call__(cls):
+        val = os.environ.get("CEPH_VOLUME_ALLOW_LOOP_DEVICES", "false").lower()
+        if val not in ("false", 'no', '0'):
+            cls.allow = True
+            if not cls.warned:
+                logger.warning(
+                    "CEPH_VOLUME_ALLOW_LOOP_DEVICES is set in your "
+                    "environment, so we will allow the use of unattached loop"
+                    " devices as disks. This feature is intended for "
+                    "development purposes only and will never be supported in"
+                    " production. Issues filed based on this behavior will "
+                    "likely be ignored."
+                )
+                cls.warned = True
+        return cls.allow
+
+
+allow_loop_devices = AllowLoopDevices()
+
+
 def get_block_devs_sysfs(_sys_block_path='/sys/block', _sys_dev_block_path='/sys/dev/block'):
     # First, get devices that are _not_ partitions
     result = list()
@@ -781,7 +802,7 @@ def get_block_devs_sysfs(_sys_block_path='/sys/block', _sys_dev_block_path='/sys
             basename = get_file_contents(os.path.join(dm_dir_path, 'name'))
             name = os.path.join("/dev/mapper", basename)
         if dev.startswith('loop'):
-            if os.environ.get("CEPH_VOLUME_USE_LOOP_DEVICES", False) is False:
+            if not allow_loop_devices():
                 continue
             # Skip loop devices that are not attached
             if not os.path.exists(os.path.join(_sys_block_path, dev, 'loop')):
@@ -815,7 +836,7 @@ def get_devices(_sys_block_path='/sys/block', device=''):
     block_devs = get_block_devs_sysfs(_sys_block_path)
 
     block_types = ['disk', 'mpath']
-    if os.environ.get("CEPH_VOLUME_USE_LOOP_DEVICES", False) is not False:
+    if allow_loop_devices():
         block_types.append('loop')
 
     for block in block_devs: