U
    AdM                  	   @   s(  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ZddlZddl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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m Z  ddl!m"Z"m#Z# ddl$m%Z% dZ&e%Z'ddde&e"ge'ededgg dZ(ee)d< e e(Z ddgddZ*edZ+G dd dZ,e-e.Z/e0e#dd d!Z1G d"d# d#e2Z3G d$d% d%e	Z4G d&d' d'e4Z5G d(d) d)e4Z6d*d+ Z7d,d- Z8d.d/ Z9d0d1 Z:e;d2d3d4Z<d5d6 Z=ee0e0f d2d7d8Z>d9d: Z?e0eeee@dd;d<d=ZAd>e5fd?e6ffZBdS )@zGrowpart: 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
Zcc_growpartZGrowpartzGrow partitionsz            growpart:
              mode: auto
              devices: ["/"]
              ignore_growroot_disabled: false
            z            growpart:
              mode: growpart
              devices:
                - "/"
                - "/dev/vdb1"
              ignore_growroot_disabled: true
            )idnametitleZdescriptionZdistros	frequencyZexamplesZactivate_by_schema_keysmetaauto/F)modedevicesignore_growroot_disabledz/cc_growpart_keydatac                   @   s   e Zd ZdZdZdZdZdS )RESIZESKIPPEDCHANGEDNOCHANGEFAILEDN)__name__
__module____qualname__r   r    r!   r"    r&   r&   >/usr/lib/python3/dist-packages/cloudinit/config/cc_growpart.pyr   f   s   r   )r   distroc           
      C   s   d }| dkr@t D ] \}}||}| r|} q2q|stdnVi }t D ]\}}|||< qH| |krntd|  ||  |}	|	 r|	}|std|  |S )Nr   zNo resizers availablezunknown resize mode %szmode %s not available)RESIZERS	available
ValueError	TypeError)
r   r(   Zresize_class_nameresizerZcurZmmapkvZmclassr&   r&   r'   resizer_factoryp   s(    

r1   c                   @   s   e Zd ZdS )ResizeFailedExceptionN)r#   r$   r%   r&   r&   r&   r'   r2      s   r2   c                   @   s8   e Zd ZedddZeedddZedd Zd	S )
Resizer)r(   c                 C   s
   || _ d S N)_distro)selfr(   r&   r&   r'   __init__   s    zResizer.__init__returnc                 C   s   d S r4   r&   )r6   r&   r&   r'   r*      s    zResizer.availablec                 C   s   d S r4   r&   )r6   diskdevpartnumpartdevr&   r&   r'   resize   s    zResizer.resizeN)	r#   r$   r%   r   r7   r   boolr*   r=   r&   r&   r&   r'   r3      s
   r3   c                   @   s   e Zd Zdd Zdd ZdS )ResizeGrowPartc                 C   sZ   t j }d|d< z,tjddg|d\}}td|r<W dS W n tjk
rT   Y nX dS )	NCLANGgrowpartz--helpenvz--update\s+TFosenvironcopyr
   researchProcessExecutionError)r6   myenvoutZ_errr&   r&   r'   r*      s    

zResizeGrowPart.availablec           
      C   sP  t j }d|d< t|}| j }tj|dd
}t j	|d}t j
|s\t |d ||d< ztjdd||g|d	 W n` tjk
r }	 z@|	jd
krttd|| t|	|	||f W Y W  5 Q R  S d }	~	X Y nX ztjd||g|d	 W n@ tjk
r8 }	 zttd|| t|	|	W 5 d }	~	X Y nX W 5 Q R X |t|fS )Nr@   rA   T)dirZ	needs_exerB   i  ZTMPDIRz	--dry-runrC      z&Failed growpart --dry-run for (%s, %s)zFailed: growpart %s %s)rF   rG   rH   get_sizer5   Zget_tmp_exec_pathr   Ztempdirpathjoinexistsmkdirr
   rK   	exit_coder   logexcLOGr2   )
r6   r:   r;   r<   rL   beforeZtmp_dirZtmpdZgrowpart_tmper&   r&   r'   r=      s<    


 


*&zResizeGrowPart.resizeNr#   r$   r%   r*   r=   r&   r&   r&   r'   r?      s   r?   c                   @   s   e Zd Zdd Zdd ZdS )ResizeGpartc                 C   s`   t j }d|d< z2tjddg|ddgd\}}td|rBW d	S W n tjk
rZ   Y nX d
S )Nr@   rA   gparthelpr   rO   )rD   Zrcszgpart recover TFrE   )r6   rL   Z_outerrr&   r&   r'   r*      s    

