Your IP : 216.73.217.13


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

U

�AdL�	@s�UdZddlZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZmZm
Z
ddlmZddlmZdd	lmZmZdd
lmZdZe	d�Zd
gZdddeeee	d�gegd�Zeed<ee�ZdZe�e�ZdZe�e�Z e�de�Z!dZ"dZ#dZ$dZ%e�&e'�Z(dd�Z)dd�Z*dd �Z+d1d!d"�Z,d2d#d$�Z-d3d%d&�Z.e/e/dd'�d(d)�Z0d4d*d+�Z1d,d-�Z2e/eeee3dd.�d/d0�Z4dS)5z-Mounts: Configure mount points and swap files�N)�Logger)�
whitespace)�dedent)�subp�
type_utils�util)�Cloud)�Config)�
MetaSchema�get_meta_doc)�PER_INSTANCEa
This module can add or remove mountpoints from ``/etc/fstab`` as well as
configure swap. The ``mounts`` config key takes a list of fstab entries to add.
Each entry is specified as a list of ``[ fs_spec, fs_file, fs_vfstype,
fs_mntops, fs-freq, fs_passno ]``. For more information on these options,
consult the manual for ``/etc/fstab``. When specifying the ``fs_spec``, if the
device name starts with one of ``xvd``, ``sd``, ``hd``, or ``vd``, the leading
``/dev`` may be omitted.

Any mounts that do not appear to either an attached block device or network
resource will be skipped with a log like "Ignoring nonexistent mount ...".

Cloud-init will attempt to add the following mount directives if available and
unconfigured in `/etc/fstab`::

    mounts:
        - ["ephemeral0", "/mnt", "auto","defaults,nofail,x-systemd.requires=cloud-init.service", "0", "2"]
        - ["swap", "none", "swap", "sw", "0", "0"]

In order to remove a previously listed mount, an entry can be added to
the `mounts` list containing ``fs_spec`` for the device to be removed but no
mountpoint (i.e. ``[ swap ]`` or ``[ swap, null ]``).

The ``mount_default_fields`` config key allows default options to be specified
for the values in a ``mounts`` entry that are not specified, aside from the
``fs_spec`` and the ``fs_file``. If specified, this must be a list containing 6
values. It defaults to::

    mount_default_fields: [none, none, "auto","defaults,nofail,x-systemd.requires=cloud-init.service", "0", "2"]

Non-systemd init systems will vary in ``mount_default_fields``.

Swap files can be configured by setting the path to the swap file to create
with ``filename``, the size of the swap file with ``size`` maximum size of
the swap file if using an ``size: auto`` with ``maxsize``. By default no
swap file is created.
a<    # Mount ephemeral0 with "noexec" flag, /dev/sdc with mount_default_fields,
    # and /dev/xvdh with custom fs_passno "0" to avoid fsck on the mount.
    # Also provide an automatically sized swap with a max size of 10485760
    # bytes.
    mounts:
        - [ /dev/ephemeral0, /mnt, auto, "defaults,noexec" ]
        - [ sdc, /opt/data ]
        - [ xvdh, /opt/data, auto, "defaults,nofail", "0", "0" ]
    mount_default_fields: [None, None, auto, "defaults,nofail", "0", "2"]
    swap:
        filename: /my/swapfile
        size: auto
        maxsize: 10485760
    �allZ	cc_mountsZMountsz%Configure mount points and swap filesz�        # Create a 2 GB swap file at /swapfile using human-readable values
        swap:
            filename: /swapfile
            size: 2G
            maxsize: 2G
        )�id�name�titleZdescription�distrosZexamplesZ	frequencyZactivate_by_schema_keys�metaz&^([x]{0,1}[shv]d[a-z][0-9]*|sr[0-9]+)$z^.+:.*z[%s]+z
