-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3860 from nickzhq/misc_refactor_I
utils_misc module refactor
- Loading branch information
Showing
6 changed files
with
496 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
# | ||
# Copyright: Red Hat (c) 2023 and Avocado contributors | ||
# Author: Houqi Zuo <[email protected]> | ||
import os | ||
import re | ||
|
||
from avocado.utils import process | ||
|
@@ -121,20 +122,20 @@ def umount(src, dst, fstype=None): | |
return True | ||
|
||
|
||
def create_filesyetem(partition_name, fstype, timeout=360): | ||
def create_filesyetem(partition, fstype, timeout=360): | ||
""" | ||
create file system. | ||
:param partition_name: partition name that to be formatted. e.g. sdb1 | ||
:param partition: partition name that to be formatted. e.g. /dev/sdb1 | ||
:param fstype: filesystem type for the disk. | ||
:param timeout: Timeout for cmd execution in seconds. | ||
""" | ||
if fstype == "xfs": | ||
mkfs_cmd = "mkfs.%s -f" % fstype | ||
else: | ||
mkfs_cmd = "mkfs.%s -F" % fstype | ||
format_cmd = "yes|%s '/dev/%s'" % (mkfs_cmd, partition_name) | ||
process.system(format_cmd, timeout=timeout, verbose=False) | ||
format_cmd = "yes|%s '%s'" % (mkfs_cmd, partition) | ||
process.run(format_cmd, timeout=timeout, verbose=False) | ||
|
||
|
||
def resize_filesystem(partition, size): | ||
|
@@ -147,7 +148,7 @@ def resize_filesystem(partition, size): | |
:param size: resize file system to size. | ||
size unit can be 'B', 'K', 'M', 'G'. | ||
support transfer size with SIZE_AVAILABLE, | ||
enlarge to maximun available size. | ||
enlarge to maximum available size. | ||
""" | ||
|
||
def get_start_size(): | ||
|
@@ -193,7 +194,7 @@ def resize_ext_fs(size): | |
) * bsize | ||
size = utils_numeric.normalize_data_size(str(size).split(".")[0], "K") | ||
resize_fs_cmd = "resize2fs %s %sK" % (partition, int(size.split(".")[0])) | ||
process.system(resize_fs_cmd, verbose=False) | ||
process.run(resize_fs_cmd, verbose=False) | ||
if flag: | ||
mount(partition, mountpoint, fstype=fstype) | ||
|
||
|
@@ -216,3 +217,65 @@ def get_mpoint_fstype(partition): | |
mount_list = process.run("cat /proc/mounts", verbose=False).stdout_text | ||
mount_info = re.search(r"%s\s(.+?)\s(.+?)\s" % partition, mount_list) | ||
return mount_info.groups() | ||
|
||
|
||
def format_disk( | ||
did, | ||
all_disks_did, | ||
partition=False, | ||
mountpoint=None, | ||
size=None, | ||
fstype="ext3", | ||
): | ||
""" | ||
Create a partition on disk in Linux host and format and mount it. | ||
:param did: Disk kname, serial or wwn. | ||
:type did: String | ||
:param all_disks_did: All disks did lists each include disk kname, | ||
serial and wwn. | ||
:type all_disks_did: List | ||
:param partition: If true, can format all disks; otherwise, | ||
only format the ones with no partition originally. | ||
:type partition: Boolean | ||
:param mountpoint: Mount point for the disk. | ||
:type mountpoint: String | ||
:param size: Partition size( such as 6G, 500M ). | ||
:type size: String | ||
:param fstype: Filesystem type for the disk. | ||
:type fstype: String | ||
:return: If disk is usable, return True. Otherwise, return False. | ||
:rtype: Boolean | ||
""" | ||
disks = block.get_disks_path(partition) | ||
for line in disks: | ||
kname = line.split("/")[-1] | ||
did_list = all_disks_did[kname] | ||
if did not in did_list: | ||
# Continue to search target disk | ||
continue | ||
if not size: | ||
size_output = process.run( | ||
"lsblk -o KNAME,SIZE|grep %s" % kname, | ||
verbose=False, | ||
shell=True, | ||
).stdout_text | ||
size = size_output.splitlines()[0].split()[1] | ||
all_disks_before = block.get_disks_path(True) | ||
devname = line | ||
block.create_partition( | ||
devname.split("/")[-1], | ||
size, | ||
"0M", | ||
) | ||
all_disks_after = block.get_disks_path(True) | ||
partname = (all_disks_after - all_disks_before).pop() | ||
create_filesyetem(partname, fstype) | ||
if not mountpoint: | ||
process.run("mkdir /mnt/%s" % kname) | ||
mountpoint = os.path.join("/mnt", kname) | ||
mount(src=partname, dst=mountpoint, fstype=fstype) | ||
if is_mounted(src=partname, dst=mountpoint, fstype=fstype): | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# | ||
# Library for interrupted thread related helper functions | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; specifically version 2 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
# | ||
# See LICENSE for more details. | ||
# | ||
# Copyright: Red Hat (c) 2024 and Avocado contributors | ||
# Author: Houqi Zuo <[email protected]> | ||
import sys | ||
import threading | ||
|
||
from virttest import error_context | ||
|
||
|
||
class InterruptedThread(threading.Thread): | ||
""" | ||
Run a function in a background thread. | ||
""" | ||
|
||
def __init__(self, target, args=(), kwargs={}): | ||
""" | ||
Initialize the instance. | ||
:param target: Function to run in the thread. | ||
:type target: Function object | ||
:param args: Arguments to pass to target. | ||
:type args: Tuple | ||
:param kwargs: Keyword arguments to pass to target. | ||
:type kwargs: Dictionary | ||
""" | ||
threading.Thread.__init__(self) | ||
self._target = target | ||
self._args = args | ||
self._kwargs = kwargs | ||
|
||
def run(self): | ||
""" | ||
Run target (passed to the constructor). No point in calling this | ||
function directly. Call start() to make this function run in a new | ||
thread. | ||
:raises: An Exception from run() of threading.Thread. | ||
""" | ||
self._e = None | ||
self._retval = None | ||
try: | ||
try: | ||
self._retval = self._target(*self._args, **self._kwargs) | ||
except Exception: | ||
self._e = sys.exc_info() | ||
raise | ||
finally: | ||
# Avoid circular references (start() may be called only once so | ||
# it's OK to delete these) | ||
del self._target, self._args, self._kwargs | ||
|
||
def join(self, timeout=None, suppress_exception=False): | ||
""" | ||
Join the thread. If target raised an exception, re-raise it. | ||
Otherwise, return the value returned by target. | ||
:param timeout: Timeout value to pass to threading.Thread.join(). | ||
:type timeout: Integer | ||
:param suppress_exception: If True, don't re-raise the exception. | ||
:type suppress_exception: Boolean | ||
""" | ||
threading.Thread.join(self, timeout) | ||
try: | ||
if self._e: | ||
if not suppress_exception: | ||
# Because the exception was raised in another thread, we | ||
# need to explicitly insert the current context into it | ||
s = error_context.exception_context(self._e[1]) | ||
s = error_context.join_contexts(error_context.get_context(), s) | ||
error_context.set_exception_context(self._e[1], s) | ||
raise self._e.with_traceback(*self._e) | ||
else: | ||
return self._retval | ||
finally: | ||
# Avoid circular references (join() may be called multiple times | ||
# so we can't delete these) | ||
self._e = None | ||
self._retval = None |
Oops, something went wrong.