+
    iK                       a  R0 t60 t R t^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHtH	t	H
t
HtHt ^ RIHtHtHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt RR	R
R.R]R. /t] ^ k RtRtRtRtR1tR2t]P@                  ! ]!4      t"R t#R t$R t%R3R lt&R3R lt'R R lt(R4R lt)R R lt*R5R lt+R t,R R lt-R  R! lt.R" R# lt/R$ R% lt0R& R' lt1R( R) lt2R* R+ lt3R3R, R- llt4R. R/ lt5R# )6z-Mounts: Configure mount points and swap filesN)DictListOptionalTuplecast)performancesubputil)Cloud)Config)
MetaSchema)PER_INSTANCEid	cc_mountsdistrosall	frequencyactivate_by_schema_keysz&^([x]{0,1}[shv]d[a-z][0-9]*|sr[0-9]+)$z^.+:.*z
/etc/fstabzcomment=cloudconfigc                     V R9   d   R# R F5  pV P                  V4      '       g   K  V P                  R4      R8X  g   K4   R# 	  R# )amiT:F)r   rootswap)	ephemeralebs)
startswithfind)nameenumnames   & </usr/lib/python3/dist-packages/cloudinit/config/cc_mounts.pyis_meta_device_namer!   ,   s@    && )??8$$32)= )     c                 L    \         P                  ! \        V 4      '       d   R # R# )TF)rematchNETWORK_NAME_FILTER)r   s   &r    is_network_devicer'   8   s    	xx#T**r"   c                     \        V4      R V: 2RV: 2.pV F4  pV : V: 2p\        P                  P                  V4      '       g   K2  Vu # 	  R# )pz-partN)strospathexists)device_pathpartition_numberpotential_suffixessuffixpotential_partition_devices   &&   r    _get_nth_partition_for_devicer3   ?   sU    !#%'
 %/:F%C"77>>455-- % r"   c                    \         P                  P                  V 4      P                  R 4      R,          p\         P                  P	                  RV4      pVeT   \         P                  P	                  V\         P                  P                  V4      P                  R 4      R,          4      p\         P                  P                  V4      # )/z/sys/block/r   )r+   r,   realpathsplitjoinr-   )r.   partition_pathdevice_namesys_paths   &&  r    _is_block_devicer<   L   s    ''"";/55c:2>Kww||M;7H!77<<bgg&&~6<<SA"E
 77>>(##r"   c                    \         P                  R V 4       T pVR8X  d   Rp\         P                  R4       \        V 4      '       d   V # \        P                  ! V4      w  rETpV'       d.   VP                  WD4      pWd8w  d   \         P                  RWd4       \        V4      '       dG   V! V4      pV'       g   R# VP                  R4      '       g   RV: 2p\         P                  RWd4       M'\        P                  ! \        V 4      '       d   RV: 2pRpVf   \        V^4      pM\        WE4      pVf   R# \        WG4      '       d	   Ve   V# V# R# )	z+Attempting to determine the real name of %sr   
ephemeral0z2Adjusted mount option from ephemeral to ephemeral0zMapped device alias %s to %sNr5   z/dev/zMapped metadata name %s to %s)LOGdebugr'   r	   expand_dotted_devnamegetr!   r   r$   r%   DEVICE_NAME_FILTERr3   r<   )	startnametransformeraliasesdevnamer.   r/   origr9   s   &&&     r    sanitize_devnamerI   V   s-   II;YG G+		FG##$($>$>w$G!KDkk+;II4dH;''!+.%%c**'24K		14E88&	22'24KN6{AF6
 !44%!!r"   c                    V ^8  d   QhR\         R\        \         ,          R\        \         \         3,          R\        /# )   original	sanitized
