| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2018, Red Hat, Inc. |
| 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 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
author: |
| 11 |
- Bryan Gurney (@bgurney-rh) |
| 12 |
|
| 13 |
module: vdo |
| 14 |
|
| 15 |
short_description: Module to control VDO |
| 16 |
|
| 17 |
|
| 18 |
description: |
| 19 |
- This module controls the VDO dedupe and compression device. |
| 20 |
- VDO, or Virtual Data Optimizer, is a device-mapper target that provides inline block-level deduplication, compression, |
| 21 |
and thin provisioning capabilities to primary storage. |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._attributes |
| 24 |
|
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: none |
| 28 |
diff_mode: |
| 29 |
support: none |
| 30 |
|
| 31 |
options: |
| 32 |
name: |
| 33 |
description: |
| 34 |
- The name of the VDO volume. |
| 35 |
type: str |
| 36 |
required: true |
| 37 |
state: |
| 38 |
description: |
| 39 |
- Whether this VDO volume should be V(present) or V(absent). If a V(present) VDO volume does not exist, it is created. |
| 40 |
If a V(present) VDO volume already exists, it is modified by updating the configuration, which takes effect when the |
| 41 |
VDO volume is restarted. Not all parameters of an existing VDO volume can be modified; the C(statusparamkeys) list |
| 42 |
in the code contains the parameters that can be modified after creation. If an V(absent) VDO volume does not exist, |
| 43 |
it is not removed. |
| 44 |
type: str |
| 45 |
choices: [absent, present] |
| 46 |
default: present |
| 47 |
activated: |
| 48 |
description: |
| 49 |
- The C(activate) status for a VDO volume. If this is set to V(false), the VDO volume cannot be started, and it does |
| 50 |
not start on system startup. However, on initial creation, a VDO volume with O(activated=false) is set to be running |
| 51 |
until stopped. This is the default behavior of the C(vdo create) command; it provides the user an opportunity to write |
| 52 |
a base amount of metadata (filesystem, LVM headers, and so on) to the VDO volume prior to stopping the volume, and |
| 53 |
leaving it deactivated until ready to use. |
| 54 |
type: bool |
| 55 |
running: |
| 56 |
description: |
| 57 |
- Whether this VDO volume is running. |
| 58 |
- A VDO volume must be activated in order to be started. |
| 59 |
type: bool |
| 60 |
device: |
| 61 |
description: |
| 62 |
- The full path of the device to use for VDO storage. |
| 63 |
- This is required if O(state=present). |
| 64 |
type: str |
| 65 |
logicalsize: |
| 66 |
description: |
| 67 |
- The logical size of the VDO volume (in megabytes, or LVM suffix format). If not specified for a new volume, this defaults |
| 68 |
to the same size as the underlying storage device, which is specified in the O(device) parameter. Existing volumes |
| 69 |
maintain their size if the O(logicalsize) parameter is not specified, or is smaller than or identical to the current |
| 70 |
size. If the specified size is larger than the current size, a C(growlogical) operation is performed. |
| 71 |
type: str |
| 72 |
deduplication: |
| 73 |
description: |
| 74 |
- Configures whether deduplication is enabled. The default for a created volume is V(enabled). Existing volumes maintain |
| 75 |
their previously configured setting unless a different value is specified in the playbook. |
| 76 |
type: str |
| 77 |
choices: [disabled, enabled] |
| 78 |
compression: |
| 79 |
description: |
| 80 |
- Configures whether compression is enabled. The default for a created volume is V(enabled). Existing volumes maintain |
| 81 |
their previously configured setting unless a different value is specified in the playbook. |
| 82 |
type: str |
| 83 |
choices: [disabled, enabled] |
| 84 |
blockmapcachesize: |
| 85 |
description: |
| 86 |
- The amount of memory allocated for caching block map pages, in megabytes (or may be issued with an LVM-style suffix |
| 87 |
of K, M, G, or T). The default (and minimum) value is V(128M). The value specifies the size of the cache; there is |
| 88 |
a 15% memory usage overhead. Each 1.25G of block map covers 1T of logical blocks, therefore a small amount of block |
| 89 |
map cache memory can cache a significantly large amount of block map data. |
| 90 |
- Existing volumes maintain their previously configured setting unless a different value is specified in the playbook. |
| 91 |
type: str |
| 92 |
readcache: |
| 93 |
description: |
| 94 |
- Enables or disables the read cache. The default is V(disabled). Choosing V(enabled) enables a read cache which may |
| 95 |
improve performance for workloads of high deduplication, read workloads with a high level of compression, or on hard |
| 96 |
disk storage. Existing volumes maintain their previously configured setting unless a different value is specified |
| 97 |
in the playbook. |
| 98 |
- The read cache feature is available in VDO 6.1 and older. |
| 99 |
type: str |
| 100 |
choices: [disabled, enabled] |
| 101 |
readcachesize: |
| 102 |
description: |
| 103 |
- Specifies the extra VDO device read cache size in megabytes. This is in addition to a system-defined minimum. Using |
| 104 |
a value with a suffix of K, M, G, or T is optional. The default value is V(0). 1.125 MB of memory per bio thread is |
| 105 |
used per 1 MB of read cache specified (for example, a VDO volume configured with 4 bio threads has a read cache memory |
| 106 |
usage overhead of 4.5 MB per 1 MB of read cache specified). Existing volumes maintain their previously configured |
| 107 |
setting unless a different value is specified in the playbook. |
| 108 |
- The read cache feature is available in VDO 6.1 and older. |
| 109 |
type: str |
| 110 |
emulate512: |
| 111 |
description: |
| 112 |
- Enables 512-byte emulation mode, allowing drivers or filesystems to access the VDO volume at 512-byte granularity, |
| 113 |
instead of the default 4096-byte granularity. |
| 114 |
- Only recommended when a driver or filesystem requires 512-byte sector level access to a device. |
| 115 |
- This option is only available when creating a new volume, and cannot be changed for an existing volume. |
| 116 |
type: bool |
| 117 |
default: false |
| 118 |
growphysical: |
| 119 |
description: |
| 120 |
- Specifies whether to attempt to execute a C(growphysical) operation, if there is enough unused space on the device. |
| 121 |
A C(growphysical) operation is executed if there is at least 64 GB of free space, relative to the previous physical |
| 122 |
size of the affected VDO volume. |
| 123 |
type: bool |
| 124 |
default: false |
| 125 |
slabsize: |
| 126 |
description: |
| 127 |
- The size of the increment by which the physical size of a VDO volume is grown, in megabytes (or may be issued with |
| 128 |
an LVM-style suffix of K, M, G, or T). Must be a power of two between 128M and 32G. The default is V(2G), which supports |
| 129 |
volumes having a physical size up to 16T. The maximum, V(32G), supports a physical size of up to 256T. This option |
| 130 |
is only available when creating a new volume, and cannot be changed for an existing volume. |
| 131 |
type: str |
| 132 |
writepolicy: |
| 133 |
description: |
| 134 |
- Specifies the write policy of the VDO volume. |
| 135 |
- The V(sync) mode acknowledges writes only after data is on stable storage. |
| 136 |
- The V(async) mode acknowledges writes when data has been cached for writing to stable storage. |
| 137 |
- The default (and highly recommended) V(auto) mode checks the storage device to determine whether it supports flushes. |
| 138 |
Devices that support flushes result in a VDO volume in V(async) mode, while devices that do not support flushes run |
| 139 |
in V(sync) mode. |
| 140 |
- Existing volumes maintain their previously configured setting unless a different value is specified in the playbook. |
| 141 |
type: str |
| 142 |
choices: [async, auto, sync] |
| 143 |
indexmem: |
| 144 |
description: |
| 145 |
- Specifies the amount of index memory in gigabytes. The default is V(0.25). The special decimal values V(0.25), V(0.5), |
| 146 |
and V(0.75) can be used, as can any positive integer. This option is only available when creating a new volume, and |
| 147 |
cannot be changed for an existing volume. |
| 148 |
type: str |
| 149 |
indexmode: |
| 150 |
description: |
| 151 |
- Specifies the index mode of the Albireo index. |
| 152 |
- The default is V(dense), which has a deduplication window of 1 GB of index memory per 1 TB of incoming data, requiring |
| 153 |
10 GB of index data on persistent storage. |
| 154 |
- The V(sparse) mode has a deduplication window of 1 GB of index memory per 10 TB of incoming data, but requires 100 |
| 155 |
GB of index data on persistent storage. |
| 156 |
- This option is only available when creating a new volume, and cannot be changed for an existing volume. |
| 157 |
type: str |
| 158 |
choices: [dense, sparse] |
| 159 |
ackthreads: |
| 160 |
description: |
| 161 |
- Specifies the number of threads to use for acknowledging completion of requested VDO I/O operations. Valid values |
| 162 |
are integer values from V(1) to V(100) (lower numbers are preferable due to overhead). The default is V(1). Existing |
| 163 |
volumes maintain their previously configured setting unless a different value is specified in the playbook. |
| 164 |
type: str |
| 165 |
biothreads: |
| 166 |
description: |
| 167 |
- Specifies the number of threads to use for submitting I/O operations to the storage device. Valid values are integer |
| 168 |
values from V(1) to V(100) (lower numbers are preferable due to overhead). The default is V(4). Existing volumes maintain |
| 169 |
their previously configured setting unless a different value is specified in the playbook. |
| 170 |
type: str |
| 171 |
cputhreads: |
| 172 |
description: |
| 173 |
- Specifies the number of threads to use for CPU-intensive work such as hashing or compression. Valid values are integer |
| 174 |
values from V(1) to V(100) (lower numbers are preferable due to overhead). The default is V(2). Existing volumes maintain |
| 175 |
their previously configured setting unless a different value is specified in the playbook. |
| 176 |
type: str |
| 177 |
logicalthreads: |
| 178 |
description: |
| 179 |
- Specifies the number of threads across which to subdivide parts of the VDO processing based on logical block addresses. |
| 180 |
Valid values are integer values from V(1) to V(100) (lower numbers are preferable due to overhead). The default is |
| 181 |
V(1). Existing volumes maintain their previously configured setting unless a different value is specified in the playbook. |
| 182 |
type: str |
| 183 |
physicalthreads: |
| 184 |
description: |
| 185 |
- Specifies the number of threads across which to subdivide parts of the VDO processing based on physical block addresses. |
| 186 |
Valid values are integer values from V(1) to V(16) (lower numbers are preferable due to overhead). The physical space |
| 187 |
used by the VDO volume must be larger than (O(slabsize) * O(physicalthreads)). The default is V(1). Existing volumes |
| 188 |
maintain their previously configured setting unless a different value is specified in the playbook. |
| 189 |
type: str |
| 190 |
force: |
| 191 |
description: |
| 192 |
- When creating a volume, ignores any existing file system or VDO signature already present in the storage device. When |
| 193 |
stopping or removing a VDO volume, first unmounts the file system stored on the device if mounted. |
| 194 |
- B(Warning:) Since this parameter removes all safety checks it is important to make sure that all parameters provided |
| 195 |
are accurate and intentional. |
| 196 |
type: bool |
| 197 |
default: false |
| 198 |
version_added: 2.4.0 |
| 199 |
notes: |
| 200 |
- In general, the default thread configuration should be used. |
| 201 |
requirements: |
| 202 |
- PyYAML |
| 203 |
- kmod-kvdo |
| 204 |
- vdo |
| 205 |
""" |
| 206 |
|
| 207 |
EXAMPLES = r""" |
| 208 |
- name: Create 2 TB VDO volume vdo1 on device /dev/md0 |
| 209 |
community.general.vdo: |
| 210 |
name: vdo1 |
| 211 |
state: present |
| 212 |
device: /dev/md0 |
| 213 |
logicalsize: 2T |
| 214 |
|
| 215 |
- name: Remove VDO volume vdo1 |
| 216 |
community.general.vdo: |
| 217 |
name: vdo1 |
| 218 |
state: absent |
| 219 |
""" |
| 220 |
|
| 221 |
RETURN = r"""#""" |
| 222 |
|
| 223 |
import re |
| 224 |
import traceback |
| 225 |
|
| 226 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 227 |
|
| 228 |
YAML_IMP_ERR = None |
| 229 |
try: |
| 230 |
import yaml |
| 231 |
|
| 232 |
HAS_YAML = True |
| 233 |
except ImportError: |
| 234 |
YAML_IMP_ERR = traceback.format_exc() |
| 235 |
HAS_YAML = False |
| 236 |
|
| 237 |
|
| 238 |
# Generate a list of VDO volumes, whether they are running or stopped. |
| 239 |
# |
| 240 |
# @param module The AnsibleModule object. |
| 241 |
# @param vdocmd The path of the 'vdo' command. |
| 242 |
# |
| 243 |
# @return vdolist A list of currently created VDO volumes. |
| 244 |
def inventory_vdos(module, vdocmd): |
| 245 |
rc, vdostatusout, err = module.run_command([vdocmd, "status"]) |
| 246 |
|
| 247 |
# if rc != 0: |
| 248 |
# module.fail_json(msg="Inventorying VDOs failed: %s" |
| 249 |
# % vdostatusout, rc=rc, err=err) |
| 250 |
|
| 251 |
vdolist = [] |
| 252 |
|
| 253 |
if rc == 2 and re.findall(r"vdoconf\.yml does not exist", err, re.MULTILINE): |
| 254 |
# If there is no /etc/vdoconf.yml file, assume there are no |
| 255 |
# VDO volumes. Return an empty list of VDO volumes. |
| 256 |
return vdolist |
| 257 |
|
| 258 |
if rc != 0: |
| 259 |
module.fail_json(msg=f"Inventorying VDOs failed: {vdostatusout}", rc=rc, err=err) |
| 260 |
|
| 261 |
vdostatusyaml = yaml.safe_load(vdostatusout) |
| 262 |
if vdostatusyaml is None: |
| 263 |
return vdolist |
| 264 |
|
| 265 |
vdoyamls = vdostatusyaml["VDOs"] |
| 266 |
|
| 267 |
if vdoyamls is not None: |
| 268 |
vdolist = list(vdoyamls.keys()) |
| 269 |
|
| 270 |
return vdolist |
| 271 |
|
| 272 |
|
| 273 |
def list_running_vdos(module, vdocmd): |
| 274 |
rc, vdolistout, err = module.run_command([vdocmd, "list"]) |
| 275 |
runningvdolist = [_f for _f in vdolistout.split("\n") if _f] |
| 276 |
return runningvdolist |
| 277 |
|
| 278 |
|
| 279 |
# Generate a string containing options to pass to the 'VDO' command. |
| 280 |
# Note that a 'create' operation will pass more options than a |
| 281 |
# 'modify' operation. |
| 282 |
# |
| 283 |
# @param params A dictionary of parameters, and their values |
| 284 |
# (values of 'None' and/or nonexistent values are ignored). |
| 285 |
# |
| 286 |
# @return vdocmdoptions A string to be used in a 'vdo <action>' command. |
| 287 |
def start_vdo(module, vdoname, vdocmd): |
| 288 |
rc, out, err = module.run_command([vdocmd, "start", f"--name={vdoname}"]) |
| 289 |
if rc == 0: |
| 290 |
module.log(f"started VDO volume {vdoname}") |
| 291 |
return rc |
| 292 |
|
| 293 |
|
| 294 |
def stop_vdo(module, vdoname, vdocmd): |
| 295 |
rc, out, err = module.run_command([vdocmd, "stop", f"--name={vdoname}"]) |
| 296 |
if rc == 0: |
| 297 |
module.log(f"stopped VDO volume {vdoname}") |
| 298 |
return rc |
| 299 |
|
| 300 |
|
| 301 |
def activate_vdo(module, vdoname, vdocmd): |
| 302 |
rc, out, err = module.run_command([vdocmd, "activate", f"--name={vdoname}"]) |
| 303 |
if rc == 0: |
| 304 |
module.log(f"activated VDO volume {vdoname}") |
| 305 |
return rc |
| 306 |
|
| 307 |
|
| 308 |
def deactivate_vdo(module, vdoname, vdocmd): |
| 309 |
rc, out, err = module.run_command([vdocmd, "deactivate", f"--name={vdoname}"]) |
| 310 |
if rc == 0: |
| 311 |
module.log(f"deactivated VDO volume {vdoname}") |
| 312 |
return rc |
| 313 |
|
| 314 |
|
| 315 |
def add_vdooptions(params): |
| 316 |
options = [] |
| 317 |
|
| 318 |
if params.get("logicalsize") is not None: |
| 319 |
options.append(f"--vdoLogicalSize={params['logicalsize']}") |
| 320 |
|
| 321 |
if params.get("blockmapcachesize") is not None: |
| 322 |
options.append(f"--blockMapCacheSize={params['blockmapcachesize']}") |
| 323 |
|
| 324 |
if params.get("readcache") == "enabled": |
| 325 |
options.append("--readCache=enabled") |
| 326 |
|
| 327 |
if params.get("readcachesize") is not None: |
| 328 |
options.append(f"--readCacheSize={params['readcachesize']}") |
| 329 |
|
| 330 |
if params.get("slabsize") is not None: |
| 331 |
options.append(f"--vdoSlabSize={params['slabsize']}") |
| 332 |
|
| 333 |
if params.get("emulate512"): |
| 334 |
options.append("--emulate512=enabled") |
| 335 |
|
| 336 |
if params.get("indexmem") is not None: |
| 337 |
options.append(f"--indexMem={params['indexmem']}") |
| 338 |
|
| 339 |
if params.get("indexmode") == "sparse": |
| 340 |
options.append("--sparseIndex=enabled") |
| 341 |
|
| 342 |
if params.get("force"): |
| 343 |
options.append("--force") |
| 344 |
|
| 345 |
# Entering an invalid thread config results in a cryptic |
| 346 |
# 'Could not set up device mapper for %s' error from the 'vdo' |
| 347 |
# command execution. The dmsetup module on the system will |
| 348 |
# output a more helpful message, but one would have to log |
| 349 |
# onto that system to read the error. For now, heed the thread |
| 350 |
# limit warnings in the DOCUMENTATION section above. |
| 351 |
if params.get("ackthreads") is not None: |
| 352 |
options.append(f"--vdoAckThreads={params['ackthreads']}") |
| 353 |
|
| 354 |
if params.get("biothreads") is not None: |
| 355 |
options.append(f"--vdoBioThreads={params['biothreads']}") |
| 356 |
|
| 357 |
if params.get("cputhreads") is not None: |
| 358 |
options.append(f"--vdoCpuThreads={params['cputhreads']}") |
| 359 |
|
| 360 |
if params.get("logicalthreads") is not None: |
| 361 |
options.append(f"--vdoLogicalThreads={params['logicalthreads']}") |
| 362 |
|
| 363 |
if params.get("physicalthreads") is not None: |
| 364 |
options.append(f"--vdoPhysicalThreads={params['physicalthreads']}") |
| 365 |
|
| 366 |
return options |
| 367 |
|
| 368 |
|
| 369 |
def run_module(): |
| 370 |
# Define the available arguments/parameters that a user can pass to |
| 371 |
# the module. |
| 372 |
# Defaults for VDO parameters are None, in order to facilitate |
| 373 |
# the detection of parameters passed from the playbook. |
| 374 |
# Creation param defaults are determined by the creation section. |
| 375 |
|
| 376 |
module_args = dict( |
| 377 |
name=dict(type="str", required=True), |
| 378 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 379 |
activated=dict(type="bool"), |
| 380 |
running=dict(type="bool"), |
| 381 |
growphysical=dict(type="bool", default=False), |
| 382 |
device=dict(type="str"), |
| 383 |
logicalsize=dict(type="str"), |
| 384 |
deduplication=dict(type="str", choices=["disabled", "enabled"]), |
| 385 |
compression=dict(type="str", choices=["disabled", "enabled"]), |
| 386 |
blockmapcachesize=dict(type="str"), |
| 387 |
readcache=dict(type="str", choices=["disabled", "enabled"]), |
| 388 |
readcachesize=dict(type="str"), |
| 389 |
emulate512=dict(type="bool", default=False), |
| 390 |
slabsize=dict(type="str"), |
| 391 |
writepolicy=dict(type="str", choices=["async", "auto", "sync"]), |
| 392 |
indexmem=dict(type="str"), |
| 393 |
indexmode=dict(type="str", choices=["dense", "sparse"]), |
| 394 |
ackthreads=dict(type="str"), |
| 395 |
biothreads=dict(type="str"), |
| 396 |
cputhreads=dict(type="str"), |
| 397 |
logicalthreads=dict(type="str"), |
| 398 |
physicalthreads=dict(type="str"), |
| 399 |
force=dict(type="bool", default=False), |
| 400 |
) |
| 401 |
|
| 402 |
# Seed the result dictionary in the object. There will be an |
| 403 |
# 'invocation' dictionary added with 'module_args' (arguments |
| 404 |
# given). |
| 405 |
result = dict( |
| 406 |
changed=False, |
| 407 |
) |
| 408 |
|
| 409 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 410 |
# this includes instantiation, a couple of common attr would be the |
| 411 |
# args/params passed to the execution, as well as if the module |
| 412 |
# supports check mode |
| 413 |
module = AnsibleModule( |
| 414 |
argument_spec=module_args, |
| 415 |
supports_check_mode=False, |
| 416 |
) |
| 417 |
|
| 418 |
if not HAS_YAML: |
| 419 |
module.fail_json(msg=missing_required_lib("PyYAML"), exception=YAML_IMP_ERR) |
| 420 |
|
| 421 |
vdocmd = module.get_bin_path("vdo", required=True) |
| 422 |
if not vdocmd: |
| 423 |
module.fail_json(msg="VDO is not installed.", **result) |
| 424 |
|
| 425 |
# Print a pre-run list of VDO volumes in the result object. |
| 426 |
vdolist = inventory_vdos(module, vdocmd) |
| 427 |
|
| 428 |
runningvdolist = list_running_vdos(module, vdocmd) |
| 429 |
|
| 430 |
# Collect the name of the desired VDO volume, and its state. These will |
| 431 |
# determine what to do. |
| 432 |
desiredvdo = module.params["name"] |
| 433 |
state = module.params["state"] |
| 434 |
|
| 435 |
# Create a desired VDO volume that doesn't exist yet. |
| 436 |
if (desiredvdo not in vdolist) and (state == "present"): |
| 437 |
device = module.params["device"] |
| 438 |
if device is None: |
| 439 |
module.fail_json(msg="Creating a VDO volume requires specifying a 'device' in the playbook.") |
| 440 |
|
| 441 |
# Create a dictionary of the options from the AnsibleModule |
| 442 |
# parameters, compile the vdo command options, and run "vdo create" |
| 443 |
# with those options. |
| 444 |
# Since this is a creation of a new VDO volume, it will contain all |
| 445 |
# all of the parameters given by the playbook; the rest will |
| 446 |
# assume default values. |
| 447 |
vdocmdoptions = add_vdooptions(module.params) |
| 448 |
rc, out, err = module.run_command( |
| 449 |
[vdocmd, "create", f"--name={desiredvdo}", f"--device={device}"] + vdocmdoptions |
| 450 |
) |
| 451 |
if rc == 0: |
| 452 |
result["changed"] = True |
| 453 |
else: |
| 454 |
module.fail_json(msg=f"Creating VDO {desiredvdo} failed.", rc=rc, err=err) |
| 455 |
|
| 456 |
if module.params["compression"] == "disabled": |
| 457 |
rc, out, err = module.run_command([vdocmd, "disableCompression", f"--name={desiredvdo}"]) |
| 458 |
|
| 459 |
if module.params["deduplication"] == "disabled": |
| 460 |
rc, out, err = module.run_command([vdocmd, "disableDeduplication", f"--name={desiredvdo}"]) |
| 461 |
|
| 462 |
if module.params["activated"] is False: |
| 463 |
deactivate_vdo(module, desiredvdo, vdocmd) |
| 464 |
|
| 465 |
if module.params["running"] is False: |
| 466 |
stop_vdo(module, desiredvdo, vdocmd) |
| 467 |
|
| 468 |
# Print a post-run list of VDO volumes in the result object. |
| 469 |
vdolist = inventory_vdos(module, vdocmd) |
| 470 |
module.log(f"created VDO volume {desiredvdo}") |
| 471 |
module.exit_json(**result) |
| 472 |
|
| 473 |
# Modify the current parameters of a VDO that exists. |
| 474 |
if desiredvdo in vdolist and state == "present": |
| 475 |
rc, vdostatusoutput, err = module.run_command([vdocmd, "status"]) |
| 476 |
vdostatusyaml = yaml.safe_load(vdostatusoutput) |
| 477 |
|
| 478 |
# An empty dictionary to contain dictionaries of VDO statistics |
| 479 |
processedvdos = {} |
| 480 |
|
| 481 |
vdoyamls = vdostatusyaml["VDOs"] |
| 482 |
if vdoyamls is not None: |
| 483 |
processedvdos = vdoyamls |
| 484 |
|
| 485 |
# The 'vdo status' keys that are currently modifiable. |
| 486 |
statusparamkeys = [ |
| 487 |
"Acknowledgement threads", |
| 488 |
"Bio submission threads", |
| 489 |
"Block map cache size", |
| 490 |
"CPU-work threads", |
| 491 |
"Logical threads", |
| 492 |
"Physical threads", |
| 493 |
"Read cache", |
| 494 |
"Read cache size", |
| 495 |
"Configured write policy", |
| 496 |
"Compression", |
| 497 |
"Deduplication", |
| 498 |
] |
| 499 |
|
| 500 |
# A key translation table from 'vdo status' output to Ansible |
| 501 |
# module parameters. This covers all of the 'vdo status' |
| 502 |
# parameter keys that could be modified with the 'vdo' |
| 503 |
# command. |
| 504 |
vdokeytrans = { |
| 505 |
"Logical size": "logicalsize", |
| 506 |
"Compression": "compression", |
| 507 |
"Deduplication": "deduplication", |
| 508 |
"Block map cache size": "blockmapcachesize", |
| 509 |
"Read cache": "readcache", |
| 510 |
"Read cache size": "readcachesize", |
| 511 |
"Configured write policy": "writepolicy", |
| 512 |
"Acknowledgement threads": "ackthreads", |
| 513 |
"Bio submission threads": "biothreads", |
| 514 |
"CPU-work threads": "cputhreads", |
| 515 |
"Logical threads": "logicalthreads", |
| 516 |
"Physical threads": "physicalthreads", |
| 517 |
} |
| 518 |
|
| 519 |
# Build a dictionary of the current VDO status parameters, with |
| 520 |
# the keys used by VDO. (These keys will be converted later.) |
| 521 |
currentvdoparams = {} |
| 522 |
|
| 523 |
# Build a "lookup table" dictionary containing a translation table |
| 524 |
# of the parameters that can be modified |
| 525 |
modtrans = {} |
| 526 |
|
| 527 |
for statfield in statusparamkeys: |
| 528 |
if statfield in processedvdos[desiredvdo]: |
| 529 |
currentvdoparams[statfield] = processedvdos[desiredvdo][statfield] |
| 530 |
|
| 531 |
modtrans[statfield] = vdokeytrans[statfield] |
| 532 |
|
| 533 |
# Build a dictionary of current parameters formatted with the |
| 534 |
# same keys as the AnsibleModule parameters. |
| 535 |
currentparams = {} |
| 536 |
for paramkey in modtrans.keys(): |
| 537 |
currentparams[modtrans[paramkey]] = modtrans[paramkey] |
| 538 |
|
| 539 |
diffparams = {} |
| 540 |
|
| 541 |
# Check for differences between the playbook parameters and the |
| 542 |
# current parameters. This will need a comparison function; |
| 543 |
# since AnsibleModule params are all strings, compare them as |
| 544 |
# strings (but if it is None; skip). |
| 545 |
for key in currentparams.keys(): |
| 546 |
if module.params[key] is not None: |
| 547 |
if str(currentparams[key]) != module.params[key]: |
| 548 |
diffparams[key] = module.params[key] |
| 549 |
|
| 550 |
if diffparams: |
| 551 |
vdocmdoptions = add_vdooptions(diffparams) |
| 552 |
if vdocmdoptions: |
| 553 |
rc, out, err = module.run_command([vdocmd, "modify", f"--name={desiredvdo}"] + vdocmdoptions) |
| 554 |
if rc == 0: |
| 555 |
result["changed"] = True |
| 556 |
else: |
| 557 |
module.fail_json(msg=f"Modifying VDO {desiredvdo} failed.", rc=rc, err=err) |
| 558 |
|
| 559 |
if "deduplication" in diffparams.keys(): |
| 560 |
dedupemod = diffparams["deduplication"] |
| 561 |
dedupeparam = "disableDeduplication" if dedupemod == "disabled" else "enableDeduplication" |
| 562 |
rc, out, err = module.run_command([vdocmd, dedupeparam, f"--name={desiredvdo}"]) |
| 563 |
|
| 564 |
if rc == 0: |
| 565 |
result["changed"] = True |
| 566 |
else: |
| 567 |
module.fail_json(msg=f"Changing deduplication on VDO volume {desiredvdo} failed.", rc=rc, err=err) |
| 568 |
|
| 569 |
if "compression" in diffparams.keys(): |
| 570 |
compressmod = diffparams["compression"] |
| 571 |
compressparam = "disableCompression" if compressmod == "disabled" else "enableCompression" |
| 572 |
rc, out, err = module.run_command([vdocmd, compressparam, f"--name={desiredvdo}"]) |
| 573 |
if rc == 0: |
| 574 |
result["changed"] = True |
| 575 |
else: |
| 576 |
module.fail_json(msg=f"Changing compression on VDO volume {desiredvdo} failed.", rc=rc, err=err) |
| 577 |
|
| 578 |
if "writepolicy" in diffparams.keys(): |
| 579 |
writepolmod = diffparams["writepolicy"] |
| 580 |
rc, out, err = module.run_command( |
| 581 |
[ |
| 582 |
vdocmd, |
| 583 |
"changeWritePolicy", |
| 584 |
f"--name={desiredvdo}", |
| 585 |
f"--writePolicy={writepolmod}", |
| 586 |
] |
| 587 |
) |
| 588 |
|
| 589 |
if rc == 0: |
| 590 |
result["changed"] = True |
| 591 |
else: |
| 592 |
module.fail_json(msg=f"Changing write policy on VDO volume {desiredvdo} failed.", rc=rc, err=err) |
| 593 |
|
| 594 |
# Process the size parameters, to determine of a growPhysical or |
| 595 |
# growLogical operation needs to occur. |
| 596 |
sizeparamkeys = [ |
| 597 |
"Logical size", |
| 598 |
] |
| 599 |
|
| 600 |
currentsizeparams = {} |
| 601 |
sizetrans = {} |
| 602 |
for statfield in sizeparamkeys: |
| 603 |
currentsizeparams[statfield] = processedvdos[desiredvdo][statfield] |
| 604 |
sizetrans[statfield] = vdokeytrans[statfield] |
| 605 |
|
| 606 |
sizeparams = {} |
| 607 |
for paramkey in currentsizeparams.keys(): |
| 608 |
sizeparams[sizetrans[paramkey]] = currentsizeparams[paramkey] |
| 609 |
|
| 610 |
diffsizeparams = {} |
| 611 |
for key in sizeparams.keys(): |
| 612 |
if module.params[key] is not None and str(sizeparams[key]) != module.params[key]: |
| 613 |
diffsizeparams[key] = module.params[key] |
| 614 |
|
| 615 |
if module.params["growphysical"]: |
| 616 |
physdevice = module.params["device"] |
| 617 |
rc, devsectors, err = module.run_command([module.get_bin_path("blockdev"), "--getsz", physdevice]) |
| 618 |
devblocks = int(devsectors) / 8 |
| 619 |
dmvdoname = f"/dev/mapper/{desiredvdo}" |
| 620 |
currentvdostats = processedvdos[desiredvdo]["VDO statistics"][dmvdoname] |
| 621 |
currentphysblocks = currentvdostats["physical blocks"] |
| 622 |
|
| 623 |
# Set a growPhysical threshold to grow only when there is |
| 624 |
# guaranteed to be more than 2 slabs worth of unallocated |
| 625 |
# space on the device to use. For now, set to device |
| 626 |
# size + 64 GB, since 32 GB is the largest possible |
| 627 |
# slab size. |
| 628 |
growthresh = devblocks + 16777216 |
| 629 |
|
| 630 |
if currentphysblocks > growthresh: |
| 631 |
result["changed"] = True |
| 632 |
rc, out, err = module.run_command([vdocmd, "growPhysical", f"--name={desiredvdo}"]) |
| 633 |
|
| 634 |
if "logicalsize" in diffsizeparams.keys(): |
| 635 |
result["changed"] = True |
| 636 |
rc, out, err = module.run_command( |
| 637 |
[vdocmd, "growLogical", f"--name={desiredvdo}", f"--vdoLogicalSize={diffsizeparams['logicalsize']}"] |
| 638 |
) |
| 639 |
|
| 640 |
vdoactivatestatus = processedvdos[desiredvdo]["Activate"] |
| 641 |
|
| 642 |
if module.params["activated"] is False and vdoactivatestatus == "enabled": |
| 643 |
deactivate_vdo(module, desiredvdo, vdocmd) |
| 644 |
if not result["changed"]: |
| 645 |
result["changed"] = True |
| 646 |
|
| 647 |
if module.params["activated"] and vdoactivatestatus == "disabled": |
| 648 |
activate_vdo(module, desiredvdo, vdocmd) |
| 649 |
if not result["changed"]: |
| 650 |
result["changed"] = True |
| 651 |
|
| 652 |
if module.params["running"] is False and desiredvdo in runningvdolist: |
| 653 |
stop_vdo(module, desiredvdo, vdocmd) |
| 654 |
if not result["changed"]: |
| 655 |
result["changed"] = True |
| 656 |
|
| 657 |
# Note that a disabled VDO volume cannot be started by the |
| 658 |
# 'vdo start' command, by design. To accurately track changed |
| 659 |
# status, don't try to start a disabled VDO volume. |
| 660 |
# If the playbook contains 'activated: true', assume that |
| 661 |
# the activate_vdo() operation succeeded, as 'vdoactivatestatus' |
| 662 |
# will have the activated status prior to the activate_vdo() |
| 663 |
# call. |
| 664 |
if ( |
| 665 |
(vdoactivatestatus == "enabled" or module.params["activated"]) |
| 666 |
and module.params["running"] |
| 667 |
and desiredvdo not in runningvdolist |
| 668 |
): |
| 669 |
start_vdo(module, desiredvdo, vdocmd) |
| 670 |
if not result["changed"]: |
| 671 |
result["changed"] = True |
| 672 |
|
| 673 |
# Print a post-run list of VDO volumes in the result object. |
| 674 |
vdolist = inventory_vdos(module, vdocmd) |
| 675 |
if diffparams: |
| 676 |
module.log(f"modified parameters of VDO volume {desiredvdo}") |
| 677 |
|
| 678 |
module.exit_json(**result) |
| 679 |
|
| 680 |
# Remove a desired VDO that currently exists. |
| 681 |
if desiredvdo in vdolist and state == "absent": |
| 682 |
rc, out, err = module.run_command([vdocmd, "remove", f"--name={desiredvdo}"]) |
| 683 |
if rc == 0: |
| 684 |
result["changed"] = True |
| 685 |
else: |
| 686 |
module.fail_json(msg=f"Removing VDO {desiredvdo} failed.", rc=rc, err=err) |
| 687 |
|
| 688 |
# Print a post-run list of VDO volumes in the result object. |
| 689 |
vdolist = inventory_vdos(module, vdocmd) |
| 690 |
module.log(f"removed VDO volume {desiredvdo}") |
| 691 |
module.exit_json(**result) |
| 692 |
|
| 693 |
# fall through |
| 694 |
# The state for the desired VDO volume was absent, and it does |
| 695 |
# not exist. Print a post-run list of VDO volumes in the result |
| 696 |
# object. |
| 697 |
vdolist = inventory_vdos(module, vdocmd) |
| 698 |
module.log(f"received request to remove non-existent VDO volume {desiredvdo}") |
| 699 |
|
| 700 |
module.exit_json(**result) |
| 701 |
|
| 702 |
|
| 703 |
def main(): |
| 704 |
run_module() |
| 705 |
|
| 706 |
|
| 707 |
if __name__ == "__main__": |
| 708 |
main() |