name : cc_mounts.cpython-39.pyc
a

puh�H�@s�UdZddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZddlm
Z
mZmZddlmZddlmZddlmZddlmZd	d
gegd�Zeed<d
Ze�e�ZdZe�e�ZdZdZ dZ!dZ"e�#e$�Z%dd�Z&dd�Z'dd�Z(d@dd�Z)dAdd�Z*e+e	e+ee+e+fe,d�dd�Z-dBd d!�Z.e+e+dd"�d#d$�Z/dCd%d&�Z0d'd(�Z1e
ee+ee+e+fee+fd)�d*d+�Z2ee	ee	e+ee+e+fee+e+fee	e+eeee+d,�d-d.�Z3eee+eee+d/�d0d1�Z4eee+e+ee+e+fee+e+feeee+d2�d3d4�Z5eee+eee+d5�d6d7�Z6eee+dd5�d8d9�Z7e,e,ee+dd:�d;d<�Z8e+eee9dd=�d>d?�Z:dS)Dz-Mounts: Configure mount points and swap files�N)�Dict�List�Optional�Tuple�cast)�performance�subp�util)�Cloud)�Config)�
MetaSchema)�PER_INSTANCEZ	cc_mounts�all)�idZdistrosZ	frequencyZactivate_by_schema_keys�metaz&^([x]{0,1}[shv]d[a-z][0-9]*|sr[0-9]+)$z^.+:.*z
/etc/fstabzcomment=cloudconfig�i@cCs8|dvrdSdD]"}|�|�r|�d�dkrdSqdS)N)Zami�root�swapT)�	ephemeralZebs�:���F)�
startswith�find)�nameZenumname�r�>/usr/lib/python3.9/site-packages/cloudinit/config/cc_mounts.py�is_meta_device_name.srcCst�|�rdSdS)NTF)�NETWORK_NAME_RE�match)rrrr�is_network_device:s
rcCsHt|�d|fd|fg}|D]$}d||f}tj�|�r|SqdS)Nzp%sz-part%sz%s%s)�str�os�path�exists)�device_path�partition_numberZpotential_suffixes�suffixZpotential_partition_devicerrr�_get_nth_partition_for_deviceAs�
r'cCsXtj�|��d�d}tj�d|�}|durLtj�|tj�|��d�d�}tj�|�S)N�/rz/sys/block/)r!r"�realpath�split�joinr#)r$�partition_pathZdevice_nameZsys_pathrrr�_is_block_deviceNs�r-cCs
t�d|�|}|dkr&d}t�d�t|�r2|St�|�\}}|}|rj|�||�}||krjt�d||�t|�r�||�}|s�dS|�d�s�d|f}t�d||�nt�	|�r�d|f}d}|dur�t
|d	�}nt
||�}|dur�dSt||��r|du�r|S|SdS)
Nz+Attempting to determine the real name of %sr�
ephemeral0z2Adjusted mount option from ephemeral to ephemeral0zMapped device alias %s to %sr(z/dev/%szMapped metadata name %s to %s�)�LOG�debugrr	Zexpand_dotted_devname�getrr�DEVICE_NAME_RErr'r-)Z	startnameZtransformer�aliasesZdevnamer$r%Zorigr,rrr�sanitize_devnameXsF




�
r5)�original�	sanitized�
fstab_devs�returncCsP||krt�d||�|dur.t�d|�dS||vrLt�d|||�dSdS)z*Get if the sanitized device name is valid.zchanged %s => %sNz+Ignoring nonexistent default named mount %sFz&Device %s already defined in fstab: %sT)r0r1)r6r7r8rrr�sanitized_devname_is_valid�s�r:c	Csd|durt��d}|d}d||d�}|dur>|dur>|}nd|r�t�|�}|j|j}||d<|dur|tt|d�|�}q�||dkr�t|d�}n|dur�|}||d<|dtkr�|}n,|d	tkr�dt}nt	t
�|t��t}t||�}||d
<i}	|��D]0\}
}t
|t��r,d|t|	|
<n||	|
<�qt�d|	d
|	d
|	d|	d|	d�|S)N�total�Zna)�avail�max_in�memr=�g�������?�max��sizez%s MBzCsuggest %s swap for %s memory with '%s' disk given max=%s [max=%s]'r?r>)r	�read_meminfor!�statvfs�f_frsize�f_bfree�min�int�GB�round�mathZsqrt�items�
isinstance�MBr0r1)�memsize�maxsize�fsysZsugg_max�inforEr=ZminsizerCZpinfo�k�vrrr�suggested_swapsize�sL


