name : cc_growpart.cpython-39.pyc
a

'�DgyI�@s(UdZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZddlm
Z
ddlmZddlmZmZddlmZmZmZmZmZddlmZdd	lmZdd
lmZddlm Z m!Z!ddl"m#Z#e#Z$d
e ge$gd�Z%ee&d<ddgdd�Z'ed�Z(Gdd�d�Z)e�*e+�Z,Gdd�de-�Z.Gdd�de
�Z/Gdd�de/�Z0Gdd�de/�Z1Gdd �d e/�Z2e3e!e4e/d!�d"d#�Z5ee6d$�d%d&�Z7ee6d$�d'd(�Z8d)d*�Z9e:d$�d+d,�Z;d-d.�Z<ee3e3fd$�d/d0�Z=d1d2�Z>e/e!d3�d4d5�Z?e3eee4dd6�d7d8�Z@d9e0fd:e1fd;e2ffZAdS)<zGrowpart: Grow partitions�N)�ABC�abstractmethod)�suppress)�Path)�Optional�Tuple)�	lifecycle�performance�subp�
temp_utils�util)�Cloud)�Config)�
MetaSchema)�ALL_DISTROS�Distro)�
PER_ALWAYSZcc_growpart)�idZdistros�	frequencyZactivate_by_schema_keys�meta�auto�/F)�mode�devices�ignore_growroot_disabledz/cc_growpart_keydatac@seZdZdZdZdZdZdS)�RESIZE�SKIPPED�CHANGED�NOCHANGE�FAILEDN)�__name__�
__module__�__qualname__rrrr�r#r#�@/usr/lib/python3.9/site-packages/cloudinit/config/cc_growpart.pyr/src@seZdZdS)�ResizeFailedExceptionN)r r!r"r#r#r#r$r%9sr%c@s:eZdZed�dd�Zeeed�dd��Zedd��Z	d	S)
�Resizer)�distrocCs
||_dS�N)�_distro)�selfr'r#r#r$�__init__>szResizer.__init__)r�returncCsdSr(r#�r*rr#r#r$�	availableAszResizer.availablecCsdSr(r#)r*�diskdev�partnum�partdev�fsr#r#r$�resizeDszResizer.resizeN)
r r!r"rr+r�list�boolr.r3r#r#r#r$r&=s
r&c@s"eZdZed�dd�Zdd�ZdS)�ResizeGrowPart�rcCsHz.tjddgddid�j}t�d|�r,WdSWntjyBYn0dS)	N�growpartz--help�LANG�C��
update_envz--update\s+TF)r
�stdout�re�search�ProcessExecutionError)r*r�outr#r#r$r.Is�
zResizeGrowPart.availablecCs\t||�}|j��}tj|dd���}tj�|d�}d|d�}	tj�|�sVt�	|d�zt
j
dd||g|	d�Wn`t
jy�}
zF|
jd	kr�t
�td
||�t|
�|
�||fWYd}
~
Wd�Sd}
~
00zt
j
d||g|	d�WnBt
j�y,}
z&t
�td||�t|
�|
�WYd}
~
n
d}
~
00Wd�n1�sD0Y|t||�fS)NT)�dirZ	needs_exer8r:)r9ZTMPDIRi�z	--dry-runr;�z&Failed growpart --dry-run for (%s, %s)zFailed: growpart %s %s)�get_sizer)Zget_tmp_exec_pathrZtempdir�os�path�join�exists�mkdirr
r@�	exit_coder�logexc�LOGr%)r*r/r0r1r2�beforeZtmp_dirZtmpdZgrowpart_tmpZmy_env�er#r#r$r3Us8



�

�
,@zResizeGrowPart.resizeN�r r!r"r4r.r3r#r#r#r$r6Hsr6c@s&eZdZdZed�dd�Zdd�ZdS)�ResizeGrowFSa�
    Use FreeBSD ``growfs`` service to grow root partition to fill available
    space, optionally adding a swap partition at the end.

    Note that the service file warns us that it uses ``awk(1)``, and as
    such requires ``/usr`` to be present. However, cloud-init is installed
    into ``/usr/local``, so we should be fine.

    We invoke the ``growfs`` with ``service growfs onestart``, so it
    doesn't need to be enabled in ``rc.conf``.
    r7cCstj�d�o|dgkS)z'growfs only works on the root partitionz/etc/rc.d/growfsr)rErF�isfiler-r#r#r$r.�szResizeGrowFS.availablec
Csjt||�}z|jjddd�Wn<tjyZ}z"t�td�t|�|�WYd}~n
d}~00|t||�fS)NZonestart�growfs)�actionZservicezFailed: service growfs onestart)	rDr)Zmanage_servicer
r@rrKrLr%)r*r/r0r1r2rMrNr#r#r$r3�s
 zResizeGrowFS.resizeN)r r!r"�__doc__r4r.r3r#r#r#r$rPysrPc@s"eZdZed�dd�Zdd�ZdS)�ResizeGpartr7cCsNz4tjddgddiddgd�j}t�d|�r2Wd	SWntjyHYn0d
S)N�gpart�helpr9r:rrC)r<Zrcszgpart recover TF)r
�stderrr>r?r@)r*r�errr#r#r$r.�s�
zResizeGpart.availablec
Cs�zt�dd|g�WnHtjy\}z.|jdkrHt�td|�t|�|�WYd}~n
d}~00t||�}zt�ddd||g�Wn@tjy�}z&t�td||�t|�|�WYd}~n
d}~00|t||�fS)	a9
        GPT disks store metadata at the beginning (primary) and at the
        end (secondary) of the disk. When launching an image with a
        larger disk compared to the original image, the secondary copy
        is lost. Thus, the metadata will be marked CORRUPT, and need to
        be recovered.
        rVZrecoverrzFailed: gpart recover %sNr3z-izFailed: gpart resize -i %s %s)r
r@rJrrKrLr%rD)r*r/r0r1r2rNrMr#r#r$r3�s
 
 zResizeGpart.resizeNrOr#r#r#r$rU�srU)rr'rr,cCs�d}|dkrDtD]$\}}||�}|j|d�r|}q6q|s�td��nZi}tD]\}}	|	||<qL||vrrtd|��|||�}
|
j|d�r�|
}|s�td|��|S)Nrr7zNo resizers availablezunknown resize mode %szmode %s not available)�RESIZERSr.�
ValueError�	TypeError)rr'rZresize_class�_name�resizerZcurZmmap�k�vZmclassr#r#r$�resizer_factory�s(

ra)r,c	Cs�d}z�z0t�|tj�}t�|dtj�WW|r6t�|�Sty~|dkrht|�YW|rft�|�SYW|rzt�|�dS0W|r�t�|�n|r�t�|�0dS)Nr�zfs)rE�open�O_RDONLY�lseek�SEEK_END�close�FileNotFoundError�get_zfs_size)�filenamer2�fdr#r#r$rD�s*
�
�
��rDc
Csn|�d�d}zt�dddd|g�\}}Wn6tjy`}zt�d||�WYd}~dSd}~00t|���S)Nrr�zpool�getz	-Hpovalue�sizezFailed: zpool get size %s: %s)�splitr
r@rL�debug�int�strip)Zdatasetrlrn�_rNr#r#r$ri�sricCs�|�d�r|dfSt�|�}|s(td��|d}|d}t��}|dkr~|s~t�t���}|dur~tj�	|�rv|dfStd��||fS)N�/dev/z,Could not determine device of '%s' % dev_entrrCz	/dev/rootz!Unable to find device '/dev/root')
�
startswithrZget_mount_infor[Zis_containerZrootdev_from_cmdlineZget_cmdlinerErFrH)�devent�result�devr2�	containerr#r#r$�
devent2dev�s

