|
| 1 | +from utility.utility import logging |
| 2 | +from node_exec import NodeExec |
| 3 | + |
| 4 | + |
| 5 | +class io_keywords: |
| 6 | + |
| 7 | + def __init__(self): |
| 8 | + pass |
| 9 | + |
| 10 | + def setup_dm_linear_device(self, volume_name, dm_device_name, node_name): |
| 11 | + """ |
| 12 | + Setup a device mapper linear device from a Longhorn volume device. |
| 13 | + Steps: |
| 14 | + 1. Get device path /dev/longhorn/{volume_name} |
| 15 | + 2. Get device size in sectors |
| 16 | + 3. Create dm-linear mapping |
| 17 | + 4. Verify device creation |
| 18 | + """ |
| 19 | + logging(f"Setting up dm-linear device {dm_device_name} from volume {volume_name} on node {node_name}") |
| 20 | + |
| 21 | + # Get device path |
| 22 | + device_path = f"/dev/longhorn/{volume_name}" |
| 23 | + cmd = f"test -e {device_path}" |
| 24 | + result = NodeExec(node_name).issue_cmd(cmd) |
| 25 | + if result and "No such file" in result: |
| 26 | + raise Exception(f"Device {device_path} does not exist") |
| 27 | + |
| 28 | + # Get device size in sectors |
| 29 | + cmd = f"blockdev --getsz {device_path}" |
| 30 | + sectors = NodeExec(node_name).issue_cmd(cmd).strip() |
| 31 | + if not sectors or not sectors.isdigit(): |
| 32 | + raise Exception(f"Failed to get device size in sectors. Result: {sectors}") |
| 33 | + |
| 34 | + # Create dm-linear device |
| 35 | + cmd = f'echo "0 {sectors} linear {device_path} 0" | dmsetup create --noudevsync {dm_device_name}' |
| 36 | + create_result = NodeExec(node_name).issue_cmd(cmd) |
| 37 | + if create_result and ("failed" in create_result.lower() or "error" in create_result.lower()): |
| 38 | + raise Exception(f"Failed to create dm device. Result: {create_result}") |
| 39 | + |
| 40 | + # Verify device creation |
| 41 | + cmd = f"dmsetup table --noudevsync {dm_device_name}" |
| 42 | + table = NodeExec(node_name).issue_cmd(cmd) |
| 43 | + if "failed" in table.lower() or "No such device" in table: |
| 44 | + raise Exception(f"Device verification failed. Table result: {table}") |
| 45 | + |
| 46 | + result = { |
| 47 | + "real_dev": device_path, |
| 48 | + "sectors": sectors, |
| 49 | + "dm_device": f"/dev/mapper/{dm_device_name}" |
| 50 | + } |
| 51 | + logging(f"Successfully created dm device {dm_device_name}: {result}") |
| 52 | + return result |
| 53 | + |
| 54 | + def format_and_mount_dm_device(self, dm_device_name, mount_point, node_name): |
| 55 | + """Format dm device as ext4 and mount it.""" |
| 56 | + logging(f"Formatting and mounting dm device {dm_device_name} to {mount_point} on node {node_name}") |
| 57 | + |
| 58 | + dm_path = f"/dev/mapper/{dm_device_name}" |
| 59 | + |
| 60 | + # Format device as ext4 |
| 61 | + cmd = f"mkfs.ext4 -F {dm_path}" |
| 62 | + NodeExec(node_name).issue_cmd(cmd) |
| 63 | + |
| 64 | + # Create mount point |
| 65 | + cmd = f"mkdir -p {mount_point}" |
| 66 | + NodeExec(node_name).issue_cmd(cmd) |
| 67 | + |
| 68 | + # Mount device with errors=continue option |
| 69 | + cmd = f"mount -t ext4 -o errors=continue {dm_path} {mount_point}" |
| 70 | + NodeExec(node_name).issue_cmd(cmd) |
| 71 | + |
| 72 | + logging(f"Successfully formatted and mounted {dm_device_name} to {mount_point}") |
| 73 | + |
| 74 | + def force_unmount_dm_device(self, mount_point, node_name): |
| 75 | + """Force unmount a dm device mount point by killing processes and using lazy unmount.""" |
| 76 | + import time |
| 77 | + logging(f"Force unmounting {mount_point} on node {node_name}") |
| 78 | + |
| 79 | + # Kill all processes using the mount point |
| 80 | + cmd = f"fuser -km {mount_point} 2>/dev/null || true" |
| 81 | + NodeExec(node_name).issue_cmd(cmd) |
| 82 | + |
| 83 | + # Wait for processes to terminate |
| 84 | + timeout = 10 |
| 85 | + start_time = time.time() |
| 86 | + while time.time() - start_time < timeout: |
| 87 | + cmd = f"fuser {mount_point} 2>/dev/null" |
| 88 | + result = NodeExec(node_name).issue_cmd(cmd) |
| 89 | + if not result or not result.strip(): |
| 90 | + logging(f"All processes using {mount_point} have terminated") |
| 91 | + break |
| 92 | + time.sleep(0.5) |
| 93 | + else: |
| 94 | + logging(f"Warning: Timeout waiting for processes to terminate on {mount_point}") |
| 95 | + |
| 96 | + # Try normal unmount first, fallback to lazy unmount |
| 97 | + cmd = f"umount {mount_point} 2>/dev/null || umount -l {mount_point} 2>/dev/null || true" |
| 98 | + NodeExec(node_name).issue_cmd(cmd) |
| 99 | + |
| 100 | + logging(f"Successfully force unmounted {mount_point}") |
| 101 | + |
| 102 | + def switch_dm_device_to_error(self, dm_device_name, sectors, node_name): |
| 103 | + """Switch device mapper device from linear to error target. This will make all I/O operations fail.""" |
| 104 | + logging(f"Switching dm device {dm_device_name} to error mode on node {node_name}") |
| 105 | + |
| 106 | + # Load the error table (inactive) |
| 107 | + cmd = f'echo "0 {sectors} error" | dmsetup load --noudevsync {dm_device_name}' |
| 108 | + NodeExec(node_name).issue_cmd(cmd) |
| 109 | + |
| 110 | + # Suspend the device without locking filesystem |
| 111 | + cmd = f"dmsetup suspend --noudevsync --nolockfs {dm_device_name}" |
| 112 | + NodeExec(node_name).issue_cmd(cmd) |
| 113 | + |
| 114 | + # Resume device to activate the loaded table |
| 115 | + cmd = f"dmsetup resume --noudevsync {dm_device_name}" |
| 116 | + NodeExec(node_name).issue_cmd(cmd) |
| 117 | + |
| 118 | + logging(f"Successfully switched {dm_device_name} to error mode") |
| 119 | + |
| 120 | + def cleanup_dm_device(self, dm_device_name, mount_point, node_name): |
| 121 | + """Cleanup device mapper device and unmount if mount_point is provided.""" |
| 122 | + logging(f"Cleaning up dm device {dm_device_name} on node {node_name}") |
| 123 | + |
| 124 | + # Lazy unmount if mount point provided |
| 125 | + if mount_point and mount_point.strip(): |
| 126 | + logging(f"Unmounting {mount_point}") |
| 127 | + cmd = f"umount -l {mount_point} 2>/dev/null || true" |
| 128 | + NodeExec(node_name).issue_cmd(cmd) |
| 129 | + |
| 130 | + # Wipe device mapper table |
| 131 | + cmd = f"dmsetup wipe_table --noudevsync {dm_device_name} --nolockfs 2>/dev/null || true" |
| 132 | + NodeExec(node_name).issue_cmd(cmd) |
| 133 | + |
| 134 | + # Remove device mapper device (deferred) |
| 135 | + cmd = f"dmsetup remove --noudevsync --deferred {dm_device_name} 2>/dev/null || true" |
| 136 | + NodeExec(node_name).issue_cmd(cmd) |
| 137 | + |
| 138 | + logging(f"Successfully cleaned up dm device {dm_device_name}") |
0 commit comments