Skip to content

Commit

Permalink
utils_disk/libvirt_disk: add way to handle disk by path
Browse files Browse the repository at this point in the history
Partitions in /proc/partitions are not persistent which
can lead to problems when a test checks if a disk was cold
plugged.

Use persistent naming from /dev/disk/by-path which allows
us to easily identify those disks and partitions with
the attached disk by their libvirt <address/>.

Add and update other functions to be compatible with this way
of dealing with partitions.

Signed-off-by: Sebastian Mitterle <[email protected]>
  • Loading branch information
smitterl committed Jul 28, 2023
1 parent 9742b58 commit 4345a92
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()
logging.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 4345a92

Please sign in to comment.