fstab_devsreturn)r*   r   r   bool)formats   "r    __annotate__rR      s8      &sm9=c3h	r"   c                    W8w  d   \         P                  RW4       Vf   \         P                  RV 4       R# W9   d    \         P                  RVW!,          4       R# R# )z*Get if the sanitized device name is valid.zchanged %s => %sz+Ignoring nonexistent default named mount %sFz&Device %s already defined in fstab: %sT)r?   r@   )rL   rM   rN   s   &&&r    sanitized_devname_is_validrT      sW     		$h:		?J		 		4!	

 r"   c           	         V f   \         P                  ! 4       R,          p V ^,          pRRRVRV /pVf   Vf   TpMV'       dy   \        P                  ! V4      pVP                  VP
                  ,          pWdR&   Vf   \        \        V^,          4      V4      pM&WR,          8  d   \        VR,          4      pMVf   TpWR&   V ^\        ,          8  d   T pMUV ^\        ,          8  d   ^\        ,          pM5\        \        P                  ! V \        ,          4      4      \        ,          p\        Wq4      pWR&   / p	VP                  4        F7  w  r\        V\        4      '       d   R	V\        ,          ,          W&   K3  WV
&   K9  	  \        P!                  R
V	R,          V	R,          V	R,          V	R,          V	R,          4       V# )Ntotalavailnamax_inmemg?maxsizez%s MBzCsuggest %s swap for %s memory with '%s' disk given max=%s [max=%s]')r	   read_meminfor+   statvfsf_frsizef_bfreeminintGBroundmathsqrtitems
isinstanceMBr?   r@   )memsizemaxsizefsyssugg_maxinfor^   rW   minsizer\   pinfokvs   &&&         r    suggested_swapsizers      s{   ##%g.{HT8WeW=D|	**T"  7??2W?#eai.(3G&%#+&G	KR	27	b&		'B,/025w DLE

a!b&)EH!H	  IIMfeghe Kr"   c                4    V ^8  d   QhR\         R\         RR/# )rK   fnamer\   rO   N)r*   )rQ   s   "r    rR   rR      s!     ; ;3 ;c ;d ;r"   c                  aa RoVV3R lp\         P                  P                  V 4      p\        P                  ! V4       \        P
                  ! V4      ^,          oSR8X  d4   \        P                  ! RRRV .4       \        P                  ! RRV .4       SR	8X  d%   \        P                  ! 4       R8  d   V! WR
4       M
 V! WR4       \         P                  P                  V 4      '       d   \        P                  ! V R4        \        P                  ! RV .4       R#   \        P                   d"    \        P                  R4       T! YR
4        Li ; i  \        P                   d    \        P                  ! T 4       h i ; i)zSize is in MiB.z6Failed to create swapfile '%s' of size %sMB via %s: %sc                   < \         P                  R V SV4       VR8X  d   RRRV,          V .pM3VR8X  d   RRRV ,          RRV,          .pM\        P                  ! R	4      h \        P                  ! VR
R7       R#   \        P                   d4   p\         P	                  SYY$4       \
        P                  ! T 4       h Rp?ii ; i)z3Creating swapfile in '%s' on fstype '%s' using '%s'	fallocatez-lz%sMddzif=/dev/zerozof=%szbs=1Mzcount=%sz:Missing dependency: 'dd' and 'fallocate' are not availableT)captureN)r?   r@   r   ProcessExecutionErrorrn   r	   del_file)ru   r\   methodcmdeerrmsgfstypes   &&&  r    create_swap$create_swapfile.<locals>.create_swap   s    		A		
 [ edlE:Ct^%T!C ,,L 	IIc4()) 	HHVU&4MM% 	s   #A= =C.C  Cbtrfstruncatez-s0chattrz+Cxfsry   rx   z4fallocate swap creation failed, will attempt with ddi  mkswapN)      )r+   r,   dirnamer	   