zResizeGpart.availablec              
   C   s   zt  dd|g W nF t jk
rZ } z&|jdkrJttd| t||W 5 d}~X Y nX t|}zt  ddd||g W n> t jk
r } zttd|| t||W 5 d}~X Y nX |t|fS )	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.
        r\   Zrecoverr   zFailed: gpart recover %sNr=   z-izFailed: gpart resize -i %s %s)r
   rK   rU   r   rV   rW   r2   rP   )r6   r:   r;   r<   rY   rX   r&   r&   r'   r=      s    
zResizeGpart.resizeNrZ   r&   r&   r&   r'   r[      s   r[   c              	   C   s4   t | t j}zt |dt jW S t | X d S )Nr   )rF   openO_RDONLYcloselseekSEEK_END)filenamefdr&   r&   r'   rP      s    rP   c                 C   s$  t j| }t j|}d| }t rVdt|  }td|}|	d|	dfS t
 rdt|  }td|}|	d|	dfS t j|std| |f t j|d}t j|std	|  t| }t j|}	t j|	}
tt j|
d
 }t jd| }||fS )Nz/sys/class/block/%s/dev/z^(/dev/.+)p([0-9])$rO      z^(/dev/.+)s([0-9])$z%s had no syspath (%s)	partitionz%s not a partitiondevz/dev/block/%s)rF   rQ   realpathbasenamer   Z
is_FreeBSDZfind_freebsd_partrI   rJ   groupZis_DragonFlyBSDZfind_dragonflybsd_partrS   r+   rR   r,   Z	load_filerstripdirname)ZdevpathZrpathZbnameZsyspathZfreebsd_partmZdragonflybsd_partZptpathptnumZrsyspathZdisksyspathZ
diskmajminZdiskdevpathr&   r&   r'   device_part_info  s,    rq   c                 C   sr   |  dr| S t| }|s$td|d }t }|dkrn|sntt }|d krntj	|rf|S td|S )Nrf   z,Could not determine device of '%s' % dev_entr   z	/dev/rootz!Unable to find device '/dev/root')

startswithr   Zget_mount_infor+   Zis_containerZrootdev_from_cmdlineZget_cmdlinerF   rQ   rS   )deventresultri   Z	containerr&   r&   r'   
devent2dev,  s    

ru   c                 C   s,   t j| }|dr(td| | |S dS )a  Returns 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)rF   rQ   rj   rr   rW   debug)blockdevrj   r&   r&   r'   get_mapped_deviceC  s
    	
rx   r8   c              
   C   s   t dstd dS zt  dd| g W nP t jk
r| } z0|jdkrXtd|  ntd|j W Y dS d}~X Y nX tt j, t  dd	|g td
|  W 5 Q R  dS Q R X dS )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.NZisLukszDetermined that %s is encryptedT)r
   ZwhichrW   rv   rK   rU   warningr   )rw   rh   rY   r&   r&   r'   is_encryptedS  s$    


r}   c              
   C   s   ddd| g}t  |d }|ds2td| z d|dd	 d
d  W S  tk
r } ztd| d| d|W 5 d }~X Y nX d S )NZdmsetupZdepsz--options=devnamer   z1 dependz5Expecting '1 dependencies' from 'dmsetup'. Received: rf   z: (rO   )zRan `z$`, but received unexpected stdout: ``)r
   rr   RuntimeErrorsplit
IndexError)rw   ZcommandZdeprY   r&   r&   r'   get_underlying_partitionn  s    
 r   c                 C   s,  t  stjdfS z<t  }t|}W 5 Q R X |d }t|}|d }W n, t	k
rz } zt
d|W 5 d}~X Y nX ztjdddd| g|d W 5 ztddd|t|g W n0 tjk
r } ztd	| W 5 d}~X Y nX zt   W n" t	k
r   ttd
 Y nX X tjd|  dfS )zUse '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}
    zNo encryption keyfile foundkeyslotzZCould not load encryption key. This is expected if the volume has been previously resized.Nry   ZluksKillSlotz--batch-modez<Failed to kill luks slot after resizing encrypted volume: %sz8Failed to remove keyfile after resizing encrypted volumez
