HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_growpart.cpython-310.pyc
o

�Ad�M�@s4UdZddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
ddlmZddl
mZddlmZddlmZddlmZdd	lmZ
dd
lmZmZmZddlmZddlmZdd
lmZm Z ddl!m"Z"m#Z#ddl$m%Z%dZ&e%Z'ddde&e"ge'ed�ed�ggd�Z(ee)d<e e(�Zddgdd�Z*ed�Z+Gdd�d�Z,e
�-e.�Z/de0d e#fd!d"�Z1Gd#d$�d$e2�Z3Gd%d&�d&e	�Z4Gd'd(�d(e4�Z5Gd)d*�d*e4�Z6d+d,�Z7d-d.�Z8d/d0�Z9d1d2�Z:d3e;fd4d5�Z<d6d7�Z=d3ee0e0ffd8d9�Z>d:d;�Z?d<e0d=ed>ed?ed@e@d3dfdAdB�ZAdCe5fdDe6ffZBdS)EzGrowpart: Grow partitions�N)�ABC�abstractmethod)�suppress)�Logger)�Path)�dedent)�Tuple)�log)�subp�
temp_utils�util)�Cloud)�Config)�
MetaSchema�get_meta_doc)�ALL_DISTROS�Distro)�
PER_ALWAYSa�Growpart resizes partitions to fill the available disk space.
This is useful for cloud instances with a larger amount of disk space available
than the pristine image uses, as it allows the instance to automatically make
use of the extra space.

The devices on which to run growpart are specified as a list under the
``devices`` key.

There is some functionality overlap between this module and the ``growroot``
functionality of ``cloud-initramfs-tools``. However, there are some situations
where one tool is able to function and the other is not. The default
configuration for both should work for most cloud instances. To explicitly
prevent ``cloud-initramfs-tools`` from running ``growroot``, the file
``/etc/growroot-disabled`` can be created. By default, both ``growroot`` and
``cc_growpart`` will check for the existence of this file and will not run if
it is present. However, this file can be ignored for ``cc_growpart`` by setting
``ignore_growroot_disabled`` to ``true``. For more information on
``cloud-initramfs-tools`` see: https://launchpad.net/cloud-initramfs-tools

Growpart is enabled by default on the root partition. The default config for
growpart is::

    growpart:
      mode: auto
      devices: ["/"]
      ignore_growroot_disabled: false
�cc_growpart�GrowpartzGrow partitionsz�            growpart:
              mode: auto
              devices: ["/"]
              ignore_growroot_disabled: false
            z�            growpart:
              mode: growpart
              devices:
                - "/"
                - "/dev/vdb1"
              ignore_growroot_disabled: true
            )�id�name�title�description�distros�	frequency�examples�activate_by_schema_keys�meta�auto�/F)�mode�devices�ignore_growroot_disabledz/cc_growpart_keydatac@seZdZdZdZdZdZdS)�RESIZE�SKIPPED�CHANGED�NOCHANGE�FAILEDN)�__name__�
__module__�__qualname__r%r&r'r(�r,r,�>/usr/lib/python3/dist-packages/cloudinit/config/cc_growpart.pyr$fs
r$r!�distroc
Cs�d}|dkr!tD]\}}||�}|��r|}nq|std��|Si}tD]\}}|||<q%||vr8td|��|||�}	|	��rD|	}|sLtd|��|S)NrzNo resizers availablezunknown resize mode %szmode %s not available)�RESIZERS�	available�
ValueError�	TypeError)
r!r.�resize_class�_name�resizer�cur�mmap�k�v�mclassr,r,r-�resizer_factoryps,��
r;c@seZdZdS)�ResizeFailedExceptionN)r)r*r+r,r,r,r-r<�sr<c@s8eZdZdefdd�Zedefdd��Zedd��Zd	S)
�Resizerr.cCs
||_dS�N)�_distro)�selfr.r,r,r-�__init__�s
zResizer.__init__�returncC�dSr>r,)r@r,r,r-r0��zResizer.availablecCrCr>r,)r@�diskdev�partnum�partdevr,r,r-�resize�rDzResizer.resizeN)	r)r*r+rrAr�boolr0rHr,r,r,r-r=�sr=c@�eZdZdd�Zdd�ZdS)�ResizeGrowPartcCsXtj��}d|d<ztjddg|d�\}}t�d|�rWdSWdStjy+YdSw)	N�C�LANG�growpartz--help��envz--update\s+TF��os�environ�copyr
�re�search�ProcessExecutionError)r@�myenv�out�_errr,r,r-r0�s
���zResizeGrowPart.availablec
CsVtj��}d|d<t|�}|j��}tj|dd���}tj�	|d�}tj�
|�s-t�|d�||d<z
tjdd||g|d	�Wn0tj
yn}	z#|	jd
krYt�td||�t|	�|	�||fWYd}	~	Wd�Sd}	~	wwztjd||g|d	�Wntj
y�}	z
t�td||�t|	�|	�d}	~	wwWd�n1s�wY|t|�fS)
NrLrMT)�dir�	needs_exerNi��TMPDIRz	--dry-runrO�z&Failed growpart --dry-run for (%s, %s)zFailed: growpart %s %s)rRrSrT�get_sizer?�get_tmp_exec_pathr�tempdir�path�join�exists�mkdirr
rW�	exit_coder�logexc�LOGr<)
r@rErFrGrX�before�tmp_dir�tmpd�growpart_tmp�er,r,r-rH�sH


