관리-도구
편집 파일: cc_mounts.cpython-39.pyc
a ����i*�g�H����������������������@���s���U�d�Z�ddlZddlZddlZddlZddlZddlmZmZm Z m Z mZ�ddlm Z mZmZ�ddlmZ�ddlmZ�ddlmZ�ddlmZ�d d geg�d�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+f�e,d�dd�Z-dBd d!�Z.e+e+dd"�d#d$�Z/dCd%d&�Z0d'd(��Z1e ee+�ee+e+f�ee+�f�d)�d*d+�Z2ee ee e+����ee+e+f�ee+e+f�ee e+��eeee+��d,�d-d.�Z3eee+��eee+��d/�d0d1�Z4eee+��e+ee+e+f�ee+e+f�eeee+��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���@c�����������������C���s8���|�dv�rdS�dD�]"}|���|�r|��d�dkr�dS�qdS�)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����r���c�����������������C���s���t��|��rdS�dS�)NTF)�NETWORK_NAME_RE�match)r���r���r���r����is_network_device:���s���� r���c�����������������C���sH���t�|�d|f�d|f�g}|D�]$}d|�|f�}tj�|�r|��S�qd�S�)Nzp%sz-part%sz%s%s)�str�os�path�exists)�device_path�partition_numberZpotential_suffixes�suffixZpotential_partition_devicer���r���r����_get_nth_partition_for_deviceA���s����� r'���c�����������������C���sX���t�j�|���d�d�}t�j�d|�}|d�urLt�j�|t�j�|��d�d��}t�j�|�S�)N�/r���z/sys/block/)r!���r"����realpath�split�joinr#���)r$����partition_pathZdevice_nameZsys_pathr���r���r����_is_block_deviceN���s�����r-���c�����������������C���s ��t��d|���|�}|dkr&d}t��d��t|��r2|�S�t�|�\}}|}|rj|�||�}||krjt��d||��t|�r�||�}|s�d�S�|�d�s�d|f�}t��d||��nt� |��r�d|f�}d�}|d�u�r�t |d �}nt ||�}|d�u�r�d�S�t||��r|d�u�r|S�|S�d�S�) 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�debugr���r ���Zexpand_dotted_devname�getr���r����DEVICE_NAME_REr���r'���r-���)Z startnameZtransformer�aliasesZdevnamer$���r%���Zorigr,���r���r���r����sanitize_devnameX���sF���� � r5���)�original� sanitized� fstab_devs�returnc�����������������C���sP���||�krt��d|�|��|du�r.t��d|���dS�||v�rLt��d|||���dS�dS�)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)r0���r1���)r6���r7���r8���r���r���r����sanitized_devname_is_valid����s�����r:���c�������������� ���C���sd��|�d�u�rt����d�}�|�d�}d||�d�}|d�u�r>|d�u�r>|}nd|r�t�|�}|j|j�}||d<�|d�u�r|tt|d��|�}q�||d�kr�t|d��}n|d�u�r�|}||d<�|�dt�k�r�|�}n,|�d t�k�r�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�MBr0���r1���)�memsize�maxsize�fsysZsugg_max�inforE���r=���ZminsizerC���Zpinfo�k�vr���r���r����suggested_swapsize����sL���� �rV���)�fnamerC���r9���c��������������������s��d�����fdd�}t�j�|��}t�|��t�|�d���dkr`t�ddd|�g��t�d d |�g���dkr�t���dk�r�||�|d ��n<z||�|d��W�n*�tjy����t � d��||�|d ��Y�n0�t�j�|��r�t�|�d��zt�d|�g��W�n"�tj�y ���t� |�����Y�n0�dS�)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��W�nD�tjy��}�z*t����|�|||��t�|�����W�Y�d�}~n d�}~0�0�d�S�) Nz3Creating swapfile in '%s' on fstype '%s' using '%s'� fallocatez-lz%sM�ddzif=/dev/zerozof=%szbs=1Mzcount=%sz:Missing dependency: 'dd' and 'fallocate' are not availableT)Zcapture)r0���r1���r����ProcessExecutionErrorrS���r ����del_file)rW���rC����method�cmd�e��errmsgZfstyper���r����create_swap����s0������� z$create_swapfile.<locals>.create_swapr/���Zbtrfs�truncatez-s�0Zchattrz+CZxfs)r@�������rY���rX���z4fallocate swap creation failed, will attempt with ddi���ZmkswapN)r!���r"����dirnamer ���� ensure_dirZget_mount_infor���Zkernel_versionrZ���r0���rS���r#����chmodr[���)rW���rC���ra����swap_dirr���r_���r����create_swapfile����s,���� ri���c�����������������C���s����t�j�|��}t|����dkrdzt���d�}W�n�tyJ���t� d��Y�dS�0�t� |��t|||d�}tt|d���}|s�t� d��dS�t �d���t|�|��W�d����n1�s�0����Y��|�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)rR���rQ���rP���r���z'Not creating swap: suggested size was 0zSetting up swap file)r!���r"���re���r ����lowerr ���rD����IOErrorr0���r1���rf���rV���rI���r���ZTimedri���)rW���rC���rQ���rh���rP���Zmibsizer���r���r����setup_swapfile��s$���� � (rm���c�������������� ���C���sd��t�|�t�st�d��dS�|��dd�}|��dd�}|��dd�}|rD|sRt�d��dS�tj�|�r�tj�d �szt�d |��|S�zHt � d ����D�](}|�|d��r�t�d|��|��W�S�q�t�d |��W�n"�t y����t�d|��|�Y�S�0�zFt�|t��r|dk�rt �|�}t�|t��rt �|�}t|||d�W�S��t �y^�}�zt�d|��W�Y�d}~n d}~0�0�dS�)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.imgrC���r���rQ���zno 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���)rW���rC���rQ���zfailed to setup swap: %s)rN����dictr0����warningr2���r1���r!���r"���r#���r ����load_text_file� splitlinesr���� Exceptionr ���Zhuman2bytesrm���)ZswapcfgrW���rC���rQ����liner^���r���r���r����handle_swapcfg1��sH���� �� "rv���)r9���c������������������C���sl���g�}�i�}g�}t�j�t�rbt�t����D�]:}t|v�r>|�|��q&|� ��}|r&|||d�<�|��|��q&|�||fS�)as��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"���r#���� FSTAB_PATHr ���rr���rs����MNT_COMMENT�appendr*���)�fstab_linesr8���� fstab_removedru���Ztoksr���r���r����parse_fstaba��s���� r|���)�mountsr8����device_aliases�default_fields�cloudr9���c�����������������C���s����g�}|�D�]�}t�|t�s$t�d|��qt|d��}t||j|d�}t|||�r`|g|dd���} n|} t| �D�].\} }|du�r�|| �| | <�qlt| | ��| | <�ql| |t | �d��7�} |� | ��q|S�)a��Sanitize 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����r4���r/���N)rN����listr0���rq���r ���r5����device_name_to_devicer:���� enumerate�lenry���)r}���r8���r~���r���r����Z updated_linesru����startZsanitized_devnameZupdated_line�index�tokenr���r���r����sanitize_mounts_configuration{��s&���� �r����)r}���r9���c�����������������C���sl���g�}g�}|�ddd��D�]F}|d�du�s2|d�|v�rRt��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. Nr���r/���r���z$Skipping nonexistent device named %s)r0���r1���ry���)r}����actlistZdev_denylistru���r���r���r����remove_nonexistent_devices���s����r����)r}����default_mount_optionsr8���r~���r����r9���c����������� ���������s����t��|��}ddd|ddgg�d�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/mntrj���rc����2)r����noner����swrc���rc���r���r����c�����������������3���s���|�]}|d����d��kV��qdS�)r���Nr���)�.0Zcfgm�Z default_mountr���r���� <genexpr>���s���z,add_default_mounts_to_cfg.<locals>.<genexpr>z-Not including %s, already previously included)�copy�deepcopyr5���r����r:���r���r ����anyr0���r1���ry���) r}���r����r8���r~���r����Z new_mountsr����r7���Zdefault_already_existsr���r����r����add_default_mounts_to_cfg���s&���� ���r����)r����r9���c�����������������C���s���dd��|�D��S�)z=Add "comment=cloudconfig" to the mount options of each entry.c�����������������S���s8���g�|�]0}|d�d��|d���dt����g�|dd�����qS�)N�����,r@���)rx����r�����entryr���r���r���� <listcomp>���s����zadd_comment.<locals>.<listcomp>r����r����r���r���r����add_comment���s�����r����c�����������������C���s$���t�dd��|�D���r t�ddg��dS�)z1Call 'swapon -a' if any entry has a swap fs type.c�����������������s���s���|�]}|d��dkV��qdS�)r<���r���Nr���r����r���r���r���r������������z*activate_swap_if_needed.<locals>.<genexpr>Zswapon�-aN)r����r���r����r���r���r����activate_swap_if_needed���s����r����)�uses_systemd�changes_made�dirsr9���c�����������������C���s^���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. FTc�����������������S���s���h�|�]}d�|v�r|d���qS�)Z mountpointr���)r�����valr���r���r���� <setcomp>���s����z"mount_if_needed.<locals>.<setcomp>Zmountr����Z systemctlz daemon-reloadN)r ���r}����values�bool�set� differencer���)r����r����r����Zdo_mountZmount_pointsr���r���r����mount_if_needed���s���� �r����)r����cfgr�����argsr9���c�������������� ������s���|j����}|rdnd}ddd|ddg}|�d|�}|�dg��}t�d |��t��\} } }|�d i��}t|| |||�} t| || ||�} t| �} t | �} t |�di���}|r�| �|ddd ddg��t| �dkr�t�d��dS�dd��| D��}dd��| D��}|D�]6}zt �|��W�q��t�y*���t �td|��Y�q�0�q�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,nobootwaitNrj���rc���r����Zmount_default_fieldsr}���zmounts configuration is %sr~���r���r����r����r���z No modifications to fstab neededc�����������������S���s���g�|�]}d���|��qS�)� )r+���r����r���r���r���r����1��r����zhandle.<locals>.<listcomp>c�����������������S���s"���g�|�]}|d����d�r|d���qS�)r/���r(���)r���)r�����dr���r���r���r����3��r����z Failed to make '%s' config-mountc�����������������S���s���g�|�]}|��d�d��qS��r����ro�����replace�r�����nr���r���r���r����;��r����c�����������������S���s���g�|�]}|��d�d��qS�r����r����r����r���r���r���r����<��r����c��������������������s���g�|�]}|��vrd�|����qS�)z- r���)r����Zdrop)�saddsr���r���r����>��r����c��������������������s���g�|�]}|��vrd�|����qS�)z+ r���)r�����add)�sdropsr���r���r����>��s���z%s � zChanges to fstab: %szNo changes to /etc/fstab made.)Zdistror����r2���r0���r1���r|���r����r����r����r����rv���ry���r����r ���rf���rt���Zlogexc�extendr+���Z write_filerw���r����r����r����)r���r����r����r����r����r����Zhardcoded_defaultsr���r}���rz���r8���r{���r~���Zupdated_cfgZswapfileZ cfg_linesr����r����Zsops�contentsr���)r����r����r����handle��s^���� ��� � � � r����)N)N)NNN)NN);�__doc__r����ZloggingrL���r!����re�typingr���r���r���r���r���Z cloudinitr���r���r ���Zcloudinit.cloudr ���Zcloudinit.configr���Zcloudinit.config.schemar���Zcloudinit.settingsr ���r����__annotations__ZDEVICE_NAME_FILTER�compiler3���ZNETWORK_NAME_FILTERr���rw���rx���rO���rJ���Z getLogger�__name__r0���r���r���r'���r-���r5���r ���r����r:���rV���ri���rm���rv���r|���r����r����r����r����r����r����r����r����r���r���r���r����<module> ���sv���� 2� 9> 0( �. �$ �