
    x[h*              
       v   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	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
Zdegeg dZeed<    ej2                  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)Optional)	lifecyclesubputil)Cloud)Config)
MetaSchema)ALL_DISTROS)
PER_ALWAYSnoblockcc_resizefs)iddistros	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      j                  j                  d      d	   j                  d
      d   j                               }||k\  r&|j                  d      }|j                  |dz   d       t        |      S )Nz%s/.snapshotsbtrfs
filesystemresizemaxz5.10z	--version
r   v   z	--enqueue)r   mount_is_read_writeospathisdirr   Versionfrom_strr   stdout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_btrfsr/   %   s     ##K0RWW]]+%6 k)
 h{C
 !((11&9 ((11		7K()	dA		sB	 
	 ++ii!

37K(:    c                 
    d|fS )N	resize2fs r(   r)   s     r.   _resize_extr5   I   s      r0   c                 
    d| fS )N
xfs_growfsr3   r4   s     r.   _resize_xfsr8   M   s    +&&r0   c                     dd| fS )Ngrowfsz-yr3   r4   s     r.   _resize_ufsr;   Q   s    dK((r0   c                     ddd| |fS )Nzpoolonlinez-er3   r4   s     r.   _resize_zfsr?   U   s    Xt[&99r0   c                     dd| fS )Nhammer2r:   r3   r4   s     r.   _resize_hammer2rB   Y   s    x--r0   c                     ddd|fS )zSingle device resizebcachefsdevicer   r3   r4   s     r.   _resize_bcachefsrF   ]   s    (F33r0   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        r.   _can_skip_resize_ufsrN   b   sh     *JCL		8T6*+  %% 88z*|qxx/GGs    A')A" A""A'r   extxfsufszfsrA   rD   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/zfsr=   statusz0zpool status returned error: [%s] for zpool [%s]z$Unable to get zpool status of %s: %sz.*(ONLINE).*r   stater   zfound zpool "%s" on disk %sz'No zpool found: [%s]: out: [%s] err: %s)r   is_containerLOGdebugwarningr   r   existsr   inforH   r#   research)r=   log_warnzpoolstatuserrrlinedisks          r.   get_device_info_from_zpoolre      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itemsrJ   )fs_typeresize_whatr)   	fstype_lcifuncs         r.   can_skip_resizero      sI    I*002 -4"V,,- r0   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 command line 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   rW   r   r   r[   rootdev_from_cmdlineget_cmdlinerX   rZ   rY   rJ   lstripstatOSErrorerrnoENOENTS_ISBLKst_modeS_ISCHR)devpathr\   	containerfulldevpathstatretexcs         r.   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;namecfgcloudargsc                    |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"        |       nt#        |       d}|t        k(  rd}t        j                  d|||       y )Nr   resize_rootfsT)addonsz+Skipping module named %s, resizing disabledrr   z)Could not determine filesystem type of %srR   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 ResizedzResizing (via forking)z$%s root filesystem (type=%s, val=%s))r   get_cfg_option_strtranslate_boolNOBLOCKrX   rY   get_mount_inforZ   r#   re   r   ro   rg   RESIZE_FS_PREFIXES_CMDSrJ   joinfork_cb	do_resize)r   r   r   r   resize_rootrk   resultr)   rj   r(   r=   r\   resizerrl   pfixroot_cmd
resize_cmdactions                     r.   handler      s   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1 h%G
 <	

 	f-JII#['388J;O g 		

 	*Fg)II.r0   c                     	 t        j                   |        y # t         j                  $ r t        j                  t        d|         w xY w)Nz$Failed to resize filesystem (cmd=%s))r   rH   r   logexcrX   )r   s    r.   r   r   K  s<    		*%% C?Ls	    0A)-__doc__rx   loggingr   r]   rv   typingr   	cloudinitr   r   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar	   cloudinit.distrosr
   cloudinit.settingsr   r   r   __annotations__	getLogger__name__rX   r/   r5   r8   r;   r?   rB   rF   rN   r   rh   strre   ro   r   listr   r   r3   r0   r.   <module>r      s3   A   	 	   + + ! # . ) )
 }!	j  g!!H!'):.4
. m
K
K
K
K !"  !"67 # <M`M M6 M% Mt M M`r0   