o
    x[h€*  ã                
   @   sr  U d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ d
Zdegeg dœZeed< e e¡Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#defdefd efd!efd"e fd#e!fd$e"fgZ$d!e#iZ%d%ee& fd&d'„Z'd(d)„ Z(d*d+„ Z)d,e&d-ed.ed/e*d%df
d0d1„Z+d2d3„ Z,dS )4z:Resizefs: cloud-config module which resizes the filesystemé    N)ÚOptional)Ú	lifecycleÚsubpÚutil)ÚCloud)ÚConfig)Ú
MetaSchema)ÚALL_DISTROS)Ú
PER_ALWAYSÚnoblockÚcc_resizefs)ÚidÚdistrosÚ	frequencyÚactivate_by_schema_keysÚmetac                 C   s¢   t  | ¡stj d|  ¡rddddd|  g}ndddd| g}tj d¡}tj t ddg¡j	 
d¡d	  
d
¡d  ¡ ¡}||krM| d¡}| |d d¡ t|ƒS )Nz%s/.snapshotsÚbtrfsÚ
filesystemÚresizeÚmaxz5.10z	--versionÚ
r   Úvéÿÿÿÿé   z	--enqueue)r   Úmount_is_read_writeÚosÚpathÚisdirr   ÚVersionÚfrom_strr   ÚstdoutÚsplitÚstripÚindexÚinsertÚtuple)Úmount_pointÚdevpthÚcmdÚbtrfs_with_queueÚsystem_btrfs_verÚidx© r,   ú>/usr/lib/python3/dist-packages/cloudinit/config/cc_resizefs.pyÚ_resize_btrfs%   s.   ÿû
ÿþü
r.   c                 C   s   d|fS )NÚ	resize2fsr,   ©r&   r'   r,   r,   r-   Ú_resize_extI   ó   r1   c                 C   s   d| fS )NÚ
xfs_growfsr,   r0   r,   r,   r-   Ú_resize_xfsM   r2   r4   c                 C   ó
   dd| fS )NÚgrowfsz-yr,   r0   r,   r,   r-   Ú_resize_ufsQ   ó   
r7   c                 C   s   ddd| |fS )NÚzpoolÚonlinez-er,   r0   r,   r,   r-   Ú_resize_zfsU   s   r;   c                 C   r5   )NÚhammer2r6   r,   r0   r,   r,   r-   Ú_resize_hammer2Y   r8   r=   c                 C   s   ddd|fS )zSingle device resizeÚbcachefsÚdevicer   r,   r0   r,   r,   r-   Ú_resize_bcachefs]   s   r@   c              
   C   sb   d}d}zt   dd|g¡ W dS  t jy0 } z|j |¡r*||jv r*W Y d }~dS |‚d }~ww )Nzgrowfs: requested sizez.is not larger than the current filesystem sizer6   z-NTF)r   ÚProcessExecutionErrorÚstderrÚ
startswith)r&   r'   Ú
skip_startÚskip_containÚer,   r,   r-   Ú_can_skip_resize_ufsb   s   ú€ûrG   r   ÚextÚxfsÚufsÚzfsr<   r>   Úreturnc              
   C   sø   t  ¡ rtjntj}tj d¡st d¡ d S zt dd| g¡\}}|r.t 	d|| ¡ W d S W n tj
yI } z|d| |ƒ W Y d }~d S d }~ww d}| d¡D ]!}t ||¡rr| |vrrd	|vrr| ¡ d
 }t d| |¡ |  S qQ|d| ||ƒ d S )Nz/dev/zfsz"Cannot get zpool info, no /dev/zfsr9   Ústatusz0zpool status returned error: [%s] for zpool [%s]z$Unable to get zpool status of %s: %sz.*(ONLINE).*r   Ústater   zfound zpool "%s" on disk %sz'No zpool found: [%s]: out: [%s] err: %s)r   Úis_containerÚLOGÚdebugÚwarningr   r   Úexistsr   ÚinforA   r!   ÚreÚsearch)r9   Úlog_warnÚzpoolstatusÚerrÚrÚlineÚdiskr,   r,   r-   Úget_device_info_from_zpool…   s<   