�rV)�fnamerCr9csd���fdd�}tj�|�}t�|�t�|�d��dkr`t�ddd|g�t�d	d
|g��dkr�t��dkr�|||d
�n<z|||d�Wn*tjy�t	�
d�|||d
�Yn0tj�|�r�t�|d�zt�d|g�Wn"tj�y
t�
|��Yn0dS)zSize is in MiB.z6Failed to create swapfile '%s' of size %sMB via %s: %sc
s�t�d|�|�|dkr*ddd||g}n*|dkrJddd|dd	|g}n
t�d
��ztj|dd�WnDtjy�}z*t��||||�t�|��WYd}~n
d}~00dS)
Nz3Creating swapfile in '%s' on fstype '%s' using '%s'�	fallocatez-lz%sM�ddzif=/dev/zerozof=%szbs=1Mzcount=%sz:Missing dependency: 'dd' and 'fallocate' are not availableT)Zcapture)r0r1r�ProcessExecutionErrorrSr	�del_file)rWrC�method�cmd�e��errmsgZfstyperr�create_swap�s0���
z$create_swapfile.<locals>.create_swapr/Zbtrfs�truncatez-s�0Zchattrz+CZxfs)r@�rYrXz4fallocate swap creation failed, will attempt with ddi�ZmkswapN)r!r"�dirnamer	�
ensure_dirZget_mount_inforZkernel_versionrZr0rSr#�chmodr[)rWrCra�swap_dirrr_r�create_swapfile�s,


ricCs�tj�|�}t|���dkrdzt��d}WntyJt�	d�YdS0t�
|�t|||d�}tt|d��}|s�t�	d�dSt
�d��t||�Wd�n1s�0Y|S)	z�
    fname: full path string of filename to setup
    size: the size to create. set to "auto" for recommended
    maxsize: the maximum size
    �autor;z)Not creating swap: failed to read meminfoN)rRrQrPrz'Not creating swap: suggested size was 0zSetting up swap file)r!r"rer �lowerr	rD�IOErrorr0r1rfrVrIrZTimedri)rWrCrQrhrPZmibsizerrr�setup_swapfiles$

�
(rmc
Csdt|t�st�d�dS|�dd�}|�dd�}|�dd�}|rD|sRt�d�dStj�|�r�tj�d	�szt�d
|�|SzHt	�
d	���D](}|�|d�r�t�d|�|WSq�t�d
|�Wn"t
y�t�d|�|YS0zFt|t��r|dk�rt	�|�}t|t��rt	�|�}t|||d�WSt
�y^}zt�d|�WYd}~n
d}~00dS)zahandle 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.imgrCrrQzno need to setup swapz/proc/swapsz:swap file %s exists, but no /proc/swaps exists, being safe� zswap file %s already in usez+swap file %s exists, but not in /proc/swapsz.swap file %s exists. Error reading /proc/swapsrj)rWrCrQzfailed to setup swap: %s)rN�dictr0�warningr2r1r!r"r#r	�load_text_file�
splitlinesr�	Exceptionr Zhuman2bytesrm)ZswapcfgrWrCrQ�liner^rrr�handle_swapcfg1sH


��


"rv)r9cCslg}i}g}tj�t�rbt�t���D]:}t|vr>|�|�q&|�	�}|r&|||d<|�|�q&|||fS)asParse /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"r#�
FSTAB_PATHr	rrrs�MNT_COMMENT�appendr*)�fstab_linesr8�
fstab_removedruZtoksrrr�parse_fstabas

r|)�mountsr8�device_aliases�default_fields�cloudr9cCs�g}|D]�}t|t�s$t�d|�qt|d�}t||j|d�}t|||�r`|g|dd�}	n|}	t|	�D].\}
}|dur�||
|	|
<qlt|	|
�|	|
<ql|	|t	|	�d�7}	|�
|	�q|S)aSanitize 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: %sr�r4r/N)rN�listr0rqr r5�device_name_to_devicer:�	enumerate�lenry)r}r8r~rr�Z
updated_linesru�startZsanitized_devnameZupdated_line�index�tokenrrr�sanitize_mounts_configuration{s&
�r�)r}r9cCslg}g}|ddd�D]F}|ddus2|d|vrRt�d|d�|�|d�q|�|�q|ddd�S)z�Remove 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.
    Nrr/rz$Skipping nonexistent device named %s)r0r1ry)r}�actlistZdev_denylistrurrr�remove_nonexistent_devices�sr�)r}�default_mount_optionsr8r~r�r9c	s�t�|�}ddd|ddggd�fD]j��d}t||j|d�}t|||�sLq"tt|��d<t�fd	d
