Skip to content

Commit

Permalink
disk_utilities: new functions provider
Browse files Browse the repository at this point in the history
Creates a new file containing a set of functions
useful for managing disks and filesystems in guest
environments

Signed-off-by: mcasquer <[email protected]>
  • Loading branch information
mcasquer committed Nov 27, 2024
1 parent cfeec15 commit ee6ef9a
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions provider/disk_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Disk utilities functions for managing disks and filesystems in guest environments.
This module contains functions related to disk detection, filesystem creation,
and command execution for various OS types.
"""

from virttest import error_context, utils_disk, utils_misc
from virttest.utils_misc import get_linux_drive_path
from virttest.utils_windows.drive import get_disk_props_by_serial_number


def get_window_disk_index_by_serial(serial, session):
idx_info = get_disk_props_by_serial_number(session, serial, ["Index"])
if idx_info:
return idx_info["Index"]


def get_disk_and_prepare_cmd(guest_cmd, img, params, session, test):
os_type = params["os_type"]
error_context.context(f"Check disk {img} in guest", test.log.debug)

img_size = params.get(f"image_size_{img}")

if os_type == "windows":
disk = get_window_disk_index_by_serial(img, session, test)
if disk:
utils_disk.update_windows_disk_attributes(session, disk)
error_context.context(f"Cleaning disk: {disk}", test.log.info)
utils_disk.clean_partition_windows(session, disk)
error_context.context(f"Formatting disk: {disk}", test.log.info)
driver = utils_disk.configure_empty_disk(session, disk, img_size, os_type)[
0
]
output_path = f"{driver}:\\test.dat"
guest_cmd = utils_misc.set_winutils_letter(session, guest_cmd).format(
output_path
)
else:
output_path = get_linux_drive_path(session, img)
guest_cmd = guest_cmd.format(output_path)

return guest_cmd


def guest_fs_mount_and_dd_cmd(os_type, tmp_dir):
if os_type != "windows":
cmd = (
f"mkdir -p {tmp_dir} && mkfs.xfs -f {{0}} && "
f"mount -t xfs {{0}} {tmp_dir} && "
f"dd if=/dev/zero of={tmp_dir}/test.img bs=1M count=100 oflag=direct && "
f"umount {tmp_dir}"
)
else:
cmd = "dd if=/dev/urandom of={} bs=1M count=10"

return cmd

0 comments on commit ee6ef9a

Please sign in to comment.