ensure_dirget_mount_infor   kernel_versionr{   r?   rn   r-   chmodr|   )ru   r\   r   swap_dirr   r   s   &&  @@r    create_swapfiler      s1    FF< wwu%HOOH  *1-F		:tS%01		8T5)*4..07:E&	+[1
 
ww~~e

5% 		8U#$ )) 	+HHKLT*	+ %% es   	D$ 
E $3EE-F
c                N   \         P                  P                  V 4      p\        V4      P	                  4       R8X  dA    \
        P                  ! 4       R,          p\
        P                  ! T4       \        Y2TR7      p\        \        VR,          4      4      pV'       g   \        P                  R4       R# \        P                  ! R4      ;_uu_ 4        \        W4       RRR4       V #   \         d    \        P                  R4        R# i ; i  + '       g   i     T # ; i)	z
fname: full path string of filename to setup
size: the size to create. set to "auto" for recommended
maxsize: the maximum size
autorV   z)Not creating swap: failed to read meminfoN)rl   rk   rj   z'Not creating swap: suggested size was 0zSetting up swap file   )r+   r,   r   r*   lowerr	   r]   IOErrorr?   r@   r   rs   rb   r   Timedr   )ru   r\   rk   r   rj   mibsizes   &&&   r    setup_swapfiler     s     wwu%H
4yF"	'')'2G
 	!!G
 #den%&G		;<			1	2	2' 
3 L#  	IIAB	 
3	2 Ls   C, D, DDD$	c                X   \        V \        4      '       g   \        P                  R4       R# V P	                  RR4      pV P	                  R^ 4      pV P	                  RR4      pV'       d	   V'       g   \        P                  R4       R# \        P                  P                  V4      '       d   \        P                  P                  R4      '       g   \        P                  R	V4       V#  \        P                  ! R4      P                  4        F;  pVP                  VR
,           4      '       g   K#  \        P                  RV4       Vu # 	  \        P                  RV4        \        V\        4      '       d   VR8w  d   \        P                  ! V4      p\        V\        4      '       d   \        P                  ! V4      p\!        WVR7      #   \         d    \        P                  RT4       Tu # i ; i  \         d"   p\        P                  RT4        Rp?R# Rp?ii ; i)zYhandle the swap config, calling setup_swap if necessary.
return None or (filename, size)
z%input for swap config was not a dict.Nfilenamez	/swap.imgr\   rk   zno need to setup swapz/proc/swapsz:swap file %s exists, but no /proc/swaps exists, being safe zswap file %s already in usez+swap file %s exists, but not in /proc/swapsz.swap file %s exists. Error reading /proc/swapsr   )ru   r\   rk   zfailed to setup swap: %s)rh   dictr?   warningrB   r@   r+   r,   r-   r	   load_text_file
splitlinesr   	Exceptionr*   human2bytesr   )swapcfgru   r\   rk   liner   s   &     r    handle_swapcfgr   /  s    gt$$;<KK
K0E;;vq!Dkk)T*GU		)*	ww~~eww~~m,,IIL L
	++M:EEG??53;//II;UC L H IICUK3dC  TV^##D)Dgs##&&w/GEgFF  	KK@% L		  3.223s8   .AG 5G G (A+G= #G:9G:=H)H$$H)c                    V ^8  d   QhR\         \        \        ,          \        \        \        3,          \        \        ,          3,          /# )rK   rO   )r   r   r*   r   )rQ   s   "r    rR   rR   _  s/     2 2U49d38nd3i?@ 2r"   c                 n   . p / p. p\         P                  P                  \        4      '       d   \        P
                  ! \        4      P                  4        FW  p\        V9   d   VP                  V4       K!  VP                  4       pV'       g   K;  W1V^ ,          &   V P                  V4       KY  	  WV3# )aW  Parse /etc/fstab.

