
    Vh;h                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlZd dlZd dlZg dag dat        t        z   g d	z   Zdd
Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
author:
  - Fabrizio Colonna (@ColOfAbRiX)
module: parted
short_description: Configure block device partitions
description:
  - This module allows configuring block device partition using the C(parted) command line tool. For a full description of
    the fields and the options check the GNU parted manual.
requirements:
  - This module requires C(parted) version 1.8.3 and above.
  - Option O(align) (except V(undefined)) requires C(parted) 2.1 or above.
  - If the version of C(parted) is below 3.1, it requires a Linux version running the C(sysfs) file system C(/sys/).
  - Requires the C(resizepart) command when using the O(resize) parameter.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  device:
    description:
      - The block device (disk) where to operate.
      - Regular files can also be partitioned, but it is recommended to create a loopback device using C(losetup) to easily
        access its partitions.
    type: str
    required: true
  align:
    description:
      - Set alignment for newly created partitions. Use V(undefined) for parted default alignment.
    type: str
    choices: [cylinder, minimal, none, optimal, undefined]
    default: optimal
  number:
    description:
      - The partition number being affected.
      - Required when performing any action on the disk, except fetching information.
    type: int
  unit:
    description:
      - Selects the current default unit that Parted will use to display locations and capacities on the disk and to interpret
        those given by the user if they are not suffixed by an unit.
      - When fetching information about a disk, it is recommended to always specify a unit.
    type: str
    choices: [s, B, KB, KiB, MB, MiB, GB, GiB, TB, TiB, '%', cyl, chs, compact]
    default: KiB
  label:
    description:
      - Disk label type or partition table to use.
      - If O(device) already contains a different label, it will be changed to O(label) and any previous partitions will be
        lost.
      - A O(name) must be specified for a V(gpt) partition table.
    type: str
    choices: [aix, amiga, bsd, dvh, gpt, loop, mac, msdos, pc98, sun]
    default: msdos
  part_type:
    description:
      - May be specified only with O(label=msdos) or O(label=dvh).
      - Neither O(part_type) nor O(name) may be used with O(label=sun).
    type: str
    choices: [extended, logical, primary]
    default: primary
  part_start:
    description:
      - Where the partition will start as offset from the beginning of the disk, that is, the "distance" from the start of
        the disk. Negative numbers specify distance from the end of the disk.
      - The distance can be specified with all the units supported by parted (except compat) and it is case sensitive, for
        example V(10GiB), V(15%).
      - Using negative values may require setting of O(fs_type) (see notes).
    type: str
    default: 0%
  part_end:
    description:
      - Where the partition will end as offset from the beginning of the disk, that is, the "distance" from the start of the
        disk. Negative numbers specify distance from the end of the disk.
      - The distance can be specified with all the units supported by parted (except compat) and it is case sensitive, for
        example V(10GiB), V(15%).
    type: str
    default: 100%
  name:
    description:
      - Sets the name for the partition number (GPT, Mac, MIPS and PC98 only).
    type: str
  flags:
    description: A list of the flags that has to be set on the partition.
    type: list
    elements: str
  state:
    description:
      - Whether to create or delete a partition.
      - If set to V(info) the module will only return the device information.
    type: str
    choices: [absent, present, info]
    default: info
  fs_type:
    description:
      - If specified and the partition does not exist, will set filesystem type to given partition.
      - Parameter optional, but see notes below about negative O(part_start) values.
    type: str
    version_added: '0.2.0'
  resize:
    description:
      - Call C(resizepart) on existing partitions to match the size specified by O(part_end).
    type: bool
    default: false
    version_added: '1.3.0'

notes:
  - When fetching information about a new disk and when the version of parted installed on the system is before version 3.1,
    the module queries the kernel through C(/sys/) to obtain disk information. In this case the units CHS and CYL are not
    supported.
  - Negative O(part_start) start values were rejected if O(fs_type) was not given. This bug was fixed in parted 3.2.153. If
    you want to use negative O(part_start), specify O(fs_type) as well or make sure your system contains newer parted.
