CasperSecurity

Current Path : /lib/python3/dist-packages/cloudinit/config/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/cloudinit/config/__pycache__/cc_mounts.cpython-310.pyc

o

t��h9K�@s�UdZddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZddlm
Z
mZmZddlmZddlmZddlmZddlmZd	d
gegd�Zeed<d
ZdZdZdZdZdZe� e!�Z"dd�Z#dd�Z$dd�Z%dLdd�Z&dLdd�Z'de(de	e(dee(e(fd e)fd!d"�Z*dMd#d$�Z+d%e(d&e(d dfd'd(�Z,dNd)d*�Z-d+d,�Z.d e
ee(ee(e(fee(ffd-d.�Z/d/ee	ee	e(dee(e(fd0ee(e(fd1ee	e(d2ed eee(fd3d4�Z0d/eee(d eee(fd5d6�Z1d/eee(d7e(dee(e(fd0ee(e(fd2ed eee(fd8d9�Z2d:eee(d eee(fd;d<�Z3d:eee(d dfd=d>�Z4d?e)d@e)dAee(d dfdBdC�Z5gfdDe6d dfdEdF�Z7dGe(dHed2edIe6d df
dJdK�Z8dS)Oz-Mounts: Configure mount points and swap files�N)�Dict�List�Optional�Tuple�cast)�performance�subp�util)�Cloud)�Config)�
MetaSchema)�PER_INSTANCE�	cc_mounts�all)�id�distros�	frequency�activate_by_schema_keys�metaz&^([x]{0,1}[shv]d[a-z][0-9]*|sr[0-9]+)$z^.+:.*z
/etc/fstabzcomment=cloudconfig�i@cCs8|dvrdSdD]}|�|�r|�d�dkrdSqdS)N)�ami�root�swapT)�	ephemeral�ebs�:���F)�
startswith�find)�name�enumname�r!�</usr/lib/python3/dist-packages/cloudinit/config/cc_mounts.py�is_meta_device_name,s�r#cCst�t|�rdSdS)NTF)�re�match�NETWORK_NAME_FILTER)rr!r!r"�is_network_device8sr'cCsHt|�d|fd|fg}|D]}d||f}tj�|�r!|SqdS)Nzp%sz-part%sz%s%s)�str�os�path�exists)�device_path�partition_number�potential_suffixes�suffix�potential_partition_devicer!r!r"�_get_nth_partition_for_device?s��r1cCsXtj�|��d�d}tj�d|�}|dur&tj�|tj�|��d�d�}tj�|�S)N�/rz/sys/block/)r)r*�realpath�split�joinr+)r,�partition_path�device_name�sys_pathr!r!r"�_is_block_deviceLs�r9cCst�d|�|}|dkrd}t�d�t|�r|St�|�\}}|}|r5|�||�}||kr5t�d||�t|�rS||�}|sAdS|�d�sKd|f}t�d||�nt�	t
|�r^d|f}d}|durjt|d	�}nt||�}|durudSt||�r�|dur�|S|SdS)
Nz+Attempting to determine the real name of %sr�
ephemeral0z2Adjusted mount option from ephemeral to ephemeral0zMapped device alias %s to %sr2z/dev/%szMapped metadata name %s to %s�)
�LOG�debugr'r	�expand_dotted_devname�getr#rr$r%�DEVICE_NAME_FILTERr1r9)�	startname�transformer�aliases�devnamer,r-�origr6r!r!r"�sanitize_devnameVsF



�
rF�original�	sanitized�
fstab_devs�returncCsP||krt�d||�|durt�d|�dS||vr&t�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)r<r=)rGrHrIr!r!r"�sanitized_devname_is_valid�s�rKc	Cs`|dur
t��d}|d}d||d�}|dur|dur|}n2|rKt�|�}|j|j}||d<|dur>tt|d�|�}n||dkrJt|d�}n|durQ|}||d<|dtkr^|}n|d	tkridt}nt	t
�|t��t}t||�}||d
<i}	|��D]\}
}t
|t�r�d|t|	|
<q�||	|
<q�t�d|	d
|	d
|	d|	d|	d�|S)N�total��na)�avail�max_in�memrO�g�������?�max��sizez%s MBzCsuggest %s swap for %s memory with '%s' disk given max=%s [max=%s]'rQrP)r	�read_meminfor)�statvfs�f_frsize�f_bfree�min�int�GB�round�math�sqrt�items�
isinstance�MBr<r=)�memsize�maxsize�fsys�sugg_max�inforWrO�minsizerU�pinfo�k�vr!r!r"�suggested_swapsize�sN
�



�rl�fnamerUcsd���fdd�}tj�|�}t�|�t�|�d��dkr0t�ddd|g�t�d	d
|g��dkrAt��dkrA|||d
�nz|||d�Wntjy^t	�
d�|||d
�Ynwtj�|�rkt�|d�z
t�d|g�WdStjy�t�
|��w)zSize is in MiB.z6Failed to create swapfile '%s' of size %sMB via %s: %sc
s�t�d|�|�|dkrddd||g}n|dkr%ddd|dd	|g}nt�d
��z
tj|dd�WdStjyP}zt��||||�t�|��d}~ww)
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)�capture)r<r=r�ProcessExecutionErrorrgr	�del_file)rmrU�method�cmd�e��errmsg�fstyper!r"�create_swap�s4���
��z$create_swapfile.<locals>.create_swapr;�btrfs�truncatez-s�0�chattrz+C�xfs)rR�rornz4fallocate swap creation failed, will attempt with ddi��mkswapN)r)r*�dirnamer	�
ensure_dir�get_mount_infor�kernel_versionrqr<rgr+�chmodrr)rmrUry�swap_dirr!rvr"�create_swapfile�s0

�
�r�cCs�tj�|�}t|���dkr2zt��d}Wnty%t�	d�YdSwt�
|�t|||d�}tt|d��}|sCt�	d�dSt
�d��t||�Wd�|S1sYwY|S)	z�
    fname: full path string of filename to setup
    size: the size to create. set to "auto" for recommended
    maxsize: the maximum size
    �autorLz)Not creating swap: failed to read meminfoN)rerdrcrz'Not creating swap: suggested size was 0zSetting up swap file)r)r*r�r(�lowerr	rV�IOErrorr<r=r�rlr[r�Timedr�)rmrUrdr�rc�mibsizer!r!r"�setup_swapfiles,
�
�

��r�c
CsZt|t�st�d�dS|�dd�}|�dd�}|�dd�}|r"|s)t�d�dStj�|�rstj�d	�s=t�d
|�|Sz$t	�
d	���D]}|�|d�rYt�d|�|WSqEt�d
|�Wnt
yrt�d|�|YSwz t|t�r�|dkr�t	�|�}t|t�r�t	�|�}t|||d�WSt
y�}z
t�d|�WYd}~dSd}~ww)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.imgrUrrdzno 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/swapsr�)rmrUrdzfailed to setup swap: %s)ra�dictr<�warningr?r=r)r*r+r	�load_text_file�
splitlinesr�	Exceptionr(�human2bytesr�)�swapcfgrmrUrd�linerur!r!r"�handle_swapcfg/sP


�
���


��r�cCslg}i}g}tj�t�r1t�t���D]}t|vr|�|�q|�	�}|r0|||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	r�r��MNT_COMMENT�appendr4)�fstab_linesrI�
fstab_removedr��toksr!r!r"�parse_fstab_s