Parse fstab, ignoring any lines containing "comment=cloudconfig".
:return: A 3-tuple containing:
    - A list of lines exactly as they appear in fstab
    - A dictionary with key being the first token in the line
      and value being the entire line
    - A list of any lines that were ignored due to "comment=cloudconfig"
)
r+   r,   r-   
FSTAB_PATHr	   r   r   MNT_COMMENTappendr7   )fstab_linesrN   fstab_removedr   tokss        r    parse_fstabr   _  s     KJM	ww~~j!!''
3>>@Dd"$$T*::<Dt&*47#""4( A M11r"   c                T   V ^8  d   QhR\         \        \         \        \        ,          ,          ,          ,          R\        \        \        3,          R\        \        \        3,          R\         \        \        ,          ,          R\        R\         \         \        ,          ,          /# )rK   mountsrN   device_aliasesdefault_fieldscloudrO   )r   r   r*   r   r
   )rQ   s   "r    rR   rR   y  sx     + +$x}-./+S#X+ cN+ #'	+
 + 
$s)_+r"   c                   . pV  F  p\        V\        4      '       g   \        P                  RV4       K1  \	        V^ ,          4      p\        WtP                  VR7      p\        WxV4      '       d   V.VR,          ,           p	MTp	\        V	4       F(  w  rVf   W:,          W&   K  \	        W,          4      W&   K*  	  W\        V	4      R ,          p	VP                  V	4       K  	  V# )zSanitize mounts to ensure we can work with devices in config.

Specifically:
 - Ensure the mounts configuration is a list of lists
 - Transform and sanitize device names
 - Ensure all tokens are strings
 - Add default options to any lines without options
z%Mount option not a list, ignoring: %srF   :   NNN)rh   listr?   r   r*   rI   device_name_to_devicerT   	enumeratelenr   )r   rN   r   r   r   updated_linesr   startsanitized_devnameupdated_lineindextokens   &&&&&       r    sanitize_mounts_configurationr   y  s     M$%%KK?FDG,..
 &e
KK-.b9LL &l3LE}&4&;#&),*=&>#	 4 	s<'8':;;\*5 6 r"   c                    V ^8  d   QhR\         \         \        ,          ,          R\         \         \        ,          ,          /# )rK   r   rO   r   r*   )rQ   s   "r    rR   rR     s*      tDI 4S	? r"   c                
   . p. pV RRR1,           Fd  pV^,          e   V^ ,          V9   d8   \         P                  RV^ ,          4       VP                  V^ ,          4       KS  VP                  V4       Kf  	  VRRR1,          # )zRemove any entries that have a device name that doesn't exist.

If the second field of a mount line is None (not the string, the value),
we skip it along with any other entries that came before it that share
the same device name.
Nz$Skipping nonexistent device named %sr   )r?   r@   r   )r   actlistdev_denylistr   s   &   r    remove_nonexistent_devicesr     sw     GLtt7?d1g5II<d1gFQ(NN4   4R4=r"   c                    V ^8  d   QhR\         \         \        ,          ,          R\        R\        \        \        3,          R\        \        \        3,          R\        R\         \         \        ,          ,          /# )rK   r   default_mount_optionsrN   r   r   rO   )r   r*   r   r
   )rQ   s   "r    rR   rR     sd     ! !cO!! S#X! cN	!
 ! 
$s)_!r"   c                  a	 \         P                  ! V 4      pRRRVRR.. RO3 F  o	S	^ ,          p\        WdP                  VR7      p\	        WgV4      '       g   K7  \        \        V4      S	^ &   \        ;QJ d    V	3R lV  4       F  '       g   K   RM	  R	M! V	3R lV  4       4      pV'       d   \        P                  R
V4       K  VP                  S	4       K  	  V# )zwAdd default mounts to the user provided mount configuration.

Add them only if no other entry has the same device name
r>   z/mntr   r   2r   c              3   H   <"   T F  q^ ,          S^ ,          8H  x  K  	  R# 5i)    N ).0cfgmdefault_mounts   & r    	<genexpr>,add_default_mounts_to_cfg.<locals>.<genexpr>  s       %
4:DG}Q''Fs   "TFz-Not including %s, already previously included)r   noner   swr   r   )copydeepcopyrI   r   rT   r   r*   anyr?   r@   r   )
r   r   rN   r   r   
new_mountsr   rM   default_already_existsr   s
   &&&&&    @r    add_default_mounts_to_cfgr     s     v&J	vv'<c3G0 a $..
	 *%JGG  Y/a!$ %