aq  
partition_info:
  description: Current partition information.
  returned: success
  type: complex
  contains:
    disk:
      description: Generic device information.
      type: dict
    partitions:
      description: List of device partitions.
      type: list
    script:
      description: Parted script executed by module.
      type: str
  sample:
    "disk":
      "dev": "/dev/sdb"
      "logical_block": 512
      "model": "VMware Virtual disk"
      "physical_block": 512
      "size": 5.0
      "table": "msdos"
      "unit": "gib"
    "partitions":
      - "begin": 0.0
        "end": 1.0
        "flags": ["boot", "lvm"]
        "fstype": ""
        "name": ""
        "num": 1
        "size": 1.0
      - "begin": 1.0
        "end": 5.0
        "flags": []
        "fstype": ""
        "name": ""
        "num": 2
        "size": 4.0
    "script": "unit KiB print "
ad  
- name: Create a new ext4 primary partition
  community.general.parted:
    device: /dev/sdb
    number: 1
    state: present
    fs_type: ext4

- name: Remove partition number 1
  community.general.parted:
    device: /dev/sdb
    number: 1
    state: absent

- name: Create a new primary partition with a size of 1GiB
  community.general.parted:
    device: /dev/sdb
    number: 1
    state: present
    part_end: 1GiB

- name: Create a new primary partition for LVM
  community.general.parted:
    device: /dev/sdb
    number: 2
    flags: [lvm]
    state: present
    part_start: 1GiB

- name: Create a new primary partition with a size of 1GiB at disk's end
  community.general.parted:
    device: /dev/sdb
    number: 3
    state: present
    fs_type: ext3
    part_start: -1GiB

# Example on how to read info and reuse it in subsequent task
- name: Read device information (always use unit when probing)
  community.general.parted: device=/dev/sdb unit=MiB
  register: sdb_info

- name: Remove all partitions from disk
  community.general.parted:
    device: /dev/sdb
    number: '{{ item.num }}'
    state: absent
  loop: '{{ sdb_info.partitions }}'

- name: Extend an existing partition to fill all available space
  community.general.parted:
    device: /dev/sdb
    number: "{{ sdb_info.partitions | length }}"
    part_end: "100%"
    resize: true
    state: present
)AnsibleModuleN)BKBMBGBTB)KiBMiBGiBTiB)s%cylchscompactc                    t        j                  d|       }|t        j                  d|       }|t        j                  d| z         t	        |j                  d            t	        |j                  d            t	        |j                  d            d}d	}||fS |j                  d      |j                  d      }t        |j                  d            }||fS )
zC
    Parses a string containing a size or boundary information
    z^(-?[\d.]+) *([\w%]+)?$z^(\d+),(\d+),(\d+)$z+Error interpreting parted size output: '%s'msg         )cylinderheadsectorr   )researchmodule	fail_jsonintgroupfloat)size_strunitmatchessizes       l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/parted.py
parse_unitr*      s     ii2H=G))2H=?AHL  
 GMM!,-a()'--*+

  : =='==#DW]]1%&:    c                     | j                  d      D cg c]  }|j                         dk7  s| }}|d   j                  d      j                  d      }t        |d   |      \  }}|d   ||j	                         |d   |d   t        |d	         t        |d
         d}|dv rv|d   j                  d      j                  d      }t        |d	         \  }}	t        |d         t        |d         t        |d         ||	j	                         d|d<   |dd }g }
|dd D ]  }|j                  d      j                  d      }|dk7  r!t        |d	         d   }|d
   }|d   }|d   }nd}|d	   }|d
   }|d   }|
j                  t        |d         t        |d         d   t        |d         d   ||||j                  d      D cg c]  }|dk7  s	|j                          c}|j	                         d        ||
dS c c}w c c}w )a  
    Parses the output of parted and transforms the data into
    a dictionary.

    Parted Machine Parseable Output:
    See: https://lists.alioth.debian.org/pipermail/parted-devel/2006-December/00
    0573.html
     - All lines end with a semicolon (;)
     - The first line indicates the units in which the output is expressed.
       CHS, CYL and BYT stands for CHS, Cylinder and Bytes respectively.
     - The second line is made of disk information in the following format:
       "path":"size":"transport-type":"logical-sector-size":"physical-sector-siz
       e":"partition-table-type":"model-name";
     - If the first line was either CYL or CHS, the next line will contain
       information on no. of cylinders, heads, sectors and cylinder size.
     - Partition information begins from the next line. This is of the format:
       (for BYT)
       "number":"begin":"end":"size":"filesystem-type":"partition-name":"flags-s
       et";
       (for CHS/CYL)
       "number":"begin":"end":"filesystem-type":"partition-name":"flags-set";
    
 r   ;:r         r      )devr(   r&   tablemodellogical_blockphysical_block)r   r   r   )	cylindersheadssectorscyl_sizecyl_size_unitchs_infoNr   z, )numbeginendr(   fstypenameflagsr&   generic
partitions)splitstriprstripr*   lowerr"   append)parted_outputr&   xlinesgeneric_paramsr(   rF   r>   r<   cyl_unitpartslinepart_paramsrB   rC   rD   fs                    r)   parse_partition_inforV     sJ   . &++D1E1QWWY"_QEEE 1X__S)//4N N1-t4JD$ a 

