From 4d8c348983818f97c6206535af1c9f1f1ea0faa1 Mon Sep 17 00:00:00 2001 From: Sebastian Mitterle Date: Thu, 7 Dec 2023 05:56:24 -0500 Subject: [PATCH] utils_libvirt/libvirt_disk: add function to discover new disk As the disk names like /dev/sda /dev/vda are unstable between boots, Detect new disks inside a VM (e.g. after a coldplug) as the ones that are not used. Signed-off-by: Sebastian Mitterle --- virttest/utils_libvirt/libvirt_disk.py | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/virttest/utils_libvirt/libvirt_disk.py b/virttest/utils_libvirt/libvirt_disk.py index 403bd79c4a2..011c486b418 100644 --- a/virttest/utils_libvirt/libvirt_disk.py +++ b/virttest/utils_libvirt/libvirt_disk.py @@ -24,6 +24,37 @@ LOG = logging.getLogger('avocado.' + __name__) +def get_new_disk(session): + """ + Identifies the single disk below /dev/ + that's not mounted currently. + + :param session: VM session + :param test: Test instance + :return path: absolute path of disk + e.g. /dev/sda + """ + cmd = "lsblk -n -p -l -o NAME,MOUNTPOINT" + s, o = utils_misc.cmd_status_output(cmd, + ignore_status=False, + shell=True, + verbose=True, + session=session) + if s: + raise exceptions.TestError("Couldn't determine the new disk: '%s'" % o) + current = None + lines = o.split("\n") + for line in lines: + if re.search(r'^/dev/[a-z]+\s+$', line): + current = line + if lines.index(line) == len(lines) - 1: + break + if current and current in line: + continue + logging.debug("Identified '%s' in '%s'", current.strip(), o) + return current.strip() + + def create_disk(disk_type, path=None, size="500M", disk_format="raw", extra='', session=None): """