
    VhN                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
mZ dZd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaP  
author:
  - Alexander Bulimov (@abulimov)
module: lvg
short_description: Configure LVM volume groups
description:
  - This module creates, removes or resizes volume groups.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  vg:
    description:
      - The name of the volume group.
    type: str
    required: true
  pvs:
    description:
      - List of comma-separated devices to use as physical devices in this volume group.
      - Required when creating or resizing volume group.
      - The module will take care of running pvcreate if needed.
      - O(remove_extra_pvs) controls whether or not unspecified physical devices are removed from the volume group.
    type: list
    elements: str
  pesize:
    description:
      - The size of the physical extent. O(pesize) must be a power of 2 of at least 1 sector (where the sector size is the
        largest sector size of the PVs currently used in the VG), or at least 128KiB.
      - O(pesize) can be optionally suffixed by a UNIT (k/K/m/M/g/G), default unit is megabyte.
    type: str
    default: "4"
  pv_options:
    description:
      - Additional options to pass to C(pvcreate) when creating the volume group.
    type: str
    default: ''
  pvresize:
    description:
      - If V(true), resize the physical volume to the maximum available size.
    type: bool
    default: false
    version_added: '0.2.0'
  vg_options:
    description:
      - Additional options to pass to C(vgcreate) when creating the volume group.
    type: str
    default: ''
  state:
    description:
      - Control if the volume group exists and its state.
      - The states V(active) and V(inactive) implies V(present) state. Added in 7.1.0.
      - If V(active) or V(inactive), the module manages the VG's logical volumes current state. The module also handles the
        VG's autoactivation state if supported unless when creating a volume group and the autoactivation option specified
        in O(vg_options).
    type: str
    choices: [absent, present, active, inactive]
    default: present
  force:
    description:
      - If V(true), allows to remove volume group with logical volumes.
    type: bool
    default: false
  reset_vg_uuid:
    description:
      - Whether the volume group's UUID is regenerated.
      - This is B(not idempotent). Specifying this parameter always results in a change.
    type: bool
    default: false
    version_added: 7.1.0
  reset_pv_uuid:
    description:
      - Whether the volume group's physical volumes' UUIDs are regenerated.
      - This is B(not idempotent). Specifying this parameter always results in a change.
    type: bool
    default: false
    version_added: 7.1.0
  remove_extra_pvs:
    description:
      - Remove physical volumes from the volume group which are not in O(pvs).
    type: bool
    default: true
    version_added: 10.4.0
seealso:
  - module: community.general.filesystem
  - module: community.general.lvol
  - module: community.general.parted
notes:
  - This module does not modify PE size for already present volume group.
a  
- name: Create a volume group on top of /dev/sda1 with physical extent size = 32MB
  community.general.lvg:
    vg: vg.services
    pvs: /dev/sda1
    pesize: 32

- name: Create a volume group on top of /dev/sdb with physical extent size = 128KiB
  community.general.lvg:
    vg: vg.services
    pvs: /dev/sdb
    pesize: 128K

# If, for example, we already have VG vg.services on top of /dev/sdb1,
# this VG will be extended by /dev/sdc5.  Or if vg.services was created on
# top of /dev/sda5, we first extend it with /dev/sdb1 and /dev/sdc5,
# and then reduce by /dev/sda5.
- name: Create or resize a volume group on top of /dev/sdb1 and /dev/sdc5.
  community.general.lvg:
    vg: vg.services
    pvs: /dev/sdb1,/dev/sdc5

- name: Remove a volume group with name vg.services
  community.general.lvg:
    vg: vg.services
    state: absent

- name: Create a volume group on top of /dev/sda3 and resize the volume group /dev/sda3 to the maximum possible
  community.general.lvg:
    vg: resizableVG
    pvs: /dev/sda3
    pvresize: true

- name: Deactivate a volume group
  community.general.lvg:
    state: inactive
    vg: vg.services

- name: Activate a volume group
  community.general.lvg:
    state: active
    vg: vg.services

- name: Reset a volume group UUID
  community.general.lvg:
    state: inactive
    vg: vg.services
    reset_vg_uuid: true

- name: Reset both volume group and pv UUID
  community.general.lvg:
    state: inactive
    vg: vg.services
    pvs: /dev/sdb1,/dev/sdc5
    reset_vg_uuid: true
    reset_pv_uuid: true
N)AnsibleModulez--setautoactivationc           	          g }| j                         D ]Q  }|j                         j                  d      }|j                  |d   t	        |d         t	        |d         d       S |S )N;r         )namepv_countlv_count)
splitlinesstripsplitappendint)datavgslinepartss       i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/lvg.py	parse_vgsr      sh    
C! 

""3'

!HE!HE!H
 	 J    c           	          | j                  dd      }d}| j                  |ddddd|g      \  }}}|d	k7  r| j                  d
||       ||j                         z   }|S )NdmsetupTz/dev/mapper/infoz-C--noheadings-or   r   z!Failed executing dmsetup command.msgrcerr)get_bin_pathrun_command	fail_jsonrstrip)module	dm_devicedmsetup_cmdmapper_prefixr!   dm_namer"   mapper_devices           r   find_mapper_device_namer-      sv    %%i6K"M));nVZ\bdm*noB	Qw@RSQ!GNN$44Mr   c                     g }d}|j                         D ]a  }|j                         j                  d      }|d   j                  |      rt	        | |d         |d<   |j                  |d   |d   d       c |S )Nz/dev/dm-r   r   r	   )r   vg_name)r   r   r   
startswithr-   r   )r'   r   pvs	dm_prefixr   r   s         r   	parse_pvsr3      s    
CI! 

""3'8y).vuQx@E!H

!HQx
 		 Jr   c                     |sy | j                  dd      }| j                  |dddddgd      \  }}}t        |      }|D ]  }|d	   |k(  s|} |S  d }|S )
Nr   Tr   r   zvg_name,pv_count,lv_count--separatorr   check_rcr   )r#   r$   r   )r'   vgvgs_cmddummycurrent_vgsr   test_vgthis_vgs           r   find_vgr>      s    !!%.G & 2 2G^TSnp}  @C  4D  OS 2  !TE;
K
 C 6?b G N
 Nr   c                 P    d}| j                  |dgd      \  }}}t        |v rd}|S )NFz--helpTr6   )r$   VG_AUTOACTIVATION_OPT)r'   vg_cmdautoactivation_supportedr:   vgchange_optss        r   is_autoactivation_supportedrD      s>    $"("4"4fh5GRV"4"WE=%-#' ##r   c                    d}| j                  dd      }| j                  dd      }dg}d}t        | |      }|r|j                  d       |dd	d
j                  |      dd|g}	| j	                  |	d      \  }
}}
d}d}|j                         D ]F  }|j                         j                  d      }|d   d   dk(  r|dz  }n|dz  }|s;|xs |d   dk(  }H d }|r|dkD  rd}n	|s|dkD  rd}|ra|r/|s-| j                  rd}nN| j	                  |t        d|gd       d}n0|s.|r,| j                  rd}n| j	                  |t        d|gd       d}|)| j                  rd}|S | j	                  |d||gd       d}|S )NFvgchangeTr   lv_attrr'   rA   autoactivationr   r   ,r5   r   r6   r      ar	   enabledynz
--activate)
r#   rD   r   joinr$   r   r   r   
check_moder@   )r'   r8   activechangedvgchange_cmdr9   
vgs_fieldsautoactivation_enabledrB   vgs_cmd_with_optsr:   current_vg_lv_stateslv_active_countlv_inactive_countr   r   activate_flags                    r   activate_vgr\      s   G&&z48L!!%.GJ":&Q]^*+ .$8Lm]`bde)/););<MX\);)]&EO$//1 U

""3'8A;#q O"#%;%TuQx9?T"U M#a'!+  0  ""L2Gb#Q\`"a2  ""L2Gb#Q\`"a G
 N lM2NY]^GNr   c                     | j                  dd      }t        | |      }|r+|dv r&t        |vr|dk(  r|t        dgz  }y |t        dgz  }y y y y )NvgcreateTrH   )rR   inactiverR   rN   rO   )r#   rD   r@   )r'   state	vgoptionsvgcreate_cmdrB   s        r   append_vgcreate_optionsrc   &  sk    &&z48L:&Q]^E-C$C 	1 3S99	3S99		 2 %Dr   c                 $   | j                  dd      }g d}||g|z   }| j                  |d      \  }}}|j                         j                  d      }t	        |d         }t	        |d         }	t	        |d         }
t	        |d	         }||	|
|fS )
N	pvdisplayT)	z--unitsbz	--columnsr   z
--nosuffixr5   r   r   z(dev_size,pv_size,pe_start,vg_extent_sizer6   r   r   r	   r
      )r#   r$   r   r   r   )r'   devicepvdisplay_cmdpvdisplay_opspvdisplay_cmd_device_optionsr:   	pv_valuesvaluesdev_sizepv_sizepe_startvg_extent_sizes               r   get_pv_values_for_resizerr   3  s    ''T:M VM$16#:]#J $001MX\0]E9e__$$S)F6!9~H&)nG6!9~H^Ngx88r   c                    d}| j                  dd      }t        | |      \  }}}}|||z   z
  |kD  rW| j                  rd}|S | j                  ||g      \  }}	}
t        | |      \  }}}}||k(  r| j	                  d||
|	       |S d}|S )NFpvresizeTr'   rh   z"Failed executing pvresize command.r    r!   r"   out)r#   rr   rQ   r$   r%   )r'   rh   rS   pvresize_cmdrn   ro   rp   rq   r!   rw   r"   r:   new_pv_sizes                r   	resize_pvrz   D  s    G&&z48L2JRXag2h/HgxHw&'>9G N "--|V.DELBS/Gv^d/e,E;u+%  %IbVY_b c N Nr   c                    d}| j                  dd      }|ddd|g}| j                  dd      }|d|g}| j                  |d	      \  }}}| j                  rd}|S | j                  |      \  }	}
}| j                  |d	      \  }}}|j                         |j                         k(  r| j	                  d
|z  |	||
       |S d}|S )NFr1   Tr   r   uuidpvchange-ur6   zPV (%s) UUID change failedrv   )r#   r$   rQ   r   r%   )r'   rh   rS   pvs_cmdpvs_cmd_with_optspvchange_cmdpvchange_cmd_with_optsr:   	orig_uuidpvchange_rcpvchange_outpvchange_errnew_uuids                r   reset_uuid_pvr   X  s    G!!%.G .$G&&z48L*D&9$001BT0RE9e N 392D2DE[2\/\<!'!3!34EPT!3!Ux?? 00!=!H[^jp|} N GNr   c                     d}| j                  dd      }|d|g}| j                  rd}|S | j                  |d       d}|S )NFrF   Tr~   r6   )r#   rQ   r$   )r'   r8   rS   rT   vgchange_cmd_with_optss        r   reset_uuid_vgr   o  s[    G&&z48L*D"5
 N 	1DANr   c                  J   t        t        t        dd      t        dd      t        dd      t        dd      t        d	d
      t        dd      t        ddg d      t        d	d
      t        d	d
      t        d	d
      t        d	d            dddgggd      } | j                  d   }| j                  d   }| j                  | j                  d         }| j                  | j                  d         }| j                  d   }| j                  d   j	                         }| j                  d   j	                         }| j                  | j                  d         }| j                  | j                  d         }	| j                  | j                  d         }
t        | |      }|dv }|xr |d u }d
}g }| j                  d   rt        | j                  d         }n|r| j                  d       t        |      D ]'  \  }}t        j                  j                  |      ||<   ) |rA|D ]7  }t        j                  j                  |      r#| j                  d|z         9 | j                  dd      }|r_d j                  d! t        j                   || j                  d         D              }d"j#                  |      }d#d$j#                  ||      g}ng }| j%                  |d%d&d'd(d)g|z         \  }}}|d*k7  r| j                  d+||,       t'        | |      }|D cg c]  }|d-   |v s|d.   s|d.   |k7  s| }}|r%| j                  d/|d*   d-   d0|d*   d.   d1       ||rt)        | ||2       | j*                  rd}nr| j                  d3d      }|D ]H  }| j%                  |g|z   d4t-        |      gz         \  }}}|d*k(  rd}2| j                  d5|z  ||,       J | j                  d6      } | j%                  | g|z   d7||gz   |z         \  }}}|d*k(  rd}n| j                  d8|z  ||,       n|d9k(  r| j*                  r| j/                  d:       n|d;   d*k(  s|rZ| j                  d<d      }!| j%                  |!d=|g      \  }}}|d*k(  r| j/                  d:       nU| j                  d>|z  ||,       n=| j                  d?|z         n'|d@k(  rt1        | |dA      }n|dBk(  rt1        | |d
A      }|rt3        | |      xs |}|rD cg c]-  }|d.   |k(  st        j                  j                  |d-         / }"}t        t5        |"      t5        |      z
        }#t        t5        |      t5        |"      z
        }$|
sg }#|"r0|r.|"D ])  }%|rt7        | |%C      xs |}|	st9        | |%C      xs |}+ |$s|#r0| j*                  rd}n |$r| j                  d3d      }|$D ]H  }| j%                  |g|z   d4t-        |      gz         \  }}}|d*k(  rd}2| j                  d5|z  ||,       J | j                  dDd      }&| j%                  |&|g|$z         \  }}}|d*k(  rd}n*| j                  dE|dFdGj                  |$      dH||,       |#r_| j                  dId      }'| j%                  |'d=|g|#z         \  }}}|d*k(  rd}n*| j                  dJ|dFdGj                  |#      dH||,       | j/                  |:       y c c}w c c}w )KNstrT)typerequiredlist)r   elements4)r   default boolFpresent)absentr   rR   r_   )r   r   choices)r8   r1   pesize
pv_optionsrt   
vg_optionsr`   forcereset_vg_uuidreset_pv_uuidremove_extra_pvsr   r1   )argument_specrequired_ifsupports_check_moder8   r`   r   rt   r   r   r   r   r   )r'   r8   )r   rR   r_   zNo physical volumes given.)r    zDevice %s not found.z || c              3   >   K   | ]  }d j                  |        yw)zpv_name = {0}N)format).0xs     r   	<genexpr>zmain.<locals>.<genexpr>  s#      -  &&q)-s   zvg_name = {0}z--selectz
{0} || {1}r   r   zpv_name,vg_namer5   r   r   zFailed executing pvs command.r   r   r/   zDevice z is already in z volume group.)r'   r`   ra   pvcreatez-fz$Creating physical volume '%s' failedr^   z-sz!Creating volume group '%s' failedr   )rS   r   vgremovez--forcez Failed to remove volume group %sz=Refuse to remove non-empty volume group %s without force=truerR   )r'   r8   rR   r_   ru   vgextendzUnable to extend z by  .vgreducezUnable to reduce )r   dictparamsbooleanr   r>   r   r%   	enumerateospathrealpathexistsr#   rP   	itertoolschainr   r$   r3   rc   rQ   r   	exit_jsonr\   r   setrz   r   )(r'   r8   r`   r   rt   r   	pvoptionsra   r   r   r   r=   present_statepvs_requiredrS   dev_listidxdevtest_devr   pvs_filter_pv_namepvs_filter_vg_name
pvs_filterr!   current_pvsr"   r1   pvused_pvspvcreate_cmdcurrent_devr:   rb   vgremove_cmdcurrent_devsdevs_to_removedevs_to_addrh   vgextend_cmdvgreduce_cmds(                                           r   mainr   |  s   .&51UC03vu53E9>ijFE2FE:FE:!vt<
 dUG,
 !#F( 
t	BMM'"ENN6==12E~~fmmJ78H]]8$Fl+113Il+113INN6==#ABMNN6==#ABM~~fmm4F&GHV+G>>M 4W_LGH}}Ue,-	9: h' .S((-.   	HH77>>(+  %;h%F G	H
 %%eT2!' -"6==3GH- " "1!7!7!;$l&9&9:LN`&abJJ%117NDRcertw2x  |F  3F   GK7!@RSQ ,!$i26
h(>2i=UWXaUbfhUhBiixXY{[aObdlmndopydz!{|#6)T    &22:tD#+ sK%+%7%78RVZ\_`k\lUm8m%nNBsQw"&((-SVa-afhnq(rs  &22:>!'!3!3\NY4NRVX^`bQc4cfn4n!oE37"G$$)Lr)QVX^a$bH     .:&!+u#)#6#6z4#HL%+%7%7yRT8U%VNBsQw(((6((-OSU-V[]cf(g$$)hln)o$ph!BtDGj !BuEG #6b9DWG CF^R"Y-[]J]BGG,,RZ8^L^!#l"3c(m"CDNs8}s</@@AK#!# ". ]#&/vf&M&XQXG(&36&&Q&\U\G	] n$$"G"'-':'::t'L+6 {K-3-?-?QZ@Z^bdghsdt]u@u-vNBs!Qw*. & 0 05[^i5inpvy 0 z{ (.':'::t'L)/););\2<NQ\<\)]E37&*G",,QSUXU]U]^iUj1kprx{,| &'-':'::t'L)/););\9VX<Y\j<j)kE37&*G",,QSUXU]U]^lUm1nsu{~,
W%C jh _s$   *\7\=\\)\ 7$\ __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr   r   ansible.module_utils.basicr   r@   r   r-   r3   r>   rD   r\   rc   rr   rz   r   r   r   __name__ r   r   <module>r      s    A @\|8t  	 4- 	$$7t
:9"(.
g&T zF r   