관리-도구
편집 파일: cc_growpart.cpython-39.opt-1.pyc
a ����'�DgyI����������������������@���s(��U�d�Z�ddlZddlZddlZddlZddlZddlZddlZddlZddl m Z mZ�ddlm Z �ddlmZ�ddlmZmZ�ddlmZmZmZmZmZ�ddlmZ�dd lmZ�dd lmZ�ddlm Z m!Z!�ddl"m#Z#�e#Z$d e ge$g�d�Z%ee&d<�ddgdd�Z'ed�Z(G�dd��d�Z)e�*e+�Z,G�dd��de-�Z.G�dd��de �Z/G�dd��de/�Z0G�dd��de/�Z1G�dd ��d e/�Z2e3e!e4e/d!�d"d#�Z5ee6�d$�d%d&�Z7ee6�d$�d'd(�Z8d)d*��Z9e:d$�d+d,�Z;d-d.��Z<ee3e3f�d$�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�������������������@���s���e�Zd�ZdZdZdZdZdS�)�RESIZE�SKIPPED�CHANGED�NOCHANGE�FAILEDN)�__name__� __module__�__qualname__r���r���r���r�����r#���r#����@/usr/lib/python3.9/site-packages/cloudinit/config/cc_growpart.pyr���/���s���r���c�������������������@���s���e�Zd�ZdS�)�ResizeFailedExceptionN)r ���r!���r"���r#���r#���r#���r$���r%���9���s���r%���c�������������������@���s:���e�Zd�Zed�dd�Zeeed�dd��Zedd���Z d S�) �Resizer)�distroc�����������������C���s ���||�_�d�S��N)�_distro)�selfr'���r#���r#���r$����__init__>���s����zResizer.__init__)r����returnc�����������������C���s���d�S�r(���r#����r*���r���r#���r#���r$���� availableA���s����zResizer.availablec�����������������C���s���d�S�r(���r#���)r*����diskdev�partnum�partdev�fsr#���r#���r$����resizeD���s����zResizer.resizeN) r ���r!���r"���r���r+���r����list�boolr.���r3���r#���r#���r#���r$���r&���=���s ���r&���c�������������������@���s"���e�Zd�Zed�dd�Zdd��ZdS�)�ResizeGrowPart�r���c�����������������C���sH���z.t�j�ddgddid�j}t�d|�r,W�dS�W�n�t�jyB���Y�n0�dS�) N�growpartz--help�LANG�C�� update_envz--update\s+TF)r ����stdout�re�search�ProcessExecutionError)r*���r����outr#���r#���r$���r.���I���s����� zResizeGrowPart.availablec�����������������C���s\��t�||�}|�j���}tj|dd���}tj�|d�}d|d�} tj�|�sVt� |d��zt j dd||g| d��W�n`�t jy��} �zF| jd kr�t �td ||��t| �| �||fW��Y�d�} ~ W��d�����S�d�} ~ 0�0�zt j d||g| d��W�nB�t j�y,�} �z&t �td||��t| �| �W�Y�d�} ~ n d�} ~ 0�0�W�d�����n1��sD0����Y��|t�||�fS�)NT)�dirZ needs_exer8���r:���)r9���ZTMPDIRi���z --dry-runr;�������z&Failed growpart --dry-run for (%s, %s)zFailed: growpart %s %s)�get_sizer)���Zget_tmp_exec_pathr���Ztempdir�os�path�join�exists�mkdirr ���r@���� exit_coder����logexc�LOGr%���)r*���r/���r0���r1���r2����beforeZtmp_dirZtmpdZgrowpart_tmpZmy_env�er#���r#���r$���r3���U���s8���� � � ,@zResizeGrowPart.resizeN�r ���r!���r"���r4���r.���r3���r#���r#���r#���r$���r6���H���s���r6���c�������������������@���s&���e�Zd�ZdZed�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``. r7���c�����������������C���s���t�j�d�o|dgkS�)z'growfs only works on the root partitionz/etc/rc.d/growfsr���)rE���rF����isfiler-���r#���r#���r$���r.�������s����zResizeGrowFS.availablec�������������� ���C���sj���t�||�}z|�jjddd��W�n<�tjyZ�}�z"t�td��t|�|�W�Y�d�}~n d�}~0�0�|t�||�fS�)NZonestart�growfs)�actionZservicezFailed: service growfs onestart) rD���r)���Zmanage_servicer ���r@���r���rK���rL���r%���)r*���r/���r0���r1���r2���rM���rN���r#���r#���r$���r3�������s���� zResizeGrowFS.resizeN)r ���r!���r"����__doc__r4���r.���r3���r#���r#���r#���r$���rP���y���s���rP���c�������������������@���s"���e�Zd�Zed�dd�Zdd��ZdS�)�ResizeGpartr7���c�����������������C���sN���z4t�j�ddgddiddgd�j}t�d|�r2W�d S�W�n�t�jyH���Y�n0�d S�)N�gpart�helpr9���r:���r���rC���)r<���Zrcszgpart recover TF)r ����stderrr>���r?���r@���)r*���r����errr#���r#���r$���r.�������s����� zResizeGpart.availablec�������������� ���C���s����zt���dd|g��W�nH�t�jy\�}�z.|jdkrHt�td|��t|�|�W�Y�d}~n d}~0�0�t||�}zt���ddd||g��W�n@�t�jy��}�z&t�td||��t|�|�W�Y�d}~n d}~0�0�|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. rV���Zrecoverr���zFailed: gpart recover %sNr3���z-izFailed: gpart resize -i %s %s)r ���r@���rJ���r���rK���rL���r%���rD���)r*���r/���r0���r1���r2���rN���rM���r#���r#���r$���r3�������s���� zResizeGpart.resizeNrO���r#���r#���r#���r$���rU�������s���rU���)r���r'���r���r,���c�����������������C���s����d�}|�dkrDt�D�]$\}}||�}|j|d�r|}�q6q|s�td��nZi�}t�D�]\}} | ||<�qL|�|vrrtd|����||��|�} | j|d�r�| }|s�td|����|S�)Nr���r7���zNo resizers availablezunknown resize mode %szmode %s not available)�RESIZERSr.���� ValueError� TypeError)r���r'���r���Zresize_class�_name�resizerZcurZmmap�k�vZmclassr#���r#���r$����resizer_factory����s(���� ra���)r,���c�������������� ���C���s����d�}z�z0t��|�t�j�}t��|dt�j�W�W�|r6t��|��S��ty~���|dkrht|���Y�W�|rft��|��S�Y�W�|rzt��|��d�S�0�W�|r�t��|��n|r�t��|��0�d�S�)Nr����zfs)rE����open�O_RDONLY�lseek�SEEK_END�close�FileNotFoundError�get_zfs_size)�filenamer2����fdr#���r#���r$���rD�������s*���� � � ��rD���c�������������� ���C���sn���|���d�d�}zt�dddd|g�\}}W�n6�tjy`�}�zt�d||��W�Y�d�}~d�S�d�}~0�0�t|����S�)Nr���r����zpool�getz -Hpovalue�sizezFailed: zpool get size %s: %s)�splitr ���r@���rL����debug�int�strip)Zdatasetrl���rn����_rN���r#���r#���r$���ri�������s����ri���c�����������������C���s����|���d�r|�d�fS�t�|��}|s(td��|d�}|d�}t���}|dkr~|s~t�t����}|d�u�r~tj� |�rv|d�fS�td��||fS�)N�/dev/z,Could not determine device of '%s' % dev_entr���rC���z /dev/rootz!Unable to find device '/dev/root') � startswithr���Zget_mount_infor[���Zis_containerZrootdev_from_cmdlineZget_cmdlinerE���rF���rH���)�devent�result�devr2���� containerr#���r#���r$���� devent2dev����s���� rz���c�������������� ���C���s����t��d�st�d��dS�zt���dd|�g��W�nN�t�jyz�}�z4|jdkrVt�d|���nt�d|j��W�Y�d}~dS�d}~0�0�tt�j��.�t���dd |g��t�d |���W�d����dS�1�s�0����Y��dS�)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 ����whichrL���rp���r@���rJ����warningr���)�blockdev� partitionrN���r#���r#���r$����is_encrypted ��s$���� � r����c�������������� ���C���s����ddd|�g}t���|�d�}|�d�s2td|�����z d|�d�d ��d �d����W�S��ty��}�z$td|��d|��d ��|�W�Y�d�}~n d�}~0�0�d�S�)NZdmsetupZdepsz--options=devnamer���z1 dependz5Expecting '1 dependencies' from 'dmsetup'. Received: rt���z: (rC����)zRan `z$`, but received unexpected stdout: `�`)r ���ru����RuntimeErrorro���� IndexError)r����ZcommandZdeprN���r#���r#���r$����get_underlying_partition$��s���� � ��r����c�����������������C���s���t����stjdfS�z�t�dt������B�t�����}t�|�}W�d����n1�sN0����Y��W�d����n1�sl0����Y��|d�}t � |�}|d�}W�n.�ty��}�ztd�|�W�Y�d}~n d}~0�0�z�t j ddd d |�g|d��W�zt � ddd |t|�g��W�n4�t j�y.�}�zt�d|��W�Y�d}~n d}~0�0�zt�����W�n �t�y\���t�td��Y�n0�n�zt � ddd |t|�g��W�n4�t j�y��}�zt�d|��W�Y�d}~n d}~0�0�zt�����W�n �t�y����t�td��Y�n0�0�tjd|���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���)�dataZluksKillSlotz--batch-modez<Failed to kill luks slot after resizing encrypted volume: %sz8Failed to remove keyfile after resizing encrypted volumez'Successfully resized encrypted volume '�')�KEYDATA_PATHrH���r���r���r ����Timedrc����json�load�base64Z b64decode� Exceptionr����r ����strr@���rL���r����unlinkr���rK���r���)r����r�����fZkeydatar����Zdecoded_keyr����rN���r#���r#���r$����resize_encrypted5��s����� ��F ����� ����� �� �r����c�����������������C���s��g�}z�|���||||�\}}||kr>|�|tjd||f�f��n�|d�u�sN|d�u�r�d} |d�urp|d�u�rpd||f�} nd|�} |�|tj| f��nFd} |d�ur�|d�u�r�d||||f�} nd|||f�} |�|tj| f��W�nB�t�y�} �z(|�|tjd||| f�f��W�Y�d�} ~ n d�} ~ 0�0�|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����appendr���r���r���r%���r���)r^���rv����disk�ptnumr����r2����info�old�new�msgrN���r#���r#���r$���� _call_resizern��sJ���� ������r����)r^���r'���c�����������������C���s���t���|�}g�}|�r�|�d�}d�}d�}zt|�\}}W�nF�ty|�} �z.|�|tjd| �f��W�Y�d�} ~ qW�Y�d�} ~ n d�} ~ 0�0�t�d|��|dkr�t |�t �r�|t|�|||||�7�}qzt� |�} W�nL�t�y�} �z2|�|tjd|| f�f��W�Y�d�} ~ qW�Y�d�} ~ n d�} ~ 0�0�t �| j��sDt �| j��sD|�|tjd|�f��q|�|�}|�rz�t|�}t||��r�|dd��|D��v�r�|�d|��|�d|��W�qt||�\} }|�|| |f��n|�|tjd |��d �f��W�q�t�y�} �z*|�|tjd|��d| ���f��W�Y�d�} ~ qd�} ~ 0�0�qz|�|�\}}W�nP�ttf�y��} �z2|�|tjd || f�f��W�Y�d�} ~ qW�Y�d�} ~ n d�} ~ 0�0�|t|�|||||�7�}q|S�)Nr���zunable to convert to device: %szgrowpart found fs=%srb���zstat of '%s' failed: %szdevice '%s' not a block devicec�����������������S���s���g�|�]}|d���qS�)r���r#���)�.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�poprz���r[���r����r���r���rL���rp���� isinstancerP���r����rE����stat�OSError�S_ISBLK�st_mode�S_ISCHRZget_mapped_devicer����r�����insertr����r����r���Zdevice_part_infor\���)r^���r���r'���r����rv���r����r����r����r2���rN���ZstatretZunderlying_blockdevr����r|����messager#���r#���r$����resize_devices���s����� ��" ��"��� �� ���� ��"r����)�name�cfg�cloud�argsr,���c����������� ��� ���C���s���d|vrt��dt��t|d<�|�d�}t|t�s>t��d��d�S�|�dd�}t�|�r�|dkrtt j d|��d�d d d��t��d|��d�S�t�|�d d��r�tj� d�r�t��d��t��d��d�S�t�|ddg�}t|�s�t��d��d�S�zt||j|d�}W�nH�ttf�y<�}�z*t��d||��|dk�r&|�W�Y�d�}~d�S�d�}~0�0�t�d���t|||j�} W�d�����n1��sn0����Y��| D�]8\} }}|tjk�r�t��d| |��nt��d| ||���q|d�S�)Nr8���z.No 'growpart' entry in cfg. Using default: %sz#'growpart' in config was not a dictr���r���Zoffz"Growpart's 'mode' key with value 'r����z22.2zUse 'off' instead.)Z deprecatedZdeprecated_versionZ extra_messagezgrowpart disabled: mode=%sr���Fz/etc/growroot-disabledz0growpart disabled: /etc/growroot-disabled existsz&use ignore_growroot_disabled to ignorer���r���zgrowpart: empty device list)r'���r���z,growpart unable to find resizer for '%s': %szResizing devicesz'%s' resized: %sz'%s' %s: %s)rL���rp����DEFAULT_CONFIGrm���r�����dictr���r���Zis_falser���Z deprecaterE���rF���rQ���Zget_cfg_option_list�lenra���r'���r[���r\���r ���r����r����r���r���r����) r����r����r����r����Zmycfgr���r���r^���rN���Zresized�entryrS���r����r#���r#���r$����handle��sR����� � .r����r8���rR���rV���)BrT���r����r����r����ZloggingrE���Zos.pathr>���r�����abcr���r���� contextlibr����pathlibr����typingr���r���Z cloudinitr���r ���r ���r���r���Zcloudinit.cloudr ���Zcloudinit.configr���Zcloudinit.config.schemar���Zcloudinit.distrosr���r���Zcloudinit.settingsr���r���r����__annotations__r����r����r���Z getLoggerr ���rL���r����r%���r&���r6���rP���rU���r����r4���ra���rq���rD���ri���rz���r5���r����r����r����r����r����r����rZ���r#���r#���r#���r$����<module>���sd����� 1& 9.t4