HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/config/__pycache__/cc_mounts.cpython-310.pyc
o

�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+d7d!d"�Z,d7d#d$�Z-d8d%d&�Z.d'e/d(e/d)dfd*d+�Z0d9d,d-�Z1d.d/�Z2d0e/d1ed2ed3ed4e3d)dfd5d6�Z4dS):z-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
    �all�	cc_mounts�Mountsz%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�title�description�distros�examples�	frequency�activate_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|dvrdSdD]}|�|�r|�d�dkrdSqdS)N)�ami�root�swapT)�	ephemeral�ebs�:���F)�
startswith�find)r�enumname�r$�</usr/lib/python3/dist-packages/cloudinit/config/cc_mounts.py�is_meta_device_name|s�r&cCst�|�rdSdS)NTF)�NETWORK_NAME_RE�match)rr$r$r%�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_number�potential_suffixes�suffix�potential_partition_devicer$r$r%�_get_nth_partition_for_device�s��r3cCsXtj�|��d�d}tj�d|�}|dur&tj�|tj�|��d�d�}tj�|�S)N�/r z/sys/block/)r+r,�realpath�split�joinr-)r.�partition_path�device_name�sys_pathr$r$r%�_is_block_device�s�r;c	Cs|�d|�|}|dkrd}|�d�t|�r|St�|�\}}|}|r5|�||�}||kr5|�d||�t|�rS||�}|sAdS|�d�sKd|f}|�d||�n
t�|�r]d|f}d}|durit	|d	�}nt	||�}|durtdSt
||�r�|dur|S|SdS)
Nz+Attempting to determine the real name of %sr�
ephemeral0z2Adjusted mount option from ephemeral to ephemeral0zMapped device alias %s to %sr4z/dev/%szMapped metadata name %s to %s�)�debugr)r�expand_dotted_devname�getr&r!�DEVICE_NAME_REr(r3r;)	�	startname�transformer�log�aliases�devnamer.r/�origr8r$r$r%�sanitize_devname�sF




�
rHc	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�memrL�g�������?�max��sizez%s MBzCsuggest %s swap for %s memory with '%s' disk given max=%s [max=%s]'rNrM)r�read_meminfor+�statvfs�f_frsize�f_bfree�min�int�GB�round�math�sqrt�items�
isinstance�MB�LOGr>)�memsize�maxsize�fsys�sugg_max�inforTrL�minsizerR�pinfo�k�vr$r$r%�suggested_swapsize�sN
�



�rj�fnamerR�returncs�d���fdd�}tj�|�}t�|�t�|�d��dkr%t��dks)�dkr0|||d�nz|||d	�WntjyMt	�
d
�|||d�Ynwtj�|�rZt�|d�z
t�d|g�Wd
Stjyrt�
|��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}z
tj|d
d�WdStjyJ}zt��||||�t�|��d}~ww)Nz3Creating swapfile in '%s' on fstype '%s' using '%s'�	fallocatez-lz%sM�ddzif=/dev/zerozof=%szbs=1Mzcount=%sT)�capture)r`r>r�ProcessExecutionErrorrer�del_file)rkrR�method�cmd�e��errmsg�fstyper$r%�create_swaps.��
��z$create_swapfile.<locals>.create_swapr=�xfs)rO��btrfsrnrmz4fallocate swap creation failed, will attempt with ddi��mkswapN)r+r,�dirnamer�
ensure_dir�get_mount_info�kernel_versionrrpr`rer-�chmodrq)rkrRrx�swap_dirr$rur%�create_swapfiles4
�
��
�
�r�cCs�tj�|�}t|���dkr2zt��d}Wnty%t�	d�YdSwt�
|�t|||d�}tt|d��}|sCt�	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
    �autorIz)Not creating swap: failed to read meminfoN)rcrbrarz'Not creating swap: suggested size was 0zSetting up swap file)�msg�func�args)r+r,r}r*�lowerrrS�IOErrorr`r>r~rjrX�log_timer�)rkrRrbr�ra�mibsizer$r$r%�setup_swapfileHs.
