From d303afb0de792cc14dba02eb5eb85f0cbdc86c31 Mon Sep 17 00:00:00 2001 From: Sebastian Mitterle Date: Thu, 27 Jul 2023 08:44:16 -0400 Subject: [PATCH] utils_disk/libvirt_disk: add way to handle disk by path Partitions in /proc/partitions are not persistent which can lead to problems when a test checks if a disk was cold plugged. Use persistent naming from /dev/disk/by-path which allows us to easily identify those disks and partitions with the attached disk by their libvirt
. Add and update other functions to be compatible with this way of dealing with partitions. Signed-off-by: Sebastian Mitterle --- virttest/utils_disk.py | 31 ++++++++++++++++++++++++++ virttest/utils_libvirt/libvirt_disk.py | 9 ++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/virttest/utils_disk.py b/virttest/utils_disk.py index c90b66e9d7..09326b81fb 100644 --- a/virttest/utils_disk.py +++ b/virttest/utils_disk.py @@ -26,6 +26,7 @@ from virttest import error_context from virttest import utils_numeric +from virttest import utils_misc from virttest import remote PARTITION_TABLE_TYPE_MBR = "msdos" @@ -1083,6 +1084,19 @@ def linux_disk_check(session, did): clean_partition_linux(session, did) +def get_parts_list_by_path(session=None): + """ + Get all partitions as listed on /dev/disk/by-path + """ + err, out = utils_misc.cmd_status_output("ls /dev/disk/by-path", + shell=True, session=session) + if err: + raise exceptions.TestError("Failed to list partitions in /dev/disk/by-path") + r = out.split() + LOG.debug("Partitions by path: %s", r) + return r + + def get_parts_list(session=None): """ Get all partition lists. @@ -1118,6 +1132,23 @@ def get_added_parts(session, old_parts): return added_parts +def get_added_parts_by_path(session, old_parts): + """ + Get newly added partition list comparing to old parts + Uses the information below /dev/disk/by-path instead + of /proc/partitions + + :param session: the vm session + :param old_parts: list, the old partition list as listed + below /dev/disk/by-path + :return: list, the newly added partition list + """ + new_parts = get_parts_list_by_path(session) + added_parts = list(set(new_parts).difference(set(old_parts))) + LOG.info("Added parts:%s", added_parts) + return added_parts + + def get_first_disk(session=None): """ Get the first disk device on host or guest diff --git a/virttest/utils_libvirt/libvirt_disk.py b/virttest/utils_libvirt/libvirt_disk.py index e4c86e3a5c..403bd79c4a 100644 --- a/virttest/utils_libvirt/libvirt_disk.py +++ b/virttest/utils_libvirt/libvirt_disk.py @@ -588,21 +588,22 @@ def fill_null_in_vm(vm, target, size_value=500): raise exceptions.TestError(str(e)) -def check_virtual_disk_io(vm, partition): +def check_virtual_disk_io(vm, partition, path="/dev/%s"): """ Check if the disk partition in vm can be normally used. :param vm: Vm instance :param partition: the disk partition in vm to be checked. + :param path: the folder path for the partition :return: if the disk can be used, return True """ session = None try: session = vm.wait_for_login() - cmd = ("fdisk -l /dev/{0} && mkfs.ext4 -F /dev/{0} && " - "mkdir -p test && mount /dev/{0} test && echo" + cmd = ("fdisk -l && mkfs.ext4 -F {0} && " + "mkdir -p test && mount {0} test && echo" " teststring > test/testfile && umount test" - .format(partition)) + .format(path % partition)) status, output = session.cmd_status_output(cmd) LOG.debug("Disk operation in VM:\nexit code:\n%s\noutput:\n%s", status, output)