From 704038297fd0a5b441754531e21a315b108bcbc8 Mon Sep 17 00:00:00 2001 From: Katarzyna Treder Date: Wed, 6 Nov 2024 14:54:59 +0100 Subject: [PATCH 1/2] Fix for getting disk serial number Signed-off-by: Katarzyna Treder --- test_utils/disk_finder.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_utils/disk_finder.py b/test_utils/disk_finder.py index 90ad46d..d738fa1 100644 --- a/test_utils/disk_finder.py +++ b/test_utils/disk_finder.py @@ -107,11 +107,11 @@ def get_disk_serial_number(dev_path): commands = [ f"(udevadm info --query=all --name={dev_path} | grep 'SCSI.*_SERIAL' || " f"udevadm info --query=all --name={dev_path} | grep 'ID_SERIAL_SHORT') | " - "awk --field-separator '=' '{print $NF}'", + "awk -F '=' '{print $NF}'", f"sg_inq {dev_path} 2> /dev/null | grep '[Ss]erial number:' | " "awk '{print $NF}'", f"udevadm info --query=all --name={dev_path} | grep 'ID_SERIAL' | " - "awk --field-separator '=' '{print $NF}'" + "awk -F '=' '{print $NF}'" ] for command in commands: serial = TestRun.executor.run(command).stdout From 4e0dad274f0c1d85cd2575d8986b06e779d0daf8 Mon Sep 17 00:00:00 2001 From: Katarzyna Treder Date: Wed, 6 Nov 2024 14:57:26 +0100 Subject: [PATCH 2/2] Prevent removing OS partition logical volume Signed-off-by: Katarzyna Treder --- storage_devices/lvm.py | 48 +++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/storage_devices/lvm.py b/storage_devices/lvm.py index 2b44700..3e30528 100644 --- a/storage_devices/lvm.py +++ b/storage_devices/lvm.py @@ -165,7 +165,7 @@ class LvmConfiguration: TestRun.executor.run(cmd) @staticmethod - def set_use_devices_file(use_devices_file=False): + def set_use_devices_file(use_devices_file=True): cmd = (fr"sed -i 's/^\s*#*\s*\(use_devicesfile\).*/\t\1 = " fr"{1 if use_devices_file else 0}/' {lvm_config_path}") TestRun.executor.run(cmd) @@ -460,13 +460,8 @@ class Lvm(Disk): return cls.discover_logical_volumes() @staticmethod - def remove(lv_name: str, vg_name: str): - if not lv_name: - raise ValueError("LV name needed for LV remove operation.") - if not vg_name: - raise ValueError("VG name needed for LV remove operation.") - - cmd = f"lvremove -f {vg_name}/{lv_name}" + def remove(lv_path: str): + cmd = f"lvremove -f {lv_path}" return TestRun.executor.run(cmd) @staticmethod @@ -477,22 +472,41 @@ class Lvm(Disk): cmd = f"pvremove {pv_name}" return TestRun.executor.run(cmd) + @staticmethod + def get_os_vg(): + disks = get_system_disks() + cmd = (f"pvdisplay -c | grep -e /dev/{' -e /dev/'.join(disks)} | " + "awk -F':' '$2 != \"\" {print $2}'") # display non-empty groups + os_vg_names = TestRun.executor.run(cmd).stdout + if os_vg_names: + return set(os_vg_names.split("\n")) # remove duplicates + return [] + + @staticmethod + def get_non_os_vg(): + disks = get_system_disks() + cmd = (f"pvdisplay -c | grep -Ev \'/dev/{'|/dev'.join(disks)}' | " + "awk -F':' '$2 != \"\" {print $2}\'") # display non-empty groups + non_os_vg_names = TestRun.executor.run(cmd).stdout + if non_os_vg_names: + return set(non_os_vg_names.split("\n")) # remove duplicates + return [] + @classmethod def remove_all(cls): - cmd = f"lvdisplay | grep 'LV Path' | awk '{{print $3}}'" - lvm_paths = TestRun.executor.run(cmd).stdout.splitlines() - for lvm_path in lvm_paths: - lv_name = lvm_path.split('/')[-1] - vg_name = lvm_path.split('/')[-2] - cls.remove(lv_name, vg_name) + non_os_vg_names = Lvm.get_non_os_vg() + cmd = "lvdisplay -c | awk -F':' '{{print $1,$2}}'" # prints lv_path vg_name + lvs = [tuple(lv.strip().split(' ')) for lv in TestRun.executor.run(cmd).stdout.splitlines()] + [cls.remove(lv[0]) for lv in lvs if lv[1] in non_os_vg_names] - cmd = f"vgdisplay | grep 'VG Name' | awk '{{print $3}}'" - vg_names = TestRun.executor.run(cmd).stdout.splitlines() - for vg_name in vg_names: + for vg_name in non_os_vg_names: TestRun.executor.run(f"vgchange -an {vg_name}") VolumeGroup.remove(vg_name) cmd = f"pvdisplay | grep 'PV Name' | awk '{{print $3}}'" + os_disks = get_system_disks() + # invert grep to make sure os_disks won`t be wiped during lvms cleanup + cmd += "".join([f" | grep -v {os_disk}" for os_disk in os_disks]) pv_names = TestRun.executor.run(cmd).stdout.splitlines() for pv_name in pv_names: cls.remove_pv(pv_name)