Skip to main content

Handling Main Action in Alt Disk Module

In this document, we will explain the process of handling the main action in the alt_disk module. The process involves initializing the Ansible module, validating parameters, finding a valid alternate disk, and retrieving free physical volumes.

The flow starts with initializing the Ansible module and checking if the script is running on a VIOS system. It then determines the action to perform and calls the appropriate function. The alt_disk_copy function validates the parameters and performs the alt disk copy operation. The find_valid_altdisk function checks the root volume group status and selects a suitable alternate disk. Finally, the get_free_pvs function retrieves the list of free physical volumes for the alt disk operations.

Flow drill down


Handling the main action

First, the main function initializes the Ansible module with the required parameters and checks if the script is running on a VIOS system, which is not allowed. It then determines the action to perform (copy, clean, or install) and calls the appropriate function to handle the action.

def main():
global results

module = AnsibleModule(
argument_spec=dict(
action=dict(type='str',
choices=['copy', 'clean', 'install'], default='copy'),
targets=dict(type='list', elements='str'),
disk_size_policy=dict(type='str',
choices=['minimize', 'upper', 'lower', 'nearest']),
existing_altinst_rootvg=dict(type='str'),
bundle_name=dict(type='str'),
apar_fixes=dict(type='str'),
filesets=dict(type='str'),
installp_flags=dict(type='str'),
image_location=dict(type='str'),
force=dict(type='bool', default=False),
bootlist=dict(type='bool', default=False),
remain_nim_client=dict(type='bool', default=False),
device_reset=dict(type='bool', default=False),
first_boot_script=dict(type='str'),


Validating parameters and performing alt disk copy

Next, the alt_disk_copy function validates the parameters to ensure either targets or disk_size_policy is specified. It then checks the root volume group (rootvg) and mirrors, and calls find_valid_altdisk to find a suitable alternate disk. Finally, it performs the alt disk copy operation and logs the results.

def alt_disk_copy(module, params, hdisks, allow_old_rootvg):
"""
alt_disk_copy operation

- check the rootvg, find and validate the hdisks for the operation
- perform the alt disk copy operation
"""
global mirrors

# Either hdisks must be non-empty or disk_size_policy must be
# explicitly set. This ensures the user knows what he is doing.
if not hdisks and not params['disk_size_policy']:
results['msg'] = 'Either targets or disk_size_policy must be specified'
module.fail_json(**results)

rootvg_info = check_rootvg(module)
if rootvg_info is None:
module.fail_json(**results)

if hdisks is None:
hdisks = []


Finding a valid alternate disk

Then, the find_valid_altdisk function checks the rootvg status and retrieves the list of physical volumes (PVs). It ensures no alternate or old rootvg disks already exist unless forced. It then selects a suitable alternate disk based on the disk size policy and validates the selected disks.

def find_valid_altdisk(module, hdisks, rootvg_info, disk_size_policy, force, allow_old_rootvg):
"""
Find a valid alternate disk that:
- exists,
- is not part of a VG
- with a correct size
and so can be used.
"""

# check rootvg
if rootvg_info['status'] != 0:
results['msg'] = 'Wrong rootvg state'
module.fail_json(**results)

# get pv list
pvs = get_pvs(module)
if pvs is None:
module.fail_json(**results)
# check an alternate disk does not already exist
found_altdisk = ''
found_oldrootvg = ''


Getting free physical volumes

Finally, the get_free_pvs function retrieves the list of free PVs by running system commands to check for disks with no volume groups and retrieves their sizes. This information is used to find suitable disks for the alt disk operations.

def get_free_pvs(module):
"""
Get the list of free PVs.

return: dictionary with free PVs information
"""

cmd = ['lspv']
ret, stdout, stderr = module.run_command(cmd)
if ret != 0:
results['stdout'] = stdout
results['stderr'] = stderr
results['msg'] = f'Command \'{ cmd }\' failed with return code { ret }.'
return None

# hdisk0 000018fa3b12f5cb rootvg active
free_pvs = {}
for line in stdout.split('\n'):
line = line.rstrip()
match_key = re.match(r"^(hdisk\S+)\s+(\S+)\s+(\S+)\s*(\S*)", line)
# Only match disks that have no volume groups

 

This is an auto-generated document by Swimm 🌊 and has not yet been verified by a human