ýú€þ€ÿr]   c                 C   s6   |   ¡ }t ¡ D ]\}}| |¡r|||ƒ  S qdS )NF)ÚlowerÚRESIZE_FS_PRECHECK_CMDSÚitemsrC   )Úfs_typeÚresize_whatr'   Ú	fstype_lcÚiÚfuncr,   r,   r-   Úcan_skip_resize£   s   
ÿrf   c              
   C   sr  t  ¡ }| dkr(tj | ¡s(|s(t  t  ¡ ¡} | du r"t d¡ dS t 	d| ¡ | dkr5t 	d| |¡ dS |  
d¡rAt 	d¡ | S |  
d	¡s\tj | ¡s\d	|  d
¡ }t 	d| |¡ |} zt | ¡}W n3 ty– } z'|r{|jtjkr{t 	d| |¡ n|jtjkr‰t d| |¡ n|‚W Y d}~dS d}~ww t |j¡s·t |j¡s·|r®t 	d| |¡ dS t d| |¡ dS | S )a¬  Return updated devpath if the devpath is a writable block device.

    @param devpath: Requested path to the root device we want to resize.
    @param info: String representing information about the requested device.
    @param log: Logger to which logs will be added upon error.

    @returns devpath or updated devpath per kernel command line if the device
        path is a writable block device, returns None otherwise.
    z	/dev/rootNz!Unable to find device '/dev/root'z.Converted /dev/root to '%s' per kernel cmdlineÚoverlayrootz)Not attempting to resize devpath '%s': %szgpt/z#We have a gpt label - just go aheadz/dev/ú/z:'%s' doesn't appear to be a valid device path. Trying '%s'z9Device '%s' did not exist in container. cannot resize: %sz,Device '%s' did not exist. cannot resize: %sz>device '%s' not a block device in container. cannot resize: %sz1device '%s' not a block device. cannot resize: %s)r   rO   r   r   rS   Úrootdev_from_cmdlineÚget_cmdlinerP   rR   rQ   rC   ÚlstripÚstatÚOSErrorÚerrnoÚENOENTÚS_ISBLKÚst_modeÚS_ISCHR)ÚdevpathrT   Ú	containerÚfulldevpathÚstatretÚexcr,   r,   r-   Úmaybe_get_writable_device_path«   sr   

ÿþ


ýýÿ€óüûýrx   ÚnameÚcfgÚcloudÚargsc                 C   s‚  |r|d }nt  |dd¡}t j|tgd�st d| ¡ d S d}t  |t¡}|s0t d|¡ d S |\}}}	|dkrJ| d¡d }
t	|
ƒ}|sHd S |
}d	||	|f }t d
|¡ t
||ƒ}|s`d S d }t|||ƒrqt d||¡ d S | ¡ }tD ]\}}| |¡r„|} nqw|s�t d||¡ d S |||ƒ}t d||d |¡¡ |tkr«t  t|¡ nt|ƒ d}|tkr·d}t d|||¡ d S )Nr   Úresize_rootfsT)Úaddonsz+Skipping module named %s, resizing disabledrh   z)Could not determine filesystem type of %srK   zdev=%s mnt_point=%s path=%szresize_info: %sz%Skip resize filesystem type %s for %sz.Not resizing unknown filesystem type %s for %szResizing %s (%s) using %sú ÚResizedzResizing (via forking)z$%s root filesystem (type=%s, val=%s))r   Úget_cfg_option_strÚtranslate_boolÚNOBLOCKrP   rQ   Úget_mount_inforR   r!   r]   rx   rf   r^   ÚRESIZE_FS_PREFIXES_CMDSrC   ÚjoinÚfork_cbÚ	do_resize)ry   rz   r{   r|   Úresize_rootrb   Úresultr'   ra   r&   r9   rT   Úresizerrc   ÚpfixÚroot_cmdÚ
resize_cmdÚactionr,   r,   r-   Úhandleû   st   


ÿ
þý
ÿþÿr�   c                 C   s2   zt   | ¡ W d S  t jy   t td| ¡ ‚ w )Nz$Failed to resize filesystem (cmd=%s))r   rA   r   ÚlogexcrP   )rŽ   r,   r,   r-   rˆ   K  s   þrˆ   )-Ú__doc__rn   Úloggingr   rU   rl   Útypingr   Ú	cloudinitr   r   r   Úcloudinit.cloudr   Úcloudinit.configr   Úcloudinit.config.schemar   Úcloudinit.distrosr	   Úcloudinit.settingsr
   rƒ   r   Ú__annotations__Ú	getLoggerÚ__name__rP   r.   r1   r4   r7   r;   r=   r@   rG   r…   r_   Ústrr]   rf   rx   Úlistr�   rˆ   r,   r,   r,   r-   Ú<module>   sT   ü
$ù
PP