rzc
Cs�t�d�st�d�dSzt�dd|g�WnNtjyz}z4|jdkrVt�d|�nt�d|j�WYd}~dSd}~00ttj��.t�dd	|g�t�d
|�Wd�dS1s�0YdS)z�
    Check if a device is an encrypted device. blockdev should have
    a /dev/dm-* path whereas partition is something like /dev/sda1.
    �
cryptsetupz6cryptsetup not found. Assuming no encrypted partitionsF�status�z#Determined that %s is not encryptedzZReceived unexpected exit code %s from cryptsetup status. Assuming no encrypted partitions.NZisLukszDetermined that %s is encryptedT)r
�whichrLrpr@rJ�warningr)�blockdev�	partitionrNr#r#r$�is_encrypted	s$


� r�c
Cs�ddd|g}t�|�d}|�d�s2td|����z d|�d�d	�d
�d��WSty�}z$td|�d|�d
��|�WYd}~n
d}~00dS)NZdmsetupZdepsz--options=devnamerz1 dependz5Expecting '1 dependencies' from 'dmsetup'. Received: rtz: (rC�)zRan `z$`, but received unexpected stdout: `�`)r
ru�RuntimeErrorro�
IndexError)r�ZcommandZdeprNr#r#r$�get_underlying_partition$s
� ��r�cCs�t��stjdfSz�t�dt����Bt���}t�|�}Wd�n1sN0YWd�n1sl0Y|d}t	�
|�}|d}Wn.ty�}ztd�|�WYd}~n
d}~00z�t
j
ddd	d
|g|d�Wzt
�
ddd
|t|�g�Wn4t
j�y.}zt�d|�WYd}~n
d}~00zt��Wn t�y\t�td�Yn0n�zt
�
ddd
|t|�g�Wn4t
j�y�}zt�d|�WYd}~n
d}~00zt��Wn t�y�t�td�Yn00tjd|�d�fS)z�Use 'cryptsetup resize' to resize LUKS volume.

    The loaded keyfile is json formatted with 'key' and 'slot' keys.
    key is base64 encoded. Example:
    {"key":"XFmCwX2FHIQp0LBWaLEMiHIyfxt1SGm16VvUAVledlY=","slot":5}
    zNo encryption keyfile foundzReading N�key�slotzZCould not load encryption key. This is expected if the volume has been previously resized.r{z
--key-file�-r3)�dataZluksKillSlotz--batch-modez<Failed to kill luks slot after resizing encrypted volume: %sz8Failed to remove keyfile after resizing encrypted volumez'Successfully resized encrypted volume '�')�KEYDATA_PATHrHrrr	�Timedrc�json�load�base64Z	b64decode�	Exceptionr�r
�strr@rLr�unlinkrrKr)r�r��fZkeydatar�Zdecoded_keyr�rNr#r#r$�resize_encrypted5s�
��F
�����	�����	��
�r�cCsg}z�|�||||�\}}||kr>|�|tjd||ff�n�|dusN|dur�d}	|durp|durpd||f}	nd|}	|�|tj|	f�nFd}	|dur�|dur�d||||f}	nd|||f}	|�|tj|	f�WnBt�y}
z(|�|tjd|||
ff�WYd}
~
n
d}
~
00|S)Nzno change necessary (%s, %s)�z*changed (%s, %s) size, new size is unknownz&changed (%s) size, new size is unknownzchanged (%s, %s) from %s to %szchanged (%s) from %s to %sz'failed to resize: disk=%s, ptnum=%s: %s)r3�appendrrrr%r)r^rv�disk�ptnumr�r2�info�old�new�msgrNr#r#r$�
_call_resizernsJ
������r�)r^r'cCs�t�|�}g}|�r�|�d�}d}d}zt|�\}}WnFty|}	z.|�|tjd|	f�WYd}	~	qWYd}	~	n
d}	~	00t�d|�|dkr�t	|t
�r�|t||||||�7}qzt�
|�}
WnLt�y}	z2|�|tjd||	ff�WYd}	~	qWYd}	~	n
d}	~	00t
�|
j��sDt
�|
j��sD|�|tjd|f�q|�|�}|�rz�t|�}t||��r�|dd�|D�v�r�|�d|�|�d|�Wqt||�\}
}|�||
|f�n|�|tjd	|�d
�f�Wqt�y}	z*|�|tjd|�d|	��f�WYd}	~	qd}	~	00qz|�|�\}}WnPttf�y�}	z2|�|tjd
||	ff�WYd}	~	qWYd}	~	n
d}	~	00|t||||||�7}q|S)Nrzunable to convert to device: %szgrowpart found fs=%srbzstat of '%s' failed: %szdevice '%s' not a block devicecSsg|]}|d�qS)rr#)�.0�xr#r#r$�
<listcomp>��z"resize_devices.<locals>.<listcomp>zResizing mapped device (z!) skipped as it is not encrypted.zResizing encrypted device (z
) failed: zdevice_part_info(%s) failed: %s)�copy�poprzr[r�rrrLrp�
isinstancerPr�rE�stat�OSError�S_ISBLK�st_mode�S_ISCHRZget_mapped_devicer�r��insertr�r�rZdevice_part_infor\)r^rr'r�rvr�r�r�r2rNZstatretZunderlying_blockdevr�r|�messager#r#r$�resize_devices�s�

��"
��"���
��
����	
��"r�)�name�cfg�cloud�argsr,c

Cs�d|vrt�dt�t|d<|�d�}t|t�s>t�d�dS|�dd�}t�|�r�|dkrtt	j
d|�d�d	d
d�t�d|�dSt�|�d
d��r�tj�
d�r�t�d�t�d�dSt�|ddg�}t|�s�t�d�dSzt||j|d�}WnHttf�y<}z*t�d||�|dk�r&|�WYd}~dSd}~00t�d��t|||j�}	Wd�n1�sn0Y|	D]8\}
}}|tjk�r�t�d|
|�nt�d|
||��q|dS)Nr8z.No 'growpart' entry in cfg.  Using default: %sz#'growpart' in config was not a dictrrZoffz"Growpart's 'mode' key with value 'r�z22.2zUse 'off' instead.)Z
deprecatedZdeprecated_versionZ
extra_messagezgrowpart disabled: mode=%srFz/etc/growroot-disabledz0growpart disabled: /etc/growroot-disabled existsz&use ignore_growroot_disabled to ignorerrzgrowpart: empty device list)r'rz,growpart unable to find resizer for '%s': %szResizing devicesz'%s' resized: %sz'%s' %s: %s)rLrp�DEFAULT_CONFIGrmr��dictrrZis_falserZ	deprecaterErFrQZget_cfg_option_list�lenrar'r[r\r	r�r�rrr�)
r�r�r�r�Zmycfgrrr^rNZresized�entryrSr�r#r#r$�handlesR�




�



.r�r8rRrV)BrTr�r�r�ZloggingrEZos.pathr>r��abcrr�
contextlibr�pathlibr�typingrrZ	cloudinitrr	r
rrZcloudinit.cloudr
Zcloudinit.configrZcloudinit.config.schemarZcloudinit.distrosrrZcloudinit.settingsrrr�__annotations__r�r�rZ	getLoggerr rLr�r%r&r6rPrUr�r4rarqrDrirzr5r�r�r�r�r�r�rZr#r#r#r$�<module>sd��
1&
9.t4�

© 2025 Cubjrnet7