| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2025, Klention Mali <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: lvm_pv_move_data |
| 10 |
short_description: Move data between LVM Physical Volumes (PVs) |
| 11 |
version_added: "11.2.0" |
| 12 |
description: |
| 13 |
- Moves data from one LVM Physical Volume (PV) to another. |
| 14 |
author: |
| 15 |
- Klention Mali (@klention) |
| 16 |
options: |
| 17 |
source: |
| 18 |
description: |
| 19 |
- Path to the source block device to move data from. |
| 20 |
- Must be an existing PV. |
| 21 |
type: path |
| 22 |
required: true |
| 23 |
destination: |
| 24 |
description: |
| 25 |
- Path to the destination block device to move data to. |
| 26 |
- Must be an existing PV with enough free space. |
| 27 |
type: path |
| 28 |
required: true |
| 29 |
auto_answer: |
| 30 |
description: |
| 31 |
- Answer yes to all prompts automatically. |
| 32 |
type: bool |
| 33 |
default: false |
| 34 |
atomic: |
| 35 |
description: |
| 36 |
- Makes the C(pvmove) operation atomic, ensuring that all affected LVs are moved to the destination PV, |
| 37 |
or none are if the operation is aborted. |
| 38 |
type: bool |
| 39 |
default: true |
| 40 |
autobackup: |
| 41 |
description: |
| 42 |
- Automatically backup metadata before changes (strongly advised!). |
| 43 |
type: bool |
| 44 |
default: true |
| 45 |
requirements: |
| 46 |
- LVM2 utilities |
| 47 |
- Both O(source) and O(destination) devices must exist, and the PVs must be in the same volume group. |
| 48 |
- The O(destination) PV must have enough free space to accommodate the O(source) PV's allocated extents. |
| 49 |
- Verbosity is automatically controlled by Ansible's verbosity level (using multiple C(-v) flags). |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
- name: Moving data from /dev/sdb to /dev/sdc |
| 54 |
community.general.lvm_pv_move_data: |
| 55 |
source: /dev/sdb |
| 56 |
destination: /dev/sdc |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = r""" |
| 60 |
actions: |
| 61 |
description: List of actions performed during module execution. |
| 62 |
returned: success |
| 63 |
type: list |
| 64 |
elements: str |
| 65 |
sample: [ |
| 66 |
"moved data from /dev/sdb to /dev/sdc", |
| 67 |
"no allocated extents to move", |
| 68 |
"would move data from /dev/sdb to /dev/sdc" |
| 69 |
] |
| 70 |
""" |
| 71 |
|
| 72 |
|
| 73 |
import os |
| 74 |
|
| 75 |
from ansible.module_utils.basic import AnsibleModule |
| 76 |
|
| 77 |
from ansible_collections.community.general.plugins.module_utils._lvm import pvmove_runner, pvs_runner |
| 78 |
|
| 79 |
|
| 80 |
def main(): |
| 81 |
module = AnsibleModule( |
| 82 |
argument_spec=dict( |
| 83 |
source=dict(type="path", required=True), |
| 84 |
destination=dict(type="path", required=True), |
| 85 |
auto_answer=dict(type="bool", default=False), |
| 86 |
atomic=dict(type="bool", default=True), |
| 87 |
autobackup=dict(type="bool", default=True), |
| 88 |
), |
| 89 |
supports_check_mode=True, |
| 90 |
) |
| 91 |
|
| 92 |
pvs = pvs_runner(module) |
| 93 |
pvmove = pvmove_runner(module) |
| 94 |
|
| 95 |
source = module.params["source"] |
| 96 |
destination = module.params["destination"] |
| 97 |
changed = False |
| 98 |
actions = [] |
| 99 |
result = {"changed": False} |
| 100 |
|
| 101 |
# Validate device existence |
| 102 |
if not os.path.exists(source): |
| 103 |
module.fail_json(msg=f"Source device {source} not found") |
| 104 |
if not os.path.exists(destination): |
| 105 |
module.fail_json(msg=f"Destination device {destination} not found") |
| 106 |
if source == destination: |
| 107 |
module.fail_json(msg="Source and destination devices must be different") |
| 108 |
|
| 109 |
def run_pvs_field(field, device): |
| 110 |
with pvs("noheadings fields devices") as ctx: |
| 111 |
rc, out, err = ctx.run(fields=field, devices=[device]) |
| 112 |
if rc != 0: |
| 113 |
module.fail_json(msg=f"Command failed: {err}", stdout=out, stderr=err, rc=rc, cmd=ctx.cmd) |
| 114 |
return out.strip() |
| 115 |
|
| 116 |
def is_pv(device): |
| 117 |
with pvs("noheadings readonly devices", check_rc=False) as ctx: |
| 118 |
rc, dummy, dummy = ctx.run(devices=[device]) |
| 119 |
return rc == 0 |
| 120 |
|
| 121 |
if not is_pv(source): |
| 122 |
module.fail_json(msg=f"Source device {source} is not a PV") |
| 123 |
if not is_pv(destination): |
| 124 |
module.fail_json(msg=f"Destination device {destination} is not a PV") |
| 125 |
|
| 126 |
vg_src = run_pvs_field("vg_name", source) |
| 127 |
vg_dest = run_pvs_field("vg_name", destination) |
| 128 |
if vg_src != vg_dest: |
| 129 |
module.fail_json( |
| 130 |
msg=f"Source and destination must be in the same VG. Source VG: '{vg_src}', Destination VG: '{vg_dest}'." |
| 131 |
) |
| 132 |
|
| 133 |
def get_allocated_pe(device): |
| 134 |
try: |
| 135 |
return int(run_pvs_field("pv_pe_alloc_count", device)) |
| 136 |
except ValueError: |
| 137 |
module.fail_json(msg=f"Invalid allocated PE count for device {device}") |
| 138 |
|
| 139 |
allocated = get_allocated_pe(source) |
| 140 |
if allocated == 0: |
| 141 |
actions.append("no allocated extents to move") |
| 142 |
else: |
| 143 |
# Check destination has enough free space |
| 144 |
def get_total_pe(device): |
| 145 |
try: |
| 146 |
return int(run_pvs_field("pv_pe_count", device)) |
| 147 |
except ValueError: |
| 148 |
module.fail_json(msg=f"Invalid total PE count for device {device}") |
| 149 |
|
| 150 |
def get_free_pe(device): |
| 151 |
return get_total_pe(device) - get_allocated_pe(device) |
| 152 |
|
| 153 |
free_pe_dest = get_free_pe(destination) |
| 154 |
if free_pe_dest < allocated: |
| 155 |
module.fail_json( |
| 156 |
msg=( |
| 157 |
f"Destination device {destination} has only {int(free_pe_dest)} free physical extents, but " |
| 158 |
f"source device {source} has {int(allocated)} allocated extents. Not enough space." |
| 159 |
) |
| 160 |
) |
| 161 |
|
| 162 |
if module.check_mode: |
| 163 |
changed = True |
| 164 |
actions.append(f"would move data from {source} to {destination}") |
| 165 |
else: |
| 166 |
with pvmove("auto_answer atomic autobackup verbosity source destination") as ctx: |
| 167 |
dummy, out, err = ctx.run(verbosity=module._verbosity) |
| 168 |
result["stdout"] = out |
| 169 |
result["stderr"] = err |
| 170 |
|
| 171 |
changed = True |
| 172 |
actions.append(f"moved data from {source} to {destination}") |
| 173 |
|
| 174 |
result["changed"] = changed |
| 175 |
result["actions"] = actions |
| 176 |
if actions: |
| 177 |
result["msg"] = f"PV data move: {', '.join(actions)}" |
| 178 |
else: |
| 179 |
result["msg"] = f"No data to move from {source}" |
| 180 |
|
| 181 |
module.exit_json(**result) |
| 182 |
|
| 183 |
|
| 184 |
if __name__ == "__main__": |
| 185 |
main() |