
    fA/              
          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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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d<    ee      Z  ej6                  e      Zd Zd Zd Z d Z!d Z"d Z#d Z$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#e	e(   fd$Z)d% Z*d& Z+d'e(d(ed)ed*e,d#df
d+Z-d, Z.y)-z:Resizefs: cloud-config module which resizes the filesystem    N)dedent)Optional)subputil)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROS)
PER_ALWAYSnoblockcc_resizefsResizefszResize filesystema          Resize a filesystem to use all available 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descriptiondistrosexamples	frequencyactivate_by_schema_keysmetac                    t        j                  |       s-t        j                  j	                  d| z        rddddd| z  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z   d       t        |      S )Nz%s/.snapshotsbtrfs
filesystemresizemaxz5.10z	--versionr   v   z	--enqueue)r   mount_is_read_writeospathisdirVersionfrom_strr   splitstripindexinserttuple)mount_pointdevpthcmdbtrfs_with_queuesystem_btrfs_veridxs         >/usr/lib/python3/dist-packages/cloudinit/config/cc_resizefs.py_resize_btrfsr3   :   s     ##K0RWW]]+%6 k)
 h{C
 ||,,V4||,,		7K()!,2237;AAC ++ii!

37K(:    c                 
    d|fS )N	resize2fs r,   r-   s     r2   _resize_extr9   [   s      r4   c                 
    d| fS )N
xfs_growfsr7   r8   s     r2   _resize_xfsr<   _   s    +&&r4   c                     dd| fS )Ngrowfsz-yr7   r8   s     r2   _resize_ufsr?   c   s    dK((r4   c                     ddd| |fS )Nzpoolonlinez-er7   r8   s     r2   _resize_zfsrC   g   s    Xt[&99r4   c                     dd| fS )Nhammer2r>   r7   r8   s     r2   _resize_hammer2rF   k   s    x--r4   c                     ddd|fS )zSingle device resizebcachefsdevicer   r7   r8   s     r2   _resize_bcachefsrJ   o   s    (F33r4   c                     d}d}	 t        j                   dd|g       y# t         j                  $ r5}|j                  j                  |      r||j                  v rY d }~y|d }~ww xY w)Nzgrowfs: requested sizez.is not larger than the current filesystem sizer>   z-NTF)r   ProcessExecutionErrorstderr
startswith)r,   r-   
skip_startskip_containes        r2   _can_skip_resize_ufsrR   t   sh     *JCL		8T6*+  %% 88z*|qxx/GGs    A')A" A""A'r   extxfsufszfsrE   rH   returnc                 z   t        j                         rt        j                  nt        j                  }t
        j                  j                  d      st        j                  d       y 	 t        j                  dd| g      \  }}|rt        j                  d||        y 	 d}|j                  d      D ]P  }t        j                  ||      s| |vsd	|vs$|j                         d
   }t        j                  d| |       |c S   |d| ||       y # t        j                  $ r} |d| |       Y d }~y d }~ww xY w)Nz/dev/zfsz"Cannot get zpool info, no /dev/zfsrA   statusz0zpool status returned error: [%s] for zpool [%s]z$Unable to get zpool status of %s: %sz.*(ONLINE).*
