Skip to content

Commit

Permalink
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 Dec 12, 2023
1 parent e1367ff commit ae230b5
Showing 1 changed file with 0 additions and 78 deletions.
78 changes: 0 additions & 78 deletions virttest/utils_libvirt/libvirt_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,84 +49,6 @@ def get_disk_by_serial(session, serial):
raise exceptions.TestError("Couldn't find disk with serial in:\n%s" % o)


def get_non_root_disk_name(session):
"""
Returns the first disk name under /dev whose device doesn't have any
partitions or volumes or itself mounting "/".
Assuming that the system is installed on a single disk, this
can be used to identify an added disk.
It also includes read-only devices as such, e.g. sr0.
It iterates over output with the following structure in mind:
vda
├─vda1 /boot
└─vda2
├─rhel-root /
└─rhel-swap [SWAP]
But also considers corner cases where everything is for example on vda and
no swap.
:param session: If given the command will be executed in this VM or
remote session.
:return (name, mount_point): A tuple containing the device name and the mount
point. If the information doesn't exist it's returned as empty
string.
:raises TestError: If there's no additional disk or no mount on "/" or when
the disk information cannot be retrieved.
"""
cmd = "lsblk -n -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 list block devices: '%s'" % o)
LOG.debug("lsblk output:\n%s", o)
lines = o.split("\n").copy()
root_disk = None
root_mounted = False
disk_pattern = re.compile(r'^([a-z]+|sr\d)[\s\t$]')
entry_pattern = re.compile(r'(.*)[\s\t]+(.*)')
idx = 0
count = 0
while -1 < idx < len(lines) + 1:
line = lines[idx]
entry = entry_pattern.match(line)
if not entry:
idx = idx + 1
continue
name, mpoint = entry.groups()
is_disk = disk_pattern.match(name)
if root_mounted:
if is_disk:
root_disk = line
break
else:
idx = idx - 1
continue
if mpoint == "/":
root_mounted = True
if is_disk:
root_disk = line
break
else:
# we have found "/" but it's not a disk
# go back to find the disk
idx = idx - 1
continue
idx = idx + 1
if not root_disk:
raise exceptions.TestError("'/' not mounted in\n%s" % o)
non_root_disks = [x for x in lines
if x != root_disk and disk_pattern.match(x)]
if not non_root_disks:
raise exceptions.TestError("No non root disks found in\n%s" % o)
else:
LOG.debug("Identified non_root_disks %s in\n%s", non_root_disks, o)
return entry_pattern.match(non_root_disks[0]).groups()


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

0 comments on commit ae230b5

Please sign in to comment.