/etc/fstabzcomment=cloudconfig�i@cCs8|dkrdSdD]"}|�|�r|�d�dkrdSqdS)N)Zami�root�swapT)�	ephemeralZebs�:���F)�
startswith�find)rZenumname�r�</usr/lib/python3/dist-packages/cloudinit/config/cc_mounts.py�is_meta_device_name|srcCst�|�rdSdS)NTF)�NETWORK_NAME_RE�match)rrrr�is_network_device�s
r cCsHt|�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_device�s�
r(cCsXtj�|��d�d}tj�d|�}|dk	rLtj�|tj�|��d�d�}tj�|�S)N�/rz/sys/block/)r"r#�realpath�split�joinr$)r%�partition_pathZdevice_nameZsys_pathrrr�_is_block_device�s�r.c	Cs
|�d|�|}|dkr&d}|�d�t|�r2|St�|�\}}|}|rj|�||�}||krj|�d||�t|�r�||�}|s�dS|�d�s�d|f}|�d||�nt�|�r�d|f}d}|dkr�t	|d	�}nt	||�}|dkr�dSt
||��r|dk	�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�)�debugr rZexpand_dotted_devname�getrr�DEVICE_NAME_RErr(r.)	Z	startnameZtransformer�log�aliasesZdevnamer%r&Zorigr-rrr�sanitize_devname�sH




�
r6c	Csd|dkrt��d}|d}d||d�}|dkr>|dkr>|}nd|r�t�|�}|j|j}||d<|dkr|tt|d�|�}q�||dkr�t|d�}n|dkr�|}||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�memr9�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�MB�LOGr1)�memsize�maxsize�fsysZsugg_max�inforAr9Zminsizer?Zpinfo�k�vrrr�suggested_swapsize�sL