�
�
�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.imgrRrrbzno 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�)rkrRrbzfailed to setup swap: %s)r^�dictr`�warningr@r>r+r,r-r�	load_file�
splitlinesr!�	Exceptionr*�human2bytesr�)�swapcfgrkrRrb�linertr$r$r%�handle_swapcfgjsP


�
���


��r�r�cfg�cloudrDr�c%	sd}|j��}|rd}ddd|ddg}|�d|�}ddd|d	ddggd
�g}g}	d|vr1|d}	t�d|	�g}
i}g}tj�t�rst	�
t���D](}
t|
vrV|�
|
�qJzt�|
�}Wn	tyfYnw|
||d
<|
�
|
�qJ|�di�}tt|	��D]u}t|	|t�s�|�d|dt�|	|��qt|	|d
�}t||j||d�}||kr�|�d||f�|dur�|�d|�q||vr�|�d|||�q||	|d
<tt|	|��D]}|	||dur�q�t|	||�|	||<q�qtt|	��D]U}tt|��D](}t|	|�|k�r|	|�
||��q|	||du�r*|||	||<�q|	|ddu�rPt|�D]}|	|d
|	|d
k�rNd|	|d<�q9q�|D]d}|d
}t||j||d�}||k�rp|�d||f�|du�r}|�d|��qS||v�r�|�d|||��qS||d
<d}|	D]}|d
|d
k�r�d}n�q�|�r�|�d|��qS|	�
|��qSg}|	D]}|ddu�r�|�d|d
��q�|�
|��q�t|�di��}|�r�|�
|dddddg�t|�d
k�r�|�d�dSg}d}d}g}|D]-}
d|
d	tf|
d	<|
d dk�rd}|
d�d!��r(|�
|
d�|�
d"�|
���qd#d$�t	��� �D�}|D]&}zt	�!|�Wnt�yYt	�"|d%|�Ynw|�sd||v�rdd}�q?d&d$�|D��d'd$�|D���fd(d$��D��fd)d$��D�} |
�#|�d*d+�|
�}!t	�$t|!�g}"|�r�|"�
d,d-g�t| �d
k�r�|�d.�n|�d/| �d}|�r�|"�
d0d-g�|�r�|"�
d1d2g�d3}#|"D]0}$d4d5�|$�}#zt%�%|$�|�|#d6�W�q�t%j&�y|�|#d7�t	�"||#d7�Y�q�wdS)8Nzdefaults,nobootwaitz=defaults,nofail,x-systemd.requires=cloud-init.service,_netdevr��0�2�mount_default_fieldsr<z/mnt�)r�noner�swr�r��mountszmounts configuration is %sr�device_aliasesz,Mount option %s not a list, got a %s insteadr=)rEzchanged %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 %srr�r�z No modifications to fstab neededz%s,%srJr4�	cSs g|]\}}d|vr|d�qS)�
mountpointr$)�.0rhrir$r$r%�
<listcomp>=s
��zhandle.<locals>.<listcomp>z Failed to make '%s' config-mountcS�g|]}t�d|��qS�r���WS�sub�r��nr$r$r%r�K�cSr�r�r�r�r$r$r%r�Lr�c�g|]
}|�vrd|�qS)z- r$)r��drop)�saddsr$r%r�Nscr�)z+ r$)r��add)�sdropsr$r%r�Ns
��z%s
�
�swaponz-azNo changes to /etc/fstab made.zChanges to fstab: %s�mount�	systemctlz
daemon-reloadz#Activating swap and mounts with: %szActivate mounts: %s:r��PASS�FAIL)'�distro�uses_systemdr@r`r>r+r,r-�
FSTAB_PATHrr�r��MNT_COMMENT�appendr�r6r��range�lenr^�listr�r�obj_namer*rH�device_name_to_devicerer�r!r7r�r]r~�logexc�extend�
write_filerrp)%rr�r�rDr��def_mnt_optsr��defvals�defmnts�cfgmnt�fstab_lines�
fstab_devs�
fstab_removedr��toksr��i�start�	sanitized�j�defmnt�
cfgmnt_has�cfgm�actlist�x�swapret�cc_lines�needswap�need_mount_all�dirs�mount_points�d�sops�contents�
activate_cmds�fmtrsr$)r�r�r%�handle�sH
��
��
�����
�


��

����

��r�)N)NNN)NN)5�__doc__�loggingr[r+�rer�stringr�textwrapr�	cloudinitrrr�cloudinit.cloudr�cloudinit.configr	�cloudinit.config.schemar
r�cloudinit.settingsr�MODULE_DESCRIPTION�examplerr�__annotations__�DEVICE_NAME_FILTER�compilerA�NETWORK_NAME_FILTERr'r�r�r�r_rY�	getLogger�__name__r`r&r)r3r;rHrjr*r�r�r�r�r�r$r$r$r%�<module>s�(����






19
8"0������