""^A./nQ/0G ~8??3'--c2'4(Xa[)!%8A;' %^^-

 ab	Eab	 kk#&,,S1 5=k!n-a0D ^Fq>DNE D ^Fq>DNE{1~&A/2k!n-a0).T):FAa2gaggiFJJL	
 		'< e44} Ft Gs   HH

HHc                    |j                         }| dk(  ry|dv rKt        dt        t        j                  |       dz
  dz              }d}|t        t              k  r	t        |   }d}|t        v rdt        j                  |      z  }n |t        v rdt        j                  |      z  }| |z  dz  }|d	k  r|d
z   }n|dk  r|dz   }n|dz   }|d	k  rd}n
|dk  rd}nd}t        ||      |fS )a  
    Formats a size in bytes into a different unit, like parted does. It doesn't
    manage CYL and CHS formats, though.
    This function has been adapted from https://github.com/Distrotech/parted/blo
    b/279d9d869ff472c52b9ec2e180d568f0c99e30b0/libparted/unit.c
    r   )g        br.   r   r   r         ?g      @rX        @@      @
   g{Gzt?d   g?g      ?r   r   )
rK   maxr"   mathlog10lenunits_siindex	units_iecround)
size_bytesr&   rd   
multiplieroutputw	precisions          r)   format_disk_sizerl   g  s    ::<D Q ,,AsZZ
#c)S0
  3x= E?D Jxx~~d33
		yt44
:%3F {UN	#TMSL2v		
S		 #T))r+   c                     t        |       }d}|t        v rdt        j                  |      z  }n@|t        v rdt        j                  |      dz   z  }n|dv rdt        j                  d      z  }||z  }t	        |      S )NrZ   r[   r\   r   rY   r	   )r$   rc   rd   re   r"   )r%   r&   r(   rh   ri   s        r)   convert_to_bytesrn     s{    ?DJxx~~d33
			 5 9:
	.	.x~~d33
JFv;r+   c           	      ^   t         j                  j                  |       }d|z  }t        |dz   d      }t        |dz   d      }t	        t        |dz   d            }t	        t        |dz   d            }t	        t        |d	z   d            |z  }t        ||      \  }	}| d
|	||||d|dg dS )z
    Fetches device information directly from the kernel and it is used when
    parted cannot work because of a missing label. It always returns a 'unknown'
    label.
    z/sys/block/%sz/device/vendorUnknownz/device/modelr6   z/queue/logical_block_sizer   z/queue/physical_block_sizez/sizeunknown )r4   r5   r(   r&   r7   r8   r6   rE   )ospathbasenameread_recordr"   rl   )
devicer&   device_namebasevendorr6   logic_block
phys_blockrg   r(   s
             r)   get_unlabeled_device_infor}     s     ''""6*K[(D 00)<F.8Ek$)D"DaHIK[(D!DaHIJ[34{BJ!*d3JD$ (( &.
  r+   c                    t        |       }|rt        | |      S t        dd| dd|dg}t        j	                  |      \  }}}|dk7  r/d|vr+t        j                  dd	j                  |      z  |||
       t        ||      S )z^
    Fetches information about a disk and its partitions and it returns a
    dictionary.
    -s-mz--r&   printr   unrecognised disk labelz?Error while getting device information with parted script: '%s'rr   r   rcouterr)check_parted_labelr}   parted_execr    run_commandr!   joinrV   )rw   r&   label_neededcommandr   r   r   s          r)   get_device_infor     s     &f-L(66D$fdGLG%%g.LBS	Qw,C7 XXg./s 	 	
  T**r+   c                     t               \  }}}|dk(  r|dk\  s|dkD  ryt        j                  t        dd| dg      \  }}}|dk7  rd|j	                         v ry	y)
