From ee6ef9a3c0e565ea3550174da617dd7874031e50 Mon Sep 17 00:00:00 2001 From: mcasquer Date: Tue, 26 Nov 2024 14:53:17 +0100 Subject: [PATCH] disk_utilities: new functions provider Creates a new file containing a set of functions useful for managing disks and filesystems in guest environments Signed-off-by: mcasquer --- provider/disk_utilities.py | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 provider/disk_utilities.py diff --git a/provider/disk_utilities.py b/provider/disk_utilities.py new file mode 100644 index 0000000000..7811250f3b --- /dev/null +++ b/provider/disk_utilities.py @@ -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