3 \�me�N � @ sz U d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl m Z mZmZ ddl mZ ddlmZ ddlmZmZ dd lmZ d Zed�ZdgZd ddeeeed�geg d�Zeee�Z dZeje�ZdZeje�Zejde �ZdZ dZ!d0Z"d1Z#ej$e%�Z&dd� Z'dd� Z(dd� Z)d2d d!�Z*d3d"d#�Z+d4d$d%�Z,e-e-dd&�d'd(�Z.d5d)d*�Z/d+d,� Z0e-eee1dd-�d.d/�Z2dS )6z-Mounts: Configure mount points and swap files� N)� 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 values to be specified for the fields in a ``mounts`` entry that are not specified, aside from the ``fs_spec`` and the ``fs_file`` fields. 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. .. note:: If multiple mounts are specified where a subsequent mount's mountpoint is inside of a previously declared mount's mountpoint (i.e. the 1st mount has a mountpoint of ``/abc`` and the 2nd mount has a mountpoint of ``/abc/def``) then this will not work as expected - ``cc_mounts`` first creates the directories for all the mountpoints **before** it starts to perform any mounts and so the sub-mountpoint directory will not be created correctly inside the parent mountpoint. For systems using util-linux's ``mount`` program this issue can be worked around by specifying ``X-mount.mkdir`` as part of a ``fs_mntops`` value for the subsequent mount entry. 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�title�description�distrosZexamplesZ frequencyZactivate_by_schema_keysz&^([x]{0,1}[shv]d[a-z][0-9]*|sr[0-9]+)$z^.+:.*z[%s]+z /etc/fstabzcomment=cloudconfig� � � c C s: | d krdS x(dD ] }| j |�r| jd�dkrdS qW d S ) N�ami�root�swapT� ephemeral�ebs�:� F)r r r )r r ���)� startswith�find)r Zenumname� r �/usr/lib/python3.6/cc_mounts.py�is_meta_device_name� s r! c C s t j| �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}x(|D ] }d| |f }tjj|�r |S q W d 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_device� s r, c C sX t jj| �jd�d }t jjd|�}|d k rLt jj|t jj|�jd�d �}t jj|�S )N�/r z/sys/block/r r )r&