Skip to content

Commit

Permalink
Merge pull request #3729 from smitterl/get_parts_list_by_path
Browse files Browse the repository at this point in the history
utils_disk/libvirt_disk: add way to handle disk by path
  • Loading branch information
chunfuwen committed Sep 5, 2023
2 parents 9ab0c67 + d303afb commit 7c8692a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
31 changes: 31 additions & 0 deletions virttest/utils_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions virttest/utils_libvirt/libvirt_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7c8692a

Please sign in to comment.