z
    Determines if parted needs a label to complete its duties. Versions prior
    to 3.1 don't return data when there is no label. For more information see:
    http://upstream.rosalinux.ru/changelogs/libparted/3.1/changelog.html
    r   r   Fr   r   r   r   r   T)parted_versionr    r   r   rK   )rw   parted_majorparted_minordummyr   r   r   s          r)   r   r     sj     )7(8%L,la/L14D %%{D$&PQLBS	Qw,		;r+   c                    | j                  d      D cg c]  }|j                         dk7  s| }}t        |      dk(  ryt        j                  d|d   j                               }|yt        |j                  d            }t        |j                  d            }d}|j                  d      t        |j                  d            }|||fS c c}w )	zM
    Returns version tuple from the output of "parted --version" command
    r-   r.   r   )NNNz#^parted.+\s(\d+)\.(\d+)(?:\.(\d+))?r   r   r   )rH   rI   rb   r   r   r"   r#   )r   rN   rO   r'   majorminorrevs          r)   parse_parted_versionr     s     		$;11779?Q;E;
5zQ ii>a@PQG a !Ea !E
C}}Q#'--"#%) <s
   CCc                      t         j                  t        dg      \  } }}| dk7  rt         j                  d| ||       t	        |      \  }}}|t         j                  dd|       |||fS )zP
    Returns the major and minor version of parted installed on the system.
    z	--versionr   zFailed to get parted version.r   )r   r   r   )r    r   r   r!   r   )r   r   r   r   r   r   s         r)   r   r     s}     %%{K&@ALBS	Qw/BCS 	 	
 /s3UE3}<L%r+   c           	      $   d|z  }|dk(  rd}	 t               dk\  rd}nd}| rot        j                  s^t        d|d|d|d	| 	}t        j	                  |      \  }}}|d
k7  r+t        j                  d|j                         z  |||       yyyy)z
    Runs a parted script.
    z-a %s	undefinedr.   )r   r3   @   z-s -fr   rr   z -m z -- r   z%Error while running parted script: %sr   N)r   r    
check_moder   r   r!   rI   )	scriptrw   alignalign_optionscript_optionr   r   r   r   s	            r)   partedr   &  s     U?L
 :%f'',7V\^de))'2C7;gmmoM3C    (vr+   c                     	 t        | d      5 }|j                         j                         cddd       S # 1 sw Y   yxY w# t        $ r |cY S w xY w)z8
    Reads the first line of a file and returns it.
    rN)openreadlinerI   IOError)	file_pathdefaultrU   s      r)   rv   rv   F  sM    )S! 	(Q::<%%'	( 	( 	( s(   A 6	A ?A A AAc                 0    t        fd| D              S )zf
    Looks if a partition that has a specific value for a specific attribute
    actually exists.
    c              3   <   K   | ]  }|   xr |   k(    y wN ).0part	attributenumbers     r)   	<genexpr>zpart_exists.<locals>.<genexpr>V  s3      &* 	Y 	"Y6!	"s   )any)rG   r   r   s    ``r)   part_existsr   Q  s    
  .8  r+   c                 .    t        |       \  }}|t        v S )z7
    Checks if the input string is an allowed size
    )r*   parted_units)r%   r(   r&   s      r)   check_size_formatr   \  s     H%JD$<r+   c                  v
   d} d}d}t        t        t        dd      t        ddg d      t        d	
      t        ddt              t        ddg d      t        ddg d      t        dd      t        dd      t        d
      t        d
      t        dd      t        ddg d      t        dd            dddggdddgggd      adddddt        _        t        j
                  d    }t        j
                  d!   }t        j
                  d   }t        j
                  d"   }t        j
                  d#   }t        j
                  d$   }t        j
                  d%   }	t        j
                  d&   }
t        j
                  d'   }t        j
                  d   }t        j
                  d(   }t        j
                  d)   }t        j
                  d*   }t        j                  d+d      a||d,k  rt        j                  d-.       t        |	      s t        j                  d/t        |	      0       t        |
      s t        j                  d1t        |
      0       t        ||      }|d2   }|dk(  r8|d3   j                  d4d       |k7  }|r|d5|z  z  }|r'|st        |d6|      s|d7|d8|d9|z  nd|	d8|