4:%
 %
4:%
 "
 "IIEuM-()* r"   c                    V ^8  d   QhR\         \         \        ,          ,          R\         \         \        ,          ,          /# )rK   r   rO   r   )rQ   s   "r    rR   rR     s*      d3i T$s)_ r"   c                    V  Uu. uF2  pVR,          V^,           R\          2.,           VR,          ,           NK4  	  up# u upi )z=Add "comment=cloudconfig" to the mount options of each entry.:N   N,:r   NN)r   )r   entrys   & r    add_commentr     sP     E 	b	az;-011E"I==  s   8A c                T    V ^8  d   QhR\         \         \        ,          ,          RR/# )rK   r   rO   Nr   )rQ   s   "r    rR   rR     s"     $ $T$s)_ $ $r"   c                    \         ;QJ d    R V  4       F  '       g   K   RM	  RM! R V  4       4      '       d   \        P                  ! RR.4       R# R# )z1Call 'swapon -a' if any entry has a swap fs type.c              3   8   "   T F  q^,          R8H  x  K  	  R# 5i)rK   r   Nr   )r   r   s   & r    r   *activate_swap_if_needed.<locals>.<genexpr>  s     
37%8v7s   TFswapon-aN)r   r   )r   s   &r    activate_swap_if_neededr     s9    
s
37
3sss
37
333		8T"# 4r"   c                V    V ^8  d   QhR\         R\         R\        \        ,          RR/# )rK   uses_systemdchanges_madedirsrO   N)rP   r   r*   )rQ   s   "r    rR   rR     s0     6 66&*626s)6	6r"   c                |   RpV'       d   RpMe\         P                  ! 4       P                  4        Uu0 uF  pRV9   g   K  VR,          kK  	  pp\        \	        V4      P                  V4      4      pV'       d=   \        P                  ! RR.4       V '       d   \        P                  ! RR.4       R# R# R# u upi )	zCall 'mount -a' if needed.

If changes were made, always call 'mount -a'. Otherwise, call 'mount -a'
if any of the directories in the mount list are not already mounted.
FT
mountpointmountr   	systemctlzdaemon-reloadN)r	   r   valuesrP   set
differencer   )r   r   r   do_mountvalmount_pointss   &&&   r    mount_if_neededr     s     H {{}++-
-s" C- 	 

 D	,,\:;		7D/"II{O45  
s   	B9B9c                >    V ^8  d   QhR\         \        ,          RR/# )rK   ds_remove_entryrO   N)r   r*   )rQ   s   "r    rR   rR     s"     O O8C= OD Or"   c                   a \         P                  P                  \        4      '       g   R # \        .pV '       d   VP                  V 4       \        \        R4      ;_uu_ 4       pVP                  4       pR R R 4       . pRpX FX  o\        ;QJ d    V3R lV 4       F  '       d   K   RM	  RM! V3R lV 4       4      '       d   RpKG  VP                  S4       KZ  	   V'       dO   \        \        R4      ;_uu_ 4       pVP                  V4       R R R 4       \        P                  R\        4       R # R #   + '       g   i     L; i  + '       g   i     LA; i  \         d"   p\        P                  RT4        R p?R # R p?ii ; i)NrFc              3   ,   <"   T F	  qS9   x  K  	  R # 5iNr   )r   r   r   s   & r    r    cleanup_fstab.<locals>.<genexpr>  s     9.}.s   Twz%Removed resource disk entries from %sz4Failed to clean resource disk entries from fstab: %s)r+   r,   r-   r   r   r   open	readlinesr   
writelinesr?   rn   r   r   )r   remove_entriesflines	new_lineschangedr   r   s   &      @r    cleanup_fstabr	    s   77>>*%%!]No.	j#		! 
IG39.93339.999G	 Oj#&&!Y' 'HH<jI  
	 '&  OJANNOsB   %D9!E )E E"E 9E		E	E F*FFc          
      L    V ^8  d   QhR\         R\        R\        R\        RR/# )rK   r   cfgr   argsrO   N)r*   r   r
   r   )rQ   s   "r    rR   rR   !  s6     E4 E4 E46 E4% E4t E4 E4r"   c                   VP                   P                  4       pV'       d   RMRpRRRVRR.pVP                  RV4      pVP                  R. 4      p\        P	                  R	V4       \        4       w  rpVP                  R
