
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me*              	   @   sd  U d 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	 ddl
mZ ddlmZ ddlmZmZ ddlmZ dd	lmZ d
Zdddedegddgeg dZeeeZ ej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)d* Z$d+d, Z%e&eee'dd-d.d/Z(d0d1 Z)dS )2z:Resizefs: cloud-config module which resizes the filesystem    N)dedent)subputil)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROS)
PER_ALWAYSZnoblockZcc_resizefsZResizefszResize filesystema          Resize a filesystem to use all avaliable space on partition. This
        module is useful along with ``cc_growpart`` and will ensure that if the
        root partition has been resized the root filesystem will be resized
        along with it. By default, ``cc_resizefs`` will resize the root
        partition and will block the boot process while the resize command is
        running. Optionally, the resize operation can be performed in the
        background while cloud-init continues running modules. This can be
        enabled by setting ``resize_rootfs`` to ``noblock``. This module can be
        disabled altogether by setting ``resize_rootfs`` to ``false``.z@resize_rootfs: false  # disable root filesystem resize operationzAresize_rootfs: noblock  # runs resize operation in the background)idnametitledescriptionZdistrosZexamplesZ	frequencyZactivate_by_schema_keysc             C   s   t j|  r0tjjd|  r0ddddd|  g}ndddd| g}t jjd}t jjtjddgd jd	d j	 }||kr|j
d}|j|d
 d t|S )Nz%s/.snapshotsbtrfsZ
filesystemresizemaxz5.10z	--versionr   v   z	--enqueue)r   Zmount_is_read_writeospathisdirZVersionZfrom_strr   splitstripindexinserttuple)mount_pointdevpthcmdZbtrfs_with_queueZsystem_btrfs_veridx r!   !/usr/lib/python3.6/cc_resizefs.py_resize_btrfs8   s    
"
r#   c             C   s   d|fS )NZ	resize2fsr!   )r   r   r!   r!   r"   _resize_extY   s    r$   c             C   s   d| fS )NZ
xfs_growfsr!   )r   r   r!   r!   r"   _resize_xfs]   s    r%   c             C   s
   dd| fS )Ngrowfsz-yr!   )r   r   r!   r!   r"   _resize_ufsa   s    r'   c             C   s   ddd| |fS )NzpoolZonlinez-er!   )r   r   r!   r!   r"   _resize_zfse   s    r)   c             C   s
   dd| fS )Nhammer2r&   r!   )r   r   r!   r!   r"   _resize_hammer2i   s    r+   c             C   s   ddd|fS )zSingle device resizebcachefsZdevicer   r!   )r   r   r!   r!   r"   _resize_bcachefsm   s    r-   c             C   sd   d}d}yt j dd|g W nB t jk
r^ } z$|jj|rJ||jkrJdS |W Y d d }~X nX dS )Nzgrowfs: requested sizez.is not larger than the current filesystem sizer&   z-NTF)r   ProcessExecutionErrorstderr
startswith)r   r   Z
skip_startZskip_container!   r!   r"   _can_skip_resize_ufsr   s    r2   r   ZextZxfsZufszfsr*   r,   c             C   s6   | j  }x(tj D ]\}}|j|r|||S qW dS )NF)lowerRESIZE_FS_PRECHECK_CMDSitemsr0   )fs_typeresize_whatr   	fstype_lcifuncr!   r!   r"   can_skip_resize   s
    
r<   c             C   s  t j }| dkrTtjj|  rT| rTt jt j } | dkrHtjd dS tj	d|  | dkrntj	d| | dS | j
drtj	d | S | j
d	 rtjj|  rd	| jd
 }tj	d| | |} ytj| }W nf tk
r4 } zH|o|jtjkrtj	d| | n"|jtjkr"tjd| | n|dS d}~X nX tj|j r~tj|j r~|rltj	d| | ntj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 commandline 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 cmdlineZ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   Zis_containerr   r   existsZrootdev_from_cmdlineZget_cmdlineLOGwarningdebugr0   lstripstatOSErrorerrnoENOENTS_ISBLKst_modeS_ISCHR)ZdevpathinfoZ	containerZfulldevpathZstatretexcr!   r!   r"   maybe_get_writable_device_path   s^    



 rL   )r   cfgcloudargsreturnc             C   s  t |dkr|d }ntj|dd}tj|tgdsDtjd|  d S d}tj|t}|shtjd| d S |\}}}	|dkr|j	dd }
tj
|
}|sd S |
}d	||	|f }tjd
| t||}|sd S d }t|||rtjd|| d S |j }x$tD ]\}}|j| r|}P  qW |s2tjd|| d S |||}tjd||dj| |tkrxtjtjtjdt|fd ntjtjdt|fd d}|tkrd}tjd||| d S )Nr   Zresize_rootfsT)Zaddonsz+Skipping module named %s, resizing disabledr=   z)Could not determine filesystem type of %sr3   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 zbackgrounded Resizing)Zlogfuncmsgr;   rO   ZResizingZResizedzResizing (via forking)z$%s root filesystem (type=%s, val=%s))lenr   Zget_cfg_option_strZtranslate_boolNOBLOCKr?   rA   Zget_mount_infor@   r   Zget_device_info_from_zpoolrL   r<   r4   RESIZE_FS_PREFIXES_CMDSr0   joinZfork_cbZlog_time	do_resize)r   rM   rN   rO   Zresize_rootr8   resultr   r7   r   r(   rJ   Zresizerr9   ZpfixZroot_cmd
resize_cmdactionr!   r!   r"   handle   sv    








r[   c             C   s:   yt j |  W n& t jk
r4   tjtd|   Y nX d S )Nz$Failed to resize filesystem (cmd=%s))r   r.   r   Zlogexcr?   )rY   r!   r!   r"   rW   F  s
    rW   )*__doc__rE   Zloggingr   rC   textwrapr   Z	cloudinitr   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r   Zcloudinit.distrosr	   Zcloudinit.settingsr
   rT   metaZ	getLogger__name__r?   r#   r$   r%   r'   r)   r+   r-   r2   rU   r5   r<   rL   strlistr[   rW   r!   r!   r!   r"   <module>	   sX   

!
PY