Skip to content

Commit

Permalink
utils_libvirt/libvirt_disk: add function to get disk by serial
Browse files Browse the repository at this point in the history
Use the SERIAL that can be set via libvirt, to select
its disk path under /dev/.

Suggested-by: Chunfu Wen <[email protected]>
Signed-off-by: Sebastian Mitterle <[email protected]>
  • Loading branch information
smitterl committed Jan 2, 2024
1 parent ebf8a71 commit 51863db
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions virttest/utils_libvirt/libvirt_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ def get_non_root_disk_name(session):
return name.strip(), mpoint.strip()


def get_disk_by_serial(session, serial):
"""
Gets the disk by its serial value
:param session: VM session
:param serial: The expected serial value
:return path: absolute path of disk
e.g. /dev/sda
:raises TestError: If the disk cannot be found.
"""
cmd = "lsblk -n -p -l -o NAME,SERIAL"
s, o = utils_misc.cmd_status_output(cmd,
ignore_status=False,
shell=True,
verbose=True,
session=session)
if s:
raise exceptions.TestError("Couldn't list block devices: '%s'" % o)
for line in o.split("\n"):
match = re.match(r'(/dev/[a-z]+)[\s\t]+%s' % serial, line)
if match:
return match.groups()[0]
raise exceptions.TestError("Couldn't find disk with serial in:\n%s" % o)


def create_disk(disk_type, path=None, size="500M", disk_format="raw", extra='',
session=None):
"""
Expand Down

0 comments on commit 51863db

Please sign in to comment.