�
�
��	
����zResizeGrowPart.resizeN�r)r*r+r0rHr,r,r,r-rK��
rKc@rJ)�ResizeGpartcCs^tj��}d|d<ztjddg|ddgd�\}}t�d|�r!Wd	SWd
Stjy.Yd
Sw)NrLrM�gpart�helprr^)rP�rcszgpart recover TFrQ)r@rX�_out�errr,r,r-r0�s
���zResizeGpart.availablec
Cs�z
t�dd|g�Wn$tjy.}z|jdkr$t�td|�t|�|�WYd}~nd}~wwt|�}zt�ddd||g�WntjyY}z
t�td||�t|�|�d}~ww|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.
        rq�recoverrzFailed: gpart recover %sNrHz-izFailed: gpart resize -i %s %s)r
rWrfrrgrhr<r_)r@rErFrGrmrir,r,r-rH�s$

���
��zResizeGpart.resizeNrnr,r,r,r-rp�rorpc	Cs8t�|tj�}zt�|dtj�Wt�|�St�|�w)Nr)rR�open�O_RDONLY�lseek�SEEK_END�close)�filename�fdr,r,r-r_�sr_c
Cs$tj�|�}tj�|�}d|}t��r+dt�|�}t�d|�}|�	d�|�	d�fSt�
�rFdt�|�}t�d|�}|�	d�|�	d�fStj�|�sTt
d||f��tj�|d�}tj�|�sgtd	|��t�|���}tj�|�}	tj�|	�}
t�tj�|
d
����}tj�d|�}||fS)Nz/sys/class/block/%s�/dev/z^(/dev/.+)p([0-9])$r^�z^(/dev/.+)s([0-9])$z%s had no syspath (%s)�	partitionz%s not a partition�devz
/dev/block/%s)rRrb�realpath�basenamer�
is_FreeBSD�find_freebsd_partrUrV�group�is_DragonFlyBSD�find_dragonflybsd_partrdr1rcr2�	load_file�rstrip�dirname)
�devpath�rpath�bname�syspath�freebsd_part�m�dragonflybsd_part�ptpath�ptnum�rsyspath�disksyspath�
diskmajmin�diskdevpathr,r,r-�device_part_infos,r�cCsr|�d�r|St�|�}|std��|d}t��}|dkr7|s7t�t���}|dur7tj�	|�r3|Std��|S)Nr~z,Could not determine device of '%s' % dev_entrz	/dev/rootz!Unable to find device '/dev/root')
�
startswithr�get_mount_infor1�is_container�rootdev_from_cmdline�get_cmdlinerRrbrd)�devent�resultr��	containerr,r,r-�
devent2dev,s

r�cCs,tj�|�}|�d�rt�d||�|SdS)aReturns underlying block device for a mapped device.

    If it is mapped, blockdev will usually take the form of
    /dev/mapper/some_name

    If blockdev is a symlink pointing to a /dev/dm-* device, return
    the device pointed to. Otherwise, return None.
    z/dev/dm-z$%s is a mapped device pointing to %sN)rRrbr�r�rh�debug)�blockdevr�r,r,r-�get_mapped_deviceCs
	
r�rBc
Cs�t�d�st�d�dSz
t�dd|g�Wn'tjy=}z|jdkr+t�d|�nt�d|j�WYd}~dSd}~wwttj��t�dd	|g�t�d
|�	Wd�dS1s^wYdS)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.N�isLukszDetermined that %s is encryptedT)r
�whichrhr�rWrf�warningr)r�r�rmr,r,r-�is_encryptedSs*


���
 �r�c
Cs�ddd|g}t�|�d}|�d�std|����zd|�d�d	�d
�d��WStyA}ztd|�d|�d
��|�d}~ww)N�dmsetup�depsz--options=devnamerz1 dependz5Expecting '1 dependencies' from 'dmsetup'. Received: r~z: (r^�)zRan `z$`, but received unexpected stdout: `�`)r
r��RuntimeError�split�
IndexError)r��command�deprmr,r,r-�get_underlying_partitionns 
� ����r�cCs�t��s	tjdfSz(t���
}t�|�}Wd�n1swY|d}t�|�}|d}Wnt	yB}zt
d�|�d}~wwzKtjdddd	|g|d
�Wzt�ddd|t|�g�Wntj
yx}zt�d
|�WYd}~nd}~wwzt��WnMt	y�t�td�Yn?wzt�ddd|t|�g�Wntj
y�}zt�d
|�WYd}~nd}~wwzt��Wwt	y�t�td�Ywwtjd|�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 foundN�key�slotzZCould not load encryption key. This is expected if the volume has been previously resized.r�z
--key-file�-rH)�data�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_PATHrdr$r%rw�json�load�base64�	b64decode�	Exceptionr�r
�strrWrhr��unlinkrrgr&)r�r��f�keydatar��decoded_keyr�rmr,r,r-�resize_encrypteds�

�
�������	��������	�����
�r�cCs�t�|�}g}|�rV|�d�}zt|�}Wnty2}z|�|tjd|f�WYd}~qd}~wwzt�|�}Wnt	yY}z|�|tjd||ff�WYd}~qd}~wwt�
|j�srt�|j�sr|�|tjd|f�qt
|�}|r�z?t|�}t||�r�|dd�|D�vr�|�d|�|�d|�Wqt||�\}	}
|�||	|
f�n
|�|tjd|�d�f�Wn!ty�}z|�|tjd	|�d
|��f�WYd}~nd}~wwqzt|�\}}Wn"ttf�y}z|�|tjd||ff�WYd}~qd}~wwz-|�|||�\}
}|
|k�r"|�|tjd||ff�n|�|tjd
|||
|ff�Wn!t�yS}z|�|tjd|||ff�WYd}~nd}~ww|s
|S)Nrzunable to convert to device: %szstat of '%s' failed: %szdevice '%s' not a block devicecSsg|]}|d�qS)rr,)�.0�xr,r,r-�
<listcomp>�sz"resize_devices.<locals>.<listcomp>zResizing mapped device (z!) skipped as it is not encrypted.zResizing encrypted device (z
) failed: zdevice_part_info(%s) failed: %szno change necessary (%s, %s)zchanged (%s, %s) from %s to %sz'failed to resize: disk=%s, ptnum=%s: %s)rT�popr�r1�appendr$r%rR�stat�OSError�S_ISBLK�st_mode�S_ISCHRr�r�r��insertr�r�r(r�r2rHr'r&r<)r5r"�infor�r�rm�statret�underlying_blockdevr�r��message�diskr��old�newr,r,r-�resize_devices�s�

����

����
���
��
�������

����


��
����	������{r�r�cfg�cloudr	�argsc
Cs�d|vr|�dt�t|d<|�d�}t|t�s |�d�dS|�dd�}t�|�rA|dkr8|�d|�d��|�d	|�dSt�|�d
d��r\tj	�
d�r\|�d
�|�d�dSt�|ddg�}t|�so|�d�dSzt
||j�}Wn$ttfy�}	z|�d||	f�|dkr�|	�WYd}	~	dSd}	~	wwtj|jdt||fd�}
|
D]\}}}
|tjkr�|�d||
f�q�|�d|||
f�q�dS)NrNz.No 'growpart' entry in cfg.  Using default: %sz#'growpart' in config was not a dictr!r�offzDEPRECATED: growpart mode 'z#' is deprecated. Use 'off' instead.zgrowpart disabled: mode=%sr#Fz/etc/growroot-disabledz0growpart disabled: /etc/growroot-disabled existsz&use ignore_growroot_disabled to ignorer"r zgrowpart: empty device listz,growpart unable to find resizer for '%s': %sr�)�logfunc�msg�funcr�z'%s' resized: %sz'%s' %s: %s)r��DEFAULT_CONFIG�get�
isinstance�dictr�r�is_falserRrb�isfile�get_cfg_option_list�lenr;r.r1r2�log_timer�r$r&r�)rr�r�r	r��mycfgr!r"r5rm�resized�entry�actionr�r,r,r-�handle9s\�




�


���
�r�rNrq)C�__doc__r�rTr�rR�os.pathrUr��abcrr�
contextlibr�loggingr�pathlibr�textwrapr�typingr�	cloudinitr	r
rr�cloudinit.cloudr
�cloudinit.configr�cloudinit.config.schemarr�cloudinit.distrosrr�cloudinit.settingsr�MODULE_DESCRIPTIONrr�__annotations__r�r�r$�	getLoggerr)rhr�r;r�r<r=rKrpr_r�r�r�rIr�r�r�r��listr�r/r,r,r,r-�<module>s������

3'+7�����
�8