�|D��}|r�t�	d|�q"|�
��q"|S)zAdd default mounts to the user provided mount configuration.

    Add them only if no other entry has the same device name
    r.z/mntrjrc�2)r�noner�swrcrcrr�c3s|]}|d�dkVqdS)rNr)�.0Zcfgm�Z
default_mountrr�	<genexpr>�sz,add_default_mounts_to_cfg.<locals>.<genexpr>z-Not including %s, already previously included)�copy�deepcopyr5r�r:rr �anyr0r1ry)	r}r�r8r~r�Z
new_mountsr�r7Zdefault_already_existsrr�r�add_default_mounts_to_cfg�s&
���r�)r�r9cCsdd�|D�S)z=Add "comment=cloudconfig" to the mount options of each entry.cSs8g|]0}|dd�|d�dt��g|dd��qS)N��,r@)rx�r��entryrrr�
<listcomp>�s�zadd_comment.<locals>.<listcomp>r�r�rrr�add_comment�s�r�cCs$tdd�|D��r t�ddg�dS)z1Call 'swapon -a' if any entry has a swap fs type.css|]}|ddkVqdS)r<rNrr�rrrr���z*activate_swap_if_needed.<locals>.<genexpr>Zswapon�-aN)r�rr�rrr�activate_swap_if_needed�sr�)�uses_systemd�changes_made�dirsr9cCs^d}|rd}n(dd�t����D�}tt|��|��}|rZt�ddg�|rZt�ddg�d	S)
z�Call '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.
    FTcSsh|]}d|vr|d�qS)Z
mountpointr)r��valrrr�	<setcomp>�s�z"mount_if_needed.<locals>.<setcomp>Zmountr�Z	systemctlz
daemon-reloadN)r	r}�values�bool�set�
differencer)r�r�r�Zdo_mountZmount_pointsrrr�mount_if_needed�s
�r�)r�cfgr��argsr9c	s�|j��}|rdnd}ddd|ddg}|�d|�}|�dg�}t�d	|�t�\}	}
}|�d
i�}t||
|||�}
t|
||
||�}
t|
�}
t	|
�}
t
|�di��}|r�|
�|ddd
ddg�t|
�dkr�t�d�dSdd�|
D�}dd�|
D�}|D]6}zt
�|�Wq�t�y*t
�td|�Yq�0q�dd�|D��dd�|D���fdd��D��fdd��D�}|	�|�dd�|	�}t
�t|�|�r�t�d|�n
t�d�t|
�t|t|�|�dS)z Handle the mounts configuration.z:defaults,nofail,x-systemd.after=cloud-init.service,_netdevzdefaults,nobootwaitNrjrcr�Zmount_default_fieldsr}zmounts configuration is %sr~rr�r�rz No modifications to fstab neededcSsg|]}d�|��qS)�	)r+r�rrrr�1r�zhandle.<locals>.<listcomp>cSs"g|]}|d�d�r|d�qS)r/r()r)r��drrrr�3r�z Failed to make '%s' config-mountcSsg|]}|�dd��qS�r�ro��replace�r��nrrrr�;r�cSsg|]}|�dd��qSr�r�r�rrrr�<r�csg|]}|�vrd|���qS)z- r)r�Zdrop)�saddsrrr�>r�csg|]}|�vrd|���qS)z+ r)r��add)�sdropsrrr�>sz%s
�
zChanges to fstab: %szNo changes to /etc/fstab made.)Zdistror�r2r0r1r|r�r�r�r�rvryr�r	rfrtZlogexc�extendr+Z
write_filerwr�r�r�)rr�r�r�r�r�Zhardcoded_defaultsrr}rzr8r{r~Zupdated_cfgZswapfileZ	cfg_linesr�r�Zsops�contentsr)r�r�r�handles^
���
�
�
�

r�)N)N)NNN)NN);�__doc__r�ZloggingrLr!�re�typingrrrrrZ	cloudinitrrr	Zcloudinit.cloudr
Zcloudinit.configrZcloudinit.config.schemarZcloudinit.settingsr
r�__annotations__ZDEVICE_NAME_FILTER�compiler3ZNETWORK_NAME_FILTERrrwrxrOrJZ	getLogger�__name__r0rrr'r-r5r r�r:rVrirmrvr|r�r�r�r�r�r�r�r�rrrr�<module>	sv�





2�
9>
0(



�. 



�$ 
�

© 2025 Cubjrnet7