Skip to content

Commit

Permalink
utils_libvirt/libvirt_disk: add function to discover new disk
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
smitterl committed Dec 7, 2023
1 parent be4af65 commit 4d8c348
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions virttest/utils_libvirt/libvirt_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 4d8c348

Please sign in to comment.