Current File : //lib/python2.7/site-packages/cloudinit/config/cc_mounts.pyc
�
�jbc@sdZddlmZddlZddlZddlZddlmZddlm	Z	dZ
eje
�Zejde�Z
dZd	Zeje�Zd
�Zd�Zdd�Zd
�Zdddd�Zd�Zddd�Zd�Zd�ZdS(s<
Mounts
------
**Summary:** configure mount points and swap files

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.

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. ``[ sda1 ]`` or ``[ sda1, 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 7
values. It defaults to::

    mount_default_fields: [none, none, "auto", "defaults,nobootwait", "0", "2"]

On a systemd booted system that default is the mostly equivalent::

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

Note that `nobootwait` is an upstart specific boot option that somewhat
equates to the more standard `nofail`.

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.

**Internal name:** ``cc_mounts``

**Module frequency:** per instance

**Supported distros:** all

**Config keys**::

    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: <file>
        size: <"auto"/size in bytes>
        maxsize: <size in bytes>
i����(t
whitespaceN(t
type_utils(tutils&^([x]{0,1}[shv]d[a-z][0-9]*|sr[0-9]+)$s[%s]+s
/etc/fstabscomment=cloudconfigcCsM|dkrtSx6d	D].}|j|�r|jd�dkrtSqWtS(
Ntamitroottswapt	ephemeraltebst:i����(RRR(RR(tTruet
startswithtfindtFalse(tnametenumname((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pytis_meta_device_nameTs
$cCs^t|�d|fd|fg}x4|D],}d||f}tjj|�r*|Sq*WdS(Nsp%ss-part%ss%s%s(tstrtostpathtexiststNone(tdevice_pathtpartition_numbertpotential_suffixestsuffixtpotential_partition_device((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pyt_get_nth_partition_for_device`s
cCs�tjj|�jd�d}tjjd|�}|dk	rqtjj|tjj|�jd�d�}ntjj|�S(Nt/i����s/sys/block/(RRtrealpathtsplittjoinRR(Rtpartition_pathtdevice_nametsys_path((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pyt_is_block_devicejs	(cCs3|jd|�|}|dkr8d}|jd�ntj|�\}}t|�r�|}||�}|sudS|jd�s�d|f}n|jd||�ntj|�r�d|f}nd}|dkr�t|d�}nt||�}|dkrdSt	||�r/|dk	r+|S|SdS(	Ns+Attempting to determine the real name of %sRt
ephemeral0s2Adjusted mount option from ephemeral to ephemeral0Rs/dev/%ssMapped metadata name %s to %si(
tdebugRtexpand_dotted_devnameRRR
tDEVICE_NAME_REtmatchRR"(t	startnamettransformertlogtdevnameRRtorigR((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pytsanitize_devnamess8	c	s|dkrtj�d}nd�d�}idd6|d6|d6}|dkrk|dkrk|}n�|r�tj|�}|j|j}||d<|dkr�tt|d	�|�}q�||d
kr�t|d
�}q�n|dkr�|}n||d<d�d
�fd��fd�fd	�d�fd��fd�fd�d�fg}d}xc|D][\}	}
||	krvt|
|�|�}||dkr�|d	�kr�d}PnPqvqvW|dk	r�|}n||d<d}i}xH|j	�D]:\}
}t
|t�r=d||||
<q
|||
<q
Wtjd|d|d|d|d|d�|S(Nttotaliiitnatavailtmax_intmemig�������?tmaxicSs|dS(Ni((tx((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pyt<lambda>�scsd�S(Ni((R4(tGB(s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pyR5�scSs|S(N((R4((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pyR5�sicsd�S(Ni((R4(R6(s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pyR5�si@cSs|dS(Ni((R4((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pyR5�sitsizeis%s MBsCsuggest %s swap for %s memory with '%s' disk given max=%s [max=%s]'i@i(
RRtread_meminfoRtstatvfstf_frsizetf_bfreetmintinttitemst
isinstancetLOGR$(tmemsizetmaxsizetfsystsugg_maxtinfoR9R0tformulasR7ttoptfunctMBtpinfotktv((R6s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pytsuggested_swapsize�sX
	
	
 	
	csd���fd�}tjj|�}tj|�tj|�d��dkri|||d�ncy|||d�WnLtjk
r�}tj�||d|�tjd�|||d�nXtj	|d	�ytj
d
|g�Wn$tjk
rtj|��nXdS(
sSize is in MiB.s6Failed to create swapfile '%s' of size %sMB via %s: %scs�tjd|�|�|dkr;ddd||g}n,|dkrgddd|dd	|g}nytj|d
t�Wn<tjk
r�}tj�||||�tj|�nXdS(Ns3Creating swapfile in '%s' on fstype '%s' using '%s't	fallocates-ls%sMtddsif=/dev/zerosof=%ssbs=1Mscount=%stcapture(R@R$RtsubpR	tProcessExecutionErrortwarningtdel_file(tfnameR7tmethodtcmdte(terrmsgtfstype(s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pytcreate_swap�s	
itxfstbtrfsRORNsWill attempt with dd.i�tmkswapN(R\R](RRtdirnameRt
ensure_dirtget_mount_infoRRR@RStchmodRQRT(RUR7R[tswap_dirRX((RYRZs>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pytcreate_swapfile�s&


c	Cs�tjj|�}t|�j�dkr�ytj�d}Wntk
r_tj	d�dSXtj
|�td|d|d|�}ntt|d��}|s�tj	d
�dStj
tj	ddd
td||g�|S(s�
    fname: full path string of filename to setup
    size: the size to create. set to "auto" for recommended
    maxsize: the maximum size
    tautoR.s)Not creating swap: failed to read meminfoNRCRBRAiis'Not creating swap: suggested size was 0tmsgsSetting up swap fileRHtargsi(RRR_RtlowerRR8tIOErrorR@R$R`RMR=tlog_timeRd(RUR7RBRcRAtmibsize((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pytsetup_swapfiles"




c	Cs�t|t�s tjd�dS|jdd�}|jdd�}|jdd�}|o_|sstjd�dStjj	|�r.tjj	d	�s�tjd
|�|Sy[xDt
jd	�j�D]-}|j
|d�r�tjd|�|Sq�Wtjd
|�Wq.tk
r*tjd|�|SXnykt|t�r^|dkr^t
j|�}nt|t�rt
j|�}ntd|d|d|�SWn#tk
r�}tjd|�nXdS(sdhandle the swap config, calling setup_swap if necessary.
       return None or (filename, size)
    s%input for swap config was not a dict.tfilenames	/swap.imgR7iRBsno need to setup swapNs/proc/swapss:swap file %s exists, but no /proc/swaps exists, being safet sswap file %s already in uses+swap file %s exists, but not in /proc/swapss.swap file %s exists. Error reading /proc/swapsReRUsfailed to setup swap: %s(R?tdictR@RSRtgetR$RRRRt	load_filet
splitlinesR
t	ExceptionRthuman2bytesRl(tswapcfgRUR7RBtlineRX((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pythandle_swapcfg+sB

	
	c+Cs\d}|jj�}|r$d}nddd|ddg}|jd|�}ddd|d	ddgd
dd
dddgg}g}	d
|kr�|d
}	ntjd|	�g}
i}g}x~tjt�j	�D]g}
t
|
kr�|j|
�q�nytj
|
�}Wntk
r$nX|
||d<|
j|
�q�WxVtt|	��D]B}t|	|t�s�|jd|dtj|	|��qWnt|	|d�}t||j|�}||kr�|jd||f�n|dkr|jd|�qWn)||kr4|jd|||�qWn||	|d<xTtt|	|��D]<}|	||dkryqYqYt|	||�|	||<qYWqWWx�tt|	��D]�}xqtt|��D]]}t|	|�|kr�|	|j||�q�|	||dkr�|||	||<q�q�W|	|ddkr�xDt|�D]3}|	|d|	|dkrKd|	|d<qKqKWq�q�Wx|D]�}|d}t||j|�}||kr�|jd||f�n|dkr�|jd|�q�n)||kr&|jd|||�q�n||d<t}x,|	D]$}|d|dkr=t}Pq=q=W|r�|jd|�q�n|	j|�q�Wg}xB|	D]:}|ddkr�|jd|d�q�|j|�q�Wt|jd
i��}|r|j|dd
dddg�nt|�dkr@|jd�dSg}t}t}g}x||D]t}
d|
d	t
f|
d	<|
dd
kr�t}n|
djd�r�|j|
d�n|jdj|
��q_Wgtj�j �D]"\}}d|kr�|d^q�}xb|D]Z} ytj!| �Wn$tk
rVtj"|d| �nX|r| |krt}qqWg|D]}!tj#d |!�^q~}"g|D]}!tj#d |!�^q�}#g|#D]}$|$|"kr�d!|$^q�g|"D]}%|%|#kr�d"|%^q�}&|
j$|�d#d$j|
�}'tj%t|'�g}(|rc|(jd%d&g�nt|&�dkr�|jd'�n|jd(|&�t}|r�|(jd)d&g�|r�|(jd*d+g�q�nd,})x||(D]t}*d-d j|*�})y!tj&|*�|j|)d.�Wq�tj'k
rS|j|)d/�tj"||)d/�q�Xq�WdS(0Nsdefaults,nobootwaits5defaults,nofail,x-systemd.requires=cloud-init.serviceRet0t2tmount_default_fieldsR#s/mntiRtnonetswtmountssmounts configuration is %sis,Mount option %s not a list, got a %s insteadischanged %s => %ss#Ignoring nonexistent named mount %ss&Device %s already defined in fstab: %sschanged default device %s => %ss+Ignoring nonexistent default named mount %ss-Not including %s, already previously includeds$Skipping nonexistent device named %ss No modifications to fstab neededs%s,%siRs	t
mountpoints Failed to make '%s' config-mountRns- s+ s%s
s
tswapons-asNo changes to /etc/fstab made.sChanges to fstab: %stmountt	systemctls
daemon-reloads#Activating swap and mounts with: %ssActivate mounts: %s:tPASStFAIL((tdistrotuses_systemdRRpR@R$RRqt
FSTAB_PATHRrtMNT_COMMENTtappendtWSRRstrangetlenR?tlistRSRtobj_nameRR-tdevice_name_to_deviceRERR	RwR
RR}R>R`tlogexctsubtextendt
write_fileRQRR(+t_nametcfgtcloudR*t_argstdef_mnt_optsR�tdefvalstdefmntstcfgmnttfstab_linest
fstab_devst
fstab_removedRvttokstitstartt	sanitizedtjtdefmntt
cfgmnt_hastcfgmtactlistR4tswaprettcc_linestneedswaptneed_mount_alltdirsRKRLtmount_pointstdtntsaddstsdropstdroptaddtsopstcontentst
activate_cmdstfmtRW((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pythandleXs	


		$

	

	
"

	


%%&*


(t__doc__tstringRtloggingtos.pathRtret	cloudinitRRtDEVICE_NAME_FILTERtcompileR&R�R�R�t	getLoggert__name__R@RRRR"R-RMRdRlRwR�(((s>/usr/lib/python2.7/site-packages/cloudinit/config/cc_mounts.pyt<module>?s*		
		(G	,	-