�
r��mounts�device_aliases�default_fields�cloudcCs�g}|D]Y}t|t�st�d|�qt|d�}t||j|d�}t|||�r0|g|dd�}	n|}	t|	�D]\}
}|durE||
|	|
<q6t|	|
�|	|
<q6|	|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�rCr;N)ra�listr<r�r(rF�device_name_to_devicerK�	enumerate�lenr�)r�rIr�r�r��
updated_linesr��start�sanitized_devname�updated_line�index�tokenr!r!r"�sanitize_mounts_configurationys&
�r�cCslg}g}|ddd�D]#}|ddus|d|vr)t�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)r<r=r�)r��actlist�dev_denylistr�r!r!r"�remove_nonexistent_devices�sr��default_mount_optionsc	s�t�|�}ddd|ddggd�fD]5��d}t||j|d�}t|||�s&qtt|��d<t�fd	d
�|D��}|rAt�	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/mntr�r|�2)r�noner�swr|r|rr�c3s �|]}|d�dkVqdS)rNr!)�.0�cfgm��
default_mountr!r"�	<genexpr>�s�
�z,add_default_mounts_to_cfg.<locals>.<genexpr>z-Not including %s, already previously included)�copy�deepcopyrFr�rKrr(�anyr<r=r�)	r�r�rIr�r��
new_mountsr�rH�default_already_existsr!r�r"�add_default_mounts_to_cfg�s&
���r�r�cCsdd�|D�S)z=Add "comment=cloudconfig" to the mount options of each entry.cSs8g|]}|dd�|d�dt��g|dd��qS)N��,rR)r��r��entryr!r!r"�
<listcomp>�s*��zadd_comment.<locals>.<listcomp>r!�r�r!r!r"�add_comment�s�r�cCs(tdd�|D��rt�ddg�dSdS)z1Call 'swapon -a' if any entry has a swap fs type.css�|]	}|ddkVqdS)rMrNr!r�r!r!r"r��s�z*activate_swap_if_needed.<locals>.<genexpr>�swapon�-aN)r�rr�r!r!r"�activate_swap_if_needed�s�r��uses_systemd�changes_made�dirscCsfd}|rd}ndd�t����D�}tt|��|��}|r/t�ddg�|r1t�ddg�d	Sd	Sd	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)�
mountpointr!)r��valr!r!r"�	<setcomp>�s
�z"mount_if_needed.<locals>.<setcomp>�mountr��	systemctlz
daemon-reloadN)r	r��values�bool�set�
differencer)r�r�r��do_mount�mount_pointsr!r!r"�mount_if_needed�s
��r��ds_remove_entriesc
stj�t�sdStg}ttd��}|��}Wd�n1swYg}d}|D]�t�fdd�g|�|�D��r>d}q*|���q*z(|rjttd��
}|�	|�Wd�n1s\wYt
�dt�WdSWdSty�}z
t
�
d|�WYd}~dSd}~ww)	N�rFc3s�|]}|�vVqdS�Nr!r��r�r!r"r�s�z cleanup_fstab.<locals>.<genexpr>T�wz%Removed resource disk entries from %sz4Failed to clean resource disk entries from fstab: %s)r)r*r+r�r��open�	readlinesrr��
writelinesr<rgr�r�)r��
base_entry�f�lines�	new_lines�changedrur!r�r"�
cleanup_fstabs0
�����r�r�cfg�argsc	s�|j��}|r	dnd}ddd|ddg}|�d|�}|�dg�}t�d	|�t�\}	}
}|�d
i�}t||
|||�}
t|
||
||�}
t|
�}
t	|
�}
t
|�di��}|r^|
�|ddd
ddg�|
sgt�d�dSdd�|
D�}dd�|
D�}|D]}zt�
|�Wqwty�t�td|�Yqwwdd�|D��dd�|D���fdd��D��fdd��D�}|	�|�dd�|	�}t�t|�|r�t�d|�nt�d�t|
�t|t|�|�dS)z Handle the mounts configuration.z:defaults,nofail,x-systemd.after=cloud-init.service,_netdevzdefaults,nobootwaitNr�r|r��mount_default_fieldsr�zmounts configuration is %sr�rr�r�z No modifications to fstab neededcSsg|]}d�|��qS)�	)r5r�r!r!r"r�Iszhandle.<locals>.<listcomp>cSs"g|]
}|d�d�r|d�qS)r;r2)r)r��dr!r!r"r�Ks"z Failed to make '%s' config-mountcS�g|]}|�dd��qS�r�r���replace�r��nr!r!r"r�S�cSr�r�r�r�r!r!r"r�Tr�c�g|]}|�vrd|���qS)z- r!)r��drop)�saddsr!r"r�Vscr�)z+ r!)r��add)�sdropsr!r"r�Vsz%s
�
zChanges to fstab: %szNo changes to /etc/fstab made.)�distror�r?r<r=r�r�r�r�r�r�r�r	r�r��logexc�extendr5�
write_filer�r�r�r�)rr�r�r�r�r��hardcoded_defaultsr�r�r�rIr�r��updated_cfg�swapfile�	cfg_linesr�r��sops�contentsr!)r�r�r"�handles`
���
�
�
��

rr�)NNN)NN)9�__doc__r��loggingr^r)r$�typingrrrrr�	cloudinitrrr	�cloudinit.cloudr
�cloudinit.configr�cloudinit.config.schemar�cloudinit.settingsr
r�__annotations__r@r&r�r�rbr\�	getLogger�__name__r<r#r'r1r9rFr(r�rKrlr�r�r�r�r�r�r�r�r�r�r�r�rr!r!r!r"�<module>s��



1��
�
�
9
>(0�
�
�
��

�".
��
�
��

�"$���
�"
Hacker Blog, Shell İndir, Sql İnjection, XSS Attacks, LFI Attacks, Social Hacking, Exploit Bot, Proxy Tools, Web Shell, PHP Shell, Alfa Shell İndir, Hacking Training Set, DDoS Script, Denial Of Service, Botnet, RFI Attacks, Encryption
Telegram @BIBIL_0DAY