From 259d9942537247f063c18019ae16cbac3f224b83 Mon Sep 17 00:00:00 2001 From: Alexander Bulimov Date: Mon, 11 Mar 2013 13:17:36 +0400 Subject: [PATCH 1/3] lvg module for managing LVM volume groups --- library/lvg | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100755 library/lvg diff --git a/library/lvg b/library/lvg new file mode 100755 index 00000000000..064462953bd --- /dev/null +++ b/library/lvg @@ -0,0 +1,145 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2013, Alexander Bulimov +# based on lvol module by Jeroen Hoekx +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +DOCUMENTATION = ''' +--- +author: Alexander Bulimov +module: lvg +short_description: Configure LVM volume groups +description: + - This module creates or removes volume groups. +version_added: "1.1" +options: + vg: + description: + - The name of the volume group. + required: true + dev: + description: + - List of comma-separated devices to use in this volume group. + required: true + state: + choices: [ "present", "absent" ] + default: present + description: + - Control if the volume group exists. + required: false + force: + choices: [ "yes", "no" ] + default: no + description: + - If yes, allows to remove volume group with logical volumes. + required: false +examples: + - description: Create a volume group on top of /dev/sda1. + code: lvg vg=vg.services dev=/dev/sda1 + - description: Create a volume group on top of /dev/sdb1 and /dev/sdc5. + code: lvg vg=vg.services dev=/dev/sdb1,/dev/sdc5 + - description: Remove a volume group with name vg.services. + code: lvg vg=vg.services state=absent +notes: + - module does not check device list for already present volume group +''' + +def parse_vgs(data): + vgs = [] + for line in data.splitlines(): + parts = line.strip().split(';') + vgs.append({ + 'name': parts[0], + 'pv_count': int(parts[1]), + 'lv_count': int(parts[2]), + }) + return vgs + +def main(): + module = AnsibleModule( + argument_spec = dict( + vg=dict(required=True), + dev=dict(), + state=dict(choices=["absent", "present"], default='present'), + force=dict(choices=["yes", "no"], default='no'), + ), + supports_check_mode=True, + ) + + vg = module.params['vg'] + if module.params['dev']: + dev = module.params['dev'].replace(',',' ') + dev_list = module.params['dev'].split(',') + state = module.params['state'] + force = module.params['force'] + + if state=='present' and not dev: + module.fail_json(msg="No dev given.") + + if state=='present': + for test_dev in dev_list: + if not os.path.exists(test_dev): + module.fail_json(msg="No dev %s found."%test_dev) + + rc,current_vgs,err = module.run_command("vgs --noheadings -o vg_name,pv_count,lv_count --separator ';'") + + if rc != 0: + module.fail_json(msg="Failed creating volume group %s."%vg, rc=rc, err=err) + + changed = False + + vgs = parse_vgs(current_vgs) + + for test_vg in vgs: + if test_vg['name'] == vg: + this_vg = test_vg + break + else: + this_vg = None + + if this_vg is None: + if state == 'present': + ### create VG + if module.check_mode: + changed = True + else: + rc,_,err = module.run_command("vgcreate %s %s"%(vg, dev)) + if rc == 0: + changed = True + else: + module.fail_json(msg="Creating volume group '%s' failed"%(vg), rc=rc, err=err) + else: + if state == 'absent': + if module.check_mode: + module.exit_json(changed=True) + else: + if this_vg['lv_count'] == 0 or force == "yes": + ### remove VG + rc,_,err = module.run_command("vgremove --force %s"%(vg)) + if rc == 0: + module.exit_json(changed=True) + else: + module.fail_json(msg="Failed to remove volume group %s"%(vg),rc=rc, err=err) + else: + module.fail_json(msg="Refuse to remove non-empty volume group %s without force=yes"%(vg)) + + module.exit_json(changed=changed) + +# this is magic, see lib/ansible/module_common.py +#<> +main() From c8c0fe945bf711ebc263669c09b75b481463729d Mon Sep 17 00:00:00 2001 From: Alexander Bulimov Date: Tue, 12 Mar 2013 14:06:39 +0400 Subject: [PATCH 2/3] various fixes in lvg module, added ability to reduce and extend VG, added Physical Extent parameter, added explicit creation of physical volumes --- library/lvg | 114 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 100 insertions(+), 14 deletions(-) diff --git a/library/lvg b/library/lvg index 064462953bd..3a0b2ab866c 100755 --- a/library/lvg +++ b/library/lvg @@ -25,7 +25,7 @@ author: Alexander Bulimov module: lvg short_description: Configure LVM volume groups description: - - This module creates or removes volume groups. + - This module creates, removes or resizes volume groups. version_added: "1.1" options: vg: @@ -36,6 +36,11 @@ options: description: - List of comma-separated devices to use in this volume group. required: true + pesize: + description: + - The size of the physical extent in megabytes. + default: 4 + required: false state: choices: [ "present", "absent" ] default: present @@ -44,19 +49,19 @@ options: required: false force: choices: [ "yes", "no" ] - default: no + default: "no" description: - If yes, allows to remove volume group with logical volumes. required: false examples: - - description: Create a volume group on top of /dev/sda1. - code: lvg vg=vg.services dev=/dev/sda1 + - description: Create a volume group on top of /dev/sda1 with physical extent size = 32MB. + code: lvg vg=vg.services dev=/dev/sda1 pesize=32 - description: Create a volume group on top of /dev/sdb1 and /dev/sdc5. code: lvg vg=vg.services dev=/dev/sdb1,/dev/sdc5 - description: Remove a volume group with name vg.services. code: lvg vg=vg.services state=absent notes: - - module does not check device list for already present volume group + - module does not modify PE size for already present volume group ''' def parse_vgs(data): @@ -70,36 +75,63 @@ def parse_vgs(data): }) return vgs +def parse_pvs(data): + pvs = [] + for line in data.splitlines(): + parts = line.strip().split(';') + pvs.append({ + 'name': parts[0], + 'vg_name': parts[1], + }) + return pvs + def main(): module = AnsibleModule( argument_spec = dict( vg=dict(required=True), dev=dict(), + pesize=dict(), state=dict(choices=["absent", "present"], default='present'), - force=dict(choices=["yes", "no"], default='no'), + force=dict(type='bool', default='no'), ), supports_check_mode=True, ) vg = module.params['vg'] + state = module.params['state'] + force = module.boolean(module.params['force']) + if module.params['pesize']: + pesize = int(module.params['pesize']) + else: + pesize = 4 + if module.params['dev']: dev = module.params['dev'].replace(',',' ') dev_list = module.params['dev'].split(',') - state = module.params['state'] - force = module.params['force'] - - if state=='present' and not dev: + elif state == 'present': module.fail_json(msg="No dev given.") if state=='present': + ### check given devices for test_dev in dev_list: if not os.path.exists(test_dev): module.fail_json(msg="No dev %s found."%test_dev) + ### get pv list + rc,current_pvs,err = module.run_command("pvs --noheadings -o pv_name,vg_name --separator ';'") + if rc != 0: + module.fail_json(msg="Failed executing pvs command.",rc=rc, err=err) + + ### check pv for devices + pvs = parse_pvs(current_pvs) + used_pvs = filter(lambda pv: pv['name'] in dev_list and pv['vg_name'] and pv['vg_name'] != vg, pvs) + if used_pvs: + module.fail_json(msg="dev %s is already in %s volume group."%(used_pvs[0]['name'],used_pvs[0]['vg_name'])) + rc,current_vgs,err = module.run_command("vgs --noheadings -o vg_name,pv_count,lv_count --separator ';'") if rc != 0: - module.fail_json(msg="Failed creating volume group %s."%vg, rc=rc, err=err) + module.fail_json(msg="Failed executing vgs command.",rc=rc, err=err) changed = False @@ -118,17 +150,24 @@ def main(): if module.check_mode: changed = True else: - rc,_,err = module.run_command("vgcreate %s %s"%(vg, dev)) + ### create PV + for current_dev in dev_list: + rc,_,err = module.run_command("pvcreate %s"%current_dev) + if rc == 0: + changed = True + else: + module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err) + rc,_,err = module.run_command("vgcreate -s %s %s %s"%(pesize, vg, dev)) if rc == 0: changed = True else: - module.fail_json(msg="Creating volume group '%s' failed"%(vg), rc=rc, err=err) + module.fail_json(msg="Creating volume group '%s' failed"%vg, rc=rc, err=err) else: if state == 'absent': if module.check_mode: module.exit_json(changed=True) else: - if this_vg['lv_count'] == 0 or force == "yes": + if this_vg['lv_count'] == 0 or force: ### remove VG rc,_,err = module.run_command("vgremove --force %s"%(vg)) if rc == 0: @@ -138,6 +177,53 @@ def main(): else: module.fail_json(msg="Refuse to remove non-empty volume group %s without force=yes"%(vg)) + ### resize VG + action = None + current_devs = map(lambda x: x['name'], filter(lambda pv: pv['vg_name'] == vg, pvs)) + devs_to_remove = list(set(current_devs) - set(dev_list)) + devs_to_add = list(set(dev_list) - set(current_devs)) + if devs_to_remove and devs_to_add: + devs_string = ' '.join([`dev` for dev in devs_to_add]) + devs_to_remove_string = ' '.join([`dev` for dev in devs_to_remove]) + action = 'modify' + elif devs_to_remove: + devs_string = ' '.join([`dev` for dev in devs_to_remove]) + action = 'reduce' + elif devs_to_add: + devs_string = ' '.join([`dev` for dev in devs_to_add]) + action = 'extend' + + if action: + if module.check_mode: + changed = True + else: + if action == 'extend' or action == 'modify': + tool = 'vgextend' + ### create PV + for current_dev in devs_to_add: + rc,_,err = module.run_command("pvcreate %s"%current_dev) + if rc == 0: + changed = True + else: + module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err) + else: + tool = 'vgreduce --force' + + ### first we add or remove PV + rc,_,err = module.run_command("%s %s %s"%(tool, vg, devs_string)) + if rc == 0: + changed = True + else: + module.fail_json(msg="Unable to %s %s by %s."%(action, vg, devs_string),rc=rc,err=err) + ### then if we need - remove some PV + if action == 'modify': + tool = 'vgreduce --force' + rc,_,err = module.run_command("%s %s %s"%(tool, vg, devs_to_remove_string)) + if rc == 0: + changed = True + else: + module.fail_json(msg="Unable to reduce %s by %s."%(vg, devs_to_remove_string),rc=rc,err=err) + module.exit_json(changed=changed) # this is magic, see lib/ansible/module_common.py From 902d6347c773c1cca1cf44622eb649afac6e419d Mon Sep 17 00:00:00 2001 From: Alexander Bulimov Date: Thu, 14 Mar 2013 13:32:16 +0400 Subject: [PATCH 3/3] now using 'type=' for module parameters, replaced lambda with list comprehensions, simplyfied extend/reduce part of module, renamed dev parameter to pvs --- library/lvg | 78 +++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/library/lvg b/library/lvg index 3a0b2ab866c..08cc66865c9 100755 --- a/library/lvg +++ b/library/lvg @@ -32,9 +32,9 @@ options: description: - The name of the volume group. required: true - dev: + pvs: description: - - List of comma-separated devices to use in this volume group. + - List of comma-separated devices to use as physical devices in this volume group. required: true pesize: description: @@ -51,13 +51,13 @@ options: choices: [ "yes", "no" ] default: "no" description: - - If yes, allows to remove volume group with logical volumes. + - If yes, allows to remove or reduce volume group with logical volumes. required: false examples: - description: Create a volume group on top of /dev/sda1 with physical extent size = 32MB. - code: lvg vg=vg.services dev=/dev/sda1 pesize=32 + code: lvg vg=vg.services pvs=/dev/sda1 pesize=32 - description: Create a volume group on top of /dev/sdb1 and /dev/sdc5. - code: lvg vg=vg.services dev=/dev/sdb1,/dev/sdc5 + code: lvg vg=vg.services pvs=/dev/sdb1,/dev/sdc5 - description: Remove a volume group with name vg.services. code: lvg vg=vg.services state=absent notes: @@ -89,8 +89,8 @@ def main(): module = AnsibleModule( argument_spec = dict( vg=dict(required=True), - dev=dict(), - pesize=dict(), + pvs=dict(type='list'), + pesize=dict(type='int', default=4), state=dict(choices=["absent", "present"], default='present'), force=dict(type='bool', default='no'), ), @@ -100,22 +100,19 @@ def main(): vg = module.params['vg'] state = module.params['state'] force = module.boolean(module.params['force']) - if module.params['pesize']: - pesize = int(module.params['pesize']) - else: - pesize = 4 + pesize = module.params['pesize'] - if module.params['dev']: - dev = module.params['dev'].replace(',',' ') - dev_list = module.params['dev'].split(',') + if module.params['pvs']: + dev_string = ' '.join(module.params['pvs']) + dev_list = module.params['pvs'] elif state == 'present': - module.fail_json(msg="No dev given.") + module.fail_json(msg="No physical volumes given.") if state=='present': ### check given devices for test_dev in dev_list: if not os.path.exists(test_dev): - module.fail_json(msg="No dev %s found."%test_dev) + module.fail_json(msg="Device %s not found."%test_dev) ### get pv list rc,current_pvs,err = module.run_command("pvs --noheadings -o pv_name,vg_name --separator ';'") @@ -124,9 +121,9 @@ def main(): ### check pv for devices pvs = parse_pvs(current_pvs) - used_pvs = filter(lambda pv: pv['name'] in dev_list and pv['vg_name'] and pv['vg_name'] != vg, pvs) + used_pvs = [ pv for pv in pvs if pv['name'] in dev_list and pv['vg_name'] and pv['vg_name'] != vg ] if used_pvs: - module.fail_json(msg="dev %s is already in %s volume group."%(used_pvs[0]['name'],used_pvs[0]['vg_name'])) + module.fail_json(msg="Device %s is already in %s volume group."%(used_pvs[0]['name'],used_pvs[0]['vg_name'])) rc,current_vgs,err = module.run_command("vgs --noheadings -o vg_name,pv_count,lv_count --separator ';'") @@ -157,7 +154,7 @@ def main(): changed = True else: module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err) - rc,_,err = module.run_command("vgcreate -s %s %s %s"%(pesize, vg, dev)) + rc,_,err = module.run_command("vgcreate -s %s %s %s"%(pesize, vg, dev_string)) if rc == 0: changed = True else: @@ -178,27 +175,16 @@ def main(): module.fail_json(msg="Refuse to remove non-empty volume group %s without force=yes"%(vg)) ### resize VG - action = None - current_devs = map(lambda x: x['name'], filter(lambda pv: pv['vg_name'] == vg, pvs)) + current_devs = [ pv['name'] for pv in pvs if pv['vg_name'] == vg ] devs_to_remove = list(set(current_devs) - set(dev_list)) devs_to_add = list(set(dev_list) - set(current_devs)) - if devs_to_remove and devs_to_add: - devs_string = ' '.join([`dev` for dev in devs_to_add]) - devs_to_remove_string = ' '.join([`dev` for dev in devs_to_remove]) - action = 'modify' - elif devs_to_remove: - devs_string = ' '.join([`dev` for dev in devs_to_remove]) - action = 'reduce' - elif devs_to_add: - devs_string = ' '.join([`dev` for dev in devs_to_add]) - action = 'extend' - - if action: + + if devs_to_add or devs_to_remove: if module.check_mode: changed = True else: - if action == 'extend' or action == 'modify': - tool = 'vgextend' + if devs_to_add: + devs_to_add_string = ' '.join(devs_to_add) ### create PV for current_dev in devs_to_add: rc,_,err = module.run_command("pvcreate %s"%current_dev) @@ -206,19 +192,17 @@ def main(): changed = True else: module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err) - else: - tool = 'vgreduce --force' + ### add PV to our VG + rc,_,err = module.run_command("vgextend %s %s"%(vg, devs_to_add_string)) + if rc == 0: + changed = True + else: + module.fail_json(msg="Unable to extend %s by %s."%(vg, devs_to_add_string),rc=rc,err=err) - ### first we add or remove PV - rc,_,err = module.run_command("%s %s %s"%(tool, vg, devs_string)) - if rc == 0: - changed = True - else: - module.fail_json(msg="Unable to %s %s by %s."%(action, vg, devs_string),rc=rc,err=err) - ### then if we need - remove some PV - if action == 'modify': - tool = 'vgreduce --force' - rc,_,err = module.run_command("%s %s %s"%(tool, vg, devs_to_remove_string)) + ### remove some PV from our VG + if devs_to_remove: + devs_to_remove_string = ' '.join(devs_to_remove) + rc,_,err = module.run_command("vgreduce --force %s %s"%(vg, devs_to_remove_string)) if rc == 0: changed = True else: