
    VhV                         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  edddd      Zd Zd	 Zd
 Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
author:
  - Jeroen Hoekx (@jhoekx)
  - Alexander Bulimov (@abulimov)
  - Raoul Baudach (@unkaputtbar112)
  - Ziga Kern (@zigaSRC)
module: lvol
short_description: Configure LVM logical volumes
description:
  - This module creates, removes or resizes logical volumes.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  vg:
    type: str
    required: true
    description:
      - The volume group this logical volume is part of.
  lv:
    type: str
    description:
      - The name of the logical volume.
  size:
    type: str
    description:
      - The size of the logical volume, according to lvcreate(8) C(--size), by default in megabytes or optionally with one
        of [bBsSkKmMgGtTpPeE] units; or according to lvcreate(8) C(--extents) as a percentage of [VG|PVS|FREE|ORIGIN]; Float
        values must begin with a digit.
      - When resizing, apart from specifying an absolute size you may, according to lvextend(8)|lvreduce(8) C(--size), specify
        the amount to extend the logical volume with the prefix V(+) or the amount to reduce the logical volume by with prefix
        V(-).
      - Resizing using V(+) or V(-) was not supported prior to community.general 3.0.0.
      - Please note that when using V(+), V(-), or percentage of FREE, the module is B(not idempotent).
  state:
    type: str
    description:
      - Control if the logical volume exists. If V(present) and the volume does not already exist then the O(size) option
        is required.
    choices: [absent, present]
    default: present
  active:
    description:
      - Whether the volume is active and visible to the host.
    type: bool
    default: true
  force:
    description:
      - Shrink or remove operations of volumes requires this switch. Ensures that filesystems never get corrupted/destroyed
        by mistake.
    type: bool
    default: false
  opts:
    type: str
    description:
      - Free-form options to be passed to the lvcreate command.
  snapshot:
    type: str
    description:
      - The name of a snapshot volume to be configured. When creating a snapshot volume, the O(lv) parameter specifies the
        origin volume.
  pvs:
    type: list
    elements: str
    description:
      - List of physical volumes (for example V(/dev/sda, /dev/sdb)).
  thinpool:
    type: str
    description:
      - The thin pool volume name. When you want to create a thin provisioned volume, specify a thin pool volume name.
  shrink:
    description:
      - Shrink if current size is higher than size requested.
    type: bool
    default: true
  resizefs:
    description:
      - Resize the underlying filesystem together with the logical volume.
      - Supported for C(ext2), C(ext3), C(ext4), C(reiserfs) and C(XFS) filesystems. Attempts to resize other filesystem types
        result in failure.
    type: bool
    default: false
notes:
  - You must specify lv (when managing the state of logical volumes) or thinpool (when managing a thin provisioned volume).
a
  
- name: Create a logical volume of 512m
  community.general.lvol:
    vg: firefly
    lv: test
    size: 512

- name: Create a logical volume of 512m with disks /dev/sda and /dev/sdb
  community.general.lvol:
    vg: firefly
    lv: test
    size: 512
    pvs:
      - /dev/sda
      - /dev/sdb

- name: Create cache pool logical volume
  community.general.lvol:
    vg: firefly
    lv: lvcache
    size: 512m
    opts: --type cache-pool

- name: Create a logical volume of 512g.
  community.general.lvol:
    vg: firefly
    lv: test
    size: 512g

- name: Create a logical volume the size of all remaining space in the volume group
  community.general.lvol:
    vg: firefly
    lv: test
    size: 100%FREE

- name: Create a logical volume with special options
  community.general.lvol:
    vg: firefly
    lv: test
    size: 512g
    opts: -r 16

- name: Extend the logical volume to 1024m.
  community.general.lvol:
    vg: firefly
    lv: test
    size: 1024

- name: Extend the logical volume to consume all remaining space in the volume group
  community.general.lvol:
    vg: firefly
    lv: test
    size: +100%FREE

- name: Extend the logical volume by given space
  community.general.lvol:
    vg: firefly
    lv: test
    size: +512M

- name: Extend the logical volume to take all remaining space of the PVs and resize the underlying filesystem
  community.general.lvol:
    vg: firefly
    lv: test
    size: 100%PVS
    resizefs: true

- name: Resize the logical volume to % of VG
  community.general.lvol:
    vg: firefly
    lv: test
    size: 80%VG
    force: true

- name: Reduce the logical volume to 512m
  community.general.lvol:
    vg: firefly
    lv: test
    size: 512
    force: true

- name: Reduce the logical volume by given space
  community.general.lvol:
    vg: firefly
    lv: test
    size: -512M
    force: true

- name: Set the logical volume to 512m and do not try to shrink if size is lower than current one
  community.general.lvol:
    vg: firefly
    lv: test
    size: 512
    shrink: false

- name: Remove the logical volume.
  community.general.lvol:
    vg: firefly
    lv: test
    state: absent
    force: true

- name: Create a snapshot volume of the test logical volume.
  community.general.lvol:
    vg: firefly
    lv: test
    snapshot: snap1
    size: 100m

- name: Deactivate a logical volume
  community.general.lvol:
    vg: firefly
    lv: test
    active: false

- name: Create a deactivated logical volume
  community.general.lvol:
    vg: firefly
    lv: test
    size: 512g
    active: false

- name: Create a thin pool of 512g
  community.general.lvol:
    vg: firefly
    thinpool: testpool
    size: 512g

- name: Create a thin volume of 128g
  community.general.lvol:
    vg: firefly
    lv: test
    thinpool: testpool
    size: 128g
N)AnsibleModuleC)LANGLC_ALLLC_MESSAGESLC_CTYPEc                 T    dt        |       z  dt        |      z  z   t        |      z   S )Ni@B i  )int)majorminorpatchs      j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/lvol.py	mkversionr      s(    #e*$E
):;c%jHH    c           	      4   g }| j                         D ]  }|j                         j                  d      }|j                  |d   j	                  dd      j	                  dd      t        |d         |d   d   d	k(  |d   d   d
k(  |d   d   dk(  d        |S )N;r   [ ]         atV)namesizeactivethinpoolthinvol)
splitlinesstripsplitappendreplacefloat)datalvslinepartss       r   	parse_lvsr.      s    
C! 

""3'

!H$$S"-55c2>%(OQx{c)q!+as*
 	 Jr   c           
          g }| j                         D ]^  }|j                         j                  d      }|j                  |d   t	        |d         t	        |d         t	        |d         d       ` |S )Nr   r   r   r      )r   r    freeext_size)r$   r%   r&   r'   r)   )r*   vgsr,   r-   s       r   	parse_vgsr4     ss    
C! 

""3'

!H%(O%(OeAh	
 	 Jr   c                    | j                  dd      }| j                  |dg      \  }}}|dk7  ry t        j                  d|      }|sy t	        |j                  d      |j                  d      |j                  d	            S )
NlvmTrequiredversionr   z7LVM version:\s+(\d+)\.(\d+)\.(\d+).*(\d{4}-\d{2}-\d{2})r   r   r0   )get_bin_pathrun_commandresearchr   group)modulever_cmdrcouterrms         r   get_lvm_versionrE     sz    !!%$!7G%%w	&:;LBS	Qw
		LcRAQWWQZQWWQZ88r   c                  z   t        t        t        dd      t        d      t        d      t        d      t        ddddg      t        dd	
      t        dd
      t        dd
      t        d      t        dd      t        dd	
      t        d            dddgf      } t        | _        t	        |       }|| j                  d       t        ddd      }||k\  rdg}ng }| j                  d   }| j                  d   }| j                  d   }t        j                  | j                  d   xs d      }| j                  d   }| j                  | j                  d         }	| j                  | j                  d         }
| j                  | j                  d         }| j                  | j                  d         }| j                  d   }d}d }d }| j                  d!   }| j                  d"   xs g }| j                  rd#g}ng }|r|j                  d$      rd$}|d%d  }n|j                  d&      rd&}|d%d  }d'|v rp|j                  d'd%      }t        |d(         }|d)kD  r| j                  d*       |d%   }|d+k(  r|| j                  d,       n|d-vr| j                  d.       d/}d}d'|vrI|d0   j                         d1v r
|d0   }|d(d0 }	 t        |       |d(   j!                         s
t#               	 |d/k(  rd }n|}| j%                  d3d4      }| j'                  |d5d6d7d8d9|j                         d:d;|g
      \  }}}|d(k7  r3|dk(  r| j)                  d	d<|z  =       n| j                  d<|z  ||>       t+        |      }|d(   }| j%                  d?d4      }| j'                  |d@d5d6d7dAd9|j                         d:d;|g      \  }}}|d(k7  r3|dk(  r| j)                  d	d<|z  =       n| j                  d<|z  ||>       d	} t-        |      }!|rO|!D ].  }"|"dB   |k(  s	|"dB   |k(  s|"d   s|s n-| j                  dC       0 | j                  dD|dE|dF       |}#n6|r2|r-|!D ]  }"|"dB   |k(  s n | j                  dG|dE|dF       |}#n|}#n|}#|!D ]"  }"|"dB   |#|#j/                  dHd%      d0   fv s |"}$ n d }$d}%|$9|dk(  rE|!|d&k(  sdIvr| j                  dJ||dK       |s|r.|s,|!D ]  }"|"dB   |k(  s|"dL   s|s n | j                  dM       | j%                  dNd4      }&|&g|z   |z   }'|%|r|'dO|z  ||gz  }'|'dPdQ|g|z   |dH|gz   z  }'nd|rA|r!|d/k(  r| j                  d	dRS       dT}|'dQ|gz  }'|'dO|z  ||gz  }'|'|dU|dH|gz   z  }'n!|'dQ|gz  }'|'dO|z  ||gz  }'|'||gz   |z   z  }'| j'                  |'      \  }}(}|d(k(  rd} n+| j                  dV|z  ||>       n|dk(  r|	s| j                  dW|$dB   z         | j%                  dXd4      })| j'                  |)g|z   dY|dH|$dB   gz         \  }}(}|d(k(  r| j)                  dZ       n| j                  d[|z  ||>       n|sn|d/k(  rd }*|d\   }+d]k(  s|d^k(  r|d   z  d)z  },n|d\   z  d)z  },|d$k(  r	|,|$d   z  },n|d&k(  r|$d   |,z
  },|,|,|d_   z  z  },|$d   |,k  rO|+d(kD  r |+|,|$d   z
  k\  r| j%                  d`d4      g}*n| j                  da|$dB   db|,|$d   z
  |dc|+|dd	       n^|
r\|$d   |,|d_   z   kD  rN|,d%k  r| j                  de|$dB   z         n0|	s| j                  df|$dB   z         n| j%                  dgd4      dYg}*|*rx|r|*dhgz  }*|*|z   }'|r|'dO|z  |||gz  }'n|'dO|z  ||gz  }'|'|dH|$dB   g|z   z  }'| j'                  |'      \  }}-}di|-v r| j                  dj|dk|||||-l       n|d(k(  rd} dm|$dB   dn|,|}%ndo|v sdo|-v r| j)                  d	||$dB   |$d   p       ndq|v sdq|-v r| j)                  d	||$dB   |$d   dr|s       n| j                  dj|dk||||>       nd }*t        |      |$d   kD  s|d$k(  r| j%                  d`d4      g}*nn|
rt        |      |$d   k  s|d&k(  rVt        |      d(k(  r| j                  de|$dB   z         |	s| j                  dt|$dB   z         n| j%                  dgd4      dYg}*|*r|r|*dhgz  }*|*|z   }'|r|'dO|z  |||gz  }'n|'dO|z  ||gz  }'|'|dH|$dB   g|z   z  }'| j'                  |'      \  }}-}di|-v r| j                  dj|dk|||||-l       nn|d(k(  rd} nfdo|v sdo|-v r| j)                  d	||$dB   |$d   p       nBdq|v sdq|-v r| j)                  d	||$dB   |$d   dr|s       n| j                  dj|dk||||>       |$|rt| j%                  dud4      }.| j'                  |.dv|dH|$dB   g      \  }}(}|d(k(  r$| j)                  |$d    xs | ||$dB   |$d   p       n| j                  dw|z  ||>       nr| j%                  dud4      }.| j'                  |.dx|dH|$dB   g      \  }}(}|d(k(  r#| j)                  |$d   xs | ||$dB   |$d   p       n| j                  dy|z  ||>       | j)                  | |%S       y # t"        $ r | j                  d2|z         Y 7w xY w)zNstrT)typer8   )rH   presentabsent)rH   defaultchoicesboolF)rH   rK   list)rH   elements)vglvr    optsstateforceshrinkr!   snapshotpvsresizefsr"   rQ   r"   )argument_specsupports_check_moderequired_one_ofz Failed to get LVM version number)msgr   c   z--yesrP   r    rR   r   rS   rT   rU   r!   rX   LrD   rV   rW   z--test+r   -%r   d   z*Size percentage cannot be larger than 100%ORIGINz8Percentage of ORIGIN supported only for snapshot volumes)VGPVSFREErc   z5Specify extents as a percentage of VG|PVS|FREE|ORIGINlbskmgtpezBad size specification of '%s'r3   r7   z--noheadingsz
--nosuffixz-oz vg_name,size,free,vg_extent_sizez--unitsz--separatorr   zVolume group %s does not exist.)changedstdout)r\   rA   rC   r+   z-azlv_name,size,lv_attrr   z-Snapshots of thin pool LVs are not supported.zSnapshot origin LV z  does not exist in volume group .zThin pool LV /)rd   re   rf   rc   NzBad size specification of 'z' for creating LVr#   zNo size given.lvcreatez-%sz-sz-nz1Thin volume sizing with percentage not supported.)rj   r\   r   z-Tz#Creating logical volume '%s' failedz:Sorry, no removal of logical volume %s without force=true.lvremovez--force)rj   z"Failed to remove logical volume %sr1   rd   re   r2   lvextendzLogical Volume z4 could not be extended. Not enough free space left (z required / z available)z)Sorry, no shrinking of %s to 0 permitted.z,Sorry, no shrinking of %s without force=truelvreducez
--resizefszReached maximum COW sizezUnable to resize z to )r\   rA   rC   rB   zVolume z resized to zmatches existing size)rj   rP   rQ   r    znot larger than existing sizez+Original size is larger than requested size)rj   rP   rQ   r    r\   rC   z-Sorry, no shrinking of %s without force=true.lvchangez-ayz$Failed to activate logical volume %sz-anz&Failed to deactivate logical volume %s)r   dictLVOL_ENV_VARSrun_command_environ_updaterE   	fail_jsonr   paramsshlexr&   boolean
check_mode
startswithr   lowerr)   isdigit
ValueErrorr:   r;   	exit_jsonr4   r.   rsplit)/r?   version_foundversion_yesoptyesoptrP   rQ   r    rR   rS   rT   rU   r!   rX   r"   size_opt	size_unitsize_operatorrV   rW   test_opt
size_partssize_percent
size_wholeunitvgs_cmdrA   current_vgsrC   r3   this_vglvs_cmdcurrent_lvsrj   r+   test_lvcheck_lvthis_lvr\   lvcreate_cmdcmddummylvremove_cmdtool	size_freesize_requestedrB   lvchange_cmds/                                                  r   mainr   %  sU   .5!5!E9x>STFE2VT2VT2u%&51vu5u%
 !:
!F* )6F% $F+M?@q!R(N&	t	B	t	B== D;;v}}V,23DMM'"ENN6==12E^^FMM(34F^^FMM(34F~~fmmJ78H}}Z(HHIM}}Z(H
--

$"C :??3M8D__S!M8D $;C+Jz!}-Lc!  %Q R#AJX%(*:  %_ `#BB  %\ ]HI d?Bx~~:- H	AbzNdAw($,& ) 3 !!%$!7G!--	.,6XZceieoeoeq  tA  CF  HJ  	KLBS 
QwHU3TWY3YZ!BR!GBTWX
K
 C!fG !!%$!7G!--	$d<RT]_c_i_i_kmz|  BD  	EFBS 
QwHU3TWY3YZ!BR!GBTWXG
K
 C 	hGv"$8(Cz*8$$)X$Y	h _ace!fg	 l6?h.l   ]egi%j kHH 6?xa)@)DEEG
 
CI( C'J>c,c$$_lnr)s$th" ;Gv",1C; $$)9$: "..zD.IL.8+f4C#EH,i.HIICdH-42r8J7KKK3((<o(p"HD":%C(D)*DEEttH%=>>>bz!(D)*DEEtrd{S((#//4NBsQw  %JR%OTV\_ `H  %aelmset%u v!..zD.IL#//0IYcegnougvXwLx0xyNBsQw   .  %IR%PUW]` a_DIT!Z5%8!-!?#!E!-!?#!E#'&/1#%!(>!A ~
0CCDNv/M	nwv6V(W"//
T/JKD$$$V_~/ORVXacgi %  GFOnwz?R.RR!A%$$)TX_`fXg)h$i$$)W[bci[j)k$l"//
T/JIVD\N*DXo EH,-y.YZZCEH,i.HIIC2wv783>>%11#6C-4$$"dT])^cekntw$x1W"G9@.Z^_C,37NRU7U$$UrgfoT[\bTc$d4;?^be?e$$UrgfoT[\bTc  jW  ]`$  a$$"dT])^cekn$o DT{WV_,0D++J+FGE$K'&/9]c=Q;!#$$)TX_`fXg)h$i$$)X\cdj\k)l$m"//
T/JIVD\N*DXo EH,-y.YZZCEH,i.HIIC2wv783>>%11#6C-4$$"dT])^cekntw$x1W"G,37NRU7U$$UrgfoT[\bTc$d4;?^be?e$$UrgfoT[\bTc  jW  ]`$  a$$"dT])^cekn$o!..zD.IL#//uQSU\]cUdFe0fgNBsQw  wx/@+@*LWRTY`agYhovw}o~   %Kr%RWY_b c!..zD.IL#//uQSU\]cUdFe0fgNBsQw  '(*;*FwBSZ[aSbipqwix y  %MQS%TY[ad e
W#.I  N  %E%L MNs   (l l:9l:__main__)
__future__r   r   r   rH   __metaclass__DOCUMENTATIONEXAMPLESr<   rx   ansible.module_utils.basicr   rs   rt   r   r.   r4   rE   r   __name__ r   r   <module>r      sv    A @XtFP 
  4	I
9~/B
 zF r   