o
    _                     @   sr   d dl mZ ddlmZ ddlT d dlmZmZ d dlZ	d dl
mZ d dlZe	j Ze ZG dd dejZdS )	    N   )base)*)PopenPIPE)commandsc                   @   st   e Zd ZdZedd Zdd Zedd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zeddddd ZdS )MountsPluginz.
	Plugin for tuning options of mount-points.
	c                 C   s   i }d}t g dttddd \}}dd | D D ]W}t|dk r%q|dd \}}}t|dkr8|d nd}	t|dkrD|d nd}
|d	krM|}q|d
ksU|dvrVq|
du s^|
dkr_q||
t ||	d ||
 d | q|| _dS )z
		Gets the information about disks, partitions and mountpoints. Stores information about used filesystem and
		creates a list of all underlying devices (in case of LVM) for each mountpoint.
		N)lsblkz-rnozTYPE,RM,KNAME,FSTYPE,MOUNTPOINTT)stdoutstderr	close_fdsuniversal_newlinesc                 S   s   g | ]}|  qS  )split).0liner   r   =/usr/lib/python3/dist-packages/tuned/plugins/plugin_mounts.py
<listcomp>   s    z>MountsPlugin._generate_mountpoint_topology.<locals>.<listcomp>      disk1)partlvmz[SWAP])disksdevice_name
filesystemr   )	r   r   communicate
splitlineslen
setdefaultsetadd_mountpoint_topology)clsmountpoint_topologycurrent_diskr
   r   columnsdevice_typedevice_removabler   r   
mountpointr   r   r   _generate_mountpoint_topology   s0   
z*MountsPlugin._generate_mountpoint_topologyc                 C   s*   |    d| _t| j | _t | _d S )NT)r+   _devices_supportedr!   r#   keys_free_devices_assigned_devicesselfr   r   r   _init_devices5   s   zMountsPlugin._init_devicesc                 C   s   dd iS )Ndisable_barriersr   r0   r   r   r   _get_config_options;   s   z MountsPlugin._get_config_optionsc                 C   s   d|_ d|_d S )NFT)_has_dynamic_tuning_has_static_tuningr1   instancer   r   r   _instance_initA   s   
zMountsPlugin._instance_initc                 C   s   d S )Nr   r7   r   r   r   _instance_cleanupE   s   zMountsPlugin._instance_cleanupc                 C   s,   t  d| }|D ]
}t|   S dS )zV
		Get device cache type. This will work only for devices on SCSI kernel subsystem.
		z+/sys/block/%s/device/scsi_disk/*/cache_typeN)globcmd	read_filestrip)r1   devicesource_filenamessource_filenamer   r   r   _get_device_cache_typeH   s   z#MountsPlugin._get_device_cache_typec                 C   s,   | j | d D ]}| |dkr dS qdS )zr
		Checks if the device has 'write back' cache. If the cache type cannot be determined, asume some other cache.
		r   z
write backTF)r#   rB   )r1   r*   r?   r   r   r   _mountpoint_has_writeback_cacheQ   s
   z,MountsPlugin._mountpoint_has_writeback_cachec                 C   s   t d/}|D ]}| }|d d dkrq|d |kr"|d } n
q	 W d   dS W d   n1 s6w   Y  |d}|D ] }|d\}}	}
|d	ksX|d
kr[|
dkr[ dS |d
krb dS qBdS )zP
		Checks if a given mountpoint is mounted with barriers enabled or disabled.
		z/proc/mountsr   /r   r   N,=	nobarrierbarrier0FT)openr   	partition)r1   r*   mounts_filer   r'   option_listoptionsoptionnamesepvaluer   r   r   _mountpoint_has_barriersZ   s,   

z%MountsPlugin._mountpoint_has_barriersc                 C   s   d|dd| g}t | dS )z
		Remounts partition.
		z/usr/bin/mountz-oz
remount,%sN)r<   execute)r1   rK   rN   remount_commandr   r   r   _remount_partitionw   s   zMountsPlugin._remount_partitionr3   T)
per_devicec           
      C   sT  | j d|d}t| dk}|p| |}|r|sd S d }| j| d ds+d}n5|s5| |r5d}n+| |}	|	d u rAd}n|	d	krT|rQt	t
j|  d
S d}n|r`tt
j|  d	S |d urot	d||f  d S | j||	 t	d|  | |d d S |rd S | j|}	|	d u rd S t	d|  | |d | j| d S )Nr3   )command_namer   forcer   extzfilesystem not supportedzdevice uses write back cachezunknown current settingFTzbarriers already disabledz#not disabling barriers on '%s' (%s)zdisabling barriers on '%s'z	barrier=0zenabling barriers on '%s'z	barrier=1)_storage_keystrlower_option_boolr#   
startswithrC   rS   loginfoconstsSTR_VERIFY_PROFILE_OKerrorSTR_VERIFY_PROFILE_FAIL_storager!   rV   getunset)
r1   startrR   r*   verifyignore_missingstorage_keyrY   reject_reasonoriginal_valuer   r   r   _disable_barriers~   sR   
zMountsPlugin._disable_barriersN)__name__
__module____qualname____doc__classmethodr+   r2   r4   r9   r:   rB   rC   rS   rV   command_customro   r   r   r   r   r      s    
#
		
r   )tuned.constsrb    r   
decorators
subprocessr   r   
tuned.logstunedtuned.utils.commandsr   r;   logsrg   r`   r<   Pluginr   r   r   r   r   <module>   s    