d8z  }|r
|rd:|d8|}|rt        |d6|      r~|D cg c]  }|d6   |k(  s| c}d;   }t        |d<   |      }t        |
|      \  }}|d=k(  r"t        t        |d3   d>         |z  d?z        }|}t        ||      }||k7  r|d@|d8|
d8z  }|r5||z  }t!        |||       d} d}t        j"                  st        ||      d2   }t        |d6|      st        j"                  r| rt        j"                  rd(g i}n|D cg c]  }|d6   |k(  s| c}d;   }|!|j                  d'd       |k7  r|dA|dB|dCz  }|rdD|v rdE|vr|j%                  dE       t'        t)        |d(         t)        |      z
        }t'        t)        |      t)        |d(         z
        }|D ]  }|dF|d8|dGz  } |D ]  }|dF|d8|dHz  } |r
|rd:|d8|}|r[||z  }d} t!        |||       nF|dk(  r7t        |d6|      st        j"                  r$dI|z  }||z  }d} t!        |||       n
|dk(  rdJ|z  }t        ||      }t        j+                  | |d3   |d2   |j-                         K       y c c}w c c}w )LNFr.   strT)typerequiredoptimal)r   minimalnoner   r   )r   r   choicesr"   )r   r   msdos)
aixamigabsddvhgptloopmacr   pc98sunprimary)extendedlogicalr   z0%)r   r   z100%list)r   elementsinfo)absentr   presentbool)rw   r   r   r&   label	part_type
part_startpart_endfs_typerC   rD   stateresizer   r   r   r   )argument_specrequired_ifsupports_check_modeC)LANGLC_ALLLC_MESSAGESLC_CTYPErw   r   r&   r   r   r   r   rC   rD   r   r   r   r   z,The partition number must be greater then 0.r   zZThe argument 'part_start' doesn't respect required format.The size unit is case sensitive.)r   r   zXThe argument 'part_end' doesn't respect required format.The size unit is case sensitive.rG   rF   r5   zmklabel %s r?   zmkpart rr   z%s zunit r   rA   r   r(   r^   zresizepart zname z '"z"' espbootzset z on z off zrm %s zunit '%s' print )changeddiskrG   r   )r   dictr   r    run_command_environ_updateparamsget_bin_pathr   r!   r   r*   r   getr   rn   r"   r   r   rL   r   set	exit_jsonrI   )r   output_scriptr   rw   r   r   r&   r   r   r   r   rC   r   rD   r   r   current_devicecurrent_partsmklabel_neededp	partitioncurrent_part_endr(   parsed_unitdesired_part_end	flags_offflags_onrU   final_device_statuss                                r)   mainr   d  s    GMFUT2E9>uvU# 5%F E7  =I  J yBde5uf5e$ 5! FU3 E6;XY VU37
< i(,h
+
 !E#FH 25TWeh(iF% ]]8$FMM'"E]]8$F== DMM'"Ek*I|,J}}Z(H== DMM'"EMM'"EmmI&G]]8$F %%h5K fqjKLZ(3:& 	 	

 X&38$ 	 	
 %VT2N"<0M	 (	266wENme++F .M5RX0Y#*#6B>	 F F%)62F k-?$1HqQuX5GHKI/	%0@$G *8T :D+c!Cy 9& ABTISPQ"/kB#33  V#M665)GF$$ / =l K}eV48I8I6,,$bM	(5L15V9KQLQO	 IMM&$$?4$G >>  E>fE&9LL( !Yw%7!83u:!EF	E
S71C-D DE! <A;;F< # =A&!<<F= F%)62F V#MG665)	(	}eV48I8I&FV#MG665)	&*T1 *&$7
 +&|4""$	  a I> Ms   #T11T1T6T6__main__)r.   r   ) 
__future__r   r   r   r   __metaclass__DOCUMENTATIONRETURNEXAMPLESansible.module_utils.basicr   r`   r   rs   rc   re   r   r*   rV   rl   rn   r}   r   r   r   r   r   rv   r   r   r   __name__r   r+   r)   <module>r     s    A @rh(
T8v 5  	 	 )(	)#&II<U5p1*h>+4*6&@ ~B zF r+   