�rS)�fnamer?�returncs�d���fdd�}tj�|�}t�|�t�|�d��dkrJt��dksR�dkr`|||d�n>z|||d	�Wn,tjk
r�t	�
d
�|||d�YnXtj�|�r�t�|d�zt�d|g�Wn"tjk
r�t�
|��YnXd
S)zSize is in MiB.z6Failed to create swapfile '%s' of size %sMB via %s: %sc
s�t�d|�|�|dkr*ddd||g}n|dkrHddd|dd	|g}ztj|d
d�WnBtjk
r�}z"t��||||�t�|��W5d}~XYnXdS)Nz3Creating swapfile in '%s' on fstype '%s' using '%s'�	fallocatez-lz%sM�ddzif=/dev/zerozof=%szbs=1Mzcount=%sT)Zcapture)rLr1r�ProcessExecutionErrorrPr�del_file)rTr?�method�cmd�e��errmsgZfstyperr�create_swaps*��
z$create_swapfile.<locals>.create_swapr0Zxfs)r<�ZbtrfsrWrVz4fallocate swap creation failed, will attempt with ddi�ZmkswapN)r"r#�dirnamer�
ensure_dirZget_mount_infoZkernel_versionrrXrLrPr$�chmodrY)rTr?r_�swap_dirrr]r�create_swapfiles0
�
��

recCs�tj�|�}t|���dkrfzt��d}Wn tk
rLt�	d�YdSXt�
|�t|||d�}tt|d��}|s�t�	d�dStj
tj	dt||gd	�|S)
z�
    fname: full path string of filename to setup
    size: the size to create. set to "auto" for recommended
    maxsize: the maximum size
    �autor7z)Not creating swap: failed to read meminfoN)rOrNrMrz'Not creating swap: suggested size was 0zSetting up swap file)�msg�func�args)r"r#rar!�lowerrr@�IOErrorrLr1rbrSrEZlog_timere)rTr?rNrdrMZmibsizerrr�setup_swapfileHs0

�
�rlc
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
k
r�t�d|�|YSXzFt|t��r
|dk�r
t	�|�}t|t��r t	�|�}t|||d�WSt
k
�r^}zt�d|�W5d}~XYnXdS)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.imgr?rrNzno 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/swapsrf)rTr?rNzfailed to setup swap: %s)rJ�dictrL�warningr2r1r"r#r$r�	load_file�
splitlinesr�	Exceptionr!Zhuman2bytesrl)ZswapcfgrTr?rN�liner\rrr�handle_swapcfgjsJ


��


ru)r�cfg�cloudr4rirUc%	s4d}|j��}|rd}ddd|ddg}|�d|�}ddd|d	ddgd
dd
dddgg}g}	d
|krj|d
}	t�d|	�g}
i}g}tj�t�r�t	�
t���D]R}
t|
kr�|�
|
�q�zt�|
�}Wntk
r�YnX|
||d<|
�
|
�q�|�di�}tt|	��D�]}t|	|t��s>|�d|dt�|	|���qt|	|d�}t||j||d�}||k�r||�d||f�|dk�r�|�d|��qn ||k�r�|�d|||��q||	|d<tt|	|��D]6}|	||dk�r�q�nt|	||�|	||<�qԐqtt|	��D]�}tt|��D]N}t|	|�|k�rV|	|�
||�n"|	||dk�r,|||	||<�q,|	|ddk�rt|�D],}|	|d|	|dk�r�d|	|d<�q��q|D]�}|d}t||j||d�}||k�r|�d||f�|dk�r"|�d|��q�n ||k�rB|�d|||��q�||d<d}|	D]"}|d|dk�rRd}�qv�qR|�r�|�d|��q�|	�
|��q�g}|	D]0}|ddk�r�|�d|d�n
|�
|��q�t|�d
i��}|�r|�
|dd
dddg�t|�dk�r|�d�dSg}d}d}g}|D]Z}
d|
d	tf|
d	<|
dd
k�rZd}|
d�d ��rx|�
|
d�|�
d!�|
���q0d"d#�t	��� �D�}|D]N}zt	�!|�Wn$tk
�r�t	�"|d$|�YnX|�s�||k�r�d}�q�d%d#�|D��d&d#�|D���fd'd#��D��fd(d#��D�} |
�#|�d)d*�|
�}!t	�$t|!�g}"|�rr|"�
d+d,g�t| �dk�r�|�d-�n|�d.| �d}|�r�|"�
d/d,g�|�r�|"�
d0d1g�d2}#|"D]b}$d3d4�|$�}#zt%�%|$�|�|#d5�Wn2t%j&k
�r*|�|#d6�t	�"||#d6�YnX�q�dS)7Nzdefaults,nobootwaitz=defaults,nofail,x-systemd.requires=cloud-init.service,_netdevrf�0�2Zmount_default_fieldsr/z/mnt�rZnone�sw�mountszmounts configuration is %sr�device_aliasesz,Mount option %s not a list, got a %s insteadr0)r5zchanged %s => %sz#Ignoring nonexistent named mount %sz&Device %s already defined in fstab: %szchanged default device %s => %sz+Ignoring nonexistent default named mount %sFTz-Not including %s, already previously includedz$Skipping nonexistent device named %sz No modifications to fstab neededz%s,%sr8r)�	cSs g|]\}}d|kr|d�qS)Z
mountpointr)�.0rQrRrrr�
<listcomp>=szhandle.<locals>.<listcomp>z Failed to make '%s' config-mountcSsg|]}t�d|��qS�rn��WS�sub�r�nrrrr�KscSsg|]}t�d|��qSr�r�r�rrrr�Lscsg|]}|�krd|�qS)z- r)rZdrop)�saddsrrr�Nscsg|]}|�krd|�qS)z+ r)r�add)�sdropsrrr�Nsz%s
�
Zswaponz-azNo changes to /etc/fstab made.zChanges to fstab: %sZmountZ	systemctlz
daemon-reloadz#Activating swap and mounts with: %szActivate mounts: %s:rnZPASSZFAIL)'Zdistro�uses_systemdr2rLr1r"r#r$�
FSTAB_PATHrrqrr�MNT_COMMENT�appendr�r+rs�range�lenrJ�listrprZobj_namer!r6Zdevice_name_to_devicerPrurr,r|rIrbZlogexc�extendZ
write_filerrX)%rrvrwr4riZdef_mnt_optsr�ZdefvalsZdefmntsZcfgmntZfstab_linesZ
fstab_devsZ
fstab_removedrtZtoksr}�i�startZ	sanitized�jZdefmntZ
cfgmnt_hasZcfgmZactlist�xZswapretZcc_linesZneedswapZneed_mount_all�dirsZmount_points�dZsops�contentsZ
activate_cmdsZfmtr[r)r�r�r�handle�sB
��
��


� �


�


��

r�)N)N)NNN)NN)5�__doc__ZloggingrHr"�rer�stringr�textwraprZ	cloudinitrrrZcloudinit.cloudrZcloudinit.configr	Zcloudinit.config.schemar
rZcloudinit.settingsrZMODULE_DESCRIPTIONZexamplerr�__annotations__ZDEVICE_NAME_FILTER�compiler3ZNETWORK_NAME_FILTERrr�r�r�rKrFZ	getLogger�__name__rLrr r(r.r6rSr!rerlrur�r�rrrr�<module>	sr(����





1
98
"1