Skip to content

Commit

Permalink
block.py: Module updating
Browse files Browse the repository at this point in the history
Refactor the utils_misc.py:
1. Split block module from utils_misc module.

Signed-off-by: Houqi (Nick) Zuo <[email protected]>
  • Loading branch information
nickzhq committed Apr 3, 2024
1 parent d7d98b1 commit 9c0fa8c
Showing 1 changed file with 76 additions and 15 deletions.
91 changes: 76 additions & 15 deletions virttest/vt_utils/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
PARTITION_TYPE_PRIMARY = "primary"


def get_disks(partition=False):
def get_disks_info(has_partition=False):
"""
List all disks or disks with no partition.
:param partition: if true, list all disks; otherwise,
list only disks with no partition
:type partition: boolean
:return: the disks info.( e.g. {kname: [kname, size, type, serial, wwn]} )
:param has_partition: If true, list disks info;
otherwise, list disks info which do NOT have partition.
:type has_partition: Boolean
:return: The disks info.( e.g. {kname: [kname, size, type, serial, wwn]} )
:rtype: dict
"""
disks_dict = {}
Expand All @@ -40,24 +40,30 @@ def get_disks(partition=False):
if platform.machine() == "s390x":
driver = "css0"
block_info = process.run(
'ls /sys/dev/block -l | grep "/%s"' % driver, verbose=False
'ls /sys/dev/block -l | grep "/%s"' % driver,
verbose=False,
shell=True,
).stdout_text
for matched in re.finditer(r"/block/(\S+)\s^", block_info, re.M):
knames = matched.group(1).split("/")
if len(knames) == 2:
parent_disks.add(knames[0])
if partition is False and knames[0] in parent_disks:
if has_partition is False and knames[0] in parent_disks:
if knames[0] in disks_dict:
del disks_dict[knames[0]]
continue

disks_dict[knames[-1]] = [knames[-1]]
o = process.run(
'lsblk -o KNAME,SIZE | grep "%s "' % knames[-1], verbose=False
'lsblk -o KNAME,SIZE | grep "%s "' % knames[-1],
verbose=False,
shell=True,
).stdout_text
disks_dict[knames[-1]].append(o.split()[-1])
o = process.run(
"udevadm info -q all -n %s" % knames[-1], verbose=False
"udevadm info -q all -n %s" % knames[-1],
verbose=False,
shell=True,
).stdout_text
for parttern in (
r"DEVTYPE=(\w+)\s^",
Expand All @@ -69,6 +75,27 @@ def get_disks(partition=False):
return disks_dict


def get_disks_path(partition_path=False):
"""
List disk path in Linux host.
:param partition_path: If true, list disk name including partition; otherwise,
list disk name excluding partition.
:type partition_path: Boolean
:return: The disks path in set(). ( e.g. {'/dev/sda2', '/dev/sda1', '/dev/sda'} or {'/dev/sda'} )
:rtype: Set
"""
cmd = "ls /dev/[vhs]d*"
if not partition_path:
cmd = "%s | grep -v [0-9]$" % cmd
cmd_obj = process.run(cmd, shell=True)
status, output = cmd_obj.exit_status, cmd_obj.stdout_text.strip()
if status != 0:
raise RuntimeError("Get disks failed with output %s" % output)
return set(output.split())


def create_partition(did, size, start, part_type=PARTITION_TYPE_PRIMARY, timeout=360):
"""
Create single partition on disk.
Expand Down Expand Up @@ -175,14 +202,18 @@ def get_disk_size(did):
:param did: disk kname. e.g. 'sdb', 'sdc'
:return: disk size.
"""
disks_info = get_disks(partition=True)
disks_info = get_disks_info(True)
disk_size = disks_info["%s" % did][1]
return int(utils_numeric.normalize_data_size(disk_size, "B").split(".")[0])


def get_partions_list():
def get_partitions_list():
"""
Get all partition lists.
Get all partition list.
:return: All partition list.
e.g. ['sda', 'sda1', 'sda2', 'dm-0', 'dm-1', 'dm-2']
:rtype: List
"""
parts_cmd = "cat /proc/partitions"
parts_out = process.run(parts_cmd, verbose=False).stdout_text
Expand All @@ -201,10 +232,12 @@ def get_disk_by_serial(serial_str):
"""
Get disk by serial in host.
:param serial_str: ID_SERIAL of disk, string value
:return: Disk name if find one with serial_str, else None
:param serial_str: ID_SERIAL of disk, string value.
:type serial_str: String
:return: Disk name if find one with serial_str, else None.
:rtype: String
"""
parts_list = get_partions_list()
parts_list = get_partitions_list()
for disk in parts_list:
cmd = "udevadm info --query=all --name=/dev/{} | grep ID_SERIAL={}".format(
disk, serial_str
Expand All @@ -215,3 +248,31 @@ def get_disk_by_serial(serial_str):
if not status:
return disk
return None


def get_drive_path(did, timeout=120):
"""
Get drive path( devname ) on host by drive serial or wwn
:param did: A drive serial( ID_SERIAL or ID_SERIAL_SHORT )
or a wwn( ID_WWN ).
:type did: String
:param timeout: Time out.
:type timeout: Integer
:return: A drive path( devname )
:rtype: String
:raises: An RuntimeError will be raised when cmd exit code is NOT 0.
"""
cmd = "for dev_path in `ls -d /sys/block/*`; do "
cmd += "echo `udevadm info -q property -p $dev_path`; done"
output_obj = process.run(cmd, timeout=timeout)
status, output = output_obj.exit_status, output_obj.stdout_text
if status != 0:
raise RuntimeError("Command running was failed. Output: %s" % output)
p = r"DEVNAME=([^\s]+)\s.*(?:ID_SERIAL|ID_SERIAL_SHORT|ID_WWN)=%s" % did
dev = re.search(p, output, re.M)
if dev:
return dev.groups()[0]
return ""

0 comments on commit 9c0fa8c

Please sign in to comment.