/ 4      p\        WWV4      p\        WWV4      p\        V4      p\        V4      p\        VP                  R/ 4      4      pV'       d   VP                  VRRRRR.4       V'       g   \        P	                  R4       R# V Uu. uF  pRP                  V4      NK  	  ppV Uu. uF,  pV^,          P                  R4      '       g   K#  V^,          NK.  	  ppV F  p \        P                  ! V4       K  	  V Uu. uF  pVP%                  RR4      NK  	  ppV Uu. uF  pVP%                  RR4      NK  	  ppV Uu. uF  pVV9  g   K  RV 2NK  	  upV Uu. uF  pVV9  g   K  RV 2NK  	  up,           pV	P'                  V4       RRP                  V	4      ,          p\        P(                  ! \*        V4       V'       d   \        P	                  RV4       M\        P	                  R4       \-        V4       \/        V\1        V4      V4       R# u upi u upi   \          d"    \        P"                  ! \        RT4        EKq  i ; iu upi u upi u upi u upi )z Handle the mounts configuration.zBdefaults,nofail,x-systemd.after=cloud-init-network.service,_netdevzdefaults,nobootwaitNr   r   r   mount_default_fieldsr   zmounts configuration is %sr   r   r   r   z No modifications to fstab needed	r5   z Failed to make '%s' config-mountr   z- z+ z%s

zChanges to fstab: %szNo changes to /etc/fstab made.)distror   rB   r?   r@   r   r   r   r   r   r   r   r8   r   r	   r   r   logexcreplaceextend
write_filer   r   r   rP   )r   r  r   r  r   r   hardcoded_defaultsr   r   r   rN   r   r   updated_cfgswapfiler   	cfg_linesdr   nsaddssdropsdropaddsopscontentss   &&&&                      r    handler"  !  s    <<,,.L  	M"  f.CS#N*-'' 2+N 36''(B2GFII*F3-8]*K]WW-r2N/NEK ,JK -[9Kk*Kcggfb12HHffdCEF
 			45/:;{e5!{I;%>+Q1)=DAaDD+D>	DOOA  ,559aQYYtS!9E5,9:Mqaiic"MF:$*@FDd%.?KbKF@#D#ss&'8
"SE
eD D y!		+..HOOJ)		($/		23K(L$t*d37 <>
  	DKK?CC	D 6:@ DsH   J'5 J,J,/J1K .K%	K*	K*,	K/:	K/1'KKc                @    V ^8  d   Qh/ ^ \         9   d
   \        ;R&   # )rK   meta)__conditional_annotations__r   )rQ   s   "r    rR   rR   	   s     3 3  j ! 4r"   r   i   @r   )NNN)NN)7r%  __doc__r   loggingre   r+   r$   typingr   r   r   r   r   	cloudinitr   r   r	   cloudinit.cloudr
   cloudinit.configr   cloudinit.config.schemar   cloudinit.settingsr   r$  rC   r&   r   r   ri   rc   	getLogger__name__r?   r!   r'   r3   r<   rI   rT   rs   r   r   r   r   r   r   r   r   r   r   r	  r"  rR   )r%  s   @r    <module>r0     s    4    	 	 4 4 - - ! # . + 	+wr	  ?  
#

!	
$.b&6r;|<-`24+\&!H$62O8E4r"   