From d5de9583ee6c4d9ccaab2b1146606a0ee316cc27 Mon Sep 17 00:00:00 2001 From: Erwan Velu Date: Tue, 9 Oct 2018 22:51:39 +0200 Subject: [PATCH] ceph_volume: Rejecting locked devices If we cannot open a block device in O_RDWR in exclusive mode, it means someone is actually using it like a raw database or similar. In that case, the device should be considered as unusable as OSDs will not be in a position to use it. Signed-off-by: Erwan Velu --- src/ceph-volume/ceph_volume/util/device.py | 1 + src/ceph-volume/ceph_volume/util/disk.py | 23 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/ceph-volume/ceph_volume/util/device.py b/src/ceph-volume/ceph_volume/util/device.py index 7831c14ca81..a8b236fd1ee 100644 --- a/src/ceph-volume/ceph_volume/util/device.py +++ b/src/ceph-volume/ceph_volume/util/device.py @@ -142,6 +142,7 @@ class Device(object): pass reject_device('removable', 1, 'removable') reject_device('ro', 1, 'read-only') + reject_device('locked', 1, 'locked') self._valid = len(self._rejected_reasons) == 0 return self._valid diff --git a/src/ceph-volume/ceph_volume/util/disk.py b/src/ceph-volume/ceph_volume/util/disk.py index a4ddb58af07..589cb887749 100644 --- a/src/ceph-volume/ceph_volume/util/disk.py +++ b/src/ceph-volume/ceph_volume/util/disk.py @@ -653,6 +653,28 @@ def is_mapper_device(device_name): return device_name.startswith(('/dev/mapper', '/dev/dm-')) +def is_locked_raw_device(disk_path): + """ + A device can be locked by a third party software like a database. + To detect that case, the device is opened in Read/Write and exclusive mode + """ + open_flags = (os.O_RDWR | os.O_EXCL) + open_mode = 0 + fd = None + + try: + fd = os.open(disk_path, open_flags, open_mode) + except OSError: + return 1 + + try: + os.close(fd) + except OSError: + return 1 + + return 0 + + def get_devices(_sys_block_path='/sys/block', _dev_path='/dev', _mapper_path='/dev/mapper'): """ Captures all available devices from /sys/block/, including its partitions, @@ -729,6 +751,7 @@ def get_devices(_sys_block_path='/sys/block', _dev_path='/dev', _mapper_path='/d metadata['human_readable_size'] = human_readable_size(float(size) * 512) metadata['size'] = float(size) * 512 metadata['path'] = diskname + metadata['locked'] = is_locked_raw_device(metadata['path']) device_facts[diskname] = metadata return device_facts -- 2.39.5