stater   zfound zpool "%s" on disk %sz'No zpool found: [%s]: out: [%s] err: %s)r   is_containerLOGdebugwarningr"   r#   existsr   inforL   r'   research)rA   log_warnzpoolstatuserrrlinedisks          r2   get_device_info_from_zpoolrj      s    --/syyS[[H77>>*%		6799gx%?@SHHB
   	A!!$' 99Q%t"3t8K::<?DII3UDAK	
 15+s  %% 7Ds   +4D D:&
D55D:c                     | j                         }t        j                         D ]!  \  }}|j                  |      s |||      c S  y)NF)lowerRESIZE_FS_PRECHECK_CMDSitemsrN   )fs_typeresize_whatr-   	fstype_lcifuncs         r2   can_skip_resizert      sI    I*002 -4"V,,- r4   c                 |   t        j                         }| dk(  rvt        j                  j	                  |       sW|sUt        j
                  t        j                               } | t        j                  d       yt        j                  d|        | dk(  rt        j                  d| |       y| j                  d      rt        j                  d       | S | j                  d	      sLt        j                  j	                  |       s-d	| j                  d
      z   }t        j                  d| |       |} 	 t        j                  |       }t        j                   |j"                        sQt        j$                  |j"                        s2|rt        j                  d| |       yt        j                  d| |       y| S # t        $ rx}|r5|j                  t        j                  k(  rt        j                  d| |       n7|j                  t        j                  k(  rt        j                  d| |       n|Y d}~yd}~ww xY w)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 cmdline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   r\   r"   r#   r`   rootdev_from_cmdlineget_cmdliner]   r_   r^   rN   lstripstatOSErrorerrnoENOENTS_ISBLKst_modeS_ISCHR)devpathra   	containerfulldevpathstatretexcs         r2   maybe_get_writable_device_pathr      s    !!#I 	;w'++D,<,<,>??KK;<		BGL-		=wM
 &!		78 g&rww~~g/Fs 33		H	

 '''"  <<(goo1NII%	  KKC
 N=  ell2IIK
 YY%,,&KK> Is   3F: :	H;A.H66H;r   cfgcloudargsc                 \   t        |      dk7  r|d   }nt        j                  |dd      }t        j                  |t        g      st
        j                  d|        y d}t        j                  |t
              }|st
        j                  d|       y |\  }}}	|dk(  r$|j                  d      d   }
t        |
      }|sy |
}d	|d
|	d|}t
        j                  d|       t        ||      }|sy d }t        |||      rt
        j                  d||       y |j                         }t        D ]  \  }}|j                  |      s|} n |st
        j                  d||       y  |||      }t
        j                  d||dj!                  |             |t        k(  r<t        j"                  t        j$                  t
        j                  dt&        |f       n,t        j$                  t
        j                  dt&        |f       d}|t        k(  rd}t
        j                  d|||       y )Nr   resize_rootfsT)addonsz+Skipping module named %s, resizing disabledrw   z)Could not determine filesystem type of %srV   zdev=z mnt_point=z path=z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)logfuncmsgrs   r   ResizingResizedzResizing (via forking)z$%s root filesystem (type=%s, val=%s))lenr   get_cfg_option_strtranslate_boolNOBLOCKr]   r^   get_mount_infor_   r'   rj   r   rt   rl   RESIZE_FS_PREFIXES_CMDSrN   joinfork_cblog_time	do_resize)r   r   r   r   resize_rootrp   resultr-   ro   r,   rA   ra   resizerrq   pfixroot_cmd
resize_cmdactions                     r2   handler     s	   
4yA~1g--c?DI{G9=		?F K  c2F?M%+"VWk %S!!$+E2,2KMDII&+FD9FGwV4		3Wk	
 	I3 x%G
 <	

 	f-JII#['388J;O g 	MMII'	
 	II		
 Fg)II.r4   c                     	 t        j                   |        y # t         j                  $ r t        j                  t        d|         w xY w)Nz$Failed to resize filesystem (cmd=%s))r   rL   r   logexcr]   )r   s    r2   r   r   f  s<    		*%% C?Ls	    0A)/__doc__r}   loggingr"   rb   r{   textwrapr   typingr   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar	   r
   cloudinit.distrosr   cloudinit.settingsr   r   r   __annotations__	getLogger__name__r]   r3   r9   r<   r?   rC   rF   rJ   rR   r   rm   strrj   rt   r   listr   r   r7   r4   r2   <module>r      s`   A   	 	      ! # < ) )
  		J }JK !-j 2 t
g!B!'):.4
. m
K
K
K
K !"  !"67 # <M`V V6 V% Vt V Vrr4   