
"T`.                 @   sb  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	 d d l m
 Z
 d d l m Z e	 Z d d d	 d
 g d d i Z Gd d   d e  Z e j e  Z d d   Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z d d   Z d d   Z d d   Z d d   Z d d    Z d! e f d" e f f Z d S)#a  
Growpart
--------
**Summary:** grow partitions

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. Each entry in the devices list can be either the path to the
device's mountpoint in the filesystem or a path to the block device in
``/dev``.

The utility to use for resizing can be selected using the ``mode`` config key.
If the ``mode`` key is set to ``auto``, then any available utility (either
``growpart`` or BSD ``gpart``) will be used. If neither utility is available,
no error will be raised. If ``mode`` is set to ``growpart``, then the
``growpart`` utility will be used. If this utility is not available on the
system, this will result in an error. If ``mode`` is set to ``off`` or
``false``, then ``cc_growpart`` will take no action.

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

**Internal name:** ``cc_growpart``

**Module frequency:** per always

**Supported distros:** all

**Config keys**::

    growpart:
        mode: <auto/growpart/off/false>
        devices:
            - "/"
            - "/dev/vdb1"
        ignore_growroot_disabled: <true/false>
    N)log)
PER_ALWAYS)subp)utilmodeautodevices/ignore_growroot_disabledFc               @   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   V   s   r   c       	      C   s   d  } |  d k rZ x0 t  D]( \ } } |   } | j   r | } Pq W| s t d   nx i  } x t  D] \ } } | | | <qg W|  | k r t d |    | |    } | j   r | } | s t d |    | S)Nr   zNo resizers availablezunknown resize mode %szmode %s not available)RESIZERS	available
ValueError	TypeError)	r   Zresize_class_nameresizerZcurZmmapkvZmclassr   r   r   resizer_factory`   s(    	r   c               @   s   e  Z d  Z d S)ResizeFailedExceptionN)r   r   r   r   r   r   r   r   ~   s   r   c               @   s(   e  Z d  Z d d   Z d d   Z d S)ResizeGrowPartc             C   sp   t  j j   } d | d <y; t j d d g d | \ } } t j d |  rS d SWn t j k
 rk Yn Xd S)	NCLANGgrowpartz--helpenvz--update\s+TF)osenvironcopyr   researchProcessExecutionError)selfmyenvoutZ_errr   r   r   r      s    
!zResizeGrowPart.availablec             C   s  t  |  } y t j d d | | g  Wnf t j k
 r } zC | j d k ru t j t d | |  t |  |  | | f SWYd  d  } ~ Xn Xy t j d | | g  WnM t j k
 r } z* t j t d | |  t |  |  WYd  d  } ~ Xn X| t  |  f S)Nr"   z	--dry-run   z&Failed growpart --dry-run for (%s, %s)zFailed: growpart %s %s)get_sizer   r)   	exit_coder   logexcLOGr   )r*   diskdevpartnumpartdevbeforeer   r   r   resize   s    
"zResizeGrowPart.resizeN)r   r   r   r   r7   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 j   } d | d <yG t j d d g d | d d d g \ } } t j d	 |  r_ d
 SWn t j k
 rw Yn Xd S)Nr    r!   gparthelpr#   Zrcsr   r-   zgpart recover TF)r$   r%   r&   r   r'   r(   r)   )r*   r+   Z_outerrr   r   r   r      s    
-zResizeGpart.availablec             C   s  y t  j  d d | g  WnY t  j k
 ru } z6 | j d k rc t j t d |  t |  |  WYd d } ~ Xn Xt |  } y  t  j  d d d | | g  WnM t  j k
 r } z* t j t d | |  t |  |  WYd d } ~ Xn 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.
        r9   Zrecoverr   zFailed: gpart recover %sNr7   z-izFailed: gpart resize -i %s %s)r   r)   r/   r   r0   r1   r   r.   )r*   r2   r3   r4   r6   r5   r   r   r   r7      s    " "zResizeGpart.resizeN)r   r   r   r   r7   r   r   r   r   r8      s   r8   c             C   sD   t  j |  t  j  } z t  j | d t  j  SWd  t  j |  Xd  S)Nr   )r$   openO_RDONLYlseekSEEK_ENDclose)filenamefdr   r   r   r.      s    r.   c             C   sW  t  j j |   } t  j j |  } d | } t j   r{ d t j |   } t j d |  } | j	 d  | j	 d  f St  j j
 |  s t d |  | f   t  j j | d  } t  j j
 |  s t d |    t j |  j   } t  j j |  } t  j j |  }	 t j t  j j |	 d	   j   }
 t  j j d
 |
  } | | f S)Nz/sys/class/block/%sz/dev/z^(/dev/.+)p([0-9])$r-      z%s had no syspath (%s)	partitionz%s not a partitiondevz/dev/block/%s)r$   pathrealpathbasenamer   Z
is_FreeBSDZfind_freebsd_partr'   r(   groupexistsr   joinr   Z	load_filerstripdirname)ZdevpathZrpathZbnameZsyspathZfreebsd_partmZptpathptnumZrsyspathZdisksyspathZ
diskmajminZdiskdevpathr   r   r   device_part_info   s$    
$rP   c             C   s   |  j  d  r |  St j |   } | s4 t d   | d } t j   } | d k r | r t j t j    } | d  k r t j j	 |  r | St d   | S)Nz/dev/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_cmdliner$   rF   rJ   )deventresultrE   Z	containerr   r   r   
devent2dev   s    
rT   c          >   C   sL  g  } x?| D]7} y t  |  } WnE t k
 rj } z% | j | t j d | f  w WYd  d  } ~ Xn Xy t j |  } WnK t k
 r } z+ | j | t j d | | f f  w WYd  d  } ~ Xn Xt j | j	  rt j
 | j	  r| j | t j d | f  q y t |  \ } } WnQ t t f k
 r{} z+ | j | t j d | | f f  w WYd  d  } ~ Xn Xyz |  j | | |  \ }	 }
 |	 |
 k r| j | t j d | | f f  n) | j | t j d | | |	 |
 f f  Wq t k
 rC} z+ | j | t j d | | | f f  WYd  d  } ~ Xq Xq W| S)Nzunable to convert to device: %szstat of '%s' failed: %szdevice '%s' not a block devicez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)rT   r   appendr   r   r$   statOSErrorS_ISBLKst_modeS_ISCHRrP   r   r7   r   r   r   r   )r   r   inforR   Zblockdevr6   ZstatretZdiskrO   oldnewr   r   r   resize_devices  sL    +r^   c             C   s  d | k r' | j  d t  t | d <| j d  } t | t  sV | j d  d  S| j d d  } t j |  r | j  d |  d  St j | j d d   r t j	 j
 d	  r | j  d
  | j  d  d  St j | d d g  } t |  s| j  d  d  Sy t |  } WnX t t f k
 rv}	 z2 | j  d | |	 f  | d k r`|	  d  SWYd  d  }	 ~	 Xn Xt j d | j  d d d t d | | f  }
 xZ |
 D]R \ } } } | t j k r| j d | | f  q| j  d | | | f  qWd  S)Nr"   z.No 'growpart' entry in cfg.  Using default: %sz#'growpart' in config was not a dictr   r   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': %sZlogfuncmsgr^   funcargsz'%s' resized: %sz'%s' %s: %s)debugDEFAULT_CONFIGget
isinstancedictZwarningr   Zis_falser$   rF   isfileZget_cfg_option_listlenr   r   r   Zlog_timer^   r   r   r[   )r   ZcfgZ_cloudr   Z_argsZmycfgr   r   r   r6   Zresizedentryactionr_   r   r   r   handleB  sD    	
rk   r"   r9   )__doc__r$   os.pathr'   rV   Z	cloudinitr   ZloggingZcloudinit.settingsr   r   r   Z	frequencyrc   objectr   Z	getLoggerr   r1   r   	Exceptionr   r   r8   r.   rP   rT   r^   rk   r   r   r   r   r   <module>A   s0   	"''1,