--key-file-r=   )dataz'Successfully resized encrypted volume '')KEYDATA_PATHrS   r   r   r_   jsonloadbase64Z	b64decode	Exceptionr   r
   strrK   rW   r|   unlinkr   rV   r    )rw   rh   fZkeydatar   Zdecoded_keyr   rY   r&   r&   r'   resize_encrypted  sV    



	 
r   c                 C   s  t  |}g }|r|d}zt|}W n@ tk
rj } z"||tjd| f W Y qW 5 d }~X Y nX zt|}W nD t	k
r } z&||tjd||f f W Y qW 5 d }~X Y nX t
|jst|js||tjd| f qt|}|rzt|}t||rd|dd |D krD|d| |d| W qt||\}	}
|||	|
f n||tjd| df W q tk
r } z"||tjd	| d
| f W 5 d }~X Y qX qzt|\}}W nJ ttfk
r  } z&||tjd||f f W Y qW 5 d }~X Y nX zZ| |||\}}||kr\||tjd||f f n||tjd||||f f W q tk
r } z ||tjd|||f f W 5 d }~X Y qX q|S )Nr   zunable to convert to device: %szstat of '%s' failed: %szdevice '%s' not a block devicec                 S   s   g | ]}|d  qS )r   r&   ).0xr&   r&   r'   
<listcomp>  s     z"resize_devices.<locals>.<listcomp>zResizing mapped device (z!) skipped as it is not encrypted.zResizing encrypted device (z
) failed: zdevice_part_info(%s) failed: %szno change necessary (%s, %s)zchanged (%s, %s) from %s to %sz'failed to resize: disk=%s, ptnum=%s: %s)rH   popru   r+   appendr   r   rF   statOSErrorS_ISBLKst_modeS_ISCHRrx   r   r}   insertr   r   r"   rq   r,   r=   r!   r    r2   )r.   r   infors   rw   rY   ZstatretZunderlying_blockdevrh   rz   messageZdiskrp   oldnewr&   r&   r'   resize_devices  s    



	



		r   )r   cfgcloudr	   argsr9   c              
   C   s  d|kr| dt  t|d< |d}t|ts@|d d S |dd}t|r|dkrp|d| d | d	|  d S t|d
drtj	
dr| d | d d S t|ddg}t|s| d d S zt||j}W nN ttfk
r< }	 z*| d||	f  |dkr&|	W Y d S d }	~	X Y nX tj|j dt||fd}
|
D ]@\}}}|tjkr|d||f  n| d|||f  qZd S )NrB   z.No 'growpart' entry in cfg.  Using default: %sz#'growpart' in config was not a dictr   r   ZoffzDEPRECATED: growpart mode 'z#' is deprecated. Use 'off' instead.zgrowpart disabled: mode=%sr   Fz/etc/growroot-disabledz0growpart disabled: /etc/growroot-disabled existsz&use ignore_growroot_disabled to ignorer   r   zgrowpart: empty device listz,growpart unable to find resizer for '%s': %sr   )Zlogfuncmsgfuncr   z'%s' resized: %sz'%s' %s: %s)rv   DEFAULT_CONFIGget
isinstancedictr|   r   Zis_falserF   rQ   isfileZget_cfg_option_listlenr1   r(   r+   r,   Zlog_timer   r   r    r   )r   r   r   r	   r   Zmycfgr   r   r.   rY   Zresizedentryactionr   r&   r&   r'   handle9  sV    








r   rB   r\   )C__doc__r   rH   r   rF   os.pathrI   r   abcr   r   
contextlibr   Zloggingr   pathlibr   textwrapr   typingr   Z	cloudinitr	   r
   r   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r   Zcloudinit.distrosr   r   Zcloudinit.settingsr   ZMODULE_DESCRIPTIONr   r   __annotations__r   r   r   Z	getLoggerr#   rW   r   r1   r   r2   r3   r?   r[   rP   rq   ru   rx   r>   r}   r   r   r   listr   r)   r&   r&   r&   r'   <